Samchon Framework for CPP  1.0.0
samchon::example::interaction Namespace Reference

An example of complicate network system. More...

Classes

class  Chief
 A chief system managing master systems. More...
 
class  ChiefDriver
 A boundary class interacting with a Chief system. More...
 
class  Master
 A master of parallel system solving something. More...
 
class  MasterDriver
 A driver for each system, master. More...
 
class  PackerMaster
 A master of parallel system solving packaging problem. More...
 
class  PackerMediator
 A mediator of parallel system solving packaging problem. More...
 
class  PackerSlave
 A slave system for solving Packer. More...
 
class  Reporter
 A reporter printing optimization result on screen. More...
 
class  Slave
 A slave system for optimizing something. More...
 
class  SlaveDriver
 A slave system's driver for optimization. More...
 
class  TSPMaster
 A master for parallel system solving TSP problem. More...
 
class  TSPSlave
 A slave system solving TSP. More...
 

Detailed Description

An example of complicate network system.

The interaction module is an example solving some problems like TSP and Packer not only in a computer in a computer but in a network level, with parallel processing system.

Principle purpose of protocol module in Samchon Framework is to constructing complicate network system interacting with another external network systems and using master, slave modules that can realize (tree-structured) parallel (or distributed) processing system.

The example interaction has built for providing guidance for those things. The interaction demonstrates howto build complicate netwrok system eailsy by considering each system as a class of a S/W, within framework of Object-Oriented Design. Of course, interaction module provides a guidance for using external system and parallel processing system modules, too.

You can learn how to construct a network system interacting with external network system and build (tree-structured) parallel processing systems which are distributing tasks (processes) by segmentation size by following the example, interaction module.

example_interaction.png
example_interaction_network_diagram.png

