Samchon Framework for CPP  1.0.0
System.hpp
1 #pragma once
2 #include <deque>
3 #include <samchon/protocol/EntityGroup.hpp>
4 #include <samchon/examples/interaction/base/SystemBase.hpp>
5 
6 #include <samchon/protocol/ClientDriver.hpp>
7 #include <samchon/examples/interaction/base/ReporterBase.hpp>
8 #include <samchon/examples/interaction/base/MonitorBase.hpp>
9 
10 namespace samchon
11 {
12 namespace examples
13 {
14 namespace interaction
15 {
16  class Monitor;
17 
18  class System
19  : public protocol::EntityGroup<std::deque<System*>, System, int>,
20  public base::SystemBase
21  {
22  private:
23  typedef protocol::EntityGroup<std::deque<System*>, System, int> super;
24 
25  // MONITOR AND PARENT (HIERARCHY) OBJECTS
26  Monitor *monitor;
27  System *parent;
28 
29  // COMMUNICATOR
30  std::shared_ptr<protocol::ClientDriver> driver;
31 
32  // ATOMIC MEMBERS
33  std::string name;
34 
35  public:
36  /* ---------------------------------------------------------
37  CONSTRUCTORS
38  --------------------------------------------------------- */
39  System(Monitor *monitor, std::shared_ptr<protocol::ClientDriver> driver, int uid)
40  : super()
41  {
42  this->monitor = monitor;
43  this->parent = nullptr;
44 
45  this->driver = driver;
46  this->setUID(uid);
47  this->name = "";
48 
49  sendData(std::make_shared<protocol::Invoke>("set_uid", uid));
50  };
51 
52  virtual ~System()
53  {
54  // ERASE THIS OBJECT FROM MAP AND PARENT
55  auto &system_map = ((base::MonitorBase*)monitor)->getSystems();
56  system_map.erase(getUID());
57 
58  if (parent != nullptr)
59  for (size_t i = 0; i < parent->size(); i++)
60  if (parent->at(i) == this)
61  {
62  parent->erase(parent->begin() + i);
63  break;
64  }
65 
66  // LET VIEWERS TO SEND SYSTEM STRUCTURE AGAIN
67  base::ReporterBase *reporter = (base::ReporterBase*)(((base::MonitorBase*)monitor)->getReporter());
68  reporter->sendSystems();
69  };
70 
71  virtual void construct(std::shared_ptr<library::XML> xml) override
72  {
73  setUID(xml->getProperty<int>("uid"));
74  name = xml->getProperty<std::string>("name");
75 
76  super::construct(xml);
77  };
78 
79  protected:
80  virtual auto createChild(std::shared_ptr<library::XML> xml) -> System*
81  {
82  auto &system_map = ((base::MonitorBase*)monitor)->getSystems();
83  int uid = xml->getProperty<int>("uid");
84 
85  if (system_map.has(uid) == false)
86  return nullptr; // NOT EXISTS
87  else
88  {
89  System *system = system_map.get(uid);
90  system->parent = this;
91 
92  return system;
93  }
94  };
95 
96  public:
97  /* ---------------------------------------------------------
98  ACCESSORS
99  --------------------------------------------------------- */
100  virtual auto key() const -> int override
101  {
102  return getUID();
103  };
104 
105  auto getParent() const -> System*
106  {
107  return parent;
108  };
109 
110  void setParent(System *val)
111  {
112  parent = val;
113  };
114 
115  /* ---------------------------------------------------------
116  INVOKE MESSAGE CHAIN
117  --------------------------------------------------------- */
118  virtual void sendData(std::shared_ptr<protocol::Invoke> invoke) override
119  {
120  driver->sendData(invoke);
121  };
122 
123  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke) override
124  {
125  if (invoke->getListener() == "construct")
126  {
127  construct(invoke->front()->getValueAsXML());
128  ((base::MonitorBase*)monitor)->constructSystemTree();
129 
130  // LET VIEWERS TO SEND SYSTEM STRUCTURE
131  Reporter *reporter = ((base::MonitorBase*)monitor)->getReporter();
132  ((base::ReporterBase*)reporter)->sendSystems();
133 
134  System *root = ((base::MonitorBase*)monitor)->getRootSystem();
135  std::cout << root->toXML()->toString() << std::endl;
136 
137  return;
138  }
139  else if (invoke->getListener() == "reportSendData")
140  {
141  std::string listener = invoke->at(0)->getValue<std::string>();
142  int to = invoke->at(1)->getValue<int>();
143  int from = invoke->at(2)->getValue<int>();
144 
145  Reporter *reporter = ((base::MonitorBase*)monitor)->getReporter();
146  auto invoke = std::make_shared<protocol::Invoke>("printSendData", listener, from, to);
147 
148  ((protocol::IProtocol*)reporter)->sendData(invoke);
149  }
150  std::cout << invoke->toXML()->toString() << std::endl;
151  };
152 
153  /* ---------------------------------------------------------
154  EXPORTERS
155  --------------------------------------------------------- */
156  virtual auto TAG() const -> std::string override
157  {
158  return "System";
159  };
160  virtual auto CHILD_TAG() const -> std::string override
161  {
162  return TAG();
163  };
164 
165  virtual auto toXML() const -> std::shared_ptr<library::XML>
166  {
167  std::shared_ptr<library::XML> xml = super::toXML();
168  xml->setProperty("uid", getUID());
169  xml->setProperty("name", name);
170 
171  return xml;
172  };
173  };
174 };
175 };
176 };
virtual void construct(std::shared_ptr< library::XML > xml)
Construct data of the Entity from an XML object.
Definition: EntityGroup.hpp:71
virtual void replyData(std::shared_ptr< Invoke >)=0
virtual void sendData(std::shared_ptr< Invoke >)=0
virtual auto toXML() const -> std::shared_ptr< library::XML >
Get an XML object represents the EntityGroup.