Samchon Framework for CPP  1.0.0
Mediator.hpp
1 #pragma once
2 #include <samchon/templates/parallel/ParallelClientArrayMediator.hpp>
3 #include <samchon/examples/interaction/SlaveDriver.hpp>
4 #include <samchon/examples/interaction/base/MasterBase.hpp>
5 
6 #include <samchon/examples/interaction/MasterDriver.hpp>
7 #include <samchon/examples/interaction/MonitorDriver.hpp>
8 #include <samchon/examples/interaction/ChiefDriver.hpp>
9 
10 #include <samchon/examples/interaction/TSPRequest.hpp>
11 #include <samchon/examples/interaction/PackerRequest.hpp>
12 
13 namespace samchon
14 {
15 namespace examples
16 {
17 namespace interaction
18 {
19  class Mediator
20  : public virtual templates::parallel::ParallelClientArrayMediator<SlaveDriver>,
21  public base::MasterBase
22  {
23  private:
24  typedef templates::parallel::ParallelClientArrayMediator<SlaveDriver> super;
25 
26  std::unique_ptr<ChiefDriver> chief;
27 
28  std::string name;
29  int master_port;
30 
31  public:
32  /* ---------------------------------------------------------
33  CONSTRUCTORS
34  --------------------------------------------------------- */
35  Mediator(int masterPort) : super()
36  {
37  this->name = "Mediator";
38  this->master_port = masterPort;
39 
40  chief.reset(new ChiefDriver(this));
41  monitor.reset(new MonitorDriver(this));
42 
43  tsp.reset(new TSPRequest(this));
44  packer.reset(new PackerRequest(this));
45  };
46  virtual ~Mediator() = default;
47 
48  virtual void construct(std::shared_ptr<library::XML> xml) override
49  {
50  setUID(xml->getProperty<int>("uid"));
51  name = xml->getProperty<std::string>("name");
52 
53  super::construct(xml);
54  };
55 
56  protected:
57  virtual auto createMediator() -> templates::parallel::MediatorSystem*
58  {
59  return new MasterDriver(this, master_port);
60  };
61 
62  virtual auto createExternalClient(std::shared_ptr<protocol::ClientDriver> driver) -> SlaveDriver*
63  {
64  return new SlaveDriver(this, driver);
65  };
66 
67  public:
68  /* ---------------------------------------------------------
69  ACCESSORS
70  --------------------------------------------------------- */
71  virtual auto getParent() const -> protocol::IProtocol*
72  {
73  return getMediator();
74  };
75 
76  /* ---------------------------------------------------------
77  INVOKE MESSAGE CHAIN
78  --------------------------------------------------------- */
79  virtual void sendData(std::shared_ptr<protocol::Invoke> invoke)
80  {
81  super::sendData(invoke);
82  };
83 
84  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke)
85  {
86  if (invoke->getListener() == "set_uid")
87  set_uid(invoke->front()->getValue<int>());
88  };
89 
90  private:
91  void set_uid(int val)
92  {
93  setUID(val);
94 
95  // NOTIFY MY ID TO SLAVES
96  sendData(std::make_shared<protocol::Invoke>("set_master_uid", val));
97 
98  // NOTIFY MY UID TO MASTER
99  // Call Master::SlaveSystem::construct()
100  auto xml = toXML();
101  {
102  xml->clear();
103  xml->setTag("system");
104  }
105  getParent()->sendData(std::make_shared<protocol::Invoke>("construct", xml));
106  };
107 
108  public:
109  /* ---------------------------------------------------------
110  EXPORTERS
111  --------------------------------------------------------- */
112  virtual auto TAG() const -> std::string
113  {
114  return super::TAG();
115  };
116 
117  virtual auto toXML() const -> std::shared_ptr<library::XML>
118  {
119  auto xml = super::toXML();
120  xml->setProperty("uid", getUID());
121  xml->setProperty("name", name);
122 
123  return xml;
124  };
125 
126  /* ---------------------------------------------------------
127  MAIN
128  --------------------------------------------------------- */
129  static void main(int masterPort, int myPort)
130  {
131  Mediator mediator(masterPort);
132  mediator.open(myPort);
133  };
134  };
135 };
136 };
137 };
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< 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.