Samchon Framework for CPP  1.0.0
Master.hpp
1 #pragma once
2 #include <samchon/templates/parallel/ParallelClientArray.hpp>
3 #include <samchon/examples/interaction/SlaveDriver.hpp>
4 #include <samchon/examples/interaction/base/MasterBase.hpp>
5 
6 #include <samchon/examples/interaction/MonitorDriver.hpp>
7 #include <samchon/examples/interaction/ChiefDriver.hpp>
8 #include <samchon/examples/interaction/TSPRequest.hpp>
9 #include <samchon/examples/interaction/PackerRequest.hpp>
10 
11 namespace samchon
12 {
13 namespace examples
14 {
15 namespace interaction
16 {
17  class Master
18  : public virtual templates::parallel::ParallelClientArray<SlaveDriver>,
19  public base::MasterBase
20  {
21  private:
22  typedef templates::parallel::ParallelClientArray<SlaveDriver> super;
23 
24  std::unique_ptr<ChiefDriver> chief;
25 
26  std::string name;
27 
28  public:
29  /* ---------------------------------------------------------
30  CONSTRUCTORS
31  --------------------------------------------------------- */
32  Master() : super()
33  {
34  name = "Master";
35 
36  chief.reset(new ChiefDriver(this));
37  monitor.reset(new MonitorDriver(this));
38 
39  tsp.reset(new TSPRequest(this));
40  packer.reset(new PackerRequest(this));
41  };
42  virtual ~Master() = default;
43 
44  virtual void construct(std::shared_ptr<library::XML> xml) override
45  {
46  setUID(xml->getProperty<int>("uid"));
47  name = xml->getProperty<std::string>("name");
48 
49  super::construct(xml);
50  };
51 
52  virtual void open(int port) override
53  {
54  std::thread(&ChiefDriver::connect, chief.get(), "127.0.0.1", 37000).detach();
55  std::thread(&MonitorDriver::connect, monitor.get()).detach();
56 
57  super::open(port);
58  };
59 
60  protected:
61  virtual auto createExternalClient(std::shared_ptr<protocol::ClientDriver> driver) -> SlaveDriver*
62  {
63  return new SlaveDriver(this, driver);
64  };
65 
66  public:
67  /* ---------------------------------------------------------
68  ACCESSORS
69  --------------------------------------------------------- */
70  virtual auto getParent() const -> protocol::IProtocol* override
71  {
72  return chief.get();
73  };
74 
75  /* ---------------------------------------------------------
76  INVOKE MESSAGE CHAIN
77  --------------------------------------------------------- */
78  virtual void sendData(std::shared_ptr<protocol::Invoke> invoke)
79  {
80  super::sendData(invoke);
81  };
82 
83  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke)
84  {
85  if (invoke->getListener() == "set_uid")
86  set_uid(invoke->front()->getValue<int>());
87  };
88 
89  private:
90  void set_uid(int val)
91  {
92  setUID(val);
93 
94  // NOTIFY MY ID TO SLAVES
95  sendData(std::make_shared<protocol::Invoke>("set_master_uid", val));
96 
97  // NOTIFY MY UID TO MASTER
98  // Call Master::SlaveSystem::construct()
99  auto xml = toXML();
100  {
101  xml->clear();
102  xml->setTag("system");
103  }
104  getParent()->sendData(std::make_shared<protocol::Invoke>("construct", xml));
105  };
106 
107  protected:
108  virtual auto _Complete_history(std::shared_ptr<templates::InvokeHistory> history) -> bool override
109  {
110  bool ret = super::_Complete_history(history);
111 
112  if (ret == true && p_invoke_queue.empty() == false)
113  {
114  // COMPLETE A HISTORY AND POP IT.
115  p_invoke_queue.front()->complete();
116  p_invoke_queue.pop();
117  }
118  return ret;
119  };
120 
121  public:
122  /* ---------------------------------------------------------
123  EXPORTERS
124  --------------------------------------------------------- */
125  virtual auto TAG() const -> std::string
126  {
127  return super::TAG();
128  };
129 
130  virtual auto toXML() const -> std::shared_ptr<library::XML>
131  {
132  auto xml = super::toXML();
133  xml->setProperty("uid", getUID());
134  xml->setProperty("name", name);
135 
136  return xml;
137  };
138 
139  /* ---------------------------------------------------------
140  STATIC MAIN
141  --------------------------------------------------------- */
142  static void main(int port)
143  {
144  Master master;
145  master.open(port);
146  };
147  };
148 };
149 };
150 };
virtual void construct(std::shared_ptr< library::XML > xml)
Construct data of the Entity from an XML object.
Definition: EntityGroup.hpp:71
virtual void open(int port)
Definition: Server.hpp:74
virtual void replyData(std::shared_ptr< Invoke >)=0
virtual void sendData(std::shared_ptr< protocol::Invoke > invoke)
virtual void sendData(std::shared_ptr< Invoke >)=0
virtual auto toXML() const -> std::shared_ptr< library::XML >
Get an XML object represents the EntityGroup.