|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AllClients.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Server - Virtual Reality Object Oriented MUD Server
|
|
4 |
Copyright (C) 2001 - 2003 VRMoo Development Team
|
|
5 |
|
|
6 |
|
|
7 |
This program is free software; you can redistribute it and/or modify
|
|
8 |
it under the terms of the GNU General Public License as published by
|
|
9 |
the Free Software Foundation; either version 2 of the License, or
|
|
10 |
(at your option) any later version.
|
|
11 |
|
|
12 |
This program is distributed in the hope that it will be useful,
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
GNU General Public License for more details.
|
|
16 |
|
|
17 |
You should have received a copy of the GNU General Public License
|
|
18 |
along with this program; if not, write to the Free Software
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
|
|
21 |
|
|
22 |
For information about VRMoo and its authors, please visit the website:
|
|
23 |
http://www.vrmoo.org/
|
|
24 |
|
|
25 |
*/
|
|
26 |
|
|
27 |
package org.vrmoo.server.client;
|
|
28 |
|
|
29 |
import java.util.HashMap;
|
|
30 |
import java.util.Iterator;
|
|
31 |
import java.util.Map;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* The <code>AllClients</code> class keeps track of all the clients connected to
|
|
35 |
* the server. It maintains a <code>Vector</code> of all the clients on the
|
|
36 |
* server. Functionality is available for clients to <code>register</code> and
|
|
37 |
* <code>unregister</code> themselves as well as to <code>broadcast</code> a
|
|
38 |
* message to all registered clients.
|
|
39 |
* <P>
|
|
40 |
* This class is not intended to be instantiated. All functionality is provided
|
|
41 |
* through static methods.
|
|
42 |
* <P>
|
|
43 |
* This class is safe to use in a multithreaded environment.
|
|
44 |
*
|
|
45 |
* @author Jeff Weston
|
|
46 |
*/
|
|
47 |
public class AllClients |
|
48 |
{ |
|
49 |
/**
|
|
50 |
* The map of all registered clients.
|
|
51 |
*/
|
|
52 |
private static Map clients = new HashMap( ); |
|
53 |
|
|
54 |
/**
|
|
55 |
* The next unique identifier we will give a client.
|
|
56 |
*/
|
|
57 |
private static int nextID = 1; |
|
58 |
|
|
59 |
/**
|
|
60 |
* The default constructor is made private. This class is not intended to be
|
|
61 |
* instantiated.
|
|
62 |
*/
|
|
63 | 0 |
private AllClients( )
|
64 |
{ |
|
65 |
} |
|
66 |
|
|
67 |
/**
|
|
68 |
* Allows a <code>ClientHandler</code> to <code>register</code> to receive
|
|
69 |
* broadcast messages. All registered clients will receive all broadcast
|
|
70 |
* messages until they <code>unregister</code> themselves.
|
|
71 |
*
|
|
72 |
* @param client the <code>ClientHandler</code> to register
|
|
73 |
*/
|
|
74 | 0 |
public static synchronized void register( ClientHandler client ) |
75 |
{ |
|
76 | 0 |
Integer clientID = new Integer( client.getClientID( ) );
|
77 | 0 |
if ( ! clients.containsKey( clientID ) )
|
78 |
{ |
|
79 | 0 |
clients.put( clientID, client ); |
80 |
} |
|
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* The given <code>ClientHandler</code> will no longer receive broadcast
|
|
85 |
* messages.
|
|
86 |
*
|
|
87 |
* @param client the <code>ClientHandler</code> to unregister
|
|
88 |
*/
|
|
89 | 0 |
public static synchronized void unregister( ClientHandler client ) |
90 |
{ |
|
91 | 0 |
Integer clientID = new Integer( client.getClientID( ) );
|
92 | 0 |
if ( clients.containsKey( clientID ) )
|
93 |
{ |
|
94 | 0 |
clients.remove( clientID ); |
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
/**
|
|
99 |
* Sends a line of text to all registered clients.
|
|
100 |
*
|
|
101 |
* @param line the line of text
|
|
102 |
*/
|
|
103 | 0 |
public static synchronized void broadcast( String line ) |
104 |
{ |
|
105 | 0 |
Iterator iter = clients.values( ).iterator( ); |
106 |
|
|
107 | 0 |
while ( iter.hasNext( ) )
|
108 |
{ |
|
109 | 0 |
ClientHandler client = ( ClientHandler ) iter.next( ); |
110 | 0 |
client.writeLine( line ); |
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
/**
|
|
115 |
* Get the next unique identifier for a client.
|
|
116 |
*
|
|
117 |
* @return the identifier
|
|
118 |
*/
|
|
119 | 0 |
public static synchronized int getNextID( ) |
120 |
{ |
|
121 | 0 |
return nextID++;
|
122 |
} |
|
123 |
|
|
124 |
/**
|
|
125 |
* Retrieve the <code>ClientHandler</code> with the given ID.
|
|
126 |
*
|
|
127 |
* @param id the ID of the <code>ClientHandler</code> to retrieve
|
|
128 |
*
|
|
129 |
* @return the <code>ClientHandler</code> matching the given id
|
|
130 |
*/
|
|
131 | 0 |
public static synchronized ClientHandler getClientByID( int id ) |
132 |
{ |
|
133 | 0 |
return ( ClientHandler ) clients.get( new Integer( id ) ); |
134 |
} |
|
135 |
} |
|
136 |
|
|