SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Echo.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_db_protocol_mngr_req_Echo_h
7 #define swcdb_db_protocol_mngr_req_Echo_h
8 
10 
11 namespace SWC { namespace Comm { namespace Protocol {
12 namespace Mngr { namespace Req {
13 
14 
15 class Echo : public DispatchHandler {
16  public:
17  typedef std::function<void(bool)> EchoCb_t;
18 
19  Echo(const ConnHandlerPtr& a_conn, EchoCb_t&& a_cb, size_t buf_sz=0)
20  : conn(a_conn), cb(std::move(a_cb)), cbp(nullptr) {
21 
22  if(!buf_sz) {
23  cbp = Buffers::make();
24 
25  } else {
26  StaticBuffer sndbuf(buf_sz);
27  uint8_t* ptr = sndbuf.base;
28  const uint8_t* end = sndbuf.base + buf_sz-4;
29 
30  uint8_t i=0;
31  while(ptr < end) {
32  if(i == 127)
33  i = 0;
34  else
35  ++i;
36  *ptr++ = i;
37  }
38 
39  cbp = Buffers::make(sndbuf);
40  }
41 
42  cbp->header.set(DO_ECHO, 60000);
43  }
44 
45  virtual ~Echo() noexcept { }
46 
47  bool run() override {
48  return conn->send_request(cbp, shared_from_this());
49  }
50 
51  void handle_no_conn() override {
52  cb(false);
53  }
54 
55  void handle(ConnHandlerPtr, const Event::Ptr& ev) override {
56  //SWC_LOGF(LOG_DEBUG, "handle: %s", ev->to_str().c_str());
57 
58  cb(ev->header.command == DO_ECHO && !ev->error);
59  }
60 
61  private:
65 };
66 
67 }}}}}
68 
69 #endif // swcdb_db_protocol_mngr_req_Echo_h
SWC::Comm::Buffers::Ptr
std::shared_ptr< Buffers > Ptr
Definition: Buffers.h:23
SWC::Comm::Protocol::Mngr::Req::Echo::conn
ConnHandlerPtr conn
Definition: Echo.h:62
SWC::Comm::Protocol::Mngr::Req::Echo::cbp
Buffers::Ptr cbp
Definition: Echo.h:64
SWC::Comm::Protocol::Mngr::Req::Echo::EchoCb_t
std::function< void(bool)> EchoCb_t
Definition: Echo.h:17
SWC::Comm::Protocol::Mngr::DO_ECHO
@ DO_ECHO
Definition: Commands.h:76
SWC::Comm::Protocol::Mngr::Req::Echo::~Echo
virtual ~Echo() noexcept
Definition: Echo.h:45
SWC::Core::Buffer::base
value_type * base
Definition: Buffer.h:131
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Core::Buffer
Definition: Buffer.h:18
SWC::Comm::Protocol::Mngr::Req::Echo::run
bool run() override
Definition: Echo.h:47
SWC::Comm::Protocol::Mngr::Req::Echo
Definition: Echo.h:15
SWC::Comm::Protocol::Mngr::Req::Echo::cb
EchoCb_t cb
Definition: Echo.h:63
Commands.h
SWC::Comm::ConnHandlerPtr
std::shared_ptr< ConnHandler > ConnHandlerPtr
Definition: AppContext.h:17
SWC::Comm::Protocol::Mngr::Req::Echo::handle_no_conn
void handle_no_conn() override
Definition: Echo.h:51
SWC::Comm::Buffers::make
static SWC_CAN_INLINE Ptr make(uint32_t reserve=0)
Definition: Buffers.h:27
SWC::Comm::DispatchHandler
Definition: DispatchHandler.h:17
SWC::Comm::Protocol::Mngr::Req::Echo::handle
void handle(ConnHandlerPtr, const Event::Ptr &ev) override
Definition: Echo.h:55
SWC::Comm::Protocol::Mngr::Req::Echo::Echo
Echo(const ConnHandlerPtr &a_conn, EchoCb_t &&a_cb, size_t buf_sz=0)
Definition: Echo.h:19
SWC::Comm::Event::Ptr
std::shared_ptr< Event > Ptr
Definition: Event.h:33