Samchon Framework for CPP  1.0.0
ExternalClientArray.cpp
1 #include <samchon/protocol/ExternalClientArray.hpp>
2 
3 #include <samchon/protocol/ExternalClient.hpp>
4 
5 #include <thread>
6 #include <boost/asio.hpp>
7 #include <samchon/library/XML.hpp>
8 
9 using namespace std;
10 using namespace samchon::library;
11 using namespace samchon::protocol;
12 
13 /* ------------------------------------------------------------------
14  CONSTRUCTORS
15 ------------------------------------------------------------------ */
16 ExternalClientArray::ExternalClientArray()
17  : super(),
18  IServer()
19 {
20 }
21 void ExternalClientArray::construct(shared_ptr<XML> xml)
22 {
23  super::construct(xml);
24 
25  if(xml->hasProperty("myIP") == true)
26  this->myIP = xml->getProperty("myIP");
27  else
28  this->myIP.clear();
29 
30  this->port = xml->getProperty<int>("port");
31 }
32 
34 {
35  this->open();
36 }
37 
38 /* ------------------------------------------------------------------
39  ISERVER METHODS
40 ------------------------------------------------------------------ */
41 auto ExternalClientArray::PORT() const -> int
42 {
43  return port;
44 }
45 auto ExternalClientArray::MY_IP() const -> string
46 {
47  return myIP;
48 }
49 
50 void ExternalClientArray::addClient(Socket *socket)
51 {
52  ExternalClient *client = dynamic_cast<ExternalClient*>(createChild(nullptr));
53 
54  if(client == nullptr)
55  throw exception("invalid factory method");
56 
57  string &ip = socket->remote_endpoint().address().to_string();
58  int port = socket->remote_endpoint().port();
59 
60  client->ip = ip;
61  client->port = port;
62  client->socket = socket;
63 
64  emplace_back(client);
65 
66  thread(&ExternalClient::start, client).detach();
67 }
68 
69 /* ------------------------------------------------------------------
70  EXPORTERS
71 ------------------------------------------------------------------ */
72 auto ExternalClientArray::toXML() const -> shared_ptr<XML>
73 {
74  shared_ptr<XML> &xml = super::toXML();
75 
76  if(myIP.empty() == false)
77  xml->setProperty("myIP", myIP);
78 
79  return xml;
80 }
An interface of a physical server.
Definition: IServer.hpp:43
An array of ExternalSystem(s).
int port
A port number of an external system.
virtual void addClient(Socket *) override
Handling connection of a physical client.
Definition: RWMutex.hpp:4
int port
A port number for accepting external clients.
Package of libraries.
Definition: library.hpp:84
virtual auto PORT() const -> int
Port number of the server.
A network driver for an external client.
std::string ip
An ip address of an external system.
virtual void start() override
Start interaction.
virtual auto MY_IP() const -> std::string
(optional) Server&#39;s IP
virtual auto toXML() const -> std::shared_ptr< library::XML >
Get an XML object represents the EntityGroup.
Package of network protocol and libraries.
Definition: protocol.hpp:185
std::string myIP
A custom ip address of my system to bind.
virtual void start() override
Start interaction.
virtual void construct(std::shared_ptr< library::XML >) override
Construct data of the Entity from an XML object.
Socket * socket
Socket for network I/O.
Definition: IClient.hpp:60
XML is a class representing xml object.
Definition: XML.hpp:72
virtual void open()
Open the server.
Definition: IServer.cpp:30
virtual void construct(std::shared_ptr< library::XML > xml)
Construct data of the Entity from an XML object.
Definition: EntityGroup.hpp:90
virtual auto toXML() const -> std::shared_ptr< library::XML > override
Get an XML object represents the EntityGroup.
virtual auto createChild(std::shared_ptr< library::XML >) -> entity_type *=0
Factory method of a child Entity.