Samchon Framework for CPP  1.0.0
User.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <samchon/protocol/IProtocol.hpp>
5 #include <samchon/TreeMap.hpp>
6 #include <samchon/templates/service/Client.hpp>
7 
8 #include <vector>
9 #include <functional>
10 #include <thread>
11 #include <samchon/library/RWMutex.hpp>
12 
13 namespace samchon
14 {
15 namespace templates
16 {
17 namespace service
18 {
19  class Server;
20 
48  class User
49  : public TreeMap<size_t, std::shared_ptr<Client>>,
50  public virtual protocol::IProtocol
51  {
52  friend class Server;
53 
54  private:
56 
57  // RELATED OBJECTS
58  Server *server;
59  library::RWMutex mtx;
60 
61  // KEY
62  std::string session_id;
63  size_t sequence;
64 
65  // ACCOUTN
66  std::string account;
67  int authority;
68 
69  public:
70  /* ---------------------------------------------------------
71  CONSTRUCTORS
72  --------------------------------------------------------- */
78  User(Server *server)
79  : super()
80  {
81  this->server = server;
82  };
83 
91  virtual ~User() = default;
92 
93  protected:
100  virtual auto createClient() -> Client* = 0;
101 
102  private:
103  void check_empty()
104  {
105  library::UniqueWriteLock uk(mtx);
106 
107  // NO CLIENT, THEN ERASE THIS USER.
108  if (empty() == true)
109  erase_user_function();
110  };
111 
112  public:
113  /* ---------------------------------------------------------
114  ACCESSORS
115  --------------------------------------------------------- */
121  auto getServer() const -> Server*
122  {
123  return server;
124  };
125 
131  auto getAccount() const -> std::string
132  {
133  return account;
134  };
135 
141  auto getAuthority() const -> int
142  {
143  return authority;
144  };
145 
146  auto getMutex() -> library::RWMutex& { return mtx; };
147  auto getMutex() const -> const library::RWMutex& { return mtx; };
148 
167  void setAccount(const std::string &account, int authority)
168  {
169  if (this->account == account) // SAME WITH BEFORE
170  return;
171  else if (this->account.empty() == false) // ACCOUNT IS CHANGED
172  {
173  // ERASE FROM ORDINARY ACCOUNT_MAP
174  library::UniqueWriteLock uk(*account_map_mtx);
175  account_map->erase(this->account);
176  }
177 
178  // SET
179  this->account = account;
180  this->authority = authority;
181 
182  // REGISTER TO ACCOUNT_MAP IN ITS SERVER
183  library::UniqueWriteLock uk(*account_map_mtx);
184  account_map->set(account, my_weak_ptr.lock());
185  };
186 
197  void logout()
198  {
199  if (account.empty() == false)
200  account_map->erase(account);
201 
202  account.clear();
203  };
204 
205  public:
206  /* ---------------------------------------------------------
207  MESSAGE CHAIN
208  --------------------------------------------------------- */
229  virtual void sendData(std::shared_ptr<protocol::Invoke> invoke) override
230  {
231  std::vector<std::thread> threadArray;
232  library::UniqueReadLock uk(mtx);
233 
234  threadArray.reserve(size());
235  for (auto it = begin(); it != end(); it++)
236  threadArray.emplace_back(&Client::sendData, it->second.get(), invoke);
237 
238  uk.unlock();
239  for (auto it = threadArray.begin(); it != threadArray.end(); it++)
240  it->join();
241  };
242 
272  virtual void replyData(std::shared_ptr<protocol::Invoke>) = 0;
273 
274  private:
275  std::weak_ptr<User> my_weak_ptr;
276 
277  library::RWMutex *account_map_mtx;
279 
280  std::function<void()> erase_user_function;
281  };
282 };
283 };
284 };
auto getServer() const -> Server *
Definition: User.hpp:121
auto getAuthority() const -> int
Definition: User.hpp:141
virtual void sendData(std::shared_ptr< protocol::Invoke > invoke) override
Definition: Client.hpp:164
auto getAccount() const -> std::string
Definition: User.hpp:131
void unlock() const
Unlock of read.
Unique lock for reading.
virtual void replyData(std::shared_ptr< protocol::Invoke >)=0
Unique lock for writing.
Customized std::map.
Definition: TreeMap.hpp:87
void setAccount(const std::string &account, int authority)
Definition: User.hpp:167
Customized std::unordered_map.
Definition: HashMap.hpp:103
virtual void sendData(std::shared_ptr< protocol::Invoke > invoke) override
Definition: User.hpp:229
virtual auto createClient() -> Client *=0