root / trunk / Windows / libs / poco / include / Poco / Net / SocketReactor.h @ 3
View | Annotate | Download (8.6 KB)
| 1 | //
|
|---|---|
| 2 | // SocketReactor.h
|
| 3 | //
|
| 4 | // $Id: //poco/1.3/Net/include/Poco/Net/SocketReactor.h#2 $
|
| 5 | //
|
| 6 | // Library: Net
|
| 7 | // Package: Reactor
|
| 8 | // Module: SocketReactor
|
| 9 | //
|
| 10 | // Definition of the SocketReactor class.
|
| 11 | //
|
| 12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
|
| 13 | // and Contributors.
|
| 14 | //
|
| 15 | // Permission is hereby granted, free of charge, to any person or organization
|
| 16 | // obtaining a copy of the software and accompanying documentation covered by
|
| 17 | // this license (the "Software") to use, reproduce, display, distribute,
|
| 18 | // execute, and transmit the Software, and to prepare derivative works of the
|
| 19 | // Software, and to permit third-parties to whom the Software is furnished to
|
| 20 | // do so, all subject to the following:
|
| 21 | //
|
| 22 | // The copyright notices in the Software and this entire statement, including
|
| 23 | // the above license grant, this restriction and the following disclaimer,
|
| 24 | // must be included in all copies of the Software, in whole or in part, and
|
| 25 | // all derivative works of the Software, unless such copies or derivative
|
| 26 | // works are solely in the form of machine-executable object code generated by
|
| 27 | // a source language processor.
|
| 28 | //
|
| 29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
| 32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
| 33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
| 34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
| 35 | // DEALINGS IN THE SOFTWARE.
|
| 36 | //
|
| 37 | |
| 38 | |
| 39 | #ifndef Net_SocketReactor_INCLUDED
|
| 40 | #define Net_SocketReactor_INCLUDED
|
| 41 | |
| 42 | |
| 43 | #include "Poco/Net/Net.h" |
| 44 | #include "Poco/Net/Socket.h" |
| 45 | #include "Poco/Runnable.h" |
| 46 | #include "Poco/Timespan.h" |
| 47 | #include "Poco/Observer.h" |
| 48 | #include "Poco/AutoPtr.h" |
| 49 | #include <map> |
| 50 | |
| 51 | |
| 52 | namespace Poco {
|
| 53 | namespace Net {
|
| 54 | |
| 55 | |
| 56 | class Socket; |
| 57 | class SocketNotification; |
| 58 | class SocketNotifier; |
| 59 | |
| 60 | |
| 61 | class Net_API SocketReactor: public Poco::Runnable
|
| 62 | /// This class, which is part of the Reactor pattern,
|
| 63 | /// implements the "Initiation Dispatcher".
|
| 64 | ///
|
| 65 | /// The Reactor pattern has been described in the book
|
| 66 | /// "Pattern Languages of Program Design" by Jim Coplien
|
| 67 | /// and Douglas C. Schmidt (Addison Wesley, 1995).
|
| 68 | ///
|
| 69 | /// The Reactor design pattern handles service requests that
|
| 70 | /// are delivered concurrently to an application by one or more
|
| 71 | /// clients. Each service in an application may consist of several
|
| 72 | /// methods and is represented by a separate event handler. The event
|
| 73 | /// handler is responsible for servicing service-specific requests.
|
| 74 | /// The SocketReactor dispatches the event handlers.
|
| 75 | ///
|
| 76 | /// Event handlers (any class can be an event handler - there
|
| 77 | /// is no base class for event handlers) can be registered
|
| 78 | /// with the addEventHandler() method and deregistered with
|
| 79 | /// the removeEventHandler() method.
|
| 80 | ///
|
| 81 | /// An event handler is always registered for a certain socket,
|
| 82 | /// which is given in the call to addEventHandler(). Any method
|
| 83 | /// of the event handler class can be registered to handle the
|
| 84 | /// event - the only requirement is that the method takes
|
| 85 | /// a pointer to an instance of SocketNotification (or a subclass of it)
|
| 86 | /// as argument.
|
| 87 | ///
|
| 88 | /// Once started, the SocketReactor waits for events
|
| 89 | /// on the registered sockets, using Socket::select().
|
| 90 | /// If an event is detected, the corresponding event handler
|
| 91 | /// is invoked. There are five event types (and corresponding
|
| 92 | /// notification classes) defined: ReadableNotification, WritableNotification,
|
| 93 | /// ErrorNotification, TimeoutNotification, IdleNotification and
|
| 94 | /// ShutdownNotification.
|
| 95 | ///
|
| 96 | /// The ReadableNotification will be dispatched if a socket becomes
|
| 97 | /// readable. The WritableNotification will be dispatched if a socket
|
| 98 | /// becomes writable. The ErrorNotification will be dispatched if
|
| 99 | /// there is an error condition on a socket.
|
| 100 | ///
|
| 101 | /// If the timeout expires and no event has occured, a
|
| 102 | /// TimeoutNotification will be dispatched to all event handlers
|
| 103 | /// registered for it. This is done in the onTimeout() method
|
| 104 | /// which can be overridden by subclasses to perform custom
|
| 105 | /// timeout processing.
|
| 106 | ///
|
| 107 | /// If there are no sockets for the SocketReactor to pass to
|
| 108 | /// Socket::select(), an IdleNotification will be dispatched to
|
| 109 | /// all event handlers registered for it. This is done in the
|
| 110 | /// onIdle() method which can be overridden by subclasses
|
| 111 | /// to perform custom idle processing. Since onIdle() will be
|
| 112 | /// called repeatedly in a loop, it is recommended to do a
|
| 113 | /// short sleep or yield in the event handler.
|
| 114 | ///
|
| 115 | /// Finally, when the SocketReactor is about to shut down (as a result
|
| 116 | /// of stop() being called), it dispatches a ShutdownNotification
|
| 117 | /// to all event handlers. This is done in the onShutdown() method
|
| 118 | /// which can be overridded by subclasses to perform custom
|
| 119 | /// shutdown processing.
|
| 120 | ///
|
| 121 | /// The SocketReactor is implemented so that it can
|
| 122 | /// run in its own thread. It is also possible to run
|
| 123 | /// multiple SocketReactors in parallel, as long as
|
| 124 | /// they work on different sockets.
|
| 125 | ///
|
| 126 | /// It is safe to call addEventHandler() and removeEventHandler()
|
| 127 | /// from another thread while the SocketReactor is running. Also,
|
| 128 | /// it is safe to call addEventHandler() and removeEventHandler()
|
| 129 | /// from event handlers.
|
| 130 | {
|
| 131 | public:
|
| 132 | SocketReactor(); |
| 133 | /// Creates the SocketReactor.
|
| 134 | |
| 135 | SocketReactor(const Poco::Timespan& timeout);
|
| 136 | /// Creates the SocketReactor, using the given timeout.
|
| 137 | |
| 138 | virtual ~SocketReactor(); |
| 139 | /// Destroys the SocketReactor.
|
| 140 | |
| 141 | void run();
|
| 142 | /// Runs the SocketReactor. The reactor will run
|
| 143 | /// until stop() is called (in a separate thread).
|
| 144 | |
| 145 | void stop();
|
| 146 | /// Stops the SocketReactor.
|
| 147 | ///
|
| 148 | /// The reactor will be stopped when the next event
|
| 149 | /// (including a timeout event) occurs.
|
| 150 | |
| 151 | void setTimeout(const Poco::Timespan& timeout); |
| 152 | /// Sets the timeout.
|
| 153 | ///
|
| 154 | /// If no other event occurs for the given timeout
|
| 155 | /// interval, a timeout event is sent to all event listeners.
|
| 156 | ///
|
| 157 | /// The default timeout is 250 milliseconds;
|
| 158 | ///
|
| 159 | /// The timeout is passed to the Socket::select()
|
| 160 | /// method.
|
| 161 | |
| 162 | const Poco::Timespan& getTimeout() const; |
| 163 | /// Returns the timeout.
|
| 164 | |
| 165 | void addEventHandler(const Socket& socket, const Poco::AbstractObserver& observer); |
| 166 | /// Registers an event handler with the SocketReactor.
|
| 167 | ///
|
| 168 | /// Usage:
|
| 169 | /// Poco::Observer<MyEventHandler, SocketNotification> obs(*this, &MyEventHandler::handleMyEvent);
|
| 170 | /// reactor.addEventHandler(obs);
|
| 171 | |
| 172 | void removeEventHandler(const Socket& socket, const Poco::AbstractObserver& observer); |
| 173 | /// Unregisters an event handler with the SocketReactor.
|
| 174 | ///
|
| 175 | /// Usage:
|
| 176 | /// Poco::Observer<MyEventHandler, SocketNotification> obs(*this, &MyEventHandler::handleMyEvent);
|
| 177 | /// reactor.removeEventHandler(obs);
|
| 178 | |
| 179 | protected:
|
| 180 | virtual void onTimeout();
|
| 181 | /// Called if the timeout expires and no other events are available.
|
| 182 | ///
|
| 183 | /// Can be overridden by subclasses. The default implementation
|
| 184 | /// dispatches the TimeoutNotification and thus should be called by overriding
|
| 185 | /// implementations.
|
| 186 | |
| 187 | virtual void onIdle();
|
| 188 | /// Called if no sockets are available to call select() on.
|
| 189 | ///
|
| 190 | /// Can be overridden by subclasses. The default implementation
|
| 191 | /// dispatches the IdleNotification and thus should be called by overriding
|
| 192 | /// implementations.
|
| 193 | |
| 194 | virtual void onShutdown();
|
| 195 | /// Called when the SocketReactor is about to terminate.
|
| 196 | ///
|
| 197 | /// Can be overridden by subclasses. The default implementation
|
| 198 | /// dispatches the ShutdownNotification and thus should be called by overriding
|
| 199 | /// implementations.
|
| 200 | |
| 201 | void dispatch(const Socket& socket, SocketNotification* pNotification); |
| 202 | /// Dispatches the given notification to all observers
|
| 203 | /// registered for the given socket.
|
| 204 | |
| 205 | void dispatch(SocketNotification* pNotification);
|
| 206 | /// Dispatches the given notification to all observers.
|
| 207 | |
| 208 | private:
|
| 209 | typedef Poco::AutoPtr<SocketNotifier> NotifierPtr;
|
| 210 | typedef Poco::AutoPtr<SocketNotification> NotificationPtr;
|
| 211 | typedef std::map<Socket, NotifierPtr> EventHandlerMap;
|
| 212 | |
| 213 | void dispatch(NotifierPtr& pNotifier, SocketNotification* pNotification);
|
| 214 | |
| 215 | enum
|
| 216 | {
|
| 217 | DEFAULT_TIMEOUT = 250000
|
| 218 | }; |
| 219 | |
| 220 | bool _stop;
|
| 221 | Poco::Timespan _timeout; |
| 222 | EventHandlerMap _handlers; |
| 223 | NotificationPtr _pReadableNotification; |
| 224 | NotificationPtr _pWritableNotification; |
| 225 | NotificationPtr _pErrorNotification; |
| 226 | NotificationPtr _pTimeoutNotification; |
| 227 | NotificationPtr _pIdleNotification; |
| 228 | NotificationPtr _pShutdownNotification; |
| 229 | Poco::FastMutex _mutex; |
| 230 | |
| 231 | friend class SocketNotifier; |
| 232 | }; |
| 233 | |
| 234 | |
| 235 | } } // namespace Poco::Net
|
| 236 | |
| 237 | |
| 238 | #endif // Net_SocketReactor_INCLUDED |
