2
// SocketReactorTest.cpp
4
// $Id: //poco/1.2/Net/testsuite/src/SocketReactorTest.cpp#1 $
6
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
33
#include "SocketReactorTest.h"
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/Net/SocketReactor.h"
37
#include "Poco/Net/SocketNotification.h"
38
#include "Poco/Net/SocketConnector.h"
39
#include "Poco/Net/SocketAcceptor.h"
40
#include "Poco/Net/StreamSocket.h"
41
#include "Poco/Net/ServerSocket.h"
42
#include "Poco/Net/SocketAddress.h"
43
#include "Poco/Observer.h"
47
using Poco::Net::SocketReactor;
48
using Poco::Net::SocketConnector;
49
using Poco::Net::SocketAcceptor;
50
using Poco::Net::StreamSocket;
51
using Poco::Net::ServerSocket;
52
using Poco::Net::SocketAddress;
53
using Poco::Net::SocketNotification;
54
using Poco::Net::ReadableNotification;
55
using Poco::Net::WritableNotification;
56
using Poco::Net::TimeoutNotification;
57
using Poco::Net::ShutdownNotification;
63
class EchoServiceHandler
66
EchoServiceHandler(StreamSocket& socket, SocketReactor& reactor):
70
_reactor.addEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
75
_reactor.removeEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
78
void onReadable(ReadableNotification* pNf)
82
int n = _socket.receiveBytes(buffer, sizeof(buffer));
85
_socket.sendBytes(buffer, n);
89
_socket.shutdownSend();
96
SocketReactor& _reactor;
99
class ClientServiceHandler
102
ClientServiceHandler(StreamSocket& socket, SocketReactor& reactor):
107
_reactor.addEventHandler(_socket, Observer<ClientServiceHandler, ReadableNotification>(*this, &ClientServiceHandler::onReadable));
108
_reactor.addEventHandler(_socket, Observer<ClientServiceHandler, WritableNotification>(*this, &ClientServiceHandler::onWritable));
109
_reactor.addEventHandler(_socket, Observer<ClientServiceHandler, TimeoutNotification>(*this, &ClientServiceHandler::onTimeout));
112
~ClientServiceHandler()
116
void onReadable(ReadableNotification* pNf)
120
int n = _socket.receiveBytes(buffer, sizeof(buffer));
123
_str.write(buffer, n);
127
_reactor.removeEventHandler(_socket, Observer<ClientServiceHandler, ReadableNotification>(*this, &ClientServiceHandler::onReadable));
134
void onWritable(WritableNotification* pNf)
137
_reactor.removeEventHandler(_socket, Observer<ClientServiceHandler, WritableNotification>(*this, &ClientServiceHandler::onWritable));
138
std::string data(1024, 'x');
139
_socket.sendBytes(data.data(), (int) data.length());
140
_socket.shutdownSend();
143
void onTimeout(TimeoutNotification* pNf)
154
static std::string data()
159
static bool timeout()
164
static bool getCloseOnTimeout()
166
return _closeOnTimeout;
169
static void setCloseOnTimeout(bool flag)
171
_closeOnTimeout = flag;
175
StreamSocket _socket;
176
SocketReactor& _reactor;
177
std::stringstream _str;
178
static std::string _data;
179
static bool _timeout;
180
static bool _closeOnTimeout;
184
std::string ClientServiceHandler::_data;
185
bool ClientServiceHandler::_timeout = false;
186
bool ClientServiceHandler::_closeOnTimeout = false;
189
class FailConnector: public SocketConnector<ClientServiceHandler>
192
FailConnector(SocketAddress& address, SocketReactor& reactor):
193
SocketConnector<ClientServiceHandler>(address, reactor),
197
reactor.addEventHandler(socket(), Observer<FailConnector, ShutdownNotification>(*this, &FailConnector::onShutdown));
200
void onShutdown(ShutdownNotification* pNf)
206
void onError(int error)
217
bool shutdown() const
229
SocketReactorTest::SocketReactorTest(const std::string& name): CppUnit::TestCase(name)
234
SocketReactorTest::~SocketReactorTest()
239
void SocketReactorTest::testSocketReactor()
242
ServerSocket ss(ssa);
243
SocketReactor reactor;
244
SocketAcceptor<EchoServiceHandler> acceptor(ss, reactor);
245
SocketAddress sa("localhost", ss.address().port());
246
SocketConnector<ClientServiceHandler> connector(sa, reactor);
248
std::string data(ClientServiceHandler::data());
249
assert (data.size() == 1024);
253
void SocketReactorTest::testSocketConnectorFail()
255
SocketReactor reactor;
256
SocketAddress sa("192.168.168.192", 12345);
257
FailConnector connector(sa, reactor);
258
assert (!connector.failed());
259
assert (!connector.shutdown());
261
assert (connector.failed());
262
assert (connector.shutdown());
266
void SocketReactorTest::testSocketConnectorTimeout()
268
ClientServiceHandler::setCloseOnTimeout(true);
271
ServerSocket ss(ssa);
272
SocketReactor reactor;
273
SocketAddress sa("localhost", ss.address().port());
274
SocketConnector<ClientServiceHandler> connector(sa, reactor);
276
assert (ClientServiceHandler::timeout());
280
void SocketReactorTest::setUp()
282
ClientServiceHandler::setCloseOnTimeout(false);
286
void SocketReactorTest::tearDown()
291
CppUnit::Test* SocketReactorTest::suite()
293
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SocketReactorTest");
295
CppUnit_addTest(pSuite, SocketReactorTest, testSocketReactor);
296
CppUnit_addTest(pSuite, SocketReactorTest, testSocketConnectorFail);
297
CppUnit_addTest(pSuite, SocketReactorTest, testSocketConnectorTimeout);