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