Logo Search packages:      
Sourcecode: poco version File versions  Download package

Poco::Net::SocketReactor Class Reference

#include <SocketReactor.h>

Inheritance diagram for Poco::Net::SocketReactor:

Poco::Runnable

List of all members.


Detailed Description

This class, which is part of the Reactor pattern, implements the "Initiation Dispatcher".

The Reactor pattern has been described in the book "Pattern Languages of Program Design" by Jim Coplien and Douglas C. Schmidt (Addison Wesley, 1995).

The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. Each service in an application may consist of several methods and is represented by a separate event handler. The event handler is responsible for servicing service-specific requests. The SocketReactor dispatches the event handlers.

Event handlers (any class can be an event handler - there is no base class for event handlers) can be registered with the addEventHandler() method and deregistered with the removeEventHandler() method.

An event handler is always registered for a certain socket, which is given in the call to addEventHandler(). Any method of the event handler class can be registered to handle the event - the only requirement is that the method takes a pointer to an instance of SocketNotification (or a subclass of it) as argument.

Once started, the SocketReactor waits for events on the registered sockets, using Socket::select(). If an event is detected, the corresponding event handler is invoked. There are five event types (and corresponding notification classes) defined: ReadableNotification, WritableNotification, ErrorNotification, TimeoutNotification and ShutdownNotification.

The ReadableNotification will be dispatched if a socket becomes readable. The WritableNotification will be dispatched if a socket becomes writable. The ErrorNotification will be dispatched if there is an error condition on a socket.

If the timeout expires and no event has occured, a TimeoutNotification will be dispatched to all event handlers registered for it. This is done in the onTimeout() method which can be overridded by subclasses to perform custom timeout processing.

Finally, when the SocketReactor is about to shut down (as a result of stop() being called), it dispatches a ShutdownNotification to all event handlers. This is done in the onShutdown() method which can be overridded by subclasses to perform custom shutdown processing.

The SocketReactor is implemented so that it can run in its own thread. It is also possible to run multiple SocketReactors in parallel, as long as they work on different sockets.

It is safe to call addEventHandler() and removeEventHandler() from another thread while the SocketReactor is running. Also, it is safe to call addEventHandler() and removeEventHandler() from event handlers.

Definition at line 61 of file SocketReactor.h.


Public Member Functions

void addEventHandler (const Socket &socket, const Poco::AbstractObserver &observer)
 Returns the timeout.
const Poco::TimespangetTimeout () const
void removeEventHandler (const Socket &socket, const Poco::AbstractObserver &observer)
void run ()
 Destroys the SocketReactor.
void setTimeout (const Poco::Timespan &timeout)
 SocketReactor (const Poco::Timespan &timeout)
 Creates the SocketReactor.
void stop ()
virtual ~SocketReactor ()
 Creates the SocketReactor, using the given timeout.

Protected Member Functions

void dispatch (SocketNotification *pNotification)
void dispatch (const Socket &socket, SocketNotification *pNotification)
virtual void onShutdown ()
virtual void onTimeout ()

Private Types

enum  { DEFAULT_TIMEOUT = 250000 }
typedef std::map< Socket,
NotifierPtr
EventHandlerMap
typedef Poco::AutoPtr
< SocketNotification
NotificationPtr
typedef Poco::AutoPtr
< SocketNotifier
NotifierPtr
 Dispatches the given notification to all observers.

Private Member Functions

void dispatch (NotifierPtr &pNotifier, SocketNotification *pNotification)

Private Attributes

EventHandlerMap _handlers
Poco::FastMutex _mutex
NotificationPtr _pErrorNotification
NotificationPtr _pReadableNotification
NotificationPtr _pShutdownNotification
NotificationPtr _pTimeoutNotification
NotificationPtr _pWritableNotification
bool _stop
Poco::Timespan _timeout

Friends

class SocketNotifier

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index