| 1 |
#include "AutomationEngine.h" |
| 2 |
|
| 3 |
#include <cassert> |
| 4 |
|
| 5 |
#include "MessageTcp.h" |
| 6 |
#include "clsocket/ActiveSocket.h" |
| 7 |
|
| 8 |
#include <fmt/format.h> |
| 9 |
|
| 10 |
|
| 11 |
namespace gameconn |
| 12 |
{ |
| 13 |
|
| 14 |
namespace |
| 15 |
{ |
| 16 |
const char* const DEFAULT_HOST = "localhost"; |
| 17 |
const int DEFAULT_PORT = 3879; |
| 18 |
|
| 19 |
inline std::string seqnoPreamble(std::size_t seq) { |
| 20 |
return fmt::format("seqno {0}\n", seq); |
| 21 |
} |
| 22 |
|
| 23 |
struct ScopedDepthCounter { |
| 24 |
int& ref; |
| 25 |
ScopedDepthCounter(int& counter) : ref(counter) { |
| 26 |
ref++; |
| 27 |
} |
| 28 |
~ScopedDepthCounter() { |
| 29 |
ref--; |
| 30 |
} |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
AutomationEngine::AutomationEngine() |
| 35 |
{} |
| 36 |
AutomationEngine::~AutomationEngine() |
| 37 |
{ |
| 38 |
disconnect(true); |
| 39 |
} |
| 40 |
|
| 41 |
bool AutomationEngine::isAlive() const |
| 42 |
{ |
| 43 |
return _connection && _connection->isAlive(); |
| 44 |
} |
| 45 |
bool AutomationEngine::hasLostConnection() const |
| 46 |
{ |
| 47 |
return _connection && !_connection->isAlive(); |
| 48 |
} |
| 49 |
|
| 50 |
bool AutomationEngine::connect() |
| 51 |
{ |
| 52 |
if (isAlive()) |
| 53 |
return true; //already connected |
| 54 |
|
| 55 |
// Make connection using clsocket |
| 56 |
// TODO: make port configurable, as it is in TDM? |
| 57 |
std::unique_ptr<CActiveSocket> connection(new CActiveSocket()); |
| 58 |
if ( |
| 59 |
!connection->Initialize() || |
| 60 |
!connection->SetNonblocking() || |
| 61 |
!connection->Open(DEFAULT_HOST, DEFAULT_PORT)) |
| 62 |
{ |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
_connection.reset(new MessageTcp()); |
| 67 |
_connection->init(std::move(connection)); |
| 68 |
if (!_connection->isAlive()) |
| 69 |
return false; |
| 70 |
|
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
void AutomationEngine::disconnect(bool force) |
| 75 |
{ |
| 76 |
if (force) { |
| 77 |
//drop everything pending |
| 78 |
_multistepProcs.clear(); |
| 79 |
_requests.clear(); |
| 80 |
} |
| 81 |
else { |
| 82 |
//try to finish all pending |
| 83 |
waitForTags(); |
| 84 |
} |
| 85 |
|
| 86 |
if (_connection) |
| 87 |
_connection.reset(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
int AutomationEngine::generateNewSequenceNumber() |
| 92 |
{ |
| 93 |
return ++_seqno; |
| 94 |
} |
| 95 |
|
| 96 |
AutomationEngine::Request* AutomationEngine::sendRequest(int tag, const std::string& request) { |
| 97 |
assert(tag < 31); |
| 98 |
if (!_connection) |
| 99 |
throw DisconnectException(); |
| 100 |
|
| 101 |
Request req; |
| 102 |
req._seqno = generateNewSequenceNumber(); |
| 103 |
req._request = request; |
| 104 |
req._tag = tag; |
| 105 |
req._finished = false; |
| 106 |
|
| 107 |
std::string fullMessage = seqnoPreamble(req._seqno) + req._request; |
| 108 |
_connection->writeMessage(fullMessage.data(), (int)fullMessage.size()); |
| 109 |
_requests.push_back(req); |
| 110 |
|
| 111 |
return &_requests.back(); |
| 112 |
} |
| 113 |
|
| 114 |
void AutomationEngine::think() { |
| 115 |
ScopedDepthCounter dc(_thinkDepth); |
| 116 |
|
| 117 |
if (_connection) { |
| 118 |
_connection->think(); |
| 119 |
|
| 120 |
//check if full response is here |
| 121 |
std::vector<char> responseBytes; |
| 122 |
while (_connection->readMessage(responseBytes)) { |
| 123 |
//validate and remove preamble |
| 124 |
int responseSeqno, lineLen; |
| 125 |
int ret = sscanf(responseBytes.data(), "response %d\n%n", &responseSeqno, &lineLen); |
| 126 |
assert(ret == 1); ret; |
| 127 |
std::string response(responseBytes.begin() + lineLen, responseBytes.end()); |
| 128 |
|
| 129 |
//find request, mark it as "no longer in progress" |
| 130 |
if (Request* req = findRequest(responseSeqno)) { |
| 131 |
req->_finished = true; |
| 132 |
req->_response = response; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
//execute callbacks for finished requests |
| 138 |
for (int i = 0; i < _requests.size(); i++) { |
| 139 |
if (_requests[i]._finished && _requests[i]._callback) { |
| 140 |
_requests[i]._callback(_requests[i]._seqno); |
| 141 |
_requests[i]._callback = {}; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
//don't do some stuff in nested think calls: |
| 146 |
// 1) don't delete finished requests (multistep proc might still need it) |
| 147 |
// 2) don't resume multistep procedures (to avoid recursion) |
| 148 |
if (_thinkDepth == 1) { |
| 149 |
//resume multistep procedures |
| 150 |
for (int i = 0; i < _multistepProcs.size(); i++) { |
| 151 |
MultistepProcedure& proc = _multistepProcs[i]; |
| 152 |
if (!isMultistepProcStillWaiting(proc, false)) { |
| 153 |
//BEWARE: it may call blocking requests, |
| 154 |
//which in turn call think recursively! |
| 155 |
resumeMultistepProcedure(proc._id); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
//remove finished requests |
| 160 |
int k = 0; |
| 161 |
for (int i = 0; i < _requests.size(); i++) { |
| 162 |
if (!_requests[i]._finished) |
| 163 |
_requests[k++] = _requests[i]; |
| 164 |
} |
| 165 |
_requests.resize(k); |
| 166 |
|
| 167 |
//remove finished multistep procedures |
| 168 |
k = 0; |
| 169 |
for (int i = 0; i < _multistepProcs.size(); i++) { |
| 170 |
if (_multistepProcs[i]._currentStep >= 0) |
| 171 |
_multistepProcs[k++] = _multistepProcs[i]; |
| 172 |
} |
| 173 |
_multistepProcs.resize(k); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
bool AutomationEngine::isMultistepProcStillWaiting(const MultistepProcedure& proc, bool waitForPoll) const |
| 178 |
{ |
| 179 |
bool stillWaits = false; |
| 180 |
|
| 181 |
for (int j = 0; j < proc._waitForSeqnos.size() && !stillWaits; j++) { |
| 182 |
int waitSeqno = proc._waitForSeqnos[j]; |
| 183 |
|
| 184 |
if (waitSeqno == SEQNO_WAIT_POLL) { |
| 185 |
if (waitForPoll) |
| 186 |
stillWaits = true; |
| 187 |
} |
| 188 |
else if (Request* req = findRequest(waitSeqno)) { |
| 189 |
if (!req->_finished) |
| 190 |
stillWaits = true; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return stillWaits; |
| 195 |
} |
| 196 |
|
| 197 |
void AutomationEngine::resumeMultistepProcedure(int id) |
| 198 |
{ |
| 199 |
MultistepProcedure* proc; |
| 200 |
do { |
| 201 |
proc = findMultistepProc(id); |
| 202 |
assert(proc); |
| 203 |
|
| 204 |
if (proc->_currentStep < 0) |
| 205 |
break; //finished |
| 206 |
|
| 207 |
auto ret = proc->_function(proc->_currentStep); |
| 208 |
|
| 209 |
proc->_currentStep = ret.nextStep; |
| 210 |
proc->_waitForSeqnos = ret.seqnoWaitList; |
| 211 |
} |
| 212 |
while (!isMultistepProcStillWaiting(*proc, true)); |
| 213 |
} |
| 214 |
|
| 215 |
bool AutomationEngine::areInProgress(const std::vector<int>& reqSeqnos, const std::vector<int>& procIds) |
| 216 |
{ |
| 217 |
for (int seqno : reqSeqnos) |
| 218 |
if (Request* req = findRequest(seqno)) |
| 219 |
if (!req->_finished) |
| 220 |
return true; |
| 221 |
for (int id : procIds) |
| 222 |
if (MultistepProcedure* proc = findMultistepProc(id)) |
| 223 |
if (proc->_currentStep >= 0) |
| 224 |
return true; |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
bool AutomationEngine::areTagsInProgress(int tagMask) |
| 229 |
{ |
| 230 |
for (int i = 0; i < _requests.size(); i++) |
| 231 |
if (tagMask & (1 << _requests[i]._tag)) |
| 232 |
if (!_requests[i]._finished) |
| 233 |
return true; |
| 234 |
for (int i = 0; i < _multistepProcs.size(); i++) |
| 235 |
if (tagMask & (1 << _multistepProcs[i]._tag)) |
| 236 |
if (_multistepProcs[i]._currentStep >= 0) |
| 237 |
return true; |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
void AutomationEngine::wait(const std::vector<int>& reqSeqnos, const std::vector<int>& procIds) |
| 242 |
{ |
| 243 |
while (areInProgress(reqSeqnos, procIds)) { |
| 244 |
if (!isAlive()) |
| 245 |
throw DisconnectException(); |
| 246 |
think(); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
void AutomationEngine::waitForTags(int tagMask) |
| 251 |
{ |
| 252 |
while (areTagsInProgress(tagMask)) { |
| 253 |
if (!isAlive()) |
| 254 |
throw DisconnectException(); |
| 255 |
think(); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
std::string AutomationEngine::executeRequestBlocking(int tag, const std::string& request) |
| 260 |
{ |
| 261 |
Request* req = sendRequest(tag, request); |
| 262 |
int seqno = req->_seqno; |
| 263 |
|
| 264 |
std::string response; |
| 265 |
req->_callback = [this, seqno, &response](int num) { |
| 266 |
Request *req = findRequest(seqno); |
| 267 |
assert(num == seqno && req && req->_finished); |
| 268 |
response = req->_response; |
| 269 |
}; |
| 270 |
|
| 271 |
wait({seqno}, {}); |
| 272 |
|
| 273 |
return response; |
| 274 |
} |
| 275 |
|
| 276 |
int AutomationEngine::executeRequestAsync(int tag, const std::string& request, const std::function<void(int)>& callback) |
| 277 |
{ |
| 278 |
Request* req = sendRequest(tag, request); |
| 279 |
req->_callback = callback; |
| 280 |
return req->_seqno; |
| 281 |
} |
| 282 |
|
| 283 |
int AutomationEngine::executeMultistepProc(int tag, const std::function<MultistepProcReturn(int)>& function, int startStep) |
| 284 |
{ |
| 285 |
assert(tag < 31); |
| 286 |
MultistepProcedure proc; |
| 287 |
proc._id = ++_procIds; |
| 288 |
proc._tag = tag; |
| 289 |
proc._function = function; |
| 290 |
proc._currentStep = startStep; |
| 291 |
_multistepProcs.push_back(proc); |
| 292 |
return proc._id; |
| 293 |
} |
| 294 |
|
| 295 |
AutomationEngine::Request* AutomationEngine::findRequest(int seqno) const |
| 296 |
{ |
| 297 |
for (int i = 0; i < _requests.size(); i++) { |
| 298 |
if (_requests[i]._seqno == seqno) |
| 299 |
return const_cast<Request*>(&_requests[i]); |
| 300 |
} |
| 301 |
return nullptr; |
| 302 |
} |
| 303 |
|
| 304 |
AutomationEngine::MultistepProcedure* AutomationEngine::findMultistepProc(int id) const |
| 305 |
{ |
| 306 |
for (int i = 0; i < _multistepProcs.size(); i++) { |
| 307 |
if (_multistepProcs[i]._id == id) |
| 308 |
return const_cast<MultistepProcedure*>(&_multistepProcs[i]); |
| 309 |
} |
| 310 |
return nullptr; |
| 311 |
} |
| 312 |
|
| 313 |
std::string AutomationEngine::getResponse(int seqno) const |
| 314 |
{ |
| 315 |
const Request* req = findRequest(seqno); |
| 316 |
|
| 317 |
//TODO: perhaps we should simply keep finished requests in memory forever?... |
| 318 |
assert(req); |
| 319 |
if (!req) |
| 320 |
return ""; |
| 321 |
|
| 322 |
assert(req->_finished); |
| 323 |
return req->_response; |
| 324 |
} |
| 325 |
|
| 326 |
} |