示例#1
0
 // Methods for the treatment of the different message types
 private void loginMessageTreatment() throws IncorrectMessageException {
   String content = message.getText().trim();
   if (content != null && !content.contains("&")) {
     if (this.userList.getUserByNick(content) != null) {
       // The nick already exist (send 301)
       String response = new Integer(Message.ERROR_MESSAGE_EXISTING_NICK).toString();
       sendDatagram(
           new User(
               null,
               messageToProcces.getAddress().getHostAddress(),
               this.messageToProcces.getPort()),
           response);
       // Comprobamos el nick pero, ¿Y si ya hay un usuario en esa IP???
     } else {
       // Everything correct, send ACK
       User newUser =
           new User(
               content,
               this.messageToProcces.getAddress().getHostAddress(),
               this.messageToProcces.getPort());
       userList.add(newUser);
       String response =
           new Integer(Message.SERVER_MESSAGE_CONNECTED).toString() + this.userList.toString();
       sendDatagram(newUser, response);
     }
   } else {
     throw new IncorrectMessageException(
         "The Login message is not correct: '" + content.toString() + "'");
   }
 }
示例#2
0
 private void crossMessagesTreatment() {
   String response = message.getSimpleResponse();
   if (response != null) {
     this.sendDatagram(this.message.getTo(), response);
   } else {
     // User disconnected
     response =
         new Integer(Message.ERROR_MESSAGE_USER_IS_DISCONNECTED).toString()
             + this.userList.toString();
     this.sendDatagram(this.message.getFrom(), response);
   }
 }
示例#3
0
 /**
  * Dispatcher for the different message types
  *
  * @throws IncorrectMessageException
  */
 private void treatMessage() throws IncorrectMessageException {
   if (message.isCrossMessage()) {
     // todos los cross messages
     crossMessagesTreatment();
   } else {
     switch (message.getMessageType()) {
       case Message.CLIENT_MESSAGE_LOGIN:
         // mensaje de login
         this.loginMessageTreatment();
         break;
       case Message.CLIENT_MESSAGE_CLOSE_CONNECTION:
         // cerrar conexion de un usuario
         this.closeConnectionTreatment();
         break;
       case Message.CLIENT_MESSAGE_GET_USERS:
         // devolver lista
         this.getConnectedUsersTreatment();
         break;
       default:
         throw new IncorrectMessageException("The message type code does not exist");
     }
   }
 }
示例#4
0
  /**
   * generates a message from the data of the datagram
   *
   * @throws IncorrectMessageException If the message cannot be correctly created.
   */
  private void generateMessage() throws IncorrectMessageException {

    // obtain all the params from the datagram data
    String ip = messageToProcces.getAddress().getHostAddress();
    int port = messageToProcces.getPort();
    User userFrom = userList.getUserByIpAndPort(ip, port);
    User userTo = null;
    String content = new String(messageToProcces.getData());
    content = content.trim();
    int id = Integer.parseInt(content.substring(0, 3));
    if (content.length() > 4) {
      // also eliminate the first &
      content = content.substring(4);
    } else {
      content = null;
    }

    // obtain the destination user if the message should have it
    if (Message.hasDestination(id)) {
      try {
        String[] splited = content.split("&");
        String nickTo = splited[0];
        userTo = userList.getUserByNick(nickTo);
        if (splited.length > 1) {
          content = content.substring(nickTo.length() + 1);
        }
      } catch (RuntimeException e) {
        throw new IncorrectMessageException("Incorrect message. No destination user found");
      }
    }

    // pass all the params to the message object
    message.setFrom(userFrom);
    message.setTo(userTo);
    message.setText(content);
    message.setMessageType(id);
  }
示例#5
0
 private void getConnectedUsersTreatment() {
   String listOfUsers = this.userList.toString();
   sendDatagram(message.getFrom(), "207" + listOfUsers);
 }