Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

true callback

  • Loading branch information...
commit 69caf879349a1d1effb3cac27ef7d637ee9c6b5e 1 parent bf710c3
Runar Ingebrigtsen authored November 08, 2011
5  src/base/Game.java
@@ -10,12 +10,9 @@
10 10
 	public String opponent(String name) throws RemoteException;
11 11
 	public void move(String name, String move) throws RemoteException;
12 12
 	public boolean amIstupid(String name) throws RemoteException;
13  
-	public boolean finished() throws RemoteException;
14 13
 	public String wins(String name) throws RemoteException;
15 14
 	public String draws(String name) throws RemoteException;
16 15
 	public String defeats(String name) throws RemoteException;
17  
-	public void withdraw() throws RemoteException;
18  
-	public boolean canceled() throws RemoteException;
19  
-	public void endTimes() throws RemoteException;
  16
+	public void withdraw(String name) throws RemoteException;
20 17
 
21 18
 }
7  src/base/Player.java
@@ -8,18 +8,17 @@
8 8
 	public String Hello() throws RemoteException;
9 9
 	public String name() throws RemoteException;
10 10
 	public void announce() throws RemoteException;
11  
-	public boolean isChanged() throws RemoteException;
12 11
 	public void newGame(String opponent) throws RemoteException;
13 12
 	public boolean agressive() throws RemoteException;
14 13
 	public String opponent() throws RemoteException;
15 14
 	public void move(String move) throws RemoteException;
16  
-	public boolean isChallenged() throws RemoteException;
17 15
 	public void challenge(String attacker) throws RemoteException;
18  
-	public boolean roundFinished() throws RemoteException;
19 16
 	public String wins() throws RemoteException;
20 17
 	public String draws() throws RemoteException;
21 18
 	public String defeats() throws RemoteException;
  19
+	
22 20
 	public void withdraw() throws RemoteException;
23  
-	public boolean gotLonely() throws RemoteException;
  21
+	public void endGame() throws RemoteException;
24 22
 
  23
+	public void gameUpdate() throws RemoteException;
25 24
 }
16  src/base/Srv.java
@@ -15,19 +15,19 @@
15 15
 	public static final String SERVICE_NAME = "SteinEngine";
16 16
 	 
17 17
 	 /**
18  
-	  * Register a {@link Player} by name.
19  
-	 * @param p		player's name
  18
+	  * Register a {@link Player} at the Server.
  19
+	 * @param p		Player
20 20
 	 * @throws RemoteException
21 21
 	 */
22 22
 	 public void register(Player p) throws RemoteException;
23 23
 
24 24
 	 /**
25  
-	 * Get array of players.
26  
-	 * @return Object[]	array of {@link Player} objects
  25
+	  * Remote a {@link Player} from the Server
  26
+	 * @param p
27 27
 	 * @throws RemoteException
28 28
 	 */
29  
-	 public Object[] getPlayers() throws RemoteException;
30  
-
  29
+	public void leave(Player p) throws RemoteException;
  30
+	 
31 31
 	 /**
32 32
 	 * Load a game at the server.
33 33
 	 * @param opponent	opponent's name
@@ -37,11 +37,13 @@
37 37
 	 */
38 38
 	 public Game loadGame(String opponent, String name) throws RemoteException;
39 39
 
  40
+	 public Object[] getPlayers() throws RemoteException;
  41
+	 
40 42
 	 /**
41 43
 	  * End a game at the server.
42 44
 	 * @param game
43 45
 	 */
44  
-	public void endGame(Game game) throws RemoteException;
  46
+	public void remove(Game game) throws RemoteException;
45 47
 	
46 48
 	
47 49
 }
249  src/client/Client.java
... ...
@@ -1,6 +1,7 @@
1 1
 package client;
2 2
 
3 3
 import java.net.MalformedURLException;
  4
+import java.rmi.ConnectException;
4 5
 import java.rmi.Naming;
5 6
 import java.rmi.NotBoundException;
6 7
 import java.rmi.RemoteException;
@@ -10,6 +11,7 @@
10 11
 import org.eclipse.swt.events.MouseEvent;
11 12
 import org.eclipse.swt.events.SelectionAdapter;
