TDME2  1.9.200
Server.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 #include <unordered_map>
6 #include <unordered_set>
7 
8 #include <tdme/tdme.h>
12 
13 using std::string;
14 using std::unique_ptr;
15 using std::unordered_map;
16 using std::unordered_set;
17 
20 
21 namespace tdme {
22 namespace network {
23 namespace udpserver {
24 
25 class ServerWorkerThreadPool;
26 
27 /**
28  * Base class for network servers
29  * @author Andreas Drewke
30  */
31 template <typename CLIENT, typename GROUP>
32 class Server {
33 public:
34  typedef unordered_set<string> ClientKeySet;
35  typedef unordered_set<string> GroupKeySet;
36 
37  // forbid class copy
39 
40  /**
41  * @brief Public constructor
42  * @param name server name
43  * @param host host where to bind the server socket
44  * @param port port to listen on
45 s * @param maxCCU max ccu
46  */
47  Server(const string& name, const string& host, uint16_t port, int maxCCU) :
48  name(name),
49  host(host),
50  port(port),
51  maxCCU(maxCCU),
52  clientKeyListsReadWriteLock("nioserver_clientlist"),
53  groupKeyListsReadWriteLock("nioserver_grouplist"),
54  ioThreadCount(1),
57  //
58  }
59 
60  /**
61  * @brief Sets up the numbers of threads to handle IO and framing
62  * @param ioThreadCount IO thread count
63  */
64  inline void setIOThreadCount(int ioThreadCount) {
65  this->ioThreadCount = ioThreadCount;
66  }
67 
68  /**
69  * @brief Sets up the number of workers that handle requests in thread pool
70  * @param workerThreadCount number of workers in thread pool
71  */
72  inline void setWorkerThreadCount(int workerThreadCount) {
73  workerThreadPoolCount = workerThreadCount;
74  }
75 
76  /**
77  * @brief Sets up max number of elements in worker thread pool queue
78  * @param maxElements max elements
79  */
80  inline void setThreadPoolMaxElements(int maxElements) {
81  workerThreadPoolMaxElements = maxElements;
82  }
83 
84  /**
85  * @brief destructor
86  */
87  virtual ~Server() {
88  }
89 
90  /**
91  * @brief get a copy of current client keys
92  * @return client list
93  */
95  // make a copy of the client key set
96  ClientKeySet _clientKeySet;
98  _clientKeySet = clientKeySet;
100  // return copy
101  return _clientKeySet;
102  }
103 
104  /**
105  * @brief retrieve a client by key, the client reference is acquired, must be released after usage
106  * @param clientKey client identification key
107  * @return client or nullptr
108  */
109  CLIENT* getClientByKey(const string& clientKey) {
111  auto it = clientKeyMap.find(clientKey);
112  auto client = it != clientKeyMap.end()?it->second:nullptr;
113  if (client != nullptr) {
114  client->acquireReference();
115  }
117  //
118  return client;
119  }
120 
121  /**
122  * @brief get a copy of current group keys
123  * @return group list
124  */
126  // make a copy of the group key set
127  GroupKeySet _groupKeySet;
129  _groupKeySet = groupKeySet;
131  // return copy
132  return _groupKeySet;
133  }
134 
135  /**
136  * @brief retrieve a group by key, the group reference is acquired, must be released after usage
137  * @param groupKey group identification key
138  * @return group or nullptr
139  */
140  GROUP* getGroupByKey(const string& groupKey) {
142  auto it = groupKeyMap.find(groupKey);
143  auto group = it != groupKeyMap.end()?it->second:nullptr;
144  if (group != nullptr) {
145  group->acquireReference();
146  }
148  //
149  return group;
150  }
151 
152 protected:
153  typedef unordered_map<string, CLIENT*> ClientKeyMap;
154  typedef unordered_map<string, CLIENT*> GroupKeyMap;
155  typedef unordered_set<CLIENT*> ClientSet;
156 
157  /**
158  * @brief sets a client identification key
159  * @param client client
160  * @param &clientKey client identification key
161  * @return if setting the key was successful
162  */
163  bool setClientKey(CLIENT* client, const string &clientKey) {
165  // set new client key association
166  auto it = clientKeyMap.find(clientKey);
167  if (it != clientKeyMap.end()) {
169  // nope, we could't set the key
170  return false;
171  }
172  // remove old client key association if changed
173  if (client->getKey() != clientKey) {
174  clientKeyMap.erase(client->getKey());
175  }
176  // set client in map
177  clientKeyMap[clientKey] = client;
178  // set client key in set
179  clientKeySet.insert(clientKey);
181  // success
182  return true;
183  }
184 
185  /**
186  * @brief closes a client connection
187  * @param client client
188  */
189  void closeClient(CLIENT* client) {
191  // erase client in client map
192  clientKeyMap.erase(client->getKey());
193  // erase client key in key set
194  clientKeySet.erase(client->getKey());
195  //
197  }
198 
199  /**
200  * @brief sets a group identification key
201  * @param group group
202  * @param &groupKey group identification key
203  * @return if setting the key was successful
204  */
205  bool setGroupKey(GROUP* group, const string &groupKey) {
207  // set new client key association
208  auto it = groupKeyMap.find(groupKey);
209  if (it != groupKeyMap.end()) {
211  // nope, we could't set the key
212  return false;
213  }
214  // remove old client key association if changed
215  if (group->getKey() != groupKey) {
216  groupKeyMap.erase(group->getKey());
217  }
218  // set group in map
219  groupKeyMap[groupKey] = group;
220  // set client key in set
221  groupKeySet.insert(groupKey);
223  // success
224  return true;
225  }
226 
227  /**
228  * @brief closes a group connection
229  * @param group group
230  */
231  void closeGroup(GROUP* group) {
233  // erase client in client map
234  groupKeyMap.erase(group->getKey());
235  // erase client key in key set
236  groupKeySet.erase(group->getKey());
237  //
239  }
240 
241  string name;
242  string host;
243  uint16_t port;
244  int maxCCU;
245 
248 
250 
253 
255 
259 };
260 
261 };
262 };
263 };
Base class for network servers.
Definition: Server.h:32
bool setClientKey(CLIENT *client, const string &clientKey)
sets a client identification key
Definition: Server.h:163
unordered_map< string, CLIENT * > GroupKeyMap
Definition: Server.h:154
CLIENT * getClientByKey(const string &clientKey)
retrieve a client by key, the client reference is acquired, must be released after usage
Definition: Server.h:109
ReadWriteLock clientKeyListsReadWriteLock
Definition: Server.h:249
ClientKeySet getClientKeySet()
get a copy of current client keys
Definition: Server.h:94
virtual ~Server()
destructor
Definition: Server.h:87
unordered_set< string > ClientKeySet
Definition: Server.h:34
void setThreadPoolMaxElements(int maxElements)
Sets up max number of elements in worker thread pool queue.
Definition: Server.h:80
void closeClient(CLIENT *client)
closes a client connection
Definition: Server.h:189
void setIOThreadCount(int ioThreadCount)
Sets up the numbers of threads to handle IO and framing.
Definition: Server.h:64
void setWorkerThreadCount(int workerThreadCount)
Sets up the number of workers that handle requests in thread pool.
Definition: Server.h:72
ReadWriteLock groupKeyListsReadWriteLock
Definition: Server.h:254
bool setGroupKey(GROUP *group, const string &groupKey)
sets a group identification key
Definition: Server.h:205
unordered_set< CLIENT * > ClientSet
Definition: Server.h:155
GROUP * getGroupByKey(const string &groupKey)
retrieve a group by key, the group reference is acquired, must be released after usage
Definition: Server.h:140
unordered_set< string > GroupKeySet
Definition: Server.h:35
unordered_map< string, CLIENT * > ClientKeyMap
Definition: Server.h:153
void closeGroup(GROUP *group)
closes a group connection
Definition: Server.h:231
ClientKeySet getGroupKeySet()
get a copy of current group keys
Definition: Server.h:125
Barrier implementation.
Definition: Barrier.h:21
Implementation for read/write lock.
Definition: ReadWriteLock.h:17
void writeLock()
Locks for writing / exclusive lock.
Definition: ReadWriteLock.h:43
void unlock()
Unlocks this read write lock.
Definition: ReadWriteLock.h:50
void readLock()
Locks for reading / shared lock.
Definition: ReadWriteLock.h:36
Definition: fwd-tdme.h:4
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6