Samchon Framework for CPP  1.0.0
Monitor.hpp
1 #pragma once
2 #include <samchon/protocol/Server.hpp>
3 #include <samchon/examples/interaction/base/MonitorBase.hpp>
4 
5 #include <samchon/examples/interaction/System.hpp>
6 #include <samchon/examples/interaction/Reporter.hpp>
7 
8 namespace samchon
9 {
10 namespace examples
11 {
12 namespace interaction
13 {
14  class Monitor
15  : public protocol::Server,
16  public base::MonitorBase
17  {
18  private:
19  int sequence;
20 
21  public:
22  /* ---------------------------------------------------------
23  CONSTRUCTORS
24  --------------------------------------------------------- */
25  Monitor() : protocol::Server()
26  {
27  reporter.reset(new Reporter(this));
28  sequence = 0;
29  };
30  virtual ~Monitor() = default;
31 
32  virtual void constructSystemTree() override
33  {
34  for (auto it = system_map.begin(); it != system_map.end(); it++)
35  it->second->clear();
36 
37  for (auto it = system_map.begin(); it != system_map.end(); it++)
38  if (it->second->getParent() != nullptr)
39  it->second->getParent()->push_back(it->second);
40  };
41 
42  protected:
43  virtual void addClient(std::shared_ptr<protocol::ClientDriver> driver) override
44  {
45  int uid = ++sequence;
46  std::unique_ptr<System> system(new System(this, driver, uid));
47 
48  system_map.emplace(uid, system.get());
49  driver->listen(system.get());
50 
51  system_map.erase(uid);
52  };
53 
54  public:
55  /* ---------------------------------------------------------
56  ACCESSORS
57  --------------------------------------------------------- */
58  virtual auto getRootSystem() const -> System*
59  {
60  if (system_map.empty())
61  return nullptr;
62 
63  // FIND ROOT (CHIEF) SYSTEM
64  // CHIEF MAY THE FIRST
65  System *system = system_map.begin()->second;
66  while (system->getParent() != nullptr)
67  system = system->getParent();
68 
69  return system;
70  };
71 
72  /* ---------------------------------------------------------
73  MAIN
74  --------------------------------------------------------- */
75  static void main()
76  {
77  Monitor monitor;
78  monitor.open(37900);
79  monitor.getReporter()->open(37950);
80  };
81  };
82 };
83 };
84 };
virtual void addClient(std::shared_ptr< ClientDriver >)=0