12 13
 import org.eclipse.swt.events.SelectionEvent;
  14
+import org.eclipse.swt.graphics.Cursor;
13 15
 import org.eclipse.swt.widgets.Button;
14 16
 import org.eclipse.swt.widgets.Composite;
15 17
 import org.eclipse.swt.widgets.Display;
@@ -51,6 +53,8 @@
51 53
 	private Label lblWins;
52 54
 	private Label lblDraws;
53 55
 	private Label lblDefeats;
  56
+	private static Cursor cursor = new Cursor(display, SWT.CURSOR_ARROW);
  57
+	private static Cursor cursor_busy = new Cursor(display, SWT.CURSOR_WAIT);
54 58
 	
55 59
 	/**
56 60
 	 * Launch the application.
@@ -60,6 +64,7 @@ public static void main(String args[]) {
60 64
 		try {
61 65
 			Display display = Display.getDefault();
62 66
 			Client shell = new Client(display);
  67
+			shell.setCursor(cursor);
63 68
 			shell.open();
64 69
 			shell.layout();
65 70
 			while (!shell.isDisposed()) {
@@ -67,6 +72,7 @@ public static void main(String args[]) {
67 72
 					display.sleep();
68 73
 				}
69 74
 			}
  75
+			System.exit(0);
70 76
 		} catch (Exception e) {
71 77
 			e.printStackTrace();
72 78
 		}
@@ -82,11 +88,8 @@ public static void main(String args[]) {
82 88
 	public Client(Display disp) throws RemoteException, MalformedURLException, NotBoundException {
83 89
 		super(disp, SWT.SHELL_TRIM);
84 90
 		display = disp;
85  
-		server = (Srv)Naming.lookup("SteinEngine");
86  
-		
87 91
 		System.out.println("Started");
88 92
 		
89  
-		
90 93
 		list = new List(this, SWT.BORDER);
91 94
 		list.setEnabled(true);
92 95
 		list.setBounds(250, 7, 174, 245);
@@ -142,30 +145,47 @@ private void LoadWelcomePane() {
142 145
 		btnJoinGame.addSelectionListener(new SelectionAdapter() {
143 146
 			@Override
144 147
 			public void widgetSelected(SelectionEvent event) {
145  
-				String name = userName.getText();
  148
+				final String name = userName.getText();
146 149
 				status.setText("Connecting");
147  
-				try {
148  
-					// Create a new Player
149  
-					player = new Gamer(name, server);
150  
-					
151  
-					// Start Thread to update player list
152  
-					updateList(player, list).start();
153  
-					
154  
-					// Dispose the register view
155  
-					composite.dispose();
156  
-					
157  
-					// Load the game view
158  
-					LoadNewGamePane();
159  
-					status.setText("Game on!"); 
160  
-				} 
161  
-				catch (Exception e) {
162  
-					errorPopup("Serious error! Contact the author of this lousy game!");
163  
-					display.dispose();
164  
-					e.printStackTrace();
165  
-				}
166  
-			}
167  
-
168  
-			
  150
+				
  151
+				composite.setCursor(cursor_busy);
  152
+				display.asyncExec(new Runnable() {
  153
+					public void run() {
  154
+						try {
  155
+							// Connect to Server TODO: localhost?
  156
+							server = (Srv)Naming.lookup("SteinEngine");
  157
+							
  158
+							// Create a new Player
  159
+							player = new Gamer(name, server, getShell(), display);
  160
+		
  161
+							// Update player list
  162
+							updateList();
  163
+							
  164
+							// Dispose the register view
  165
+							composite.dispose();
  166
+							
  167
+							// Load the game view
  168
+							LoadNewGamePane();
  169
+							status.setText("Game on!");
  170
+						}
  171
+						catch (ConnectException ce) {
  172
+							errorPopup("Unable to find the Server specified.");
  173
+							status.setText("Is the Server running?");
  174
+							composite.setCursor(cursor);
  175
+						} 
  176
+						catch (RemoteException ce) {
  177
+							errorPopup(ce.detail.getMessage());
  178
+							status.setText("Choose a different name.");
  179
+							composite.setCursor(cursor);
  180
+						}
  181
+						catch (Exception e) {
  182
+							errorPopup("Serious error! Contact the author of this lousy game!");
  183
+							display.dispose();
  184
+							e.printStackTrace();
  185
+						}
  186
+					}
  187
+				});
  188
+			};
169 189
 		});
170 190
 		btnJoinGame.setText("Join Game");
171 191
 	}
@@ -174,6 +194,7 @@ public void LoadNewGamePane()
174 194
 	{	
175 195
 		composite_2 = new Composite(this, SWT.NONE);
176 196
 		composite_2.setBounds(9, 10, 235, 221);
  197
+		composite_2.setCursor(cursor);
177 198
 
178 199
 		btnPlay = new Button(composite_2, SWT.NONE);
179 200
 		btnPlay.setToolTipText("You have to choose a player!");
@@ -199,10 +220,10 @@ public void widgetSelected(SelectionEvent e) {
199 220
 		lblChooseAPlayer.setBounds(10, 98, 215, 50);
200 221
 		lblChooseAPlayer.setText("Choose a player from the list!");
201 222
 
202  
-
203  
-		// Choose a player from the list of players
204  
-		list.addMouseListener(new MouseAdapter() {
205  
-			
  223
+		/**
  224
+		 *  Choose a player from the list of players
  225
+		 */
  226
