Samchon Framework for CPP  1.0.0
Server.cpp
1 #include <samchon/protocol/service/Server.hpp>
2 # include <samchon/protocol/service/User.hpp>
3 # include <samchon/protocol/service/IPUserPair.hpp>
4 
5 #include <boost/asio.hpp>
6 #include <mutex>
7 #include <thread>
8 #include <sstream>
9 
10 #include <samchon/SmartPointer.hpp>
11 #include <samchon/library/SQLi.hpp>
12 #include <samchon/library/Datetime.hpp>
13 
14 #include <samchon/library/XML.hpp>
15 #include <samchon/protocol/Invoke.hpp>
16 #include <samchon/protocol/InvokeParameter.hpp>
17 
18 using namespace std;
19 
20 using namespace samchon;
21 using namespace samchon::library;
22 using namespace samchon::protocol;
23 using namespace samchon::protocol::service;
24 
25 using namespace boost::asio;
26 using namespace boost::asio::ip;
27 
28 /* --------------------------------------------------------
29  CONSTRUCTORS & DESTRUCTORS
30 -------------------------------------------------------- */
31 Server::Server()
32  : super(), IServer()
33 {
34  sqli = nullptr;
35  sequence = 0;
36 }
37 Server::~Server()
38 {
39  delete sqli;
40 }
41 
42 /* --------------------------------------------------------
43  GETTERS
44 -------------------------------------------------------- */
45 auto Server::getSQLi() const -> SQLi*
46 {
47  return sqli;
48 }
49 
50 /* --------------------------------------------------------
51  ACCESSORS OF MAP
52 -------------------------------------------------------- */
53 auto Server::size() const -> size_t
54 {
55  return super::size();
56 }
57 auto Server::begin() const -> const_iterator
58 {
59  return super::begin();
60 }
61 auto Server::end() const -> const_iterator
62 {
63  return super::end();
64 }
65 
66 /* --------------------------------------------------------
67  ACCOUNT & REGISTER MANAGER
68 -------------------------------------------------------- */
69 void Server::addClient(Socket *socket)
70 {
71  thread([this, socket]()
72  {
73  //GET IP
74  string &ip = socket->remote_endpoint().address().to_v4().to_string();
75 
76  unique_ptr<UniqueWriteLock> uk(new UniqueWriteLock(mtx));
77  if (ipMap.has(ip) == false)
78  ipMap.set
79  (
80  ip,
81  shared_ptr<IPUserPair>(new IPUserPair(this, ip))
82  );
83 
84  shared_ptr<IPUserPair> pair = ipMap.get(ip);
85  sequence++;
86  uk->unlock();
87 
88  //GET SESSION_ID
89  std::string &sessionID = pair->getSessionID(socket, sequence);
90 
91  uk->lock();
92 
93  //FAILED TO GET SESSION ID
94  if (sessionID.empty() == true)
95  {
96  //ERASE PAIR FROM CONTAINER
97  if (pair->userSet.size() == 1)
98  ipMap.erase(ip);
99 
100  delete socket;
101  return;
102  }
103 
104  iterator it = find(sessionID);
105  if (it == end())
106  {
107  //CREATE USER AND LINK CONNECTION BY PAIR
108  SmartPointer<User> user( createUser() );
109  user->server = this;
110  user->sessionID = sessionID;
111  user->ipPair = pair.get();
112 
113  pair->userSet.insert(user.get());
114  it = insert({ sessionID, user }).first;
115  }
116  uk->unlock();
117  uk.reset(nullptr);
118 
119  //WILL HOLD A THREAD
120  it->second->addClient(socket);
121  }).detach();
122 }
123 void Server::eraseUser(const std::string &session)
124 {
125  Sleep(15 * 1000);
126 
127  UniqueWriteLock uk(mtx);
128  if (!(has(session) == true && get(session)->empty() == true))
129  return;
130 
131  User *user = get(session).get();
132  IPUserPair *ipPair = user->ipPair;
133  ipPair->userSet.erase(user);
134 
135  if (ipPair->userSet.empty() == true)
136  ipMap.erase(user->ipPair->ip);
137 
138  super::erase(session);
139 }
An interface of a physical server.
Definition: IServer.hpp:43
auto begin() const -> const_iterator
A const iterator of begin.
Definition: Server.cpp:57
Global shared pointer .
Definition: RWMutex.hpp:4
Dictionary< std::shared_ptr< IPUserPair > > ipMap
Dictionary of issuer of session ID of each ip.
Definition: Server.hpp:75
Package of libraries.
Definition: library.hpp:84
Relationship between IP address and User for issuing session.
Definition: IPUserPair.hpp:51
auto end() const -> const_iterator
A const iterator of end.
Definition: Server.cpp:61
User containing Client(s) with session-id.
Definition: User.hpp:56
virtual auto createUser() -> User *=0
Factory method of User.
std::set< const User * > userSet
User(s) belongs to the IP address.
Definition: IPUserPair.hpp:70
Package of cloud service as a server.
Package of network protocol and libraries.
Definition: protocol.hpp:185
Unique lock for writing.
virtual void addClient(Socket *) override
Handling connection of a client.
Definition: Server.cpp:69
std::string ip
An IP address.
Definition: IPUserPair.hpp:65
A SQL interface; DBMS connector.
Definition: SQLi.hpp:42
auto size() const -> size_t
Size of User(s).
Definition: Server.cpp:53
library::SQLi * sqli
SQLi for archiving log.
Definition: Server.hpp:67
auto has(const _Kty &key) const -> bool
Whether have the item or not.
Definition: Map.hpp:110
size_t sequence
Sequence for issuing session ID.
Definition: Server.hpp:80
auto get() const -> _Ty *
Get pointer.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7