@Override public void run() { isActive = true; try { serverSocket = new ServerSocket(Utils.getLOCAL_PORT()); notifyStatusListeners( Messages.getInstance() .getString( "T_NET_SERVER_LISTENING_ON_PORT", Integer.toString(Utils.getLOCAL_PORT()))); // $NON-NLS-1$ } catch (IOException e) { if (e instanceof BindException) { notifyStatusListeners( Messages.getInstance() .getString( "T_PORT_ALREADY_IN_USE", Integer.toString(Utils.getLOCAL_PORT()))); // $NON-NLS-1$ } else e.printStackTrace(); isActive = false; } while (isActive) { try { listenSocket = serverSocket.accept(); ois = new ObjectInputStream(listenSocket.getInputStream()); oos = new ObjectOutputStream(listenSocket.getOutputStream()); receivedMessage = ois.readInt(); System.out.println(messageToString(receivedMessage)); switch (receivedMessage) { case CONNECT: handleConnect(); break; case SEND_CODE: handleSendCode(); break; case KEEP_ALIVE: handleKeepAlive(); break; case DISCONNECT: handleDisconnect(); break; } ois.close(); oos.close(); } catch (IOException e) { System.out.println(e.getMessage()); } finally { if (listenSocket != null) try { listenSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public void setAcceptConnections(boolean newAcceptConnections) { if (newAcceptConnections != acceptConnections) { acceptConnections = newAcceptConnections; notifyStatusListeners( Messages.getInstance() .getString( "T_NET_SERVER_LISTENING_ON_PORT", //$NON-NLS-1$ Integer.toString(Utils.getLOCAL_PORT()))); if (newAcceptConnections) startServer(); else if (getConnections().isEmpty()) closeServer(); } }
@Override public void update(Connection c) { int state = c.getState(); World w = currentWorld.getWorld(); if (state == Connection.STATE_DISCONNECTED) { notifyStatusListeners( Messages.getInstance() .getString("T_CONNECTION_LOST", c.getRemoteAddress().toString())); // $NON-NLS-1$ w.removeAgent(c.getInCorridor()); w.removeAgent(c.getOutCorridor()); removeConnection(c); } else if (state == Connection.STATE_CONNECTED) { w.addAgent(c.getInCorridor(), null); w.addAgent(c.getOutCorridor(), null); notifyStatusListeners( Messages.getInstance() .getString( "T_CONNECTION_STABLISHED", c.getRemoteAddress().toString())); // $NON-NLS-1$ } }