Samchon Framework for CPP  1.0.0
Master.hpp
1 #pragma once
2 #include <samchon/protocol/master/ParallelClientArray.hpp>
3 
4 #include <samchon/example/interaction/ChiefDriver.hpp>
5 #include <samchon/example/interaction/SlaveDriver.hpp>
6 #include <samchon/protocol/Invoke.hpp>
7 
8 #include <array>
9 #include <iostream>
10 #include <mutex>
11 
12 namespace samchon
13 {
14 namespace example
15 {
16 namespace interaction
17 {
18  using namespace std;
19 
20  using namespace library;
21  using namespace protocol;
22 
49  class Master
50  : public master::ParallelClientArray
51  {
52  private:
53  typedef master::ParallelClientArray super;
54 
55  protected:
59  unique_ptr<ChiefDriver> chiefDriver;
60 
67  mutex mtx;
68 
72  size_t optimized;
73 
74  public:
75  /* ------------------------------------------------------------
76  CONSTRUCTORS
77  ------------------------------------------------------------ */
83  Master(int port)
84  : super()
85  {
86  this->port = port;
87  chiefDriver.reset(new ChiefDriver(this, port + 10));
88 
89  optimized = 0;
90  };
91  virtual ~Master() = default;
92 
93  virtual void start() override
94  {
95  array<thread, 2> threadArray =
96  {
97  thread([this]()
98  {
99  super::start();
100  }),
101  thread([this]()
102  {
103  chiefDriver->open();
104  })
105  };
106 
107  for (auto it = threadArray.begin(); it != threadArray.end(); it++)
108  it->join();
109  };
110 
111  protected:
112  virtual auto createChild(shared_ptr<XML>) -> ExternalSystem* override
113  {
114  return new SlaveDriver();
115  };
116 
117  public:
118  /* ------------------------------------------------------------
119  INVOKE MESSAGE CHAIN
120  ------------------------------------------------------------ */
121  virtual void replyData(shared_ptr<Invoke> invoke) override
122  {
123  if (invoke->getListener() == "optimize")
124  optimize(invoke->at(0)->getValueAsXML());
125  else if (invoke->getListener() == "replyOptimization")
126  replyOptimization(invoke->at(0)->getValueAsXML());
127  };
128 
129  protected:
130  virtual void addClient(Socket *socket) override
131  {
132  cout << "A client has connected." << endl;
133 
134  super::addClient(socket);
135  };
136 
147  virtual void optimize(shared_ptr<XML> xml)
148  {
149  cout << "----------------------------------------------------------------------------" << endl;
150  cout << " OPTIMIZE" << endl;
151  cout << "----------------------------------------------------------------------------" << endl;
152 
153  optimized = 0;
154  };
155 
165  virtual void replyOptimization(shared_ptr<XML> xml) = 0;
166  };
167 };
168 };
169 };
A slave system&#39;s driver for optimization.
Definition: SlaveDriver.hpp:39
A master of parallel system solving something.
Definition: Master.hpp:49
mutex mtx
A mutex for optimization.
Definition: Master.hpp:67
size_t optimized
Number of slaves who&#39;d completed an optimization.
Definition: Master.hpp:72
A boundary class interacting with a Chief system.
Definition: ChiefDriver.hpp:46
unique_ptr< ChiefDriver > chiefDriver
A boundary object interacting with a Cheif.
Definition: Master.hpp:59
Master(int port)
Construct from a port number to listen.
Definition: Master.hpp:83
virtual void optimize(shared_ptr< XML > xml)
Optimize something.
Definition: Master.hpp:147