예제 #1
0
  /*
   * method to remove this connection from everything called when the user
   * leaves the server
   */
  private void removeAllConnections() {
    System.out.println("Client: " + username + " - " + "Removing from all connected rooms");

    // removes the user from all connected chatrooms
    for (ChatRoom c : connectedRooms.values()) c.removeUser(this);

    // removes the user from the server
    System.out.println("Client: " + username + " - " + "Removing from server listing");
    users.remove(this);
    return;
  }
예제 #2
0
  /*
   * parses the input string and performs the appropriate action such as
   * joining a room or saying a message
   *
   * @param String - the string to be parsed
   */
  private String parseInput(String input) {
    // sets up regex
    String regex =
        "(((disconnect)|(make)|(join)|(exit)) "
            + "\\p{Graph}+)|"
            + "(message \\p{Graph}+ \\p{Print}+)";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(input);

    // if there is no match for the input string
    if (!m.matches()) return "Unrecognized Command " + input; // Should not occur assuming
    // client input is correct

    // find the first space in the string
    int spaceIndex = input.indexOf(' ');
    String command = input.substring(0, spaceIndex);

    // if the string contains "disconnect", set alive to false and kill
    // connection
    if (command.trim().equals("disconnect")) {
      // removeAllConnections();
      this.alive = false;
      return "disconnectedServerSent";
    }
    // if the command is to make, join or exit (it is a room command)
    else if (command.equals("make") || command.equals("join") || command.equals("exit")) {

      // find the next word in string and parse it as the room name
      String roomName = input.substring(spaceIndex + 1);

      // if making a new room
      if (command.equals("make"))
        try {
          // make a new room
          ChatRoom newChatRoom = new ChatRoom(roomName, rooms, this);
          // Constructor above automatically adds the ChatRoom to the
          // list of chat rooms of the server
          connectedRooms.put(newChatRoom.name, newChatRoom);
          informConnectedRooms();
          return "";
        } catch (IOException e) {
          // if we cant make a room there will be an error message
          return "invalidRoom " + roomName + " " + e.getMessage();
        }

      // if joining a new room
      else if (command.equals("join")) {
        // if there exists the room
        if (rooms.contains(roomName))
          try {
            // try to join the room - what could happen is the room
            // could dissapear at this stage, expect an IOException
            ChatRoom roomToJoin = rooms.getRoomFromName(roomName);
            roomToJoin.addUser(this);
            this.connectedRooms.put(roomToJoin.name, roomToJoin);
            return "";
            // if something bad happened when joining a room
          } catch (IOException e) {
            return "invalidRoom " + roomName + " " + e.getMessage();
          }
        else return "invalidRoom " + roomName + " Room name does not exist";

        // stuff for exiting a room
      } else if (command.equals("exit")) {
        // remove the room from personal listings
        ChatRoom roomToExit = connectedRooms.remove(roomName);
        if (roomToExit != null) {
          // remove the user from the room
          roomToExit.removeUser(this);
          return "disconnectedRoom " + roomName;
        }
        return "invalidRoom " + roomName + " user not connected to room";
      }

      // stuff for messaging a specific room
    } else if (command.equals("message")) {
      // splice out the target chatroom and message
      int secondSpaceIndex = input.indexOf(' ', spaceIndex + 1);
      String chatroom = input.substring(spaceIndex + 1, secondSpaceIndex);
      String message = input.substring(secondSpaceIndex + 1);

      // update the queue of the chatroom
      ChatRoom roomToMessage = connectedRooms.get(chatroom);
      if (roomToMessage != null) {
        roomToMessage.updateQueue(username + " " + message);
        return "";
      }
      return "";
    }

    return "Unrecongnized Command " + input;
  }