eris  1.4.0
A WorldForge client library.
MetaQuery.cpp
1 #include "MetaQuery.h"
2 
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 
7 #include "Metaserver.h"
8 #include "Connection.h"
9 
10 #include <Atlas/Objects/Operation.h>
11 #include <Atlas/Objects/Encoder.h>
12 
13 #include <sigc++/slot.h>
14 
15 #include <cassert>
16 
17 using namespace Atlas::Objects::Operation;
18 using WFMath::TimeStamp;
19 
20 namespace Eris {
21 
22 MetaQuery::MetaQuery(boost::asio::io_service& io_service,
23  Atlas::Bridge& bridge,
24  Meta& meta,
25  const std::string& host,
26  size_t sindex) :
27  BaseConnection(io_service, "eris-metaquery", host),
28  _meta(meta),
29  _queryNo(0),
30  m_serverIndex(sindex),
31  m_complete(false),
32  m_completeTimer(io_service) {
33  _bridge = &bridge;
34  connectRemote(host, 6767);
35 }
36 
37 // clean up is all done by the Base Connection
38 MetaQuery::~MetaQuery() = default;
39 
41  // servers must responed to a fully anonymous GET operation
42  // with pertinent info
43  Get gt;
44  gt->setSerialno(getNewSerialno());
45 
46  // send code from Connection
47  _socket->getEncoder().streamObjectsMessage(gt);
48  _socket->write();
49 
50  _stamp = TimeStamp::now();
51 
52  // save our serial-no (so we can identify replies)
53  _queryNo = gt->getSerialno();
54 
55  m_completeTimer.expires_from_now(std::chrono::seconds(10));
56  m_completeTimer.async_wait([&](boost::system::error_code ec) {
57  if (!ec) {
58  this->onQueryTimeout();
59  }
60  });
61 
62 }
63 
64 void MetaQuery::dispatch() {
65  _meta.dispatch();
66 }
67 
69  return (TimeStamp::now() - _stamp).milliseconds();
70 }
71 
72 void MetaQuery::handleFailure(const std::string& msg) {
73  _meta.queryFailure(this, msg);
74 }
75 
76 void MetaQuery::handleTimeout(const std::string&) {
77  _meta.queryTimeout(this);
78 }
79 
80 void MetaQuery::onQueryTimeout() {
81  _meta.queryTimeout(this);
82 }
83 
84 void MetaQuery::setComplete() {
85  assert(!m_complete);
86  m_complete = true;
87  m_completeTimer.cancel();
88 }
89 
90 } // of namsespace
Eris::MetaQuery::_meta
Meta & _meta
The Meta-server object which owns the query.
Definition: MetaQuery.h:56
Eris::MetaQuery::getElapsed
long getElapsed()
Access the elapsed time (in millseconds) since the query was issued.
Definition: MetaQuery.cpp:68
Eris::MetaQuery::_stamp
WFMath::TimeStamp _stamp
Time stamp of the request, to estimate ping to server.
Definition: MetaQuery.h:59
Eris::MetaQuery::handleFailure
void handleFailure(const std::string &msg) override
derived-class notification when a failure occurs
Definition: MetaQuery.cpp:72
Eris::MetaQuery::onConnect
void onConnect() override
Over-ride the default connection behaviour to issue the query.
Definition: MetaQuery.cpp:40
Eris::getNewSerialno
std::int64_t getNewSerialno()
operation serial number sequencing
Definition: Connection.cpp:390
Eris
Definition: Account.cpp:33
Eris::MetaQuery::_queryNo
std::int64_t _queryNo
The serial number of the query GET.
Definition: MetaQuery.h:58