Samchon Framework for CPP  1.0.0
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 {
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  --------------------------------------------------------------------------------- */
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 };
A slave system&#39;s driver for optimization.
Definition: SlaveDriver.hpp:39
std::shared_ptr< packer::Packer > packer
A packer solver.
A mediator of parallel system solving packaging problem.
std::mutex mtx
A mutex for optimization.
size_t optimized
Number of slaves who&#39;d completed an optimization.
void replyOptimization(std::shared_ptr< library::XML > xml)
Handle (replied) optimized value from a slave system.