|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
VisibleObjectBroker.java | 100% | 100% | 100% | 100% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Common - Virtual Reality Object Oriented MUD Common Code
|
|
4 |
Copyright (C) 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.common.objectbroker;
|
|
28 |
|
|
29 |
import org.vrmoo.common.data.BrokerableObject;
|
|
30 |
import org.vrmoo.common.data.VisibleObject;
|
|
31 |
import org.vrmoo.common.exception.VRMooException;
|
|
32 |
import org.vrmoo.common.exception.VRMooParseException;
|
|
33 |
import org.vrmoo.common.util.ParserUtility;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* This class is a singleton responsible for handling the complex tasks of
|
|
37 |
* managing <code>VisibleObject</code> instances that are distributed between
|
|
38 |
* a world on the VRMoo Server and several VRMoo Clients.
|
|
39 |
*
|
|
40 |
* @author Jeff Weston
|
|
41 |
*/
|
|
42 |
public class VisibleObjectBroker extends AbstractObjectBroker |
|
43 |
{ |
|
44 |
/**
|
|
45 |
* The client instance of this class.
|
|
46 |
*/
|
|
47 |
private static VisibleObjectBroker clientInstance = null; |
|
48 |
|
|
49 |
/**
|
|
50 |
* Construct a server instance of this class.
|
|
51 |
*/
|
|
52 | 7 |
public VisibleObjectBroker( )
|
53 |
{ |
|
54 | 7 |
this( true ); |
55 |
} |
|
56 |
|
|
57 |
/**
|
|
58 |
* The client instance of this class is a singleton, so make the constructor
|
|
59 |
* that can create the client instance private. The client instance of this
|
|
60 |
* class can only be instantiated by the <code>getClientInstance()</code>
|
|
61 |
* method.
|
|
62 |
*
|
|
63 |
* @param isServerInstance tell this instance if it is a server instance
|
|
64 |
*/
|
|
65 | 8 |
private VisibleObjectBroker( boolean isServerInstance ) |
66 |
{ |
|
67 | 8 |
super( isServerInstance );
|
68 |
} |
|
69 |
|
|
70 |
/**
|
|
71 |
* Static method for getting the client instance of this class.
|
|
72 |
*
|
|
73 |
* @return the client instance of <code>VisibleObjectBroker</code>
|
|
74 |
*/
|
|
75 | 7 |
public static synchronized VisibleObjectBroker getClientInstance( ) |
76 |
{ |
|
77 | 7 |
if ( clientInstance == null ) |
78 |
{ |
|
79 | 1 |
clientInstance = new VisibleObjectBroker( false ); |
80 |
} |
|
81 | 7 |
return clientInstance;
|
82 |
} |
|
83 |
|
|
84 |
/**
|
|
85 |
* Gets the <code>VisibleObject</code> with the specified id.
|
|
86 |
*
|
|
87 |
* @param id the id of the <code>VisibleObject</code> to retrieve
|
|
88 |
*
|
|
89 |
* @return the requested <code>VisibleObject</code>
|
|
90 |
*
|
|
91 |
* @throws VRMooException if the <code>VisibleObject</code> ID isn't found
|
|
92 |
*/
|
|
93 | 1 |
public VisibleObject getVisibleObject( int id ) |
94 |
throws VRMooException
|
|
95 |
{ |
|
96 | 1 |
return ( VisibleObject ) super.getBrokerableObject( "" + id ); |
97 |
} |
|
98 |
|
|
99 |
/**
|
|
100 |
* Public interface for adding <code>VisibleObject</code> instances.
|
|
101 |
*
|
|
102 |
* @param visibleObject the <code>VisibleObject</code> to add
|
|
103 |
*
|
|
104 |
* @throws VRMooException if the <code>VisibleObject</code> ID is not set,
|
|
105 |
* or if the <code>VisibleObject</code> already exists
|
|
106 |
*/
|
|
107 | 7 |
public void addVisibleObject( VisibleObject visibleObject ) |
108 |
throws VRMooException
|
|
109 |
{ |
|
110 | 7 |
if ( visibleObject.getName( ).equals( "0" ) ) |
111 |
{ |
|
112 | 1 |
throw new VRMooException( "The visible object ID is not set." ); |
113 |
} |
|
114 |
|
|
115 | 6 |
if ( containsBrokerableObject( visibleObject.getName( ) ) )
|
116 |
{ |
|
117 | 1 |
throw new VRMooException( "The visible object already exists." ); |
118 |
} |
|
119 |
|
|
120 | 5 |
setBrokerableObject( visibleObject, null );
|
121 |
} |
|
122 |
|
|
123 |
/**
|
|
124 |
* Public interface for replacing <code>VisibleObject</code> instances.
|
|
125 |
*
|
|
126 |
* @param visibleObject the <code>VisibleObject</code> to replace
|
|
127 |
*
|
|
128 |
* @throws VRMooException if the <code>VisibleObject</code> doesn't already
|
|
129 |
* exist
|
|
130 |
*/
|
|
131 | 2 |
public void replaceVisibleObject( VisibleObject visibleObject ) |
132 |
throws VRMooException
|
|
133 |
{ |
|
134 | 2 |
if ( ! containsBrokerableObject( visibleObject.getName( ) ) )
|
135 |
{ |
|
136 | 1 |
throw new VRMooException( |
137 |
"The visible object doesn't already exist." );
|
|
138 |
} |
|
139 |
|
|
140 | 1 |
setBrokerableObject( visibleObject, null );
|
141 |
} |
|
142 |
|
|
143 |
/**
|
|
144 |
* Remove the <code>VisibleObject</code> with the specified ID.
|
|
145 |
*
|
|
146 |
* @param id the ID of the <code>VisibleObject</code> to remove
|
|
147 |
*/
|
|
148 | 1 |
public synchronized void removeVisibleObject( int id ) |
149 |
{ |
|
150 | 1 |
removeBrokerableObject( "" + id, null ); |
151 |
} |
|
152 |
|
|
153 |
/**
|
|
154 |
* Check if the selected <code>VisibleObject</code> is in the list.
|
|
155 |
*
|
|
156 |
* @param id the ID of the <code>VisibleObject</code> that we are checking
|
|
157 |
*
|
|
158 |
* @return true if the object is found, false otherwise
|
|
159 |
*/
|
|
160 | 1 |
public synchronized boolean containsVisibleObject( int id ) |
161 |
{ |
|
162 | 1 |
return super.containsBrokerableObject( "" + id ); |
163 |
} |
|
164 |
|
|
165 |
/**
|
|
166 |
* Get the ID of all of the <code>VisibleObject</code> instances available
|
|
167 |
* from this class.
|
|
168 |
*
|
|
169 |
* @return the array of <code>VisibleObject</code> IDs
|
|
170 |
*/
|
|
171 | 1 |
public synchronized int[ ] getVisibleObjectNames( ) |
172 |
{ |
|
173 | 1 |
String[ ] names = getBrokerableObjectNames( ); |
174 | 1 |
int[ ] ids = new int[ names.length ]; |
175 | 1 |
for ( int i = 0; i < ids.length; i++ ) |
176 |
{ |
|
177 | 1 |
try
|
178 |
{ |
|
179 | 1 |
ids[ i ] = ParserUtility.parseInt( names[ i ] ); |
180 |
} |
|
181 |
catch ( VRMooParseException e )
|
|
182 |
{ |
|
183 |
///CLOVER:OFF
|
|
184 |
e.printStackTrace( ); |
|
185 |
///CLOVER:ON
|
|
186 |
} |
|
187 |
} |
|
188 | 1 |
return ids;
|
189 |
} |
|
190 |
|
|
191 |
/**
|
|
192 |
* Get the prefix used by this broker sub-class when sending or receiving
|
|
193 |
* commands.
|
|
194 |
*
|
|
195 |
* @return the prefix used for sending and receiving commands
|
|
196 |
*/
|
|
197 | 25 |
protected String getBrokerPrefix( )
|
198 |
{ |
|
199 | 25 |
return "visibleObjectBroker:"; |
200 |
} |
|
201 |
|
|
202 |
/**
|
|
203 |
* Get an instance of a brokerable object by parsing a string of encoded
|
|
204 |
* data representing the object.
|
|
205 |
*
|
|
206 |
* @param data the encoded data for the object
|
|
207 |
*
|
|
208 |
* @return the parsed <code>BrokerableObject</code>
|
|
209 |
*
|
|
210 |
* @throws VRMooParseException for any parsing errors that occur
|
|
211 |
*/
|
|
212 | 1 |
protected BrokerableObject parseBrokerableObject( String data )
|
213 |
throws VRMooParseException
|
|
214 |
{ |
|
215 | 1 |
return new VisibleObject( data ); |
216 |
} |
|
217 |
} |
|
218 |
|
|