Esempio n. 1
0
 /** Safely disconnects this ClientHandler from the Server */
 public void disconnect() {
   try {
     pool.removeFromAllQueues(this);
     if (game != null) {
       game.endGame(false); // End the game the client was in.
     }
     reader.close();
     writer.close();
     client.close();
     System.out.println("[Server] Debug (ClientHandler) - Client has disconnected.");
     pool.removeClient(name);
     connected = false;
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Esempio n. 2
0
  /**
   * parses a message
   *
   * @param message is a String array
   */
  public void parse(String[] message) {
    switch (message[0]) {
      case IProtocol.CLIENT_IDENTIFY:
        if (message.length < 1) {
          break;
        }

        String userName = message[1];

        try {
          pool.addClient(name, this);
          name = userName;
          sendRaw(IProtocol.SERVER_IDENTIFY);
        } catch (InvalidNameException e) {
          sendError(IProtocol.Error.NAME_INVALID.ordinal() + " Invalid name");
        } catch (UsedNameException e) {
          sendError(IProtocol.Error.NAME_USED.ordinal() + " Name is already in use");
        }

        break;
      case IProtocol.CLIENT_QUIT:
        disconnect();
        break;
      case IProtocol.CLIENT_MOVE_PUT:
        if (game == null) {
          break;
        }
        if (game.getTurnClient().equals(this)) {
          doMovePut(Arrays.copyOfRange(message, 1, message.length));
        } else {
          sendError(IProtocol.Error.ILLEGAL_STATE.ordinal() + " It is not your turn");
        }
        break;
      case IProtocol.CLIENT_MOVE_TRADE:
        if (game == null) {
          break;
        }
        if (game.getTurnClient().equals(this)) {
          doMoveTrade(Arrays.copyOfRange(message, 1, message.length));
        } else {
          sendError(IProtocol.Error.ILLEGAL_STATE.ordinal() + " It is not your turn");
        }
        break;
      case IProtocol.CLIENT_QUEUE:
        if (message.length < 1) {
          break;
        } else if (game != null) {
          sendError(
              IProtocol.Error.ILLEGAL_STATE.ordinal()
                  + " You cannot queue while you are in a game");
          break;
        }

        String[] queues = message[1].split(",");

        try {
          for (String queue : queues) {
            try {
              int queueSize = Integer.parseInt(queue);
              pool.addClientToQueue(this, queueSize);
            } catch (NumberFormatException e) {
              System.out.println("Player tried to join invalid queue.");
            }
          }
          sendRaw(IProtocol.SERVER_QUEUE);
        } catch (NumberFormatException e) {
          sendRaw(String.valueOf(IProtocol.Error.QUEUE_INVALID));
        }
        break;
      default:
        sendRaw(
            String.valueOf(
                IProtocol.Error.INVALID_COMMAND.ordinal()
                    + " The server does not recognise this command"));
        break;
    }
  }
Esempio n. 3
0
 /**
  * Stop waiting for connections, close all connected clients, and close this server's {@link
  * ServerSocket}.
  *
  * @throws IOException if any socket is invalid.
  */
 public void kill() throws IOException {
   _running = false;
   _clients.killall();
   _botSocket.close();
   _socket.close();
 }