Coverage report

  %line %branch
org.cb.jset.client.Client$RemoteConnectionWrapper
0% 
0% 

 1  
 // START LICENSE
 2  
 // JSet - a Java JSet card board game implementation
 3  
 // Copyright (C) 2004 Jerome Lacoste
 4  
 //
 5  
 // This program is free software; you can redistribute it and/or modify
 6  
 // it under the terms of the GNU General Public License as published by
 7  
 // the Free Software Foundation; either version 2 of the License, or (at
 8  
 // your option) any later version.
 9  
 //
 10  
 // This program is distributed in the hope that it will be useful, but
 11  
 // WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU General Public License
 16  
 // along with this program; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18  
 // END LICENSE
 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  0
       _wasDisconnected = true;
 105  0
       _remoteException = e;
 106  0
       throw new IllegalStateException("Failure to make a remote call... " + e.getMessage());
 107  
     }
 108  
 
 109  
     public RemoteConnectionWrapper(final RemoteSetGameConnection localConnection)
 110  0
     {
 111  0
       _remoteConnection = localConnection;
 112  0
     }
 113  
 
 114  
     public void matchSet(final CardSet cards) throws MatchingException
 115  
     {
 116  
       try
 117  
       {
 118  0
         _remoteConnection.matchSet(cards);
 119  
       }
 120  0
       catch (RemoteException e)
 121  
       {
 122  0
         treatConnectionProblem(e);
 123  0
       }
 124  0
     }
 125  
 
 126  
     public void close()
 127  
     {
 128  
       try
 129  
       {
 130  0
         _remoteConnection.close();
 131  
       }
 132  0
       catch (RemoteException e)
 133  
       {
 134  0
         treatConnectionProblem(e);
 135  0
       }
 136  0
     }
 137  
 
 138  
     public boolean isClosed()
 139  
     {
 140  
       try
 141  
       {
 142  0
         return _remoteConnection.isClosed();
 143  
       }
 144  0
       catch (RemoteException e)
 145  
       {
 146  0
         treatConnectionProblem(e);
 147  
       }
 148  0
       return false; // please compiler
 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; // please compiler
 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 ""; // please compiler
 195  
     }
 196  
 
 197  
     public void start()
 198  
     {
 199  
       // FIXME
 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; // please compiler
 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; // please compiler
 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; // please compiler
 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  
 //        SimplePlayer player = new SimplePlayer(playerName);
 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, class="keyword">final String gameName)
 284  
       throws NotBoundException, MalformedURLException, RemoteException
 285  
   {
 286  
     final String name = "//" + host + "/JSetServer";
 287  
 //            Object obj = Naming.lookup(getName);
 288  
 //            logger.info("Server of type:" + obj.getClass());
 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  
     // find a game with 2 players
 300  
     for (int i = 0; i < games.length; i++)
 301  
     {
 302  
       if (games[i].getName().equals(gameName))
 303  
       {
 304  
 //                    == nbPlayers &&
 305  
 //                games[i].getMaxNbPlayers() == nbPlayers &&
 306  
 //                    games[i].getCurrentNbPlayers() < nbPlayers) {
 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  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.