/**
   * Konstruktor, uebernimmt eine Socket-Verbindung vom Server, initialisiert die Ein-und
   * Ausgabestreams des Sockets und speichert seine Spielernummer, die er vom Server zugewiesen
   * bekommt. Diese wird dann zuletzt an den Communicator gesendet.
   *
   * @param connection Socketverbindung
   * @param server Server-Objekt
   * @param cnt Spielernummer
   * @param gameID Spiel-ID
   * @param name Spielname
   * @exception IOException
   * @exception SocketException
   */
  public ServerThread(Socket connection, Server server, int cnt, int gameID, String name) {
    this.connection = connection;
    this.server = server;
    this.myNumber = cnt;
    this.gameID = gameID;
    this.nameOfTheGame = name;

    try {
      out =
          new PrintWriter(
              new BufferedWriter(new OutputStreamWriter(this.connection.getOutputStream())));
      in = new BufferedReader(new InputStreamReader(this.connection.getInputStream()));
    } catch (IOException e) {
      String log = "player " + (myNumber + 1) + ":error creating ServerThread\n";
      System.out.print(log);
      this.server.log(log, server.getPort());
    }

    send("1" + Integer.toString(myNumber)); // sende die zugewiesene
    // Spielernummer
    // an den Communicator
    try {
      this.connection.setSoTimeout(1800000);
      this.connection.setSoLinger(true, 6); // optional?
    } catch (SocketException e) {
      String log = "player " + (myNumber + 1) + ": error setting socket options\n";
      System.out.print(log);
      this.server.log(log, server.getPort());
    }
  }
  /**
   * enthaelt den ausfuehrbaren Code des Threads; hier wird auf Nachrichten gewartet, die dann ueber
   * das Server-Objekt weitergeleitet werden koennen (siehe Methode forward() des Servers). Die
   * Zieladresse der Nachricht wird ueberprueft, um dann die Nachricht an die entsprechende
   * Server-Methode weiterzugeben. Im Fehlerfall wird hier die Verbindung getrennt und die Methode
   * shutdown() auf dem Server aufgerufen, um das Spiel zu entfernen.
   *
   * @exception IOException
   * @exception InterruptedIOException
   */
  public void run() {
    while (!isInterrupted()) {
      // hier wird die Zieladresse (target) aus der Nachricht extrahiert
      try {
        String message = receive();
        // System.out.println("ServerThread "+myNumber+":"+" "+message);

        if (message != null) {
          StringTokenizer ST = new StringTokenizer(message);
          int target = Integer.parseInt(ST.nextToken());
          System.out.println("sending message from " + myNumber + " to " + target);
          // falls target="-1": Nachricht an Server; sonst Nachricht
          // an Spieler x
          if (target >= -1) {
            server.forward(gameID, message);
          } else {
            send("1exit");
            server.deletePlayer(message + " " + gameID);
            interrupt();

            try {
              connection.close();
            } catch (IOException ex) {
            }
          }

        } else {
          String log = "Aborting game! Player " + (myNumber + 1) + " has left the game\n";
          System.out.print(log);
          server.log(log, server.getPort());
          interrupt();
          server.shutdown(nameOfTheGame + " " + myNumber + " " + gameID, log);
        }
      } catch (InterruptedIOException e) {
        String log =
            "Aborting game! TimeOut while receiving message"
                + " from player "
                + (myNumber + 1)
                + "\n";
        System.out.print(log);
        server.log(log, server.getPort());
        interrupt();
        server.shutdown(nameOfTheGame + " " + myNumber + " " + gameID, log);
      } catch (IOException e) {
        String log = "Aborting game! Player " + (myNumber + 1) + " has left the game\n";
        System.out.print(log);
        server.log(log, server.getPort());
        interrupt();
        server.shutdown(nameOfTheGame + " " + myNumber + " " + gameID, log);
      }
    }
  }
  public void connect() {
    try {

      establishConnectionWithClient(Server.getIP(), Server.getPort(), clientName, uname, password);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * trennt die Socketverbindung zum entsprechenden Client
  *
  * @exception IOException
  */
 public void closeConnection() {
   try {
     connection.close();
   } catch (IOException e) {
     String log = "player " + (myNumber + 1) + ": error closing connection\n";
     System.out.print(log);
     server.log(log, server.getPort());
   }
 }
Beispiel #5
0
 /**
  * Tests the action string setPort, ensuring it correctly changes the port number on the {@link
  * Server}.
  */
 @Test
 public void testSHMSetPort() {
   LOGGER.log(Level.INFO, "----- STARTING TEST testSHMSetPort -----");
   String action = "setPort";
   ArrayList<String> parameters = new ArrayList<String>();
   parameters.add("25467");
   try {
     server2.handleAction(action, parameters);
   } catch (NetworkingIOException | NetworkingRuntimeException e) {
     exception = true;
   }
   Assert.assertFalse(exception, "Exception found");
   Assert.assertEquals(server2.getPort(), 25467, "Value for port was not set correctly");
   LOGGER.log(Level.INFO, "----- TEST testSHMSetPort COMPLETED -----");
 }
 public void launchOnServer(Server sv) {
   try {
     Runtime.getRuntime()
         .exec(
             "cmd.exe /C cd \""
                 + this.pathToCod4
                 + "\" && start iw3mp.exe connect "
                 + sv.getIp()
                 + ":"
                 + sv.getPort());
   } catch (IOException e) {
     System.out.println("Error launching the .exe");
     e.printStackTrace();
   }
 }
Beispiel #7
0
 @Test
 public void exposedPortShouldBeReturned() {
   assertThat(server.getPort(), equalTo(SOME_TEXT));
 }
Beispiel #8
0
 public static int getPort() {
   return server.getPort();
 }