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 Sources - Abstract & Basic classes
- Master.hpp
2 #include <samchon/protocol/master/ParallelClientArray.hpp> 4 #include <samchon/example/interaction/ChiefDriver.hpp> 5 #include <samchon/example/interaction/SlaveDriver.hpp> 6 #include <samchon/protocol/Invoke.hpp> 20 using namespace library;
21 using namespace protocol;
50 :
public master::ParallelClientArray
53 typedef master::ParallelClientArray super;
59 unique_ptr<ChiefDriver> chiefDriver;
87 chiefDriver.reset(
new ChiefDriver(
this, port + 10));
91 virtual ~Master() =
default;
93 virtual void start()
override 95 array<thread, 2> threadArray =
107 for (
auto it = threadArray.begin(); it != threadArray.end(); it++)
112 virtual auto createChild(shared_ptr<XML>) -> ExternalSystem*
override 114 return new SlaveDriver();
121 virtual void replyData(shared_ptr<Invoke> invoke)
override 123 if (invoke->getListener() ==
"optimize")
124 optimize(invoke->at(0)->getValueAsXML());
125 else if (invoke->getListener() ==
"replyOptimization")
126 replyOptimization(invoke->at(0)->getValueAsXML());
130 virtual void addClient(Socket *socket)
override 132 cout <<
"A client has connected." << endl;
134 super::addClient(socket);
147 virtual void optimize(shared_ptr<XML> xml)
149 cout <<
"----------------------------------------------------------------------------" << endl;
150 cout <<
" OPTIMIZE" << endl;
151 cout <<
"----------------------------------------------------------------------------" << endl;
165 virtual void replyOptimization(shared_ptr<XML> xml) = 0;
Top level namespace of products built from samchon.
- Slave.hpp
2 #include <samchon/protocol/slave/ParallelClient.hpp> 4 #include <samchon/protocol/Invoke.hpp> 15 using namespace library;
16 using namespace protocol;
44 :
public protocol::slave::ParallelClient
47 typedef protocol::slave::ParallelClient super;
59 Slave(
const string &ip,
int port)
65 virtual ~Slave() =
default;
70 virtual void replyPieceData(shared_ptr<Invoke> invoke,
size_t index,
size_t size)
override 72 if (invoke->getListener() ==
"optimize")
75 invoke->at(0)->getValueAsXML(),
89 virtual void optimize(shared_ptr<XML> xml,
size_t index,
size_t size)
91 cout <<
"----------------------------------------------------------------------------" << endl;
92 cout <<
" OPTIMIZE FROM " << index <<
", SIZE: " << size << endl;
93 cout <<
"----------------------------------------------------------------------------" << endl;
101 void sendOptimization(shared_ptr<XML> xml)
103 sendData( make_shared<Invoke>(
"replyOptimization", xml) );
Top level namespace of products built from samchon.
- SlaveDriver.hpp
2 #include <samchon/protocol/master/ParallelClient.hpp> 3 #include <samchon/protocol/master/ParallelSystemArray.hpp> 5 #include <samchon/protocol/Invoke.hpp> 17 using namespace library;
18 using namespace protocol;
40 :
public virtual master::ParallelClient
43 typedef master::ParallelClient super;
53 virtual ~SlaveDriver() =
default;
55 virtual void replyData(std::shared_ptr<Invoke> invoke)
override 57 systemArray->replyData(invoke);
Top level namespace of products built from samchon.
- ChiefDriver.hpp
2 #include <samchon/protocol/IServer.hpp> 3 #include <samchon/protocol/IClient.hpp> 5 #include <samchon/protocol/Invoke.hpp> 17 using namespace library;
18 using namespace protocol;
51 typedef IServer super;
76 ChiefDriver(IProtocol *master,
int port)
80 this->master = master;
83 virtual ~ChiefDriver() =
default;
85 virtual void addClient(Socket *socket)
override 87 unique_lock<mutex> uk(mtx);
89 this->socket = socket;
93 virtual void replyData(shared_ptr<Invoke> invoke)
override 95 master->replyData(invoke);
99 virtual auto PORT() const ->
int override
Top level namespace of products built from samchon.
Example Sources - Chief System
- Chief.hpp
2 #include <samchon/protocol/ExternalServerArray.hpp> 3 #include <samchon/example/interaction/MasterDriver.hpp> 5 #include <samchon/protocol/Invoke.hpp> 7 #include <samchon/example/packer/Packer.hpp> 8 #include <samchon/example/tsp/Scheduler.hpp> 20 using namespace library;
21 using namespace protocol;
47 :
public ExternalServerArray
50 typedef ExternalServerArray super;
61 Chief(
const string &ip)
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));
67 virtual ~Chief() =
default;
80 cout <<
"1. TSP Solver, 2. Packer: ";
93 virtual auto createChild(shared_ptr<XML>) -> ExternalSystem*
95 return new MasterDriver(
this,
"",
"", 0);
102 virtual void replyData(shared_ptr<Invoke> invoke)
104 get(
"Reporter")->sendData(invoke);
119 shared_ptr<packer::ProductArray> productArray(
new packer::ProductArray());
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) );
133 packer::Packer packer(productArray);
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) );
140 get(
"Packer")->sendData
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));
165 struct tsp::GAParameters gaParameters = {.03, 30, 400, 400};
168 tsp::Scheduler scheduler(travel, gaParameters);
169 get(
"TSP")->sendData(make_shared<Invoke>(
"optimize", scheduler.toXML()));
183 cout <<
"----------------------------------------------------------------------------" << endl;
184 cout <<
" CHIEF" << endl;
185 cout <<
"----------------------------------------------------------------------------" << endl;
186 cout <<
" ip: "; cin >> ip;
Top level namespace of products built from samchon.
- MasterDriver.hpp
2 #include <samchon/protocol/ExternalServer.hpp> 12 using namespace library;
13 using namespace protocol;
36 :
public protocol::ExternalServer
39 typedef protocol::ExternalServer super;
57 MasterDriver(IProtocol *chief,
const string &name,
const string &ip,
int port)
66 virtual ~MasterDriver() =
default;
69 virtual auto createChild(shared_ptr<XML>) -> ExternalSystemRole*
override 75 virtual void replyData(shared_ptr<Invoke> invoke)
override 77 chief->replyData(invoke);
Top level namespace of products built from samchon.
Example Source - Reporter System
- Reporter.hpp
3 #include <samchon/example/interaction/ChiefDriver.hpp> 5 #include <samchon/example/tsp/Scheduler.hpp> 6 #include <samchon/example/packer/Packer.hpp> 8 #include <samchon/library/XML.hpp> 9 #include <samchon/protocol/Invoke.hpp> 21 using namespace library;
22 using namespace protocol;
47 typedef ChiefDriver super;
57 : super(nullptr, 37200)
60 virtual ~Reporter() =
default;
63 virtual void addClient(Socket *socket)
override 65 cout <<
"The chief has connected." << endl;
67 super::addClient(socket);
74 virtual void replyData(shared_ptr<Invoke> invoke)
77 shared_ptr<XML> &xml = invoke->at(0)->getValueAsXML();
79 if (xml->getTag() ==
"scheduler")
81 else if (xml->getTag() ==
"packer")
91 void printTSP(shared_ptr<XML> xml)
93 tsp::Scheduler scheduler;
94 scheduler.construct(xml);
96 cout <<
"----------------------------------------------------------------------------" << endl;
97 cout <<
" TSP SOLVER" << endl;
98 cout <<
"----------------------------------------------------------------------------" << endl;
99 cout << scheduler.toString() << endl << endl;
107 void printPacker(shared_ptr<XML> xml)
109 packer::Packer packer;
110 packer.construct(xml);
112 cout <<
"----------------------------------------------------------------------------" << endl;
113 cout <<
" PACKER" << endl;
114 cout <<
"----------------------------------------------------------------------------" << endl;
115 cout << packer.toString() << endl << endl;
129 cout <<
"----------------------------------------------------------------------------" << endl;
130 cout <<
" REPOTER" << endl;
131 cout <<
"----------------------------------------------------------------------------" << endl;
Top level namespace of products built from samchon.
Example Sources - Packer Systems
- PackerMaster.hpp
2 #include <samchon/example/interaction/Master.hpp> 4 #include <samchon/example/interaction/ChiefDriver.hpp> 5 #include <samchon/example/interaction/SlaveDriver.hpp> 6 #include <samchon/protocol/Invoke.hpp> 8 #include <samchon/example/packer/Packer.hpp> 9 #include <samchon/library/CombinedPermutationGenerator.hpp> 19 using namespace library;
20 using namespace protocol;
41 shared_ptr<packer::Packer> packer;
53 packer = make_shared<packer::Packer>();
55 virtual ~PackerMaster() =
default;
61 virtual void optimize(shared_ptr<XML> xml)
override 63 super::optimize(
nullptr);
65 packer->construct(xml);
67 CombinedPermutationGenerator caseGen(packer->size(), packer->productSize());
79 virtual void replyOptimization(shared_ptr<XML> xml)
override 81 unique_lock<mutex> uk(mtx);
83 shared_ptr<packer::Packer> packer(
new packer::Packer());
84 packer->construct(xml);
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;
90 if (this->packer->calcPrice() == 0 || packer->calcPrice() < this->packer->calcPrice())
91 this->packer = packer;
93 if (++optimized < this->size())
96 cout <<
"Parallel optimization has completed." << endl;
97 cout << packer->toString() << endl << endl;
99 chiefDriver->sendData( make_shared<Invoke>(
"replyOptimization", packer->toXML()) );
111 cout <<
"----------------------------------------------------------------------------" << endl;
112 cout <<
" PACKER MASTER" << endl;
113 cout <<
"----------------------------------------------------------------------------" << endl;
Top level namespace of products built from samchon.
- PackerMediator.hpp
2 #include <samchon/protocol/master/ParallelClientArrayMediator.hpp> 3 #include <samchon/protocol/master/ParallelSlaveClientMediator.hpp> 5 #include <samchon/example/interaction/SlaveDriver.hpp> 6 #include <samchon/example/packer/Packer.hpp> 8 #include <samchon/protocol/Invoke.hpp> 9 #include <samchon/library/CombinedPermutationGenerator.hpp> 45 :
public protocol::master::ParallelClientArrayMediator
48 typedef protocol::master::ParallelClientArrayMediator super;
54 std::shared_ptr<packer::Packer> packer;
80 dynamic_cast<protocol::master::ParallelSlaveClientMediator*
>(slave)->setAddress(
"127.0.0.1", 37300);
82 packer = std::make_shared<packer::Packer>();
85 virtual ~PackerMediator() =
default;
88 virtual auto createChild(std::shared_ptr<library::XML>) -> protocol::ExternalSystem*
override 90 return new SlaveDriver();
93 virtual void addClient(protocol::Socket *socket)
override 95 std::cout <<
"A client has connected." << std::endl;
97 super::addClient(socket);
104 virtual void sendPieceData(std::shared_ptr<protocol::Invoke> invoke,
size_t index,
size_t size)
106 if (invoke->getListener() ==
"optimize")
108 std::cout <<
"----------------------------------------------------------------------------" << std::endl;
109 std::cout <<
" OPTIMIZE FROM " << index <<
", SIZE: " << size << std::endl;
110 std::cout <<
"----------------------------------------------------------------------------" << std::endl;
114 super::sendPieceData(invoke, index, size);
116 virtual void replyData(std::shared_ptr<protocol::Invoke> invoke)
override 118 if (invoke->getListener() ==
"replyOptimization")
119 replyOptimization(invoke->at(0)->getValueAsXML());
132 void replyOptimization(std::shared_ptr<library::XML> xml)
134 std::unique_lock<std::mutex> uk(mtx);
136 std::shared_ptr<packer::Packer> packer(
new packer::Packer());
137 packer->construct(xml);
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;
143 if (this->packer->calcPrice() == 0 || packer->calcPrice() < this->packer->calcPrice())
144 this->packer = packer;
146 if (++optimized < this->size())
149 std::cout <<
"Parallel optimization has completed." << std::endl;
150 std::cout << packer->toString() << std::endl << std::endl;
152 slave->sendData(std::make_shared<protocol::Invoke>(
"replyOptimization", packer->toXML()));
167 std::cout <<
"----------------------------------------------------------------------------" << std::endl;
168 std::cout <<
" PACKER MEDIATOR" << std::endl;
169 std::cout <<
"----------------------------------------------------------------------------" << std::endl;
171 PackerMediator mediator;
Top level namespace of products built from samchon.
- PackerSlave.hpp
2 #include <samchon/example/interaction/Slave.hpp> 4 #include <samchon/example/packer/Packer.hpp> 5 #include <samchon/protocol/Invoke.hpp> 15 using namespace library;
16 using namespace protocol;
43 PackerSlave(
const string &ip,
int port)
47 virtual ~PackerSlave() =
default;
60 virtual void optimize(shared_ptr<XML> xml,
size_t index,
size_t size)
override 62 super::optimize(xml, index, size);
64 packer::Packer packer;
65 packer.construct(xml);
66 packer.optimize(index, size);
68 cout << packer.toString() << endl << endl;
69 sendOptimization(packer.toXML());
84 cout <<
"----------------------------------------------------------------------------" << endl;
85 cout <<
" PACKER SLAVE" << endl;
86 cout <<
"----------------------------------------------------------------------------" << endl;
87 cout <<
" ip: "; cin >> ip;
88 cout <<
" port: "; cin >> port;
90 PackerSlave slave(ip, port);
Top level namespace of products built from samchon.
Example Sources - TSP Systems
- TSPMaster.hpp
2 #include <samchon/example/interaction/Master.hpp> 4 #include <samchon/example/interaction/ChiefDriver.hpp> 5 #include <samchon/example/interaction/SlaveDriver.hpp> 6 #include <samchon/protocol/Invoke.hpp> 8 #include <samchon/example/tsp/Scheduler.hpp> 18 using namespace library;
19 using namespace protocol;
37 shared_ptr<tsp::Scheduler> scheduler;
49 scheduler = make_shared<tsp::Scheduler>();
51 virtual ~TSPMaster() =
default;
57 virtual void optimize(shared_ptr<XML> xml)
override 59 super::optimize(
nullptr);
61 scheduler->construct(xml);
74 virtual void replyOptimization(shared_ptr<XML> xml)
override 76 unique_lock<mutex> uk(mtx);
78 shared_ptr<tsp::Scheduler> scheduler(
new tsp::Scheduler());
79 scheduler->construct(xml);
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;
85 if (scheduler->calcDistance() < this->scheduler->calcDistance())
86 this->scheduler = scheduler;
88 if (++optimized < this->size())
92 cout <<
"Parallel optimization has completed." << endl;
93 cout << scheduler->toString() << endl << endl;
114 cout <<
"----------------------------------------------------------------------------" << endl;
115 cout <<
" TSP SOLVER MASTER" << endl;
116 cout <<
"----------------------------------------------------------------------------" << endl;
Top level namespace of products built from samchon.
- TSPSlave.hpp
2 #include <samchon/example/interaction/Slave.hpp> 4 #include <samchon/example/tsp/Scheduler.hpp> 16 using namespace library;
17 using namespace protocol;
43 TSPSlave(
const string &ip)
47 virtual ~TSPSlave() =
default;
53 virtual void optimize(shared_ptr<XML> xml,
size_t index,
size_t size)
override 55 super::optimize(xml, index, size);
57 tsp::Scheduler scheduler;
58 scheduler.construct(xml);
61 cout << scheduler.toString() << endl << endl;
62 sendData( make_shared<Invoke>(
"replyOptimization", scheduler.toXML()) );
77 cout <<
"----------------------------------------------------------------------------" << endl;
78 cout <<
" TSP SOLVER SLAVE" << endl;
79 cout <<
"----------------------------------------------------------------------------" << endl;
80 cout <<
" ip: "; cin >> ip;
Top level namespace of products built from samchon.
- See also
- protocol
-
protocol::master
-
protocol::slave
- Author
- Jeongho Nam http://samchon.org