1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.cb.jset.client;
21
22 import org.cb.jset.server.RemoteSetGame;
23 import org.cb.jset.server.RemoteSetGameServer;
24 import org.cb.jset.server.RemoteSetGamePlayer;
25 import org.cb.jset.server.RemoteSetGameConnection;
26 import org.cb.jset.SetGamePlayer;
27 import org.cb.jset.CardProperties;
28 import org.cb.jset.CardSet;
29 import org.cb.jset.SetGameConnection;
30 import org.cb.jset.MatchingException;
31 import org.cb.jset.SetGame;
32
33 import java.rmi.Naming;
34 import java.rmi.RMISecurityManager;
35 import java.rmi.RemoteException;
36 import java.rmi.NotBoundException;
37 import java.rmi.server.UnicastRemoteObject;
38 import java.io.Serializable;
39 import java.net.MalformedURLException;
40
41 /***
42 * FIXME rename. More like a RemoteGameHelper class
43 * @author jerome@coffeebreaks.org - last modified by $LastChangedBy: jerome $
44 * @version $Id: Client.java 129 2004-04-15 05:00:43Z jerome $
45 */
46 public class Client
47 {
48 private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger("Client");
49
50 static class LocalPlayerWrapper extends UnicastRemoteObject implements Serializable, RemoteSetGamePlayer
51 {
52 private final transient SetGamePlayer _localPlayer;
53
54 public LocalPlayerWrapper(final SetGamePlayer localPlayer)
55 throws RemoteException
56 {
57 _localPlayer = localPlayer;
58 }
59
60 public String getName() throws RemoteException
61 {
62 return _localPlayer.getName();
63 }
64
65 public void block() throws RemoteException
66 {
67 _localPlayer.block();
68 }
69
70 public void unblock() throws RemoteException
71 {
72 _localPlayer.unblock();
73 }
74
75 public boolean isBlocked() throws RemoteException
76 {
77 return _localPlayer.isBlocked();
78 }
79
80 public void disconnect() throws RemoteException
81 {
82 _localPlayer.disconnect();
83 }
84
85 public void cardsAdded(final CardProperties[] cards) throws RemoteException
86 {
87 _localPlayer.cardsAdded(cards);
88 }
89
90 public void setRemoved(final CardSet set) throws RemoteException
91 {
92 _localPlayer.setRemoved(set);
93 }
94 }
95
96 static class RemoteConnectionWrapper implements SetGameConnection
97 {
98 private final RemoteSetGameConnection _remoteConnection;
99 private boolean _wasDisconnected;
100 private RemoteException _remoteException;
101
102 void treatConnectionProblem(final RemoteException e)
103 {
104 _wasDisconnected = true;
105 _remoteException = e;
106 throw new IllegalStateException("Failure to make a remote call... " + e.getMessage());
107 }
108
109 public RemoteConnectionWrapper(final RemoteSetGameConnection localConnection)
110 {
111 _remoteConnection = localConnection;
112 }
113
114 public void matchSet(final CardSet cards) throws MatchingException
115 {
116 try
117 {
118 _remoteConnection.matchSet(cards);
119 }
120 catch (RemoteException e)
121 {
122 treatConnectionProblem(e);
123 }
124 }
125
126 public void close()
127 {
128 try
129 {
130 _remoteConnection.close();
131 }
132 catch (RemoteException e)
133 {
134 treatConnectionProblem(e);
135 }
136 }
137
138 public boolean isClosed()
139 {
140 try
141 {
142 return _remoteConnection.isClosed();
143 }
144 catch (RemoteException e)
145 {
146 treatConnectionProblem(e);
147 }
148 return false;
149 }
150 }
151
152
153 static class RemoteGameWrapper implements SetGame
154 {
155 private final RemoteSetGame _remoteGame;
156 private boolean _wasDisconnected;
157 private RemoteException _remoteException;
158
159 void treatConnectionProblem(final RemoteException e)
160 {
161 _wasDisconnected = true;
162 _remoteException = e;
163 throw new IllegalStateException("Failure to make a remote call... " + e.getMessage());
164 }
165
166 public RemoteGameWrapper(final RemoteSetGame remoteGame)
167 {
168 _remoteGame = remoteGame;
169 }
170
171 public int getID()
172 {
173 try
174 {
175 return _remoteGame.getID();
176 }
177 catch (RemoteException e)
178 {
179 treatConnectionProblem(e);
180 }
181 return -1;
182 }
183
184 public String getName()
185 {
186 try
187 {
188 return _remoteGame.getName();
189 }
190 catch (RemoteException e)
191 {
192 treatConnectionProblem(e);
193 }
194 return "";
195 }
196
197 public void start()
198 {
199
200 }
201
202 public SetGameConnection connect(final SetGamePlayer player)
203 {
204 try
205 {
206 final RemoteSetGamePlayer remotePlayer = new LocalPlayerWrapper(player);
207 return new RemoteConnectionWrapper(_remoteGame.connect(remotePlayer));
208 }
209 catch (RemoteException e)
210 {
211 treatConnectionProblem(e);
212 }
213 return null;
214 }
215
216 public int getMaxNbPlayers()
217 {
218 try
219 {
220 _remoteGame.getMaxNbPlayers();
221 }
222 catch (RemoteException e)
223 {
224 treatConnectionProblem(e);
225 }
226 return -1;
227 }
228
229 public int getCurrentNbPlayers()
230 {
231 try
232 {
233 _remoteGame.getCurrentNbPlayers();
234 }
235 catch (RemoteException e)
236 {
237 treatConnectionProblem(e);
238 }
239 return -1;
240 }
241 }
242
243
244 public static void main(final String[] args)
245 {
246 org.apache.log4j.PropertyConfigurator.configure("log4j.properties");
247 String host = "localhost";
248 String gameName = "TestGame";
249 String playerName = "TestGame";
250 final int nbParsedArgs = Math.min(3, args.length);
251 switch (nbParsedArgs)
252 {
253 case 3:
254 playerName = args[2];
255 case 2:
256 gameName = args[1];
257 case 1:
258 host = args[0];
259 }
260 if (System.getSecurityManager() == null)
261 {
262 System.setSecurityManager(new RMISecurityManager());
263 }
264 try
265 {
266 final RemoteSetGame game = getGame(host, gameName);
267
268 if (game != null)
269 {
270 LOGGER.info("Game getName " + game.getName());
271
272
273 final SmartAutoPlayer player = new SmartAutoPlayer(playerName);
274 player.startPlayingGame(game);
275 }
276 }
277 catch (Exception e)
278 {
279 LOGGER.error("Client Error: " + e.getMessage(), e);
280 }
281 }
282
283 public static RemoteSetGame getGame(final String host, final String gameName)
284 throws NotBoundException, MalformedURLException, RemoteException
285 {
286 final String name = "//" + host + "/JSetServer";
287
288
289 final RemoteSetGameServer jSetServer = (RemoteSetGameServer) Naming.lookup(name);
290 final RemoteSetGame[] games = jSetServer.getGames();
291 LOGGER.info(games.length + " games found on the server " + name);
292 for (int i = 0; i < games.length; i++)
293 {
294 LOGGER.info(games[i] + " <" + games[i].getName() + "> " + games[i].getMaxNbPlayers());
295 }
296
297 RemoteSetGame game = null;
298 final int nbPlayers = 2;
299
300 for (int i = 0; i < games.length; i++)
301 {
302 if (games[i].getName().equals(gameName))
303 {
304
305
306
307 LOGGER.info("Found a game:" + games[i] + " <" + games[i].getName() + "> " + games[i].getMaxNbPlayers());
308 game = games[i];
309 break;
310 }
311 }
312 if (game == null)
313 {
314 LOGGER.info("Creating a game: getName:" + gameName + " nbplayers " + nbPlayers);
315 final int gameID = jSetServer.createGame(gameName, nbPlayers);
316 LOGGER.info("Game created with ID:" + gameID);
317 game = jSetServer.getGame(gameID);
318 }
319 return game;
320 }
321 }