@Override
  public void setArrivalTerminalExit() {
    ClientCom con = new ClientCom(genRepInfo.getHostName(), genRepInfo.getPortNumber());
    Message inMessage, outMessage;

    while (!con.open()) {
      try {
        Thread.sleep((long) (10));
      } catch (InterruptedException e) {
      }
    }

    outMessage =
        new Message(
            Message.INT_INT_STR,
            Message.SET_ARRIVAL_TERMINAL_EXIT,
            arrTermExitInfo.getPortNumber(),
            arrTermExitInfo.getHostName());
    con.writeObject(outMessage);
    inMessage = (Message) con.readObject();
    con.close();

    if (inMessage.getType() != Message.ACK) {
      System.out.println("Invalid message type!");
      System.exit(1);
    }
  }
  /** @param busQueue */
  @Override
  public void updateDriverQueue(int[] busQueue) {

    ClientCom con = new ClientCom(genRepInfo.getHostName(), genRepInfo.getPortNumber());
    Message inMessage, outMessage;

    while (!con.open()) {
      try {
        Thread.sleep((long) (10));
      } catch (InterruptedException e) {
      }
    }

    outMessage = new Message(Message.INT_INTARR, Message.UPDATE_DRIVER_QUEUE, busQueue);
    con.writeObject(outMessage);
    inMessage = (Message) con.readObject();
    con.close();

    if (inMessage.getType() != Message.ACK) {
      System.out.println("Invalid message type!");
      System.exit(1);
    }
  }
예제 #3
0
  @Override
  public void action() {
    // We have to use a timeout to allow the WakerBehaviour to take place
    final ACLMessage msg = this.myAgent.blockingReceive(100);

    if (msg == null) {
      return;
    }

    final AID sender = msg.getSender();
    Message message = null;

    try {
      message = (Message) msg.getContentObject();
    } catch (final UnreadableException e) {
      System.err.println("[" + this.myAgent.getAID() + "] Error receiving message from " + sender);
      e.printStackTrace(System.err);
      return;
    }

    System.err.println(
        "INFO: [" + this.myAgent.getAID() + "] ReceiveMessage (" + message.getType() + ")");

    switch (message.getType()) {
      case FIRST_ASSIGNATION_REQUEST:
        // TODO: This should maybe be an INFORM message, with reply
        this.teacher.sendMessage(
            sender, new FirstAssignationMessage(this.firstAssignation(sender)));

        this.alumnCount += 1;

        assert this.alumnCount <= TeacherBehaviour.EXPECTED_ALUMN_COUNT;

        if (TeacherBehaviour.EXPECTED_ALUMN_COUNT == this.alumnCount) {
          this.teacher.sendMessageToType("alumn", new InitMessage());
        }

        return;
      case TEACHER_GROUP_CHANGE_REQUEST:
        final TeacherGroupChangeRequestMessage requestMessage =
            (TeacherGroupChangeRequestMessage) message;

        assert requestMessage.fromAlumn.equals(sender);

        // Ensure those alumns haven't been changed previously
        if (this.groups.get(requestMessage.fromAlumn) == requestMessage.fromGroup
            && this.groups.get(requestMessage.toAlumn) == requestMessage.toGroup) {
          this.groups.put(requestMessage.fromAlumn, requestMessage.toGroup);
          this.groups.put(requestMessage.toAlumn, requestMessage.fromGroup);
          this.teacher.sendMessageToType(
              "alumn",
              new TeacherGroupChangeMessage(
                  requestMessage.fromAlumn, requestMessage.toAlumn,
                  requestMessage.fromGroup, requestMessage.toGroup));
        } else {
          this.teacher.sendMessage(
              requestMessage.fromAlumn, new TeacherGroupChangeRequestDenegationMessage());
        }
        return;
      default:
        System.err.println(
            "ERROR: Unexpected message of type "
                + message.getType()
                + " received in TeacherBehaviour. Sender: "
                + sender
                + "; Receiver: "
                + this.teacher.getAID());
        return;
    }
  }
예제 #4
0
 // this function takes the current input and returns the next state the client should be in
 public State process(
     String input,
     TCP tcp,
     UDPSender us,
     Message udpMessage,
     Message tcpMessage,
     long timeEnteredState,
     boolean firstCall) {
   if (firstCall) {
     // if its the first time in this state print messages
     System.out.println(
         "You are disconnected. To attempt to automatically find a chatter locally, type :local <username>");
     System.out.println(
         "To talk to someone in particular use the :ip <ip address> or the :host <host name> commands");
   }
   // if there is a connection go to the appropriate connected state
   if (tcp.getActive() == true) {
     if (tcp.initiator) {
       return new ConnectedInitiator();
     } else {
       return new ConnectedReceiver();
     }
   }
   // if you give an ip command
   // CLIENT
   else if (input.startsWith(":ip ")) {
     // if there was no argument
     if (input.length() < 5) {
       System.out.println("An argument is required");
       return this;
     }
     // if the connection failed
     else if (0 > tcp.connect(input.substring(4))) {
       System.out.println("Unable to connect to IP address");
     }
     return this;
   }
   // if the host command was given do the same as ip but with a longer offset
   // CLIENT
   else if (input.startsWith(":host ")) {
     if (input.length() < 7) {
       System.out.println("An argument is required");
       return this;
     }
     if (0 > tcp.connect(input.substring(6))) {
       System.out.println("Unable to connect to IP address");
     }
     return this;
   }
   // if the local command is given
   // EC Extra Credit
   else if (input.startsWith(":local ")) {
     // check to make sure it has the proper arugments
     if (input.length() < 8) {
       System.out.println("An argument is required");
       return this;
     }
     try {
       // gather the data
       String senderUsername = User.getUserName();
       String targetUsername = input.substring(7);
       String ip = tcp.getIP();
       // send a message requesting a callback from the username given as an argument
       UDPBroadcastMessage message =
           new UDPBroadcastMessage(
               1, (long) 144, (long) 0, "", senderUsername, targetUsername, ip);
       us.sendMessage(message);
     } catch (IOException e) {
     }
     return new Waiting();
   }
   // if its any other command
   else if (input.startsWith(":")) {
     System.out.println("Invalid command");
     return this;
   }
   // if they typing is not a command
   else if (!input.equals("")) {
     System.out.println("You cannot chat in this state");
     return this;
   }
   // if you recieve a UDP broadcast
   else if (udpMessage instanceof UDPBroadcastMessage && udpMessage.getCorrect()) {
     UDPBroadcastMessage m = (UDPBroadcastMessage) udpMessage;
     // if the requested user is you
     if (m.targetUsername.trim().equals(User.userName.trim())) {
       // print a message and go to user confirm callback
       System.out.println(
           "Received a broadcast with to your username from "
               + m.senderUsername
               + " at "
               + m.senderIP);
       // store the senders ip for use later
       tcp.pendingIP = m.senderIP;
       return new UserConfirmCallback();
     }
     return this;
   } else {
     return this;
   }
 }