TDME2  1.9.200
ServerGroup.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <unordered_set>
6 
7 #include <tdme/tdme.h>
8 
11 
15 
16 using std::string;
17 using std::to_string;
18 using std::unordered_map;
19 using std::unordered_set;
20 
24 
25 namespace tdme {
26 namespace network {
27 namespace udpserver {
28 
29 /**
30  * Base class for network server groups
31  * @author Andreas Drewke
32  */
33 template <typename SERVER, typename CLIENT, typename GROUP>
34 class ServerGroup : public ServerGroupBase {
35 public:
36  typedef unordered_set<string> ClientKeySet;
37 
38  // forbid class copy
40 
41  /**
42  * Public constructor
43  */
44  ServerGroup(const uint32_t groupId) :
45  server(nullptr),
47  clientKeyListsReadWriteLock("nioservergroup_clientlists"){
48  //
49  key = "unnamed." + to_string(groupId);
50  }
51 
52  /**
53  * @brief group identification key
54  * @return group key
55  */
56  inline const string& getKey() {
57  return key;
58  }
59 
60  /**
61  * @brief sets the group identification key
62  * @param &key group identification key
63  * @return if setting the key was succesful
64  */
65  const bool setKey(const string &key) {
66  if (server->setGroupKey(this, key) == true) {
67  this->key = key;
68  return true;
69  } else {
70  return false;
71  }
72  }
73 
74  /**
75  * @brief get a copy of current group client keys
76  * @return group client list
77  */
79  // make a copy of the client key set
80  ClientKeySet _clientKeySet;
82  _clientKeySet = clientKeySet;
84  // return copy
85  return _clientKeySet;
86  }
87 
88  /**
89  * @brief Adds a client to this group
90  * @param client client
91  */
92  virtual const bool addClient(CLIENT* client) {
94  auto it = clientKeySet.find(client->getKey());
95  // check if already exists
96  if (it != clientKeySet.end()) {
98  return false;
99  }
100  clientKeySet.insert(client->getKey());
102  return true;
103  }
104 
105  /**
106  * @brief Removes a client from this group
107  * @param client client
108  */
109  virtual const bool removeClient(CLIENT* client) {
111  auto it = clientKeySet.find(client->getKey());
112  // check if not exists
113  if (it == clientKeySet.end()) {
115  return false;
116  }
117  clientKeySet.erase(client->getKey());
119  return true;
120  }
121 
122  /**
123  * @brief Shuts down this server group
124  */
125  virtual void shutdown() = 0;
126 protected:
127  typedef unordered_map<string, CLIENT*> ClientKeyMap;
128 
129  /*
130  * @brief event method called if group will be created, will be called from worker
131  */
132  virtual void onCreate() = 0;
133 
134  /*
135  * @brief event method called if client will be closed, will be called from worker
136  */
137  virtual void onClose() = 0;
138 
139  /**
140  * @brief Shuts down this group
141  */
142  virtual void close() {
143  // acquire reference for worker
145  // create request
146  ServerRequest* request = new ServerRequest(
148  this
149  );
150  // delegate it to thread pool, but make close request not declinable
151  server->workerThreadPool->addElement(request, false);
152  // server call back
153  server->closeGroup(static_cast<GROUP*>(this));
154  }
155 
156  //
157  SERVER* server;
158  uint32_t groupId;
159  string key;
160 
162 
164 };
165 
166 };
167 };
168 };
Base class for network server group.
Base class for network server groups.
Definition: ServerGroup.h:34
ClientKeySet getClientKeySet()
get a copy of current group client keys
Definition: ServerGroup.h:78
virtual const bool addClient(CLIENT *client)
Adds a client to this group.
Definition: ServerGroup.h:92
virtual void close()
Shuts down this group.
Definition: ServerGroup.h:142
unordered_set< string > ClientKeySet
Definition: ServerGroup.h:36
virtual void shutdown()=0
Shuts down this server group.
const bool setKey(const string &key)
sets the group identification key
Definition: ServerGroup.h:65
virtual const bool removeClient(CLIENT *client)
Removes a client from this group.
Definition: ServerGroup.h:109
unordered_map< string, CLIENT * > ClientKeyMap
Definition: ServerGroup.h:127
const string & getKey()
group identification key
Definition: ServerGroup.h:56
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
Reference counter implementation to be used with inheritance.
Definition: Reference.h:13
virtual void acquireReference()
Acquires a reference, incrementing the counter.
Definition: Reference.h:31
std::exception Exception
Exception base class.
Definition: Exception.h:18
Definition: fwd-tdme.h:4
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6