root / trunk / tbeta / OSX / libs / poco / include / Poco / Net / SocketAcceptor.h @ 151

View | Annotate | Download (5.9 KB)

1
//
2
// SocketAcceptor.h
3
//
4
// $Id: //poco/1.3/Net/include/Poco/Net/SocketAcceptor.h#1 $
5
//
6
// Library: Net
7
// Package: Reactor
8
// Module:  SocketAcceptor
9
//
10
// Definition of the SocketAcceptor 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_SocketAcceptor_INCLUDED
40
#define Net_SocketAcceptor_INCLUDED
41
42
43
#include "Poco/Net/Net.h"
44
#include "Poco/Net/SocketNotification.h"
45
#include "Poco/Net/ServerSocket.h"
46
#include "Poco/Net/StreamSocket.h"
47
#include "Poco/Observer.h"
48
49
50
namespace Poco {
51
namespace Net {
52
53
54
template <class ServiceHandler>
55
class SocketAcceptor
56
        /// This class implements the Acceptor part of the
57
        /// Acceptor-Connector design pattern.
58
        ///
59
        /// The Acceptor-Connector pattern has been described in the book
60
        /// "Pattern Languages of Program Design 3", edited by Robert Martin,
61
        /// Frank Buschmann and Dirk Riehle (Addison Wesley, 1997).
62
        ///
63
        /// The Acceptor-Connector design pattern decouples connection
64
        /// establishment and service initialization in a distributed system
65
        /// from the processing performed once a service is initialized.
66
        /// This decoupling is achieved with three components: Acceptors, 
67
        /// Connectors and Service Handlers.
68
        /// The SocketAcceptor passively waits for connection requests (usually
69
        /// from a remote Connector) and establishes a connection upon
70
        /// arrival of a connection requests. Also, a Service Handler is
71
        /// initialized to process the data arriving via the connection in
72
        /// an application-specific way.
73
        ///
74
        /// The SocketAcceptor sets up a ServerSocket and registers itself
75
        /// for a ReadableNotification, denoting an incoming connection request.
76
        ///
77
        /// When the ServerSocket becomes readable the SocketAcceptor accepts
78
        /// the connection request and creates a ServiceHandler to
79
        /// service the connection.
80
        ///
81
        /// The ServiceHandler class must provide a constructor that
82
        /// takes a StreamSocket and a SocketReactor as arguments,
83
        /// e.g.:
84
        ///     MyServiceHandler(const StreamSocket& socket, ServiceReactor& reactor)
85
        ///
86
        /// When the ServiceHandler is done, it must destroy itself.
87
        ///
88
        /// Subclasses can override the createServiceHandler() factory method
89
        /// if special steps are necessary to create a ServiceHandler object.
90
{
91
public:                
92
        SocketAcceptor(ServerSocket& socket):
93
                _socket(socket),
94
                _pReactor(0)
95
                /// Creates an SocketAcceptor, using the given ServerSocket.
96
        {
97
        }
98
99
        SocketAcceptor(ServerSocket& socket, SocketReactor& reactor):
100
                _socket(socket),
101
                _pReactor(0)
102
                /// Creates an SocketAcceptor, using the given ServerSocket.
103
                /// The SocketAcceptor registers itself with the given SocketReactor.
104
        {
105
                registerAcceptor(reactor);
106
        }
107
108
        virtual ~SocketAcceptor()
109
                /// Destroys the SocketAcceptor.
110
        {
111
                unregisterAcceptor();
112
        }
113
        
114
        virtual void registerAcceptor(SocketReactor& reactor)
115
                /// Registers the SocketAcceptor with a SocketReactor.
116
                ///
117
                /// A subclass can override this and, for example, also register
118
                /// an event handler for a timeout event.
119
                ///
120
                /// The overriding method must call the baseclass implementation first.
121
        {
122
                _pReactor = &reactor;
123
                _pReactor->addEventHandler(_socket, Poco::Observer<SocketAcceptor, ReadableNotification>(*this, &SocketAcceptor::onAccept));
124
        }
125
        
126
        virtual void unregisterAcceptor()
127
                /// Unregisters the SocketAcceptor.
128
                ///
129
                /// A subclass can override this and, for example, also unregister
130
                /// its event handler for a timeout event.
131
                ///
132
                /// The overriding method must call the baseclass implementation first.
133
        {
134
                if (_pReactor)
135
                {
136
                        _pReactor->removeEventHandler(_socket, Poco::Observer<SocketAcceptor, ReadableNotification>(*this, &SocketAcceptor::onAccept));
137
                }
138
        }
139
        
140
        void onAccept(ReadableNotification* pNotification)
141
        {
142
                pNotification->release();
143
                StreamSocket sock = _socket.acceptConnection();
144
                createServiceHandler(sock);
145
        }
146
        
147
protected:
148
        virtual ServiceHandler* createServiceHandler(StreamSocket& socket)
149
                /// Create and initialize a new ServiceHandler instance.
150
                ///
151
                /// Subclasses can override this method.
152
        {
153
                return new ServiceHandler(socket, *_pReactor);
154
        }
155
156
        SocketReactor* reactor()
157
                /// Returns a pointer to the SocketReactor where
158
                /// this SocketAcceptor is registered.
159
                ///
160
                /// The pointer may be null.
161
        {
162
                return _pReactor;
163
        }
164
165
        Socket& socket()
166
                /// Returns a reference to the SocketAcceptor's socket.
167
        {
168
                return _socket;
169
        }
170
171
private:
172
        SocketAcceptor();
173
        SocketAcceptor(const SocketAcceptor&);
174
        SocketAcceptor& operator = (const SocketAcceptor&);
175
        
176
        ServerSocket   _socket;
177
        SocketReactor* _pReactor;
178
};
179
180
181
} } // namespace Poco::Net
182
183
184
#endif // Net_SocketAcceptor_INCLUDED