Example Sources - Abstract & Basic classes

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 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
Slave.hpp
1 #pragma once
2 #include <samchon/protocol/slave/ParallelClient.hpp>
3 
4 #include <samchon/protocol/Invoke.hpp>
5 #include <iostream>
6 
7 namespace samchon
8 {
9 namespace example
10 {
11 namespace interaction
12 {
13  using namespace std;
14 
15  using namespace library;
16  using namespace protocol;
17 
43  class Slave
44  : public protocol::slave::ParallelClient
45  {
46  private:
47  typedef protocol::slave::ParallelClient super;
48 
49  public:
50  /* ------------------------------------------------------------
51  CONSTRUCTORS
52  ------------------------------------------------------------ */
59  Slave(const string &ip, int port)
60  : super()
61  {
62  this->ip = ip;
63  this->port = port;
64  };
65  virtual ~Slave() = default;
66 
67  /* ------------------------------------------------------------
68  INVOKE MESSAGE CHAIN
69  ------------------------------------------------------------ */
70  virtual void replyPieceData(shared_ptr<Invoke> invoke, size_t index, size_t size) override
71  {
72  if (invoke->getListener() == "optimize")
73  optimize
74  (
75  invoke->at(0)->getValueAsXML(),
76  index,
77  size
78  );
79  };
80 
81  protected:
89  virtual void optimize(shared_ptr<XML> xml, size_t index, size_t size)
90  {
91  cout << "----------------------------------------------------------------------------" << endl;
92  cout << " OPTIMIZE FROM " << index << ", SIZE: " << size << endl;
93  cout << "----------------------------------------------------------------------------" << endl;
94  };
95 
101  void sendOptimization(shared_ptr<XML> xml)
102  {
103  sendData( make_shared<Invoke>("replyOptimization", xml) );
104  };
105  };
106 };
107 };
108 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
SlaveDriver.hpp
1 #pragma once
2 #include <samchon/protocol/master/ParallelClient.hpp>
3 #include <samchon/protocol/master/ParallelSystemArray.hpp>
4 
5 #include <samchon/protocol/Invoke.hpp>
6 
7 #include <iostream>
8 
9 namespace samchon
10 {
11 namespace example
12 {
13 namespace interaction
14 {
15  using namespace std;
16 
17  using namespace library;
18  using namespace protocol;
19 
39  class SlaveDriver
40  : public virtual master::ParallelClient
41  {
42  private:
43  typedef master::ParallelClient super;
44 
45  public:
49  SlaveDriver()
50  : super()
51  {
52  };
53  virtual ~SlaveDriver() = default;
54 
55  virtual void replyData(std::shared_ptr<Invoke> invoke) override
56  {
57  systemArray->replyData(invoke);
58  };
59  };
60 };
61 };
62 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
ChiefDriver.hpp
1 #pragma once
2 #include <samchon/protocol/IServer.hpp>
3 #include <samchon/protocol/IClient.hpp>
4 
5 #include <samchon/protocol/Invoke.hpp>
6 
7 #include <mutex>
8 
9 namespace samchon
10 {
11 namespace example
12 {
13 namespace interaction
14 {
15  using namespace std;
16 
17  using namespace library;
18  using namespace protocol;
19 
46  class ChiefDriver
47  : public IServer,
48  public IClient
49  {
50  private:
51  typedef IServer super;
52 
53  protected:
57  IProtocol *master;
58 
62  int port;
63 
67  mutex mtx;
68 
69  public:
76  ChiefDriver(IProtocol *master, int port)
77  : super(),
78  IClient()
79  {
80  this->master = master;
81  this->port = port;
82  };
83  virtual ~ChiefDriver() = default;
84 
85  virtual void addClient(Socket *socket) override
86  {
87  unique_lock<mutex> uk(mtx);
88 
89  this->socket = socket;
90  listen();
91  };
92 
93  virtual void replyData(shared_ptr<Invoke> invoke) override
94  {
95  master->replyData(invoke);
96  };
97 
98  protected:
99  virtual auto PORT() const -> int override
100  {
101  return port;
102  };
103  };
104 };
105 };
106 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7

Example Sources - Chief System

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 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
MasterDriver.hpp
1 #pragma once
2 #include <samchon/protocol/ExternalServer.hpp>
3 
4 namespace samchon
5 {
6 namespace example
7 {
8 namespace interaction
9 {
10  using namespace std;
11 
12  using namespace library;
13  using namespace protocol;
14 
35  class MasterDriver
36  : public protocol::ExternalServer
37  {
38  private:
39  typedef protocol::ExternalServer super;
40 
46  IProtocol *chief;
47 
48  public:
57  MasterDriver(IProtocol *chief, const string &name, const string &ip, int port)
58  : super()
59  {
60  this->chief = chief;
61  this->name = name;
62 
63  this->ip = ip;
64  this->port = port;
65  };
66  virtual ~MasterDriver() = default;
67 
68  protected:
69  virtual auto createChild(shared_ptr<XML>) -> ExternalSystemRole* override
70  {
71  return nullptr;
72  };
73 
74  public:
75  virtual void replyData(shared_ptr<Invoke> invoke) override
76  {
77  chief->replyData(invoke);
78  };
79  };
80 };
81 };
82 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7

Example Source - Reporter System

Reporter.hpp
1 #pragma once
2 
3 #include <samchon/example/interaction/ChiefDriver.hpp>
4 
5 #include <samchon/example/tsp/Scheduler.hpp>
6 #include <samchon/example/packer/Packer.hpp>
7 
8 #include <samchon/library/XML.hpp>
9 #include <samchon/protocol/Invoke.hpp>
10 
11 #include <iostream>
12 
13 namespace samchon
14 {
15 namespace example
16 {
17 namespace interaction
18 {
19  using namespace std;
20 
21  using namespace library;
22  using namespace protocol;
23 
43  class Reporter
44  : public ChiefDriver
45  {
46  private:
47  typedef ChiefDriver super;
48 
49  public:
50  /* ---------------------------------------------------------------------------------
51  CONSTRUCTORS
52  --------------------------------------------------------------------------------- */
56  Reporter()
57  : super(nullptr, 37200)
58  {
59  };
60  virtual ~Reporter() = default;
61 
62  protected:
63  virtual void addClient(Socket *socket) override
64  {
65  cout << "The chief has connected." << endl;
66 
67  super::addClient(socket);
68  };
69 
70  public:
71  /* ---------------------------------------------------------------------------------
72  INVOKE MESSAGE CHAIN
73  --------------------------------------------------------------------------------- */
74  virtual void replyData(shared_ptr<Invoke> invoke)
75  {
76  //PRINT
77  shared_ptr<XML> &xml = invoke->at(0)->getValueAsXML();
78 
79  if (xml->getTag() == "scheduler")
80  printTSP(xml);
81  else if (xml->getTag() == "packer")
82  printPacker(xml);
83  };
84 
85  protected:
91  void printTSP(shared_ptr<XML> xml)
92  {
93  tsp::Scheduler scheduler;
94  scheduler.construct(xml);
95 
96  cout << "----------------------------------------------------------------------------" << endl;
97  cout << " TSP SOLVER" << endl;
98  cout << "----------------------------------------------------------------------------" << endl;
99  cout << scheduler.toString() << endl << endl;
100  };
101 
107  void printPacker(shared_ptr<XML> xml)
108  {
109  packer::Packer packer;
110  packer.construct(xml);
111 
112  cout << "----------------------------------------------------------------------------" << endl;
113  cout << " PACKER" << endl;
114  cout << "----------------------------------------------------------------------------" << endl;
115  cout << packer.toString() << endl << endl;
116  };
117 
118  public:
119  /* ---------------------------------------------------------------------------------
120  MAIN
121  --------------------------------------------------------------------------------- */
125  static void main()
126  {
127  string ip;
128 
129  cout << "----------------------------------------------------------------------------" << endl;
130  cout << " REPOTER" << endl;
131  cout << "----------------------------------------------------------------------------" << endl;
132  cout << endl;
133 
134  Reporter reporter;
135  reporter.open();
136  };
137  };
138 };
139 };
140 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7

Example Sources - Packer Systems

PackerMaster.hpp
1 #pragma once
2 #include <samchon/example/interaction/Master.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 <samchon/example/packer/Packer.hpp>
9 #include <samchon/library/CombinedPermutationGenerator.hpp>
10 
11 namespace samchon
12 {
13 namespace example
14 {
15 namespace interaction
16 {
17  using namespace std;
18 
19  using namespace library;
20  using namespace protocol;
21 
31  class PackerMaster
32  : public Master
33  {
34  private:
35  typedef Master super;
36 
37  protected:
41  shared_ptr<packer::Packer> packer;
42 
43  public:
44  /* ---------------------------------------------------------------------------------
45  CONSTRUCTORS
46  --------------------------------------------------------------------------------- */
50  PackerMaster()
51  : super(37300)
52  {
53  packer = make_shared<packer::Packer>();
54  };
55  virtual ~PackerMaster() = default;
56 
57  protected:
58  /* ---------------------------------------------------------------------------------
59  INVOKE MESSATE CHAIN
60  --------------------------------------------------------------------------------- */
61  virtual void optimize(shared_ptr<XML> xml) override
62  {
63  super::optimize(nullptr);
64 
65  packer->construct(xml);
66 
67  CombinedPermutationGenerator caseGen(packer->size(), packer->productSize());
68 
69  sendSegmentData
70  (
71  make_shared<Invoke>
72  (
73  "optimize",
74  packer->toXML()
75  ),
76  caseGen.size()
77  );
78  };
79  virtual void replyOptimization(shared_ptr<XML> xml) override
80  {
81  unique_lock<mutex> uk(mtx);
82 
83  shared_ptr<packer::Packer> packer(new packer::Packer());
84  packer->construct(xml);
85 
86  cout << "An optimization process from a slave system has completed" << endl;
87  cout << "\tOrdinary minimum price: " << this->packer->calcPrice()
88  << ", New Price from the slave: " << packer->calcPrice() << endl;
89 
90  if (this->packer->calcPrice() == 0 || packer->calcPrice() < this->packer->calcPrice())
91  this->packer = packer;
92 
93  if (++optimized < this->size())
94  return;
95 
96  cout << "Parallel optimization has completed." << endl;
97  cout << packer->toString() << endl << endl;
98 
99  chiefDriver->sendData( make_shared<Invoke>("replyOptimization", packer->toXML()) );
100  };
101 
102  /* ---------------------------------------------------------------------------------
103  MAIN
104  --------------------------------------------------------------------------------- */
105  public:
109  static void main()
110  {
111  cout << "----------------------------------------------------------------------------" << endl;
112  cout << " PACKER MASTER" << endl;
113  cout << "----------------------------------------------------------------------------" << endl;
114 
115  PackerMaster master;
116  master.start();
117  };
118  };
119 };
120 };
121 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
PackerMediator.hpp
1 #pragma once
2 #include <samchon/protocol/master/ParallelClientArrayMediator.hpp>
3 #include <samchon/protocol/master/ParallelSlaveClientMediator.hpp>
4 
5 #include <samchon/example/interaction/SlaveDriver.hpp>
6 #include <samchon/example/packer/Packer.hpp>
7 
8 #include <samchon/protocol/Invoke.hpp>
9 #include <samchon/library/CombinedPermutationGenerator.hpp>
10 
11 #include <iostream>
12 #include <thread>
13 #include <mutex>
14 
15 namespace samchon
16 {
17 namespace example
18 {
19 namespace interaction
20 {
44  class PackerMediator
45  : public protocol::master::ParallelClientArrayMediator
46  {
47  private:
48  typedef protocol::master::ParallelClientArrayMediator super;
49 
50  protected:
54  std::shared_ptr<packer::Packer> packer;
55 
62  std::mutex mtx;
63 
67  size_t optimized;
68 
69  public:
70  /* ---------------------------------------------------------------------------------
71  CONSTRUCTORS
72  --------------------------------------------------------------------------------- */
76  PackerMediator()
77  : super()
78  {
79  this->port = 37350;
80  dynamic_cast<protocol::master::ParallelSlaveClientMediator*>(slave)->setAddress("127.0.0.1", 37300);
81 
82  packer = std::make_shared<packer::Packer>();
83  optimized = 0;
84  };
85  virtual ~PackerMediator() = default;
86 
87  protected:
88  virtual auto createChild(std::shared_ptr<library::XML>) -> protocol::ExternalSystem* override
89  {
90  return new SlaveDriver();
91  };
92 
93  virtual void addClient(protocol::Socket *socket) override
94  {
95  std::cout << "A client has connected." << std::endl;
96 
97  super::addClient(socket);
98  };
99 
100  public:
101  /* ---------------------------------------------------------------------------------
102  INVOKE MESSATE CHAIN
103  --------------------------------------------------------------------------------- */
104  virtual void sendPieceData(std::shared_ptr<protocol::Invoke> invoke, size_t index, size_t size)
105  {
106  if (invoke->getListener() == "optimize")
107  {
108  std::cout << "----------------------------------------------------------------------------" << std::endl;
109  std::cout << " OPTIMIZE FROM " << index << ", SIZE: " << size << std::endl;
110  std::cout << "----------------------------------------------------------------------------" << std::endl;
111 
112  optimized = 0;
113  }
114  super::sendPieceData(invoke, index, size);
115  };
116  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke) override
117  {
118  if (invoke->getListener() == "replyOptimization")
119  replyOptimization(invoke->at(0)->getValueAsXML());
120  };
121 
122  protected:
132  void replyOptimization(std::shared_ptr<library::XML> xml)
133  {
134  std::unique_lock<std::mutex> uk(mtx);
135 
136  std::shared_ptr<packer::Packer> packer(new packer::Packer());
137  packer->construct(xml);
138 
139  std::cout << "An optimization process from a slave system has completed" << std::endl;
140  std::cout << "\tOrdinary minimum price: " << this->packer->calcPrice()
141  << ", New Price from the slave: " << packer->calcPrice() << std::endl;
142 
143  if (this->packer->calcPrice() == 0 || packer->calcPrice() < this->packer->calcPrice())
144  this->packer = packer;
145 
146  if (++optimized < this->size())
147  return;
148 
149  std::cout << "Parallel optimization has completed." << std::endl;
150  std::cout << packer->toString() << std::endl << std::endl;
151 
152  slave->sendData(std::make_shared<protocol::Invoke>("replyOptimization", packer->toXML()));
153  };
154 
155  public:
156  /* ---------------------------------------------------------------------------------
157  MAIN
158  --------------------------------------------------------------------------------- */
162  static void main()
163  {
164  std::string ip;
165  int port;
166 
167  std::cout << "----------------------------------------------------------------------------" << std::endl;
168  std::cout << " PACKER MEDIATOR" << std::endl;
169  std::cout << "----------------------------------------------------------------------------" << std::endl;
170 
171  PackerMediator mediator;
172  mediator.start();
173  };
174  };
175 };
176 };
177 };
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
PackerSlave.hpp
1 #pragma once
2 #include <samchon/example/interaction/Slave.hpp>
3 
4 #include <samchon/example/packer/Packer.hpp>
5 #include <samchon/protocol/Invoke.hpp>
6 
7 namespace samchon
8 {
9 namespace example
10 {
11 namespace interaction
12 {
13  using namespace std;
14 
15  using namespace library;
16  using namespace protocol;
17 
27  class PackerSlave
28  : public Slave
29  {
30  private:
31  typedef Slave super;
32 
33  public:
34  /* ---------------------------------------------------------------------------------
35  CONSTRUCTORS
36  --------------------------------------------------------------------------------- */
43  PackerSlave(const string &ip, int port)
44  : super(ip, port)
45  {
46  };
47  virtual ~PackerSlave() = default;
48 
49  protected:
50  /* ---------------------------------------------------------------------------------
51  INVOKE MESSAGE CHAIN
52  --------------------------------------------------------------------------------- */
60  virtual void optimize(shared_ptr<XML> xml, size_t index, size_t size) override
61  {
62  super::optimize(xml, index, size);
63 
64  packer::Packer packer;
65  packer.construct(xml);
66  packer.optimize(index, size);
67 
68  cout << packer.toString() << endl << endl;
69  sendOptimization(packer.toXML());
70  };
71 
72  public:
73  /* ---------------------------------------------------------------------------------
74  MAIN
75  --------------------------------------------------------------------------------- */
79  static void main()
80  {
81  string ip;
82  int port;
83 
84  cout << "----------------------------------------------------------------------------" << endl;
85  cout << " PACKER SLAVE" << endl;
86  cout << "----------------------------------------------------------------------------" << endl;
87  cout << " ip: "; cin >> ip;
88  cout << " port: "; cin >> port;
89 
90  PackerSlave slave(ip, port);
91  slave.start();
92  };
93  };
94 };
95 };
96 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7

Example Sources - TSP Systems

TSPMaster.hpp
1 #pragma once
2 #include <samchon/example/interaction/Master.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 <samchon/example/tsp/Scheduler.hpp>
9 
10 namespace samchon
11 {
12 namespace example
13 {
14 namespace interaction
15 {
16  using namespace std;
17 
18  using namespace library;
19  using namespace protocol;
20 
30  class TSPMaster
31  : public Master
32  {
33  private:
34  typedef Master super;
35 
36  protected:
37  shared_ptr<tsp::Scheduler> scheduler;
38 
39  public:
40  /* ---------------------------------------------------------------------------------
41  CONSTRUCTORS
42  --------------------------------------------------------------------------------- */
46  TSPMaster()
47  : super(37100)
48  {
49  scheduler = make_shared<tsp::Scheduler>();
50  };
51  virtual ~TSPMaster() = default;
52 
53  protected:
54  /* ---------------------------------------------------------------------------------
55  INVOKE MESSATE CHAIN
56  --------------------------------------------------------------------------------- */
57  virtual void optimize(shared_ptr<XML> xml) override
58  {
59  super::optimize(nullptr);
60 
61  scheduler->construct(xml);
62 
63  sendSegmentData
64  (
65  make_shared<Invoke>
66  (
67  "optimize",
68  scheduler->toXML()
69  ),
70  this->size()
71  );
72  };
73 
74  virtual void replyOptimization(shared_ptr<XML> xml) override
75  {
76  unique_lock<mutex> uk(mtx);
77 
78  shared_ptr<tsp::Scheduler> scheduler(new tsp::Scheduler());
79  scheduler->construct(xml);
80 
81  cout << "An optimization process from a slave system has completed" << endl;
82  cout << "\tOrdinary minimum distance: " << this->scheduler->calcDistance()
83  << ", New Price from the slave: " << scheduler->calcDistance() << endl;
84 
85  if (scheduler->calcDistance() < this->scheduler->calcDistance())
86  this->scheduler = scheduler;
87 
88  if (++optimized < this->size())
89  return;
90 
91  cout << endl;
92  cout << "Parallel optimization has completed." << endl;
93  cout << scheduler->toString() << endl << endl;
94 
95  chiefDriver->sendData
96  (
97  make_shared<Invoke>
98  (
99  "replyOptimization",
100  scheduler->toXML()
101  )
102  );
103  };
104 
105  public:
106  /* ---------------------------------------------------------------------------------
107  MAIN
108  --------------------------------------------------------------------------------- */
112  static void main()
113  {
114  cout << "----------------------------------------------------------------------------" << endl;
115  cout << " TSP SOLVER MASTER" << endl;
116  cout << "----------------------------------------------------------------------------" << endl;
117 
118  TSPMaster master;
119  master.start();
120  };
121  };
122 };
123 };
124 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
TSPSlave.hpp
1 #pragma once
2 #include <samchon/example/interaction/Slave.hpp>
3 
4 #include <samchon/example/tsp/Scheduler.hpp>
5 
6 #include <iostream>
7 
8 namespace samchon
9 {
10 namespace example
11 {
12 namespace interaction
13 {
14  using namespace std;
15 
16  using namespace library;
17  using namespace protocol;
18 
28  class TSPSlave
29  : public Slave
30  {
31  private:
32  typedef Slave super;
33 
34  public:
35  /* ---------------------------------------------------------------------------------
36  CONSTRUCTORS
37  --------------------------------------------------------------------------------- */
43  TSPSlave(const string &ip)
44  : super(ip, 37100)
45  {
46  };
47  virtual ~TSPSlave() = default;
48 
49  protected:
50  /* ---------------------------------------------------------------------------------
51  INVOKE MESSAGE CHAIN
52  --------------------------------------------------------------------------------- */
53  virtual void optimize(shared_ptr<XML> xml, size_t index, size_t size) override
54  {
55  super::optimize(xml, index, size);
56 
57  tsp::Scheduler scheduler;
58  scheduler.construct(xml);
59  scheduler.optimize();
60 
61  cout << scheduler.toString() << endl << endl;
62  sendData( make_shared<Invoke>("replyOptimization", scheduler.toXML()) );
63  };
64 
65  public:
66  /* ---------------------------------------------------------------------------------
67  MAIN
68  --------------------------------------------------------------------------------- */
72  static void main()
73  {
74  string ip;
75  int port;
76 
77  cout << "----------------------------------------------------------------------------" << endl;
78  cout << " TSP SOLVER SLAVE" << endl;
79  cout << "----------------------------------------------------------------------------" << endl;
80  cout << " ip: "; cin >> ip;
81  cout << endl;
82 
83  TSPSlave slave(ip);
84  slave.start();
85  };
86  };
87 };
88 };
89 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
See also
protocol
protocol::master
protocol::slave
Author
Jeongho Nam http://samchon.org