4
// $Id: //poco/1.2/Net/testsuite/src/DialogServer.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 "DialogServer.h"
34
#include "Poco/Net/DialogSocket.h"
35
#include "Poco/Net/SocketAddress.h"
36
#include "Poco/Timespan.h"
40
using Poco::Net::Socket;
41
using Poco::Net::DialogSocket;
42
using Poco::Net::SocketAddress;
43
using Poco::FastMutex;
47
DialogServer::DialogServer(bool acceptCommands):
48
_socket(SocketAddress()),
49
_thread("DialogServer"),
51
_acceptCommands(acceptCommands),
59
DialogServer::~DialogServer()
66
Poco::UInt16 DialogServer::port() const
68
return _socket.address().port();
72
void DialogServer::run()
75
Poco::Timespan span(250000);
78
if (_socket.poll(span, Socket::SELECT_READ))
80
DialogSocket ds = _socket.acceptConnection();
82
FastMutex::ScopedLock lock(_mutex);
83
if (!_nextResponses.empty())
85
ds.sendMessage(_nextResponses.front());
86
_nextResponses.erase(_nextResponses.begin());
94
while (ds.receiveMessage(command))
96
if (_log) std::cout << ">> " << command << std::endl;
98
FastMutex::ScopedLock lock(_mutex);
99
_lastCommands.push_back(command);
100
if (!_nextResponses.empty())
102
if (_log) std::cout << "<< " << _nextResponses.front() << std::endl;
103
ds.sendMessage(_nextResponses.front());
104
_nextResponses.erase(_nextResponses.begin());
109
catch (Poco::Exception& exc)
111
std::cerr << "DialogServer: " << exc.displayText() << std::endl;
119
const std::string& DialogServer::lastCommand() const
121
FastMutex::ScopedLock lock(_mutex);
123
static const std::string EMPTY;
124
if (_lastCommands.empty())
127
return _lastCommands.back();
131
const std::vector<std::string>& DialogServer::lastCommands() const
133
return _lastCommands;
137
std::string DialogServer::popCommand()
139
FastMutex::ScopedLock lock(_mutex);
142
if (!_lastCommands.empty())
144
command = _lastCommands.front();
145
_lastCommands.erase(_lastCommands.begin());
151
std::string DialogServer::popCommandWait()
153
std::string result(popCommand());
154
while (result.empty())
157
result = popCommand();
163
void DialogServer::addResponse(const std::string& response)
165
FastMutex::ScopedLock lock(_mutex);
167
_nextResponses.push_back(response);
171
void DialogServer::clearCommands()
173
FastMutex::ScopedLock lock(_mutex);
175
_lastCommands.clear();
179
void DialogServer::clearResponses()
181
FastMutex::ScopedLock lock(_mutex);
183
_nextResponses.clear();
187
void DialogServer::log(bool flag)