Samchon Framework for CPP  1.0.0
Chief.hpp
1 #pragma once
2 #include <samchon/protocol/ExternalServerArray.hpp>
3 #include <samchon/example/interaction/MasterDriver.hpp>
4 
5 #include <samchon/protocol/Invoke.hpp>
6 
7 #include <samchon/example/packer/Packer.hpp>
8 #include <samchon/example/tsp/Scheduler.hpp>
9 
10 #include <thread>
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 
46  class Chief
47  : public ExternalServerArray
48  {
49  private:
50  typedef ExternalServerArray super;
51 
52  public:
53  /* ---------------------------------------------------------------------------------
54  CONSTRUCTORS
55  --------------------------------------------------------------------------------- */
61  Chief(const string &ip)
62  {
63  emplace_back(new MasterDriver(this, "TSP", "127.0.0.1", 37110));
64  emplace_back(new MasterDriver(this, "Reporter", ip, 37200));
65  emplace_back(new MasterDriver(this, "Packer", ip, 37310));
66  };
67  virtual ~Chief() = default;
68 
69  virtual void start()
70  {
71  thread([this]()
72  {
73  super::start();
74  }).detach();
75 
76  while (true)
77  {
78  int no;
79 
80  cout << "1. TSP Solver, 2. Packer: ";
81  cin >> no;
82 
83  if (no == 1)
84  callTSP();
85  else if (no == 2)
86  callPacker();
87  else
88  continue;
89  }
90  };
91 
92  protected:
93  virtual auto createChild(shared_ptr<XML>) -> ExternalSystem*
94  {
95  return new MasterDriver(this, "", "", 0);
96  };
97 
98  public:
99  /* ---------------------------------------------------------------------------------
100  INVOKE MESSATE CHAIN
101  --------------------------------------------------------------------------------- */
102  virtual void replyData(shared_ptr<Invoke> invoke)
103  {
104  get("Reporter")->sendData(invoke);
105  };
106 
107  private:
116  void callPacker()
117  {
118  // CONSTRUCT PRODUCTS
119  shared_ptr<packer::ProductArray> productArray(new packer::ProductArray());
120 
121  productArray->emplace_back( new packer::Product("Eraser", 500, 10, 70) );
122  productArray->emplace_back( new packer::Product("Pencil", 400, 30, 35) );
123  productArray->emplace_back( new packer::Product("Pencil", 400, 30, 35) );
124  productArray->emplace_back( new packer::Product("Pencil", 400, 30, 35) );
125  productArray->emplace_back( new packer::Product("Book", 8000, 150, 300) );
126  productArray->emplace_back( new packer::Product("Book", 8000, 150, 300) );
127  productArray->emplace_back( new packer::Product("Drink", 1000, 75, 250) );
128  productArray->emplace_back( new packer::Product("Umbrella", 4000, 200, 1000) );
129  productArray->emplace_back( new packer::Product("Notebook-PC", 800000, 150, 850) );
130  productArray->emplace_back( new packer::Product("Tablet-PC", 600000, 120, 450) );
131 
132  // CONSTRUCT PACKER
133  packer::Packer packer(productArray);
134 
135  packer.emplace_back( new packer::WrapperArray("Large", 100, 200, 1000) );
136  packer.emplace_back( new packer::WrapperArray("Medium", 70, 150, 500) );
137  packer.emplace_back( new packer::WrapperArray("Small", 50, 100, 250) );
138 
139  // SEND
140  get("Packer")->sendData
141  (
142  make_shared<Invoke>
143  (
144  "optimize",
145  packer.toXML()
146  )
147  );
148  };
149 
158  void callTSP()
159  {
160  shared_ptr<tsp::Travel> travel = make_shared<tsp::Travel>();
161  for(int i = 0; i < 20; i++)
162  travel->emplace_back(new tsp::GeometryPoint(i + 1));
163 
164  // GENETIC ALGORITHM
165  struct tsp::GAParameters gaParameters = {.03, 30, 400, 400};
166 
167  // SEND
168  tsp::Scheduler scheduler(travel, gaParameters);
169  get("TSP")->sendData(make_shared<Invoke>("optimize", scheduler.toXML()));
170  };
171 
172  public:
173  /* ---------------------------------------------------------------------------------
174  MAIN
175  --------------------------------------------------------------------------------- */
179  static void main()
180  {
181  string ip;
182 
183  cout << "----------------------------------------------------------------------------" << endl;
184  cout << " CHIEF" << endl;
185  cout << "----------------------------------------------------------------------------" << endl;
186  cout << " ip: "; cin >> ip;
187 
188  Chief cheif(ip);
189  cheif.start();
190  };
191  };
192 };
193 };
194 };
void callTSP()
Call tsp master system.
Definition: Chief.hpp:158
A driver for each system, master.
A chief system managing master systems.
Definition: Chief.hpp:46
void callPacker()
Call packer master system.
Definition: Chief.hpp:116
static void main()
Main function.
Definition: Chief.hpp:179
Chief(const string &ip)
Construct from ip address.
Definition: Chief.hpp:61