-
Notifications
You must be signed in to change notification settings - Fork 3
/
GroupServer.cpp
318 lines (245 loc) · 9.76 KB
/
GroupServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
OSCgroups -- open sound control groupcasting infrastructure
Copyright (C) 2005 Ross Bencina
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "GroupServer.h"
#include <string>
#include <set>
#include <iostream>
#include <ctime>
#include <algorithm>
#include <iterator>
#include <cassert>
#include <cstring>
#include "ip/NetworkingUtils.h"
#if defined(__BORLANDC__) // workaround for BCB4 release build intrinsics bug
namespace std {
using ::__strcpy__; // avoid error: E2316 '__strcpy__' is not a member of 'std'.
}
#endif
/*
TODO:
switch to using char * instead of string:: to avoid allocating new strings
every time we execute a find()
*/
static std::ostream& Log()
{
std::time_t t;
std::time( &t );
// ctime() returns a constant width 26 char string including trailing \0
// the fields are all constant width.
char s[26];
std::strcpy( s, std::ctime( &t ) );
s[24] = '\0'; // remove trailing null
std::cout << s << ": ";
return std::cout;
}
GroupServer::GroupServer( int timeoutSeconds, int maxUsers, int maxGroups )
: timeoutSeconds_( timeoutSeconds )
, maxUsers_( maxUsers )
, maxGroups_( maxGroups )
, userCount_( 0 )
, groupCount_( 0 )
{
}
GroupServer::~GroupServer()
{
for( const_user_iterator i = users_begin(); i != users_end(); ++i )
delete i->second;
for( const_group_iterator i = groups_begin(); i != groups_end(); ++i )
delete i->second;
}
User *GroupServer::CreateUser( const char *userName, const char *userPassword )
{
Log() << "creating user '" << userName << "'." << std::endl;
User *user = new User( userName, userPassword );
std::pair<user_iterator,bool> r =
users_.insert( std::make_pair( user->name, user ) );
// we assume the caller didn't try to create a user with an existing name
assert( r.second == true );
++userCount_;
return user;
}
User *GroupServer::FindUser( const char *userName )
{
user_iterator user = users_.find( userName );
return ( user != users_.end() ) ? user->second : (User*) 0;
}
void GroupServer::PurgeStaleUsers()
{
std::time_t currentTime = std::time(0);
for( std::map< std::string, User* >::iterator i = users_.begin();
i != users_.end(); /* nothing */ ){
unsigned long inactiveSeconds =
i->second->SecondsSinceLastAliveReceived( currentTime );
if( inactiveSeconds >= (unsigned int)timeoutSeconds_ ){
Log() << "purging user '" << i->second->name << "' after "
<< inactiveSeconds << " seconds of inactivity." << std::endl;
SeparateUserFromAllGroups( i->second );
delete i->second;
--userCount_;
// i think this is safe, we increment i before erasing the
// element referenced by its old value...
std::map< std::string, User* >::iterator j = i;
++i;
users_.erase( j );
}else{
++i;
}
}
}
Group *GroupServer::CreateGroup(
const char *groupName, const char *groupPassword )
{
Log() << "creating group '" << groupName << "'." << std::endl;
Group *group = new Group( groupName, groupPassword );
std::pair<group_iterator, bool> r =
groups_.insert( std::make_pair( group->name, group ) );
// we assume the caller didn't try to create an existing group
assert( r.second == true );
++groupCount_;
return group;
}
Group *GroupServer::FindGroup( const char *groupName )
{
group_iterator group = groups_.find( groupName );
return ( group != groups_.end() ) ? group->second : (Group*) 0;
}
// The User <-> Group relation is managed as a bidirectional link (each User
// contains a set of groups which it is associated with, and each Group
// maintains a set of Users which it is associated with). The Associate*
// and Separate* methods below create and destroy the bidirectional link
// RemoveUserReferenceFromGroup is a helper function which only destroys
// one side of the link.
void GroupServer::AssociateUserWithGroup( User *user, Group* group )
{
Log() << "adding user '" << user->name
<< "' to group '" << group->name << "'." << std::endl;
user->groups_.insert( group );
group->users_.insert( user );
}
void GroupServer::RemoveUserReferenceFromGroup( User *user, Group* group )
{
Log() << "removing user '" << user->name
<< "' from group '" << group->name << "'." << std::endl;
std::set< User* >::iterator i = group->users_.find( user );
assert( i != group->users_.end() );
group->users_.erase( i );
if( group->users_.empty() ){
Log() << "purging empty group '" << group->name << "'." << std::endl;
groups_.erase( group->name );
delete group;
--groupCount_;
}
}
void GroupServer::SeparateUserFromGroup( User *user, Group* group )
{
user->groups_.erase( group );
RemoveUserReferenceFromGroup( user, group );
}
void GroupServer::SeparateUserFromAllGroups( User *user )
{
for( std::set<Group*>::iterator i = user->groups_.begin();
i != user->groups_.end(); ++i ){
RemoveUserReferenceFromGroup( user, *i);
}
user->groups_.clear();
}
static void UpdateEndpoint( IpEndpointName& dest, const IpEndpointName& src,
const char *userName, const char *whichEndpoint )
{
if( dest != src ){
char endpointString[ IpEndpointName::ADDRESS_AND_PORT_STRING_LENGTH ];
src.AddressAndPortAsString( endpointString );
Log() << "updating " << whichEndpoint << " endpoint for user '"
<< userName << "' to " << endpointString << "." << std::endl;
dest = src;
}
}
GroupServer::UserStatus GroupServer::UserAlive(
const char *userName, const char *userPassword,
const IpEndpointName& privateEndpoint,
const IpEndpointName& publicEndpoint,
const char **groupNamesAndPasswords,
UserStatus *userGroupsStatus, int groupCount )
{
// find or create the user, aborting if the password for an existing
// user is incorrect, or if the maximum number of users has been reached
User *user = FindUser( userName );
if( user ){
if( user->password.compare( userPassword ) != 0 ){
Log() << "attempt to update user '"
<< userName << "' with incorrect password." << std::endl;
return USER_STATUS_WRONG_PASSWORD;
}
}else{
if( userCount_ == maxUsers_ ){
Log() << "user limit reached, user '"
<< userName << "' not admitted." << std::endl;
return USER_STATUS_SERVER_LIMIT_REACHED;
}else{
user = CreateUser( userName, userPassword );
}
}
UpdateEndpoint(
user->privateEndpoint, privateEndpoint, userName, "private" );
UpdateEndpoint( user->publicEndpoint, publicEndpoint, userName, "public" );
user->lastAliveMessageArrivalTime = time(0);
// previousButNotCurrentGroups begins containing all the groups the user
// was in before the current message was received. We remove those which
// the user is still a member of, leaving the set that the user is no
// longer a member of. We then use the set to remove associations with
// non-current groups.
std::set<Group*> previousButNotCurrentGroups( user->groups_ );
for( int i=0; i < groupCount; ++i ){
const char *groupName = groupNamesAndPasswords[i*2];
const char *groupPassword = groupNamesAndPasswords[i*2 + 1];
Group *group = FindGroup( groupName );
if( group ){
if( user->IsMemberOf( group ) ){
// check that previousButNotCurrentGroups contains group before
// removing it. it won't if we were passed the same group
// multiple times
std::set<Group*>::iterator j =
previousButNotCurrentGroups.find( group );
if( j != previousButNotCurrentGroups.end() )
previousButNotCurrentGroups.erase( j );
userGroupsStatus[i] = USER_STATUS_OK;
}else{
// user isn't in group so join it
if( group->password.compare( groupPassword ) == 0 ){
AssociateUserWithGroup( user, group );
userGroupsStatus[i] = USER_STATUS_OK;
}else{
userGroupsStatus[i] = USER_STATUS_WRONG_PASSWORD;
}
}
}else{ // group doesn't exist
if( groupCount_ == maxGroups_ ){
Log() << "group limit reached, group '"
<< groupName << "' not created." << std::endl;
userGroupsStatus[i] = USER_STATUS_SERVER_LIMIT_REACHED;
}else{
group = CreateGroup( groupName, groupPassword );
AssociateUserWithGroup( user, group );
userGroupsStatus[i] = USER_STATUS_OK;
}
}
}
for( std::set<Group*>::iterator j = previousButNotCurrentGroups.begin();
j != previousButNotCurrentGroups.end(); ++j ){
SeparateUserFromGroup( user, *j );
}
return USER_STATUS_OK;
}