Samchon Framework for CPP  1.0.0
MonitorDriver.hpp
1 #pragma once
2 #include <samchon/protocol/IProtocol.hpp>
3 
4 #include <samchon/examples/interaction/base/SystemBase.hpp>
5 #include <samchon/protocol/ServerConnector.hpp>
6 
7 #include <samchon/templates/external/base/ExternalSystemArrayBase.hpp>
8 
9 namespace samchon
10 {
11 namespace examples
12 {
13 namespace interaction
14 {
15  class MonitorDriver : public protocol::IProtocol
16  {
17  private:
18  base::SystemBase *system;
19  std::unique_ptr<protocol::ServerConnector> connector;
20 
21  public:
22  /* ---------------------------------------------------------
23  CONSTRUCTORS
24  --------------------------------------------------------- */
25  MonitorDriver(base::SystemBase *system)
26  {
27  this->system = system;
28  connector.reset(new protocol::ServerConnector(this));
29  };
30  virtual ~MonitorDriver() = default;
31 
32  void connect()
33  {
34  connector->connect("127.0.0.1", 37900);
35  };
36 
37  /* ---------------------------------------------------------
38  INVOKE MESSAGE CHAIN
39  --------------------------------------------------------- */
40  virtual void sendData(std::shared_ptr<protocol::Invoke> invoke) override
41  {
42  connector->sendData(invoke);
43  };
44  void reportSendData(int to, std::shared_ptr<protocol::Invoke> invoke)
45  {
46  if (system->getUID() == -1)
47  return;
48 
49  sendData(std::make_shared<protocol::Invoke>("reportSendData", to, invoke->getListener()));
50  };
51  void sendSystemStructure()
52  {
53  if (system->getUID() == -1)
54  return;
55 
56  std::shared_ptr<library::XML> xml = system->toXML();
57 
58  auto system_array = dynamic_cast<templates::external::base::ExternalSystemArrayBase*>(system);
59  if (system_array != nullptr)
60  {
61  auto children = system_array->_Get_children();
62  for (size_t i = 0; i < children.size(); i++)
63  {
64  auto child = (base::SystemBase*)(children.at(i).get());
65  if (child->getUID() == -1)
66  return;
67  }
68  }
69 
70  xml->setTag("system");
71  sendData(std::make_shared<protocol::Invoke>("construct", xml));
72  };
73 
74  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke) override
75  {
76  if (invoke->getListener() == "set_uid")
77  system->setUID(invoke->front()->getValue<int>());
78  };
79  };
80 };
81 };
82 };
virtual void replyData(std::shared_ptr< Invoke >)=0
virtual void sendData(std::shared_ptr< Invoke >)=0