~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Net/testsuite/src/DialogServer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// DialogServer.cpp
 
3
//
 
4
// $Id: //poco/1.2/Net/testsuite/src/DialogServer.cpp#1 $
 
5
//
 
6
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
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:
 
15
// 
 
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.
 
22
// 
 
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.
 
30
//
 
31
 
 
32
 
 
33
#include "DialogServer.h"
 
34
#include "Poco/Net/DialogSocket.h"
 
35
#include "Poco/Net/SocketAddress.h"
 
36
#include "Poco/Timespan.h"
 
37
#include <iostream>
 
38
 
 
39
 
 
40
using Poco::Net::Socket;
 
41
using Poco::Net::DialogSocket;
 
42
using Poco::Net::SocketAddress;
 
43
using Poco::FastMutex;
 
44
using Poco::Thread;
 
45
 
 
46
 
 
47
DialogServer::DialogServer(bool acceptCommands):
 
48
        _socket(SocketAddress()),
 
49
        _thread("DialogServer"),
 
50
        _stop(false),
 
51
        _acceptCommands(acceptCommands),
 
52
        _log(false)
 
53
{
 
54
        _thread.start(*this);
 
55
        _ready.wait();
 
56
}
 
57
 
 
58
 
 
59
DialogServer::~DialogServer()
 
60
{
 
61
        _stop = true;
 
62
        _thread.join();
 
63
}
 
64
 
 
65
 
 
66
Poco::UInt16 DialogServer::port() const
 
67
{
 
68
        return _socket.address().port();
 
69
}
 
70
 
 
71
 
 
72
void DialogServer::run()
 
73
{
 
74
        _ready.set();
 
75
        Poco::Timespan span(250000);
 
76
        while (!_stop)
 
77
        {
 
78
                if (_socket.poll(span, Socket::SELECT_READ))
 
79
                {
 
80
                        DialogSocket ds = _socket.acceptConnection();
 
81
                        {
 
82
                                FastMutex::ScopedLock lock(_mutex);
 
83
                                if (!_nextResponses.empty())
 
84
                                {
 
85
                                        ds.sendMessage(_nextResponses.front());
 
86
                                        _nextResponses.erase(_nextResponses.begin());
 
87
                                }
 
88
                        }
 
89
                        if (_acceptCommands)
 
90
                        {
 
91
                                try
 
92
                                {
 
93
                                        std::string command;
 
94
                                        while (ds.receiveMessage(command))
 
95
                                        {
 
96
                                                if (_log) std::cout << ">> " << command << std::endl;
 
97
                                                {
 
98
                                                        FastMutex::ScopedLock lock(_mutex);
 
99
                                                        _lastCommands.push_back(command);
 
100
                                                        if (!_nextResponses.empty())
 
101
                                                        {
 
102
                                                                if (_log) std::cout << "<< " << _nextResponses.front() << std::endl;
 
103
                                                                ds.sendMessage(_nextResponses.front());
 
104
                                                                _nextResponses.erase(_nextResponses.begin());
 
105
                                                        }
 
106
                                                }
 
107
                                        }
 
108
                                }
 
109
                                catch (Poco::Exception& exc)
 
110
                                {
 
111
                                        std::cerr << "DialogServer: " << exc.displayText() << std::endl;
 
112
                                }
 
113
                        }
 
114
                }
 
115
        }
 
116
}
 
117
 
 
118
 
 
119
const std::string& DialogServer::lastCommand() const
 
120
{
 
121
        FastMutex::ScopedLock lock(_mutex);
 
122
        
 
123
        static const std::string EMPTY;
 
124
        if (_lastCommands.empty())
 
125
                return EMPTY;
 
126
        else
 
127
                return _lastCommands.back();
 
128
}
 
129
 
 
130
 
 
131
const std::vector<std::string>& DialogServer::lastCommands() const
 
132
{
 
133
        return _lastCommands;
 
134
}
 
135
 
 
136
 
 
137
std::string DialogServer::popCommand()
 
138
{
 
139
        FastMutex::ScopedLock lock(_mutex);
 
140
 
 
141
        std::string command;
 
142
        if (!_lastCommands.empty())
 
143
        {
 
144
                command = _lastCommands.front();
 
145
                _lastCommands.erase(_lastCommands.begin());
 
146
        }
 
147
        return command;
 
148
}
 
149
 
 
150
 
 
151
std::string DialogServer::popCommandWait()
 
152
{
 
153
        std::string result(popCommand());
 
154
        while (result.empty())
 
155
        {
 
156
                Thread::sleep(100);
 
157
                result = popCommand();
 
158
        }
 
159
        return result;
 
160
}
 
161
 
 
162
 
 
163
void DialogServer::addResponse(const std::string& response)
 
164
{
 
165
        FastMutex::ScopedLock lock(_mutex);
 
166
 
 
167
        _nextResponses.push_back(response);
 
168
}
 
169
 
 
170
        
 
171
void DialogServer::clearCommands()
 
172
{
 
173
        FastMutex::ScopedLock lock(_mutex);
 
174
 
 
175
        _lastCommands.clear();
 
176
}
 
177
 
 
178
                
 
179
void DialogServer::clearResponses()
 
180
{
 
181
        FastMutex::ScopedLock lock(_mutex);
 
182
        
 
183
        _nextResponses.clear();
 
184
}
 
185
 
 
186
 
 
187
void DialogServer::log(bool flag)
 
188
{
 
189
        _log = flag;
 
190
}