View Javadoc

1   /***
2    * 
3    */
4   package pl.agh.iosr.ballamigos.webapp.core;
5   
6   import java.rmi.NoSuchObjectException;
7   import java.rmi.NotBoundException;
8   import java.rmi.RemoteException;
9   import java.rmi.registry.LocateRegistry;
10  import java.rmi.registry.Registry;
11  import java.rmi.server.UnicastRemoteObject;
12  import java.util.List;
13  import java.util.Properties;
14  
15  import pl.agh.iosr.ballamigos.client.room.MatchesListModel;
16  import pl.agh.iosr.ballamigos.common.Decision;
17  import pl.agh.iosr.ballamigos.common.Match;
18  import pl.agh.iosr.ballamigos.common.MatchProperties;
19  import pl.agh.iosr.ballamigos.common.Player;
20  import pl.agh.iosr.ballamigos.common.ServerStatistics;
21  import pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface;
22  import pl.agh.iosr.ballamigos.common.communication.rmi.ServerInterface;
23  import pl.agh.iosr.ballamigos.common.exceptions.LoginException;
24  import pl.agh.iosr.ballamigos.common.exceptions.MatchAlreadyExistsException;
25  import pl.agh.iosr.ballamigos.common.exceptions.MatchDoesntExistException;
26  import pl.agh.iosr.ballamigos.common.exceptions.MatchFullException;
27  import pl.agh.iosr.ballamigos.common.exceptions.PlayerAlreadyExistsException;
28  import pl.agh.iosr.ballamigos.common.exceptions.PlayerAlreadyInGameException;
29  import pl.agh.iosr.ballamigos.common.exceptions.PlayerAlreadyLoggedException;
30  
31  /***
32   * @author wrobel
33   *
34   */
35  public class WebApplicationCommunicator implements ClientInterface {
36  	private String playerLogin;
37  	private String playerPassword;
38  	private String roomName;
39  	private Match match;
40  	private boolean matchStartedFlag = false;
41  	private boolean matchReceivedFlag = false;
42  	private boolean playerEnteredFlag = false;
43  	/***
44  	 * List of matches
45  	 */
46  	private MatchesListModel matches;
47  	
48  	/***
49  	 * list of all rooms names
50  	 */
51  	private List<String> roomNames;
52  	
53  	/***
54  	 * remote server to comunicate with.
55  	 */
56  	private ServerInterface server;
57  	
58  	
59  	/***
60  	 * Constructor that tries to locate server via rmiregistry
61  	 * @param serverName - inet addres of rmiregistry
62  	 * @param port - port of rmiregistry
63  	 * @throws RemoteException - can't connect to rmiregistry
64  	 * @throws NotBoundException - there's no server bounded in rmiregistry
65  	 */
66  	public WebApplicationCommunicator(String serverName, int port) throws RemoteException, NotBoundException{
67  		    Registry registry = LocateRegistry.getRegistry(serverName,port);
68  		    server = (ServerInterface) registry.lookup("ServerBallAmigos");
69  	}
70  
71  	 public WebApplicationCommunicator(Properties props) throws NumberFormatException, RemoteException, NotBoundException {		
72           Registry registry = LocateRegistry.getRegistry(props.getProperty("Host"), Integer.parseInt(props.getProperty("Port")));
73  		 server = (ServerInterface) registry.lookup("ServerBallAmigos");
74  	 }
75  
76  	/***
77  	 * tries to log to a server using login and password
78  	 * @param login - player login.
79  	 * @param passwd - player password.
80  	 * @throws RemoteException - server connection failed
81  	 * @throws PlayerAlreadyLoggedException - there is a player logged with this login.
82  	 * @throws LoginException 
83  	 */
84  	public void login(String login, String passwd) throws RemoteException, PlayerAlreadyLoggedException, LoginException
85  	{
86  		try
87  		{
88  			UnicastRemoteObject.unexportObject(this,true);
89  		}
90  		catch(NoSuchObjectException e)
91  		{
92  			
93  		}
94  		UnicastRemoteObject.exportObject(this, 0);
95  		server.loginPlayer(new Player(login,passwd), this);
96  		this.roomNames = server.getRooms();
97  		this.playerLogin = login;
98  		this.playerPassword = passwd;
99  		this.matches = new MatchesListModel();
100 	}
101 	
102 	/***
103 	 * 
104 	 * @param login
105 	 * @param passwd
106 	 * @throws RemoteException
107 	 */
108 	public void createPlayer(String login, String passwd) throws RemoteException, PlayerAlreadyExistsException
109 	{
110 		server.createPlayer(login,passwd);
111 	}
112 	
113 	/***
114 	 * tells server to bind player to a room
115 	 * @param roomName - name of the room that player wants to join
116 	 * @throws RemoteException - server connection failed
117 	 */
118 	public void moveToRoom(String roomName) throws RemoteException
119 	{
120 		server.bindToRoom(playerLogin, roomName);
121 		this.roomName = roomName;
122 	}
123 	
124 	/***
125 	 * requests list of players names in player room from server.
126 	 * @return list of players names in players room
127 	 * @throws RemoteException - server connection failed
128 	 */
129 	public List<String> getPlayersInRoom() throws RemoteException
130 	{
131 		return server.getPlayersInRoom(roomName);
132 	}
133 	
134 	/* (non-Javadoc)
135 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#reciveMessage(java.lang.String)
136 	 */
137 	public void reciveMessage(String arg0) throws RemoteException {
138 		return;
139 
140 	}
141 
142 	/* (non-Javadoc)
143 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#reciveMessage(java.lang.String, java.lang.String)
144 	 */
145 	public void reciveMessage(String arg0, String arg1) throws RemoteException {
146 		return;
147 	}
148 
149 	/* (non-Javadoc)
150 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#reciveMessage(java.lang.String, java.lang.String, java.lang.String)
151 	 */
152 	public void reciveMessage(String arg0, String arg1, String arg2)
153 			throws RemoteException {
154 		return;
155 
156 	}
157 
158 	/*** 
159 	 * Used to notify client that a player has entered the room he is in.
160 	 * @param playerName - name of player that entered room
161 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#playerEnteredRoom(java.lang.String)
162 	 */
163 	public void playerEnteredRoom(String playerName) throws RemoteException {
164 		// TODO Auto-generated method stub
165 
166 	}
167 
168 
169 	/*** 
170 	 * Used to notify client that a player has left the room he is in.
171 	 * @param playerName - name of player that left room
172 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#playerLeftRoom(java.lang.String)
173 	 */
174 	public void playerLeftRoom(String playerName)
175 	{
176 		// TODO Auto-generated method stub
177 
178 	}
179 
180 	/***
181 	 * logs this player out of server.
182 	 *
183 	 */
184 	public void logout() throws RemoteException
185 	{
186 		server.logout(playerLogin);
187 	}
188 	
189 	/***
190 	 * tries to create and register a match on the server
191 	 * @param name - name of this match
192 	 */
193 	public void createMatch(String name) throws RemoteException, MatchAlreadyExistsException, PlayerAlreadyInGameException
194 	{	
195 		match = server.createMatch(name, playerLogin);
196 		synchronized(this){
197 			matchStartedFlag = false;
198 		}
199 	}
200 	
201 
202 	/*** 
203 	 * Notyfies that a macth in this room was created.
204 	 * @param matchName - name match that was created.
205 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#matchCreated(java.lang.String)
206 	 */
207 	public void matchCreated(String matchName) throws RemoteException
208 	{
209 		this.matches.addMatch(matchName);
210 	}
211 
212 	/*** 
213 	 * Notyfies that a macth in this room was removed.
214 	 * @param matchName - name match that was removed.
215 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#matchRemoved(java.lang.String)
216 	 */
217 	public void matchRemoved(String matchName) throws RemoteException {
218 		this.matches.removeMatch(matchName);
219 
220 	}
221 
222 	/***
223 	 * Joins match if posible. 
224 	 * @param matchName - name of a match to join
225 	 */
226 	public void joinMatch(String matchName) throws RemoteException, MatchFullException, PlayerAlreadyInGameException 
227 	{
228 		match = server.joinMatch(matchName, playerLogin);
229 	}
230 	
231 	/*** 
232 	 * Implements playerJoins method. Used by server to notify clients when a player has joined their room.
233 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#playerJoins(java.lang.String)
234 	 */
235 	public synchronized void playerJoins(String playerName) throws RemoteException
236 	{
237 		playerEnteredFlag= true;
238 		notify();
239 	}
240 
241 	public void playerQuitedMatch()
242 	{
243 	
244 	}
245 	
246 
247 	public synchronized boolean matchReceived()
248 	{
249 		return matchReceivedFlag;
250 	}
251 	
252 	/*** 
253 	 * Implements reciveCurrentMatch method. Used by server to send results of their moves (current match state). It tells display panel to display animation a this move
254 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#reciveCurrentMatch(pl.agh.iosr.ballamigos.common.Match)
255 	 */
256 	public synchronized void reciveCurrentMatch(Match match) throws RemoteException
257 	{
258 		matchReceivedFlag= true;
259 		this.match = match;
260 		notify();
261 	}
262 
263 	public synchronized boolean playerEntered()
264 	{
265 		return playerEnteredFlag;
266 	}
267 	/***
268 	 * Check if current match is active
269 	 * @return
270 	 */
271 	public synchronized boolean matchStarted()
272 	{
273 		return matchStartedFlag;
274 	}
275 	/*** 
276 	 * Implements startMatch method. Tells client that match has started
277 	 * @see pl.agh.iosr.ballamigos.common.communication.rmi.ClientInterface#startMatch()
278 	 */
279 	public synchronized void startMatch() throws RemoteException
280 	{
281 		matchStartedFlag = true;
282 		notify();
283 	}
284 
285 	/***
286 	 * Sends a signal to server to start match, if succed start the match on this clienmt to.
287 	 *
288 	 */
289 	public void sendStartMatch() throws RemoteException
290 	{
291 		server.startMatch(match.getMatchName(), playerLogin);
292 	}
293 	
294 	/***
295 	 * Change properties of current match
296 	 * @param properties
297 	 * @throws RemoteException
298 	 */
299 	public void changeMatchProperties(MatchProperties properties) throws RemoteException, MatchDoesntExistException
300 	{
301 		server.changeMatchProperties(playerLogin, match.getMatchName(), properties);
302 		match.setProperties(properties);
303 	}
304 	
305 	/***
306 	 * Sends current player move decision.
307 	 * @param decision - contains player choises for this round.
308 	 */
309 	public synchronized void sendDecision(Decision decision) throws RemoteException
310 	{			
311 		server.makeDecision(decision, playerLogin);
312 		matchReceivedFlag = false;
313 	}
314 	
315 	public List<String> getRoomNames() throws RemoteException {
316 		return server.getRooms();
317 	}
318 
319 	public ServerStatistics getServerStatistics() throws RemoteException {
320 		return server.getServerStatistics();
321 	}
322 
323 	/***
324 	 * @return Returns the match.
325 	 */
326 	public Match getMatch() {
327 		return match;
328 	}
329 
330 	/***
331 	 * @param match The match to set.
332 	 */
333 	public void setMatch(Match match) {
334 		this.match = match;
335 	}
336 
337 	/***
338 	 * @return Returns the matches.
339 	 */
340 	public MatchesListModel getMatches() {
341 		return matches;
342 	}
343 
344 	/***
345 	 * @param matches The matches to set.
346 	 */
347 	public void setMatches(MatchesListModel matches) {
348 		this.matches = matches;
349 	}
350 
351 	/***
352 	 * @return Returns the playerLogin.
353 	 */
354 	public String getPlayerLogin() {
355 		return playerLogin;
356 	}
357 
358 	/***
359 	 * @param playerLogin The playerLogin to set.
360 	 */
361 	public void setPlayerLogin(String playerLogin) {
362 		this.playerLogin = playerLogin;
363 	}
364 
365 	/***
366 	 * @return Returns the roomName.
367 	 */
368 	public String getRoomName() {
369 		return roomName;
370 	}
371 
372 	/***
373 	 * @param roomName The roomName to set.
374 	 */
375 	public void setRoomName(String roomName) {
376 		this.roomName = roomName;
377 	}
378 
379 	/***
380 	 * @return Returns the server.
381 	 */
382 	public ServerInterface getServer() {
383 		return server;
384 	}
385 
386 	/***
387 	 * @param server The server to set.
388 	 */
389 	public void setServer(ServerInterface server) {
390 		this.server = server;
391 	}
392 
393 	/***
394 	 * @param roomNames The roomNames to set.
395 	 */
396 	public void setRoomNames(List<String> roomNames) {
397 		this.roomNames = roomNames;
398 	}
399 	
400 	/***Gets a list of matches for the chosen room.
401 	 * 
402 	 * @return Returns a list of matches in the current room
403 	 * @throws RemoteException when there a problems with RMI
404 	 */
405 	public List<String> getMatchesInRoom() throws RemoteException
406 	{
407 		return server.getMatchesInRoom(roomName);
408 	}
409 	
410 }