comparison oscgroups/GroupServer.h @ 76:0ae87af84e2f

added oscgroups
author Rob Canning <rob@foo.net>
date Sun, 13 Jul 2014 10:07:41 +0100
parents
children
comparison
equal deleted inserted replaced
75:3a2845e3156e 76:0ae87af84e2f
1 /*
2 OSCgroups -- open sound control groupcasting infrastructure
3 Copyright (C) 2005 Ross Bencina
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifndef INCLUDED_GROUPSERVER_H
21 #define INCLUDED_GROUPSERVER_H
22
23 // GroupServer.h/cpp implements user and group admission control using passwords
24 // Users live as long as they are sending alive messages more frequency
25 // than the timeout value. Groups live as long as they have members.
26 // Once Users and Groups are destroyed they can be reused with different
27 // passwords.
28 // This module has no dependence on the messaging protocol, although it does
29 // assume IPV4 addresses.
30
31
32 #include <ctime>
33 #include <map>
34 #include <set>
35 #include <string>
36
37 #include "ip/IpEndpointName.h"
38
39 class User;
40 class Group;
41 class GroupServer;
42
43
44 class User{
45 friend class GroupServer;
46
47 std::set<Group*> groups_;
48
49 public:
50 User( const char *userName, const char *userPassword )
51 : name( userName )
52 , password( userPassword ) {}
53
54 std::string name;
55 std::string password;
56
57 IpEndpointName privateEndpoint;
58 IpEndpointName publicEndpoint;
59
60 std::time_t lastAliveMessageArrivalTime;
61
62 unsigned long SecondsSinceLastAliveReceived( std::time_t currentTime )
63 { return (unsigned long)std::difftime( currentTime, lastAliveMessageArrivalTime ); }
64
65 bool IsMemberOf( Group *group ) const
66 { return groups_.count( group ) == 1; }
67
68 typedef std::set<Group*>::const_iterator const_group_iterator;
69 const_group_iterator groups_begin() const { return groups_.begin(); }
70 const_group_iterator groups_end() const { return groups_.end(); }
71 };
72
73
74 class Group{
75 friend class GroupServer;
76
77 std::set<User*> users_;
78
79 public:
80 Group( const char *groupName, const char *groupPassword )
81 : name( groupName )
82 , password( groupPassword ) {}
83
84 std::string name;
85 std::string password;
86
87 typedef std::set<User*>::const_iterator const_user_iterator;
88 const_user_iterator users_begin() const { return users_.begin(); }
89 const_user_iterator users_end() const { return users_.end(); }
90 };
91
92
93 class GroupServer{
94 const int timeoutSeconds_;
95 const int maxUsers_;
96 const int maxGroups_;
97
98 typedef std::map< std::string, User* > user_map;
99 typedef user_map::iterator user_iterator;
100 user_map users_;
101 int userCount_;
102
103 typedef std::map< std::string, Group* > group_map;
104 typedef group_map::iterator group_iterator;
105 group_map groups_;
106 int groupCount_;
107
108 User *CreateUser( const char *userName, const char *userPassword );
109
110 Group *CreateGroup( const char *groupName, const char *groupPassword );
111 void AssociateUserWithGroup( User *user, Group* group );
112 void RemoveUserReferenceFromGroup( User *user, Group* group );
113 void SeparateUserFromGroup( User *user, Group* group );
114 void SeparateUserFromAllGroups( User *user );
115
116 GroupServer(); // no default ctor
117 GroupServer( const GroupServer& ); // no copy ctor
118 GroupServer& operator=( const GroupServer& ); // no assignment operator
119
120 public:
121 GroupServer( int timeoutSeconds, int maxUsers, int maxGroups );
122 ~GroupServer();
123
124 enum UserStatus {
125 USER_STATUS_UNKNOWN,
126 USER_STATUS_OK,
127 USER_STATUS_WRONG_PASSWORD,
128 USER_STATUS_SERVER_LIMIT_REACHED
129 };
130
131 UserStatus UserAlive( const char *userName, const char *userPassword,
132 const IpEndpointName& privateEndpoint,
133 const IpEndpointName& publicEndpoint,
134 const char **groupNamesAndPasswords,
135 UserStatus *userGroupsStatus, int groupCount );
136
137 typedef user_map::const_iterator const_user_iterator;
138 const_user_iterator users_begin() const { return users_.begin(); }
139 const_user_iterator users_end() const { return users_.end(); }
140 User *FindUser( const char *userName );
141
142 typedef group_map::const_iterator const_group_iterator;
143 const_group_iterator groups_begin() const { return groups_.begin(); }
144 const_group_iterator groups_end() const { return groups_.end(); }
145 Group *FindGroup( const char *groupName );
146
147 void PurgeStaleUsers();
148 };
149
150
151 #endif /* INCLUDED_GROUPSERVER_H */