コード例 #1
0
 @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();
         }
     }
   }
 }
コード例 #2
0
  @Override
  public boolean shield(Segment otherSegment) {
    boolean shielded = false;
    AliveAgent thisAgent = getThisAgent();

    if (isAlive() && thisAgent.useEnergy(Utils.getBLUE_ENERGY_CONSUMPTION())) {
      thisAgent.setColor(Color.BLUE);
      shielded = true;
    }
    return shielded;
  }
コード例 #3
0
 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();
   }
 }
コード例 #4
0
 private void handleConnect() {
   try {
     int program_version = ois.readInt();
     port = ois.readInt();
     address = listenSocket.getInetAddress();
     netCode = ois.readInt();
     Connection c = checkConnectionNetCode();
     if (c != null) {
       oos.writeInt(ALREADY_CONNECTED);
       oos.flush();
       System.out.println("->ALREADY_CONNECTED"); // $NON-NLS-1$
     } else {
       if (isAcceptingConnections()) {
         if (connections.size() < Utils.getMAX_CONNECTIONS()) {
           if (Utils.VERSION == program_version) {
             Connection newConnection = newConnection();
             if (newConnection != null) {
               oos.writeInt(CONNECTED);
               newConnection.setState(Connection.STATE_CONNECTED);
               System.out.println("->CONNECTED"); // $NON-NLS-1$
             } else {
               oos.writeInt(ALREADY_CONNECTED);
               System.out.println("->ALREADY_CONNECTED"); // $NON-NLS-1$
             }
             oos.flush();
           } else {
             oos.writeInt(INCOMPATIBLE_PROGRAM_VERSION);
             oos.flush();
             System.out.println("->INCOMPATIBLE_PROGRAM_VERSION"); // $NON-NLS-1$
           }
         } else {
           oos.writeInt(TOO_MANY_CONNECTIONS);
           oos.flush();
           System.out.println("->TOO_MANY_CONNECTIONS"); // $NON-NLS-1$
         }
       } else {
         oos.writeInt(NOT_ACCEPTING_CONNECTIONS);
         oos.flush();
         System.out.println("->NOT_ACCEPTING_CONNECTIONS"); // $NON-NLS-1$
       }
     }
   } catch (IOException ex) {
     System.out.println(ex.getMessage());
   }
 }
コード例 #5
0
  /**
   * The starting point. Checks arguments, read preferences from disk, and starts the program.
   *
   * @param args Only one optional argument at this time: the random number generator seed.
   */
  public static void main(String[] args) {
    if (args.length > 1) {
      System.err.println("java -jar biogenesis.jar [random seed]");
    } else if (args.length == 1) {
      try {
        long seed = Long.parseLong(args[0]);
        Utils.random.setSeed(seed);
      } catch (NumberFormatException e) {
        System.err.println("java -jar biogenesis.jar [random seed]");
      }
    }
    Utils.readPreferences();
    SwingUtilities.invokeLater(
        new Runnable() {
          @Override
          public void run() {

            new STBiogenesis().start();
          }
        });
  }