TDME2 1.9.121
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ServerGroup.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#include <exception>
6#include <sstream>
7#include <string>
8
9#include <tdme/tdme.h>
10
13
17
18using std::ostringstream;;
19using std::string;
20
24
25namespace tdme {
26namespace network {
27namespace udpserver {
28
29/**
30 * Base class for network server groups
31 * @author Andreas Drewke
32 */
33template <typename SERVER, typename CLIENT, typename GROUP>
35public:
36 typedef std::set<std::string> ClientKeySet;
37
38 ServerGroup(const uint32_t groupId) :
39 server(NULL),
41 clientKeyListsReadWriteLock("nioservergroup_clientlists"){
42 //
43 ostringstream tmp;
44 tmp << "unnamed.";
45 tmp << groupId;
46 key = tmp.str();
47 }
48
49 /**
50 * @brief group identification key
51 * @return group key
52 */
53 const string& getKey() {
54 return key;
55 }
56
57 /**
58 * @brief sets the group identification key
59 * @param &key group identification key
60 * @return if setting the key was succesful
61 */
62 const bool setKey(const string &key) {
63 if (server->setGroupKey(this, key) == true) {
64 this->key = key;
65 return true;
66 } else {
67 return false;
68 }
69 }
70
71 /**
72 * @brief get a copy of current group client keys
73 * @return group client list
74 */
76 // make a copy of the client key set
77 ClientKeySet _clientKeySet;
79 _clientKeySet = clientKeySet;
81 // return copy
82 return _clientKeySet;
83 }
84
85 /**
86 * @brief Adds a client to this group
87 * @param client client
88 */
89 virtual const bool addClient(CLIENT* client) {
91 typename ClientKeySet::iterator it = clientKeySet.find(client->getKey());
92 // check if already exists
93 if (it != clientKeySet.end()) {
95 return false;
96 }
97 clientKeySet.insert(client->getKey());
99 return true;
100 }
101
102 /**
103 * @brief Removes a client from this group
104 * @param client client
105 */
106 virtual const bool removeClient(CLIENT* client) {
108 typename ClientKeySet::iterator it = clientKeySet.find(client->getKey());
109 // check if not exists
110 if (it == clientKeySet.end()) {
112 return false;
113 }
114 clientKeySet.erase(client->getKey());
116 return true;
117 }
118
119 /**
120 * @brief Shuts down this server group
121 */
122 virtual void shutdown() = 0;
123protected:
124 typedef std::map<const std::string, CLIENT*> ClientKeyMap;
125
126 /*
127 * @brief event method called if group will be created, will be called from worker
128 */
129 virtual void onCreate() = 0;
130
131 /*
132 * @brief event method called if client will be closed, will be called from worker
133 */
134 virtual void onClose() = 0;
135
136 /**
137 * @brief Shuts down this group
138 */
139 virtual void close() {
140 // acquire reference for worker
142 // create request
143 ServerRequest* request = new ServerRequest(
145 this
146 );
147 // delegate it to thread pool, but make close request not declinable
148 server->workerThreadPool->addElement(request, false);
149 // server call back
150 server->closeGroup(static_cast<GROUP*>(this));
151 }
152
153 //
154 SERVER* server;
155 uint32_t groupId;
156 std::string key;
157
159
161};
162
163};
164};
165};
Base class for network server group.
Base class for network server groups.
Definition: ServerGroup.h:34
ServerGroup(const uint32_t groupId)
Definition: ServerGroup.h:38
ClientKeySet getClientKeySet()
get a copy of current group client keys
Definition: ServerGroup.h:75
virtual const bool addClient(CLIENT *client)
Adds a client to this group.
Definition: ServerGroup.h:89
virtual void close()
Shuts down this group.
Definition: ServerGroup.h:139
virtual void shutdown()=0
Shuts down this server group.
std::map< const std::string, CLIENT * > ClientKeyMap
Definition: ServerGroup.h:124
std::set< std::string > ClientKeySet
Definition: ServerGroup.h:36
const bool setKey(const string &key)
sets the group identification key
Definition: ServerGroup.h:62
virtual const bool removeClient(CLIENT *client)
Removes a client from this group.
Definition: ServerGroup.h:106
const string & getKey()
group identification key
Definition: ServerGroup.h:53
Implementation for read/write lock.
Definition: ReadWriteLock.h:21
void writeLock()
Locks for writing / exclusive lock.
void unlock()
Unlocks this read write lock.
void readLock()
Locks for reading / shared lock.
Reference counter implementation to be used with inheritance.
Definition: Reference.h:10
void acquireReference()
acquires a reference, incrementing the counter
Definition: Reference.cpp:16
std::exception Exception
Exception base class.
Definition: Exception.h:19
Definition: fwd-tdme.h:4