public synchronized FreerailsPrincipal getPrincipal(ConnectionToServer c) { Player p; if (c == null) { /* No connection implies the principal is the server itself */ return Player.AUTHORITATIVE; } if ((p = (Player) principals.get(c)) == null) { return Player.NOBODY; } return p.getPrincipal(); }
/** * TODO deprecate strings in favour of certificates XXX currently we only support a single player * per connection. * * @param player the player trying to connect * @return true if the connection was successfully identified with the specified player */ public synchronized boolean addConnection(ConnectionToServer c, Player player, byte[] signature) { logger.log(Level.INFO, "Authenticating player " + player.getName()); /* determine whether this identity already exists */ NonNullElements i = new NonNullElements(KEY.PLAYERS, serverGameEngine.getWorld(), Player.AUTHORITATIVE); while (i.next()) { Player p = (Player) i.getElement(); if (p.getName().equals(player.getName())) { /* this player already exists */ /* is this identity already connected ? */ if (principals.containsValue(p)) { logger.log(Level.INFO, "Player " + p.getName() + " is already" + " connected"); return false; } /* is this player the same as the one which previously * connected under the same name? */ logger.log(Level.FINE, "Verifying player " + p + " with " + player); if (!p.verify(player, signature)) { logger.log(Level.WARNING, "Couldn't verify signature of player " + p.getName()); return false; } principals.put(c, p); /* set the connection world */ if (c instanceof LocalConnection) { // don't create a WorldView, since the local client needs // to synchronize against the world object itself. c.setWorld(serverGameEngine.getWorld()); } else { c.setWorld(new WorldView(serverGameEngine.getWorld(), p.getPrincipal())); } return true; } } /* this player does not already exist */ logger.log( Level.INFO, "Adding player " + player.getName() + " to " + serverGameEngine.getWorld()); MoveConfirmer mc = new MoveConfirmer(serverGameEngine.getMoveExecuter(), serverGameEngine.getMoveChainFork()); AddPlayerMove m = new AddPlayerMove(serverGameEngine.getWorld(), player); MoveStatus ms = mc.confirmMove(m, null); assert ms == MoveStatus.MOVE_OK; /* * get the newly created player-with-principal */ World w = serverGameEngine.getWorld(); assert (w != null); player = (Player) w.get(KEY.PLAYERS, w.size(KEY.PLAYERS, Player.AUTHORITATIVE) - 1, Player.AUTHORITATIVE); principals.put(c, player); /* Perform any moves necessary for adding a new player */ serverGameEngine .getMoveExecuter() .processMove(statGatherer.generateNewPlayerMove(player.getPrincipal()), c); serverGameEngine .getMoveExecuter() .processMove(scenario.getSetupMoves(serverGameEngine.getWorld(), player.getPrincipal()), c); /* set the connection world */ c.setWorld(new WorldView(serverGameEngine.getWorld(), player.getPrincipal())); return true; }