Samchon Framework for CPP  1.0.0
Chief.hpp
1 #pragma once
2 #include <samchon/templates/distributed/DistributedClientArray.hpp>
3 # include <samchon/examples/interaction/MasterSystem.hpp>
4 #include <samchon/examples/interaction/base/ChiefBase.hpp>
5 
6 #include <samchon/examples/interaction/TSPProcess.hpp>
7 #include <samchon/examples/interaction/PackerProcess.hpp>
8 
9 namespace samchon
10 {
11 namespace examples
12 {
13 namespace interaction
14 {
15  class Chief
16  : public virtual templates::distributed::DistributedClientArray<MasterSystem>,
17  public base::ChiefBase
18  {
19  private:
20  typedef templates::distributed::DistributedClientArray<MasterSystem> super;
21 
22  public:
23  /* ---------------------------------------------------------
24  CONSTRUCTORS
25  --------------------------------------------------------- */
26  Chief() : super()
27  {
28  monitor.reset(new MonitorDriver(this));
29 
30  insertProcess(std::make_shared<TSPProcess>(this));
31  insertProcess(std::make_shared<PackerProcess>(this));
32  };
33  virtual ~Chief() = default;
34 
35  virtual void construct(std::shared_ptr<library::XML> xml) override
36  {
37  setUID(xml->getProperty<int>("uid"));
38 
39  super::construct(xml);
40  };
41 
42  virtual void open(int port) override
43  {
44  std::thread(&MonitorDriver::connect, monitor.get()).detach();
45 
46  super::open(port);
47  };
48 
49  protected:
50  virtual auto createProcess(std::shared_ptr<library::XML> xml) -> templates::distributed::DistributedProcess* override
51  {
52  if (xml->getProperty("name") == "tsp")
53  return new TSPProcess(this);
54  else
55  return new PackerProcess(this);
56  };
57 
58  virtual auto createExternalClient(std::shared_ptr<protocol::ClientDriver> driver) -> MasterSystem* override
59  {
60  return new MasterSystem(this, driver);
61  };
62 
63  public:
64  /* ---------------------------------------------------------
65  INVOKE MESSAGE CHAIN
66  --------------------------------------------------------- */
67  virtual void sendData(std::shared_ptr<protocol::Invoke> invoke) override
68  {
69  super::sendData(invoke);
70  };
71 
72  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke) override
73  {
74  if (invoke->getListener() == "set_uid")
75  set_uid(invoke->front()->getValue<size_t>());
76  };
77 
78  private:
79  void set_uid(int val)
80  {
81  setUID(val);
82 
83  // NOTIFY MY ID TO SLAVES (MASTER SYSTEMS)
84  sendData(std::make_shared<protocol::Invoke>("set_chief_uid", getUID()));
85  };
86 
87  public:
88  /* ---------------------------------------------------------
89  EXPORTERS
90  --------------------------------------------------------- */
91  virtual auto TAG() const -> std::string override
92  {
93  return super::TAG();
94  };
95 
96  virtual auto toXML() const -> std::shared_ptr<library::XML> override
97  {
98  auto xml = super::toXML();
99  xml->setProperty("uid", getUID());
100  xml->setProperty("name", "chief");
101 
102  return xml;
103  };
104 
105  /* ---------------------------------------------------------
106  STATIC MAIN
107  --------------------------------------------------------- */
108  static void main()
109  {
110  Chief chief;
111  std::thread(&Chief::open, &chief, 37000).detach();
112 
113  while (true)
114  {
115 #ifdef _WIN32
116  system("cls");
117 #else
118  system("clear");
119 #endif
120 
121  std::cout << "Select what to do." << std::endl;
122  std::cout << " 1. Solve TSP" << std::endl;
123  std::cout << " 2. Solve Packer" << std::endl;
124 
125  int no = std::cin.get();
126  if (no == 1)
127  std::dynamic_pointer_cast<TSPProcess>(chief.getProcess("tsp"))->solve();
128  else if (no == 2)
129  std::dynamic_pointer_cast<PackerProcess>(chief.getProcess("packer"))->solve();
130  }
131  };
132  };
133 };
134 };
135 };
virtual void open(int port)
Definition: Server.hpp:74
virtual void construct(std::shared_ptr< library::XML > xml) override
virtual void replyData(std::shared_ptr< Invoke >)=0
virtual void sendData(std::shared_ptr< protocol::Invoke > invoke)
virtual auto toXML() const -> std::shared_ptr< library::XML > override
virtual void sendData(std::shared_ptr< Invoke >)=0