Samchon Framework for CPP  1.0.0
User.cpp
1 #include <samchon/protocol/service/User.hpp>
2 # include <samchon/protocol/service/Server.hpp>
3 # include <samchon/protocol/service/Client.hpp>
4 
5 #include <boost/asio.hpp>
6 
7 #include <mutex>
8 #include <samchon/library/Semaphore.hpp>
9 #include <samchon/protocol/Invoke.hpp>
10 
11 using namespace std;
12 
13 using namespace samchon;
14 using namespace samchon::library;
15 using namespace samchon::protocol;
16 using namespace samchon::protocol::service;
17 
18 using namespace boost::asio;
19 using namespace boost::asio::ip;
20 
21 /* --------------------------------------------------------
22  CONSTRUCTORS
23 -------------------------------------------------------- */
24 User::User(Server *server)
25  : super()
26 {
27  this->server = server;
28 
29  semaphore = new Semaphore(2);
30  sequence = 0;
31 
32  id = "guest";
33  authority = 1;
34 }
35 User::~User()
36 {
37 }
38 
39 /* --------------------------------------------------------
40  GETTERS
41 -------------------------------------------------------- */
42 auto User::getServer() const -> Server*
43 {
44  return server;
45 }
46 auto User::getID() const -> std::string
47 {
48  return id;
49 }
50 auto User::getAuthority() const -> int
51 {
52  return authority;
53 }
54 
55 auto User::getSemaphore() const -> Semaphore*
56 {
57  return semaphore;
58 }
59 
60 auto User::size() const -> size_t
61 {
62  return super::size();
63 }
64 auto User::begin() const -> const_iterator
65 {
66  return super::begin();
67 }
68 auto User::end() const -> const_iterator
69 {
70  return super::end();
71 }
72 
73 /* --------------------------------------------------------
74  CLIENT FACTORY
75 -------------------------------------------------------- */
76 void User::addClient(Socket *socket)
77 {
78  UniqueWriteLock uk(mtx);
79 
80  size_t no = ++sequence;
81 
83  client->user = this;
84  client->no = no;
85  client->socket = socket;
86 
87  this->set(no, client);
88 
89  uk.unlock();
90 
91  client->listen();
92  this->eraseClient(no);
93 }
94 void User::eraseClient(size_t no)
95 {
96  KEEP_USER_ALIVE;
97 
98  UniqueWriteLock uk(mtx);
99  {
100  super::erase(no);
101  }
102  uk.unlock();
103 
104  if (empty() == true)
105  server->eraseUser(sessionID);
106 }
107 
108 /* --------------------------------------------------------
109  AUTHENTIFICATION
110 -------------------------------------------------------- */
111 void User::goLogin(shared_ptr<Invoke> invoke)
112 {
113  bool result = doLogin(invoke);
114 
115  shared_ptr<Invoke> reply(new Invoke("handleLogin"));
116  reply->emplace_back(new InvokeParameter("result", result));
117  reply->emplace_back(new InvokeParameter("authority", authority));
118 
119  this->sendData(reply);
120 }
121 void User::goJoin(shared_ptr<Invoke> invoke)
122 {
123  bool success = doJoin(invoke);
124 
125  shared_ptr<Invoke> reply(new Invoke("handleJoin"));
126  reply->emplace_back(new InvokeParameter("success", success));
127 
128  this->sendData(reply);
129 }
131 {
132  id = "guest";
133  authority = 0;
134 
135  shared_ptr<Invoke> invoke(new Invoke("doLogout"));
136  this->sendData(invoke);
137 }
138 
139 /* --------------------------------------------------------
140  MESSAGE CHAIN
141 -------------------------------------------------------- */
142 void User::sendData(shared_ptr<Invoke> invoke)
143 {
144  KEEP_USER_ALIVE;
145  UniqueReadLock uk(mtx);
146 
147  for(auto it = begin(); it != end(); it++)
148  thread(&Client::sendData, it->second, invoke).detach();
149 }
150 void User::replyData(shared_ptr<Invoke> invoke)
151 {
152  KEEP_USER_ALIVE;
153 
154  if (invoke->getListener() == "goLogin")
155  {
156  this->goLogin(invoke);
157  }
158  else if(invoke->getListener() == "goJoin")
159  {
160  this->goJoin(invoke);
161  }
162  else if(invoke->getListener() == "goLogout")
163  {
164  this->goLogout();
165  }
166 }
virtual void replyData(std::shared_ptr< Invoke >) override
Reply a message.
Definition: User.cpp:150
void goLogin(std::shared_ptr< Invoke >)
Log-in.
Definition: User.cpp:111
void unlock()
Unlock on writing.
library::Semaphore * semaphore
Semaphore to limit number of thread.
Definition: User.hpp:118
void sendData(std::shared_ptr< Invoke >)
Send Invoke message to (physical) client.
Definition: Client.cpp:64
auto getSemaphore() const -> library::Semaphore *
Get semaphore.
Definition: User.cpp:55
int authority
Authority allocated to user.
Definition: User.hpp:200
Global shared pointer .
auto getID() const -> std::string
Get user&#39;s account id.
Definition: User.cpp:46
virtual auto doLogin(std::shared_ptr< Invoke >) -> bool=0
Login and return whether success.
Definition: RWMutex.hpp:4
Server * server
Server containing the user.
Definition: User.hpp:73
Package of libraries.
Definition: library.hpp:84
virtual void goLogout()
Log-out.
Definition: User.cpp:130
void addClient(Socket *)
Add a client in user.
Definition: User.cpp:76
auto getAuthority() const -> int
Get authority of user.
Definition: User.cpp:50
auto getServer() const -> Server *
Get server.
Definition: User.cpp:42
Unique lock for reading.
Package of cloud service as a server.
Package of network protocol and libraries.
Definition: protocol.hpp:185
Unique lock for writing.
A server for (cloud) service.
Definition: Server.hpp:48
std::string sessionID
Session ID of the user.
Definition: User.hpp:94
virtual auto createClient() -> Client *=0
Factory method of a Client.
std::string id
Account id.
Definition: User.hpp:133
Standard message of network I/O.
Definition: Invoke.hpp:47
A parameter of an Invoke.
void eraseClient(size_t)
Erase a Client.
Definition: User.cpp:94
library::RWMutex mtx
Mutex for container.
Definition: User.hpp:104
virtual void sendData(std::shared_ptr< Invoke >) override
Send a message.
Definition: User.cpp:142
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
void goJoin(std::shared_ptr< Invoke >)
Join as a member.
Definition: User.cpp:121
virtual auto doJoin(std::shared_ptr< Invoke >) -> bool=0
Join and returns whether success.