root/oscgroups/trunk/GroupServer.h

Revision 31, 4.5 kB (checked in by ross, 3 years ago)

updated to use new osc/... ip/... include directory conventions from oscpack

  • Property svn:eol-style set to native
Line 
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 <map>
33 #include <set>
34 #include <string>
35
36 #include "ip/IpEndpointName.h"
37
38 class User;
39 class Group;
40 class GroupServer;
41
42
43 class User{
44     friend class GroupServer;
45    
46     std::set<Group*> groups_;
47
48 public:
49     User( const char *userName, const char *userPassword )
50         : name( userName )
51         , password( userPassword ) {}
52        
53     std::string name;
54     std::string password;
55    
56     IpEndpointName privateEndpoint;
57         IpEndpointName publicEndpoint;
58
59     unsigned long lastAliveMessageArrivalTime;
60
61     unsigned long SecondsSinceLastAliveReceived( unsigned long currentTime )
62             { return currentTime - lastAliveMessageArrivalTime; }
63
64     bool IsMemberOf( Group *group ) const
65             { return groups_.count( group ) == 1; }
66    
67     typedef std::set<Group*>::const_iterator const_group_iterator;
68     const_group_iterator groups_begin() const { return groups_.begin(); }
69     const_group_iterator groups_end() const { return groups_.end(); }
70 };
71
72
73 class Group{
74     friend class GroupServer;
75    
76     std::set<User*> users_;
77      
78 public:
79     Group( const char *groupName, const char *groupPassword )
80         : name( groupName )
81         , password( groupPassword ) {}
82        
83     std::string name;
84     std::string password;
85    
86     typedef std::set<User*>::const_iterator const_user_iterator;
87     const_user_iterator users_begin() const { return users_.begin(); }
88     const_user_iterator users_end() const { return users_.end(); }
89 };
90
91
92 class GroupServer{
93     const int timeoutSeconds_;
94     const int maxUsers_;
95     const int maxGroups_;
96    
97     typedef std::map< std::string, User* > user_map;
98     typedef user_map::iterator user_iterator;
99     user_map users_;
100     int userCount_;
101    
102     typedef std::map< std::string, Group* > group_map;
103     typedef group_map::iterator group_iterator;
104     group_map groups_;
105     int groupCount_;
106
107     User *CreateUser( const char *userName, const char *userPassword );
108
109     Group *CreateGroup( const char *groupName, const char *groupPassword );
110     void AssociateUserWithGroup( User *user, Group* group );
111     void RemoveUserReferenceFromGroup( User *user, Group* group );
112     void SeparateUserFromGroup( User *user, Group* group );
113     void SeparateUserFromAllGroups( User *user );
114
115 public:
116     GroupServer( int timeoutSeconds, int maxUsers, int maxGroups );
117     ~GroupServer();
118
119     enum UserStatus {
120             USER_STATUS_UNKNOWN,
121             USER_STATUS_OK,
122             USER_STATUS_WRONG_PASSWORD,
123             USER_STATUS_SERVER_LIMIT_REACHED
124         };
125
126     UserStatus UserAlive( const char *userName, const char *userPassword,
127             const IpEndpointName& privateEndpoint,
128                         const IpEndpointName& publicEndpoint,
129             const char **groupNamesAndPasswords,
130             UserStatus *userGroupsStatus, int groupCount );
131
132     typedef user_map::const_iterator const_user_iterator;
133     const_user_iterator users_begin() const { return users_.begin(); }
134     const_user_iterator users_end() const { return users_.end(); }
135     User *FindUser( const char *userName );
136
137     typedef group_map::const_iterator const_group_iterator;
138     const_group_iterator groups_begin() const { return groups_.begin(); }
139     const_group_iterator groups_end() const { return groups_.end(); }
140     Group *FindGroup( const char *groupName );
141
142     void PurgeStaleUsers();
143 };
144
145
146 #endif /* INCLUDED_GROUPSERVER_H */
Note: See TracBrowser for help on using the browser.