コード例 #1
0
 private void handleKeepAlive() {
   Connection c = null;
   try {
     netCode = ois.readInt();
     c = checkConnectionNetCode();
     if (c != null && c.getState() == Connection.STATE_CONNECTED) {
       oos.writeInt(ACK_KEEP_ALIVE);
       oos.flush();
       System.out.println("->ACK_KEEP_ALIVE"); // $NON-NLS-1$
     } else {
       System.out.println("->NOT_CONNECTED"); // $NON-NLS-1$
       oos.writeInt(NOT_CONNECTED);
       oos.flush();
     }
   } catch (IOException e) {
     System.out.println("handleKeepAlive: " + e.getMessage()); // $NON-NLS-1$
     if (c != null) {
       c.setState(Connection.STATE_DISCONNECTED);
       System.out.println(
           "Connection closed with "
               + c.getRemoteAddress()
               + //$NON-NLS-1$
               ":"
               + c.getRemotePort()); // $NON-NLS-1$
     }
   }
 }
コード例 #2
0
  private void handleSendCode() {
    Connection c = null;
    GeneticCode code;

    try {
      netCode = ois.readInt();
      c = checkConnectionNetCode();
      if (c != null && c.getState() == Connection.STATE_CONNECTED) {
        oos.writeInt(WAITING_CODE);
        oos.flush();
        System.out.println("->WAITING_CODE"); // $NON-NLS-1$
        code = (GeneticCode) ois.readObject();
        System.out.println("Genetic code"); // $NON-NLS-1$
        oos.writeInt(CODE_RECEIVED);
        oos.flush();
        System.out.println("->CODE_RECEIVED"); // $NON-NLS-1$
        c.getInCorridor().receiveOrganism(code);
      } else {
        System.out.println("->NOT_CONNECTED"); // $NON-NLS-1$
        oos.writeInt(NOT_CONNECTED);
        oos.flush();
      }
    } catch (IOException e) {
      System.out.println("handleSendCode: " + e.getMessage()); // $NON-NLS-1$
      if (c != null) {
        c.setState(Connection.STATE_DISCONNECTED);
        System.out.println(
            "Connection closed with "
                + c.getRemoteAddress()
                + //$NON-NLS-1$
                ":"
                + c.getRemotePort()); // $NON-NLS-1$
      }
    } catch (ClassNotFoundException e) {
      System.out.println("handleSendCode: " + e.getMessage()); // $NON-NLS-1$
      if (c != null) {
        c.setState(Connection.STATE_DISCONNECTED);
        System.out.println(
            "Connection closed with "
                + c.getRemoteAddress()
                + //$NON-NLS-1$
                ":"
                + c.getRemotePort()); // $NON-NLS-1$
      }
    }
  }
コード例 #3
0
 private Connection checkConnectionDuplicity() {
   synchronized (connections) {
     for (Connection c : connections) {
       if (c.getRemoteAddress().equals(address) && c.getRemotePort() == port) {
         System.out.println("Duplicated connection"); // $NON-NLS-1$
         return c;
       }
     }
   }
   return null;
 }
コード例 #4
0
  @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$
    }
  }