+		list.addMouseListener(new MouseAdapter() {	
206 227
 			@Override
207 228
 			public void mouseUp(MouseEvent e) {
208 229
 				if (list.getSelection().length != 0)
@@ -210,7 +231,9 @@ public void mouseUp(MouseEvent e) {
210 231
 				else
211 232
 					return;
212 233
 
213  
-				// Enable gameplay, when other user is selected.
  234
+				/*
  235
+				 *  Enable gameplay, when other user is selected.
  236
+				 */
214 237
 				try {
215 238
 					if (selected.equals(player.name())) {
216 239
 						status.setText("Schizophrenia?");
@@ -227,7 +250,9 @@ public void mouseUp(MouseEvent e) {
227 250
 			}
228 251
 		});
229 252
 
230  
-		// Start a game with the selected player
  253
+		/*
  254
+		 *  Start a game with the selected player
  255
+		 */
231 256
 		btnPlay.addSelectionListener(new SelectionAdapter() {
232 257
 			@Override
233 258
 			public void widgetSelected(SelectionEvent e) {
@@ -277,47 +302,17 @@ public void widgetSelected(SelectionEvent e) {
277 302
 		btnCancel.setText("Cancel");
278 303
 
279 304
 		btnRock = new Button(composite_1, SWT.NONE);
280  
-		btnRock.addSelectionListener(new SelectionAdapter() {
281  
-			@Override
282  
-			public void widgetSelected(SelectionEvent e) {
283  
-				gameButtonsSwitch(false);
284  
-				try {
285  
-					player.move("Rock");
286  
-				} catch (RemoteException e1) {
287  
-					e1.printStackTrace();
288  
-				}
289  
-			}
290  
-		});
  305
+		addButtonMove(btnRock, "Rock");
291 306
 		btnRock.setBounds(0, 196, 75, 25);
292 307
 		btnRock.setText("Rock");
293 308
 
294 309
 		btnScissors = new Button(composite_1, SWT.NONE);
295  
-		btnScissors.addSelectionListener(new SelectionAdapter() {
296  
-			@Override
297  
-			public void widgetSelected(SelectionEvent e) {
298  
-				gameButtonsSwitch(false);
299  
-				try {
300  
-					player.move("Scissors");
301  
-				} catch (RemoteException e1) {
302  
-					e1.printStackTrace();
303  
-				}
304  
-			}
305  
-		});
  310
+		addButtonMove(btnScissors, "Scissors");
306 311
 		btnScissors.setBounds(0, 0, 75, 25);
307 312
 		btnScissors.setText("Scissors");
308 313
 
309 314
 		btnPaper = new Button(composite_1, SWT.NONE);
310  
-		btnPaper.addSelectionListener(new SelectionAdapter() {
311  
-			@Override
312  
-			public void widgetSelected(SelectionEvent e) {
313  
-				gameButtonsSwitch(false);
314  
-				try {
315  
-					player.move("Paper");
316  
-				} catch (RemoteException e1) {
317  
-					e1.printStackTrace();
318  
-				}
319  
-			}
320  
-		});
  315
+		addButtonMove(btnPaper, "Paper");
321 316
 		btnPaper.setBounds(155, 0, 75, 25);
322 317
 		btnPaper.setText("Paper");
323 318
 		
@@ -337,6 +332,26 @@ public void widgetSelected(SelectionEvent e) {
337 332
 		lblDefeats.setBounds(153, 58, 75, 15);
338 333
 	}
339 334
 	
  335
+	
  336
+	/**
  337
+	 * Add a player move listener to the given button
  338
+	 * @param btn
  339
+	 * @param move
  340
+	 */
  341
+	private void addButtonMove(Button btn, final String move) {
  342
+		btn.addSelectionListener(new SelectionAdapter() {
  343
+			@Override
  344
+			public void widgetSelected(SelectionEvent e) {
  345
+				gameButtonsSwitch(false);
  346
+				try {
  347
+					player.move(move);
  348
+				} catch (RemoteException e1) {
  349
+					e1.printStackTrace();
  350
+				}
  351
+			}
  352
+		});
  353
+	}
  354
+	
340 355
 	/*
341 356
 	 * Load the welcome label
342 357
 	 * @param composite
@@ -351,7 +366,7 @@ public void sayWelcome(Composite composite)
351 366
 	}
352 367
 	
353 368
 	/*
354  
-	 * Disable all buttons from game pane
  369
+	 * Toggle all buttons in game pane
355 370
 	 */
356 371
 	public void gameButtonsSwitch(boolean on){
357 372
 		btnRock.setEnabled(on);
@@ -361,7 +376,7 @@ public void gameButtonsSwitch(boolean on){
361 376
 	}
362 377
 	
363 378
 	/*
364  
-	 * Display error message at crash.
  379
+	 * Display error Popup message
365 380
 	 */
366 381
 	private void errorPopup(String message)
367 382
 	{
@@ -372,79 +387,53 @@ private void errorPopup(String message)
372 387
 	}
373 388
 	
374 389
 
  390
+
375 391
 	/*
376 392
 	 * Leave the game, close the window
377 393
 	 */
378 394
 	public void quit()
379  
-	{
380  
-		// TODO leave server
381  
-//		try {
382  
-//			if (client != null)
383  
-//				client.Leave();
384  
-//		} 
385  
-//		catch (Exception e) { e.printStackTrace(); }
  395
+	{//TODO: fixup
  396
+		try {
  397
+			server.leave(player);
  398
+		} catch (RemoteException e) {
  399
+			e.printStackTrace();
  400
+		}
  401
+//		server = null;
  402
+//		player = null;
386 403
 		this.close();
  404
+//		this.dispose();
  405
+//		display.close();
  406
+//		System.exit(0);
387 407
 	}
388 408
 	
389  
-	@Override
390 409
 	protected void checkSubclass() {
391 410
 		// Disable the check that prevents subclassing of SWT components
392 411
 	}
393 412
 
394  
-	private Thread updateList(final Player p, final List l) {
395  
-		return new Thread() {
396  
-			public void run() {
397  
-				while(true) {
398  
-					try {
399  
-						Thread.sleep(100);
400  
-						display.asyncExec(new Runnable() {
401  
-							public void run() {
402  
-								try {
403  
-									// Check if the user list has changed, update
404  
-									if (p.isChanged()) {
405  
-										l.removeAll();
406  
-										Object[] gamers = server.getPlayers();
407  
-										for(int i=0; i<gamers.length; i++){
408  
-											Player g = (Player)gamers[i];
409  
-											l.add(g.name());
410  
-										}
411  
-									}
412  
-									
413  
-									// Check if player is challenged, update
414  
-									if (p.isChallenged()) {
415  
-										LoadGamePane();
416  
-									}
417  
-									
418  
-									// Check if game round is ended, update
419  
-									if (p.roundFinished()) {
420  
-										gameButtonsSwitch(true);
421  
-										lblWins.setText(p.wins() + " wins");
422  
-										lblDraws.setText(p.draws() + " draws");
423  
-										lblDefeats.setText(p.defeats() + " defeats");
424  
-									}
425  
-									
426  
-									if (p.gotLonely()) {
427  
-										// TODO: null left the building
428  
-										errorPopup(p.opponent() + " left the building.");
429  
-										composite_1.dispose();
430  
-										try {
431  
-											player.withdraw();
432  
-										} catch (RemoteException e1) {
433  
-											e1.printStackTrace();
434  
-										}
435  
-										LoadNewGamePane();
436  
-									}
437  
-										
438  
-								} catch (RemoteException e) {
439  
-									e.printStackTrace();
440  
-								}
441  
-							}
442  
-						});
443  
-					} catch (InterruptedException e) {
444  
-						e.printStackTrace();
445  
-					}
446  
-				}
447  
-			}
448  
-		};
  413
+	
  414
+	/**
  415
+	 * Update list of players
  416
+	 * @throws RemoteException
  417
+	 */
  418
+	public void updateList() throws RemoteException {
  419
+		list.removeAll();
  420
+		Object[] gamers = server.getPlayers();
  421
+		for(int i=0; i<gamers.length; i++){
  422
+			Player g = (Player)gamers[i];
  423
+			list.add(g.name());
  424
+		}
  425
+	}
  426
+	
  427
+	//TODO: cleanup
  428
+	public void statusUpdate() throws RemoteException {
  429
+		lblWins.setText(player.wins() + " wins");
  430
+		lblDraws.setText(player.draws() + " draws");
  431
+		lblDefeats.setText(player.defeats() + " defeats");
  432
+	}
  433
+	
  434
+	public void opponentLeft() throws RemoteException {
  435
+		errorPopup(player.opponent() + " left the building.");
  436
+		composite_1.dispose();
  437
+		LoadNewGamePane();
449 438
 	}
450 439
 }
89  src/client/Gamer.java
@@ -6,6 +6,9 @@
6 6
 import java.rmi.RemoteException;
7 7
 import java.rmi.server.UnicastRemoteObject;
8 8
 
  9
+import org.eclipse.swt.widgets.Display;
  10
+import org.eclipse.swt.widgets.Shell;
  11
+
9 12
 import base.Game;
10 13
 import base.Player;
11 14
 import base.Srv;
@@ -20,18 +23,18 @@
20 23
 
21 24
 	private String name;
22 25
 	private Srv server;
23  
-	private boolean listHasChanged;
24 26
 	private Game game;
25  
-	private boolean challengeReceived = false;
26  
-
  27
+	private Client shell;
  28
+	private Display display;
27 29
 	private String challenger;
28 30
 	
29  
-	protected Gamer(String _name, Srv srv) throws RemoteException {
  31
+	protected Gamer(String _name, Srv srv, Shell _shell, Display _display) throws RemoteException {
30 32
 		name = _name;
31 33
 		server = srv;
  34
+		shell = (Client) _shell;
  35
+		display = _display;
32 36
 		server.register(this);
33  
-		listHasChanged = true;
34  
-		System.out.println("regd!");
  37
+		System.out.println("Registered at Server.");
35 38
 	}
36 39
 
37 40
 	/* (non-Javadoc)
@@ -49,14 +52,15 @@ public String name() throws RemoteException {
49 52
 
50 53
 	@Override
51 54
 	public void announce() throws RemoteException {
52  
-		listHasChanged = true;
53  
-	}
54  
-
55  
-	@Override
56  
-	public boolean isChanged() throws RemoteException {
57  
-		if (!listHasChanged) return false;
58  
-		listHasChanged = false;
59  
-		return true;
  55
+		display.asyncExec(new Runnable() {
  56
+			public void run() {
  57
+				try {
  58
+					shell.updateList();
  59
+				} catch (RemoteException e) {
  60
+					e.printStackTrace();
  61
+				}
  62
+			}
  63
+		});
60 64
 	}
61 65
 
62 66
 	@Override
@@ -84,23 +88,31 @@ public void move(String move) throws RemoteException {
84 88
 
85 89
 	@Override
86 90
 	public void challenge(String attacker) throws RemoteException {
87  
-		challengeReceived = true;
88 91
 		challenger = attacker;
89  
-	}
90  
-
91  
-	@Override
92  
-	public boolean isChallenged() throws RemoteException {
93  
-		if (!challengeReceived) return false;
94  
-		game = server.loadGame(name, challenger);
95  
-		challengeReceived = false;
96  
-		return true;
  92
+		display.asyncExec(new Runnable() {
  93
+			public void run() {
  94
+				try {
  95
+					game = server.loadGame(name, challenger);
  96
+					shell.LoadGamePane();
  97
+				} catch (RemoteException e) {
  98
+					e.printStackTrace();
  99
+				}
  100
+			}
  101
+		});
97 102
 	}
98 103
 	
99 104
 	@Override
100  
-	public boolean roundFinished() throws RemoteException {
101  
-		if (game != null)
102  
-			return game.finished();
103  
-		return false;
  105
+	public void gameUpdate() throws RemoteException {
  106
+		display.asyncExec(new Runnable() {
  107
+			public void run() {
  108
+				try {
  109
+					shell.gameButtonsSwitch(true);
  110
+					shell.statusUpdate();
  111
+				} catch (RemoteException e) {
  112
+					e.printStackTrace();
  113
+				}
  114
+			}
  115
+		});
104 116
 	}
105 117
 
106 118
 	@Override
@@ -120,20 +132,21 @@ public String defeats() throws RemoteException {
120 132
 
121 133
 	@Override
122 134
 	public void withdraw() throws RemoteException {
123  
-		if (game != null) game.withdraw();
124  
-		game = null;
  135
+		game.withdraw(name);
  136
+		server.remove(game);
125 137
 	}
126  
-
  138
+	
127 139
 	@Override
128  
-	public boolean gotLonely() throws RemoteException {
129  
-		if (game != null)
130  
-			if (game.canceled()) {
131  
-				// TODO: move endTimes to server
132  
-				game.endTimes();
133  
-				game = null;
134  
-				return true;
  140
+	public void endGame() throws RemoteException {
  141
+		display.asyncExec(new Runnable() {
  142
+			public void run() {
  143
+				try {
  144
+					shell.opponentLeft();
  145
+				} catch (RemoteException e) {
  146
+					e.printStackTrace();
  147
+				}
135 148
 			}
136  
-		return false;
  149
+		});
137 150
 	}
138 151
 
139 152
 }
31  src/server/Play.java
@@ -25,15 +25,12 @@
25 25
 	private String title;
26 26
 	private String owM = null;
27 27
 	private String opM = null;
28  
-	private boolean ongoingRound;
29  
-	private boolean gameOver;
30 28
 
31 29
 	protected Play(Player _opponent, Player _owner) throws RemoteException {
32 30
 		opponent = _opponent;
33 31
 		owner = _owner;
34 32
 		setTitle();
35 33
 		opponent.challenge(owner.name());
36  
-		ongoingRound=true;
37 34
 	}
38 35
 
39 36
 	@Override
@@ -62,7 +59,6 @@ public String title() throws RemoteException {
62 59
 
63 60
 	@Override
64 61
 	public void move(String name, String move) throws RemoteException {
65  
-		ongoingRound=true;
66 62
 		if (owner.name().equals(name))
67 63
 			owM = move;
68 64
 		if (opponent.name().equals(name))
@@ -81,7 +77,9 @@ public void move(String name, String move) throws RemoteException {
81 77
 					opponentWins++;
82 78
 			
83 79
 			// Round finished
84  
-			ongoingRound=false;
  80
+			// TODO: update Players
  81
+			owner.gameUpdate();
  82
+			opponent.gameUpdate();
85 83
 			owM = opM = null;
86 84
 		}
87 85
 	}
@@ -93,11 +91,6 @@ public boolean amIstupid(String name) throws RemoteException {
93 91
 	}
94 92
 
95 93
 	@Override
96  
-	public boolean finished() throws RemoteException {
97  
-		return !ongoingRound;
98  
-	}
99  
-
100  
-	@Override
101 94
 	public String wins(String name) throws RemoteException {
102 95
 		if (owner.name().equals(name))
103 96
 			return ""+ownerWins;
@@ -117,19 +110,11 @@ public String defeats(String name) throws RemoteException {
117 110
 	}
118 111
 
119 112
 	@Override
120  
-	public void withdraw() throws RemoteException {
121  
-		gameOver=true;
122  
-	}
123  
-	
124  
-	@Override
125  
-	public boolean canceled() throws RemoteException {
126  
-		return gameOver;
127  
-	}
128  
-
129  
-	@Override
130  
-	public void endTimes() {
131  
-		System.out.println(title + " terminated.");
132  
-		owner = opponent = null;
  113
+	public void withdraw(String name) throws RemoteException {
  114
+		if (owner.name().equals(name))
  115
+			opponent.endGame();
  116
+		else
  117
+			owner.endGame();
133 118
 	}
134 119
 
135 120
 }
41  src/server/Server.java
@@ -34,6 +34,10 @@
34 34
 	 * games - list of initiated games 
35 35
 	 */
36 36
 	public Hashtable<String,Play> games;
  37
+	/**
  38
+	 * playercount - number of players registered
  39
+	 */
  40
+	private int playercount = 0;
37 41
 	
38 42
 	
39 43
 	/**
@@ -50,7 +54,7 @@ protected Server() throws RemoteException, MalformedURLException {
50 54
 	}
51 55
 
52 56
 	/**
53  
-	 * Create a Server instance, bind to SERVICE_NAME and start the Action thread.
  57
+	 * Create a Server instance, and bind it to the RMI registry SERVICE_NAME.
54 58
 	 *   
55 59
 	 * @param args	No arguments expected or handled.
56 60
 	 */
@@ -59,7 +63,6 @@ public static void main(String[] args) {
59 63
 			Server s = new Server();
60 64
 			LocateRegistry.createRegistry(1099);
61 65
 			Naming.rebind(SERVICE_NAME, s);
62  
-			new Action(s).run();
63 66
 		} catch (RemoteException e) {
64 67
 			e.printStackTrace();
65 68
 		} catch (MalformedURLException e) {
@@ -70,10 +73,22 @@ public static void main(String[] args) {
70 73
 	/* (non-Javadoc)
71 74
 	 * @see base.Srv#getPlayers()
72 75
 	 */
73  
-	@Override
74  
-	public Object[] getPlayers(){
75  
-		Object[] u = players.values().toArray();
76  
-		return u;
  76
+	public Object[] getPlayers() {
  77
+		return players.values().toArray();
  78
+	}
  79
+	
  80
+	/**
  81
+	 * @param except	which player to not annouce
  82
+	 * @throws RemoteException
  83
+	 */
  84
+	private void announce(String except) throws RemoteException{
  85
+		Object[] list = getPlayers();
  86
+		if(list.length != 0)
  87
+			for(int i=0; i<list.length; i++){
  88
+				Player p = (Player)list[i];
  89
+				if (!p.name().equals(except))
  90
+					p.announce();
  91
+			}
77 92
 	}
78 93
 	
79 94
 	/* (non-Javadoc)
@@ -81,9 +96,18 @@ public static void main(String[] args) {
81 96
 	 */
82 97
 	@Override
83 98
 	public void register(Player p) throws RemoteException {
  99
+		if (players.containsKey(p.name()))
  100
+			throw new RemoteException("A player by that name is already present.");
84 101
 		players.put(p.name(), p);
  102
+		announce(p.name());
85 103
 		System.out.println(p.name() + " joined.");
86 104
 	}
  105
+	
  106
+	@Override
  107
+	public void leave(Player p) throws RemoteException {
  108
+		System.out.println(p.name() + " left.");
  109
+		players.remove(p.name());
  110
+	}
87 111
 
88 112
 	/* (non-Javadoc)
89 113
 	 * @see base.Srv#createGame(java.lang.String, java.lang.String)
@@ -100,8 +124,11 @@ public Game loadGame(String opponent, String name) throws RemoteException {
100 124
 		return game;
101 125
 	}
102 126
 	
  127
+	/* (non-Javadoc)
  128
+	 * @see base.Srv#endGame(base.Game)
  129
+	 */
103 130
 	@Override
104  
-	public void endGame(Game game) throws RemoteException {
  131
+	public void remove(Game game) throws RemoteException {
105 132
 		games.remove(game.title());
106 133
 	}
107 134
 	

0 notes on commit 69caf87

Please sign in to comment.
Something went wrong with that request. Please try again.