Ejemplo n.º 1
0
  /**
   * This method waits for input from the console. Once it is received, it sends it to the client's
   * message handler.
   */
  public void accept() {
    try {
      BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
      String message;

      while (true) {
        message = fromConsole.readLine();
        if (client == null) {
          client = new ChatClient(this);
        }
        client.handleMessageFromClientUI(message);
      }
    } catch (Exception ex) {
      System.out.println("Unexpected error while reading from console!");
    }
  }
Ejemplo n.º 2
0
  public void run() {

    try {
      msgOut = new PrintWriter(socket.getOutputStream(), true);
      msgIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {
      String input;
      String output;

      TextTransferProtocol ttp = new TextTransferProtocol(this); // set up ttp parsey thing
      String first = msgIn.readLine();

      if (first.startsWith(ChatServer.REQ)) {
        if (first.indexOf("/message") > -1) {
          isPing = true;
          msgOut.println(ChatServer.getMotd());
          // ChatServer.pushToChat("ping");
        } else if (first.indexOf("/login") > -1) {
          // ChatServer.pushToChat("login");
          if (name == null) {
            name = first.substring(8, first.length());
            // ChatServer.pushToChat("setting name to " + name);
            // msgOut.println("setting name to " + name);
            if (ChatServer.users.contains(name)) {
              ChatServer.pushToChat(
                  Colors.RED
                      + "User "
                      + name
                      + " already exists, changing to "
                      + name
                      + "_"
                      + ChatServer.users.size()
                      + 1);
              name = name + "_" + ChatServer.users.size();
            }
            ChatServer.addUser(name);
            ChatServer.restoreClientRank(name);
            ChatServer.updateUsers();
          }

          ChatServer.sendAll(Colors.YELLOW + name + " has joined the server");
          ChatServer.sendOne(clientID, ChatServer.getLoginMessage());
          while ((input = msgIn.readLine()) != null) { // continue doing that until ends]
            // ChatServer.pushToChat(input);
            output = ttp.processIn(input);
            if (output != null)
              ChatServer.sendAll(getRankColor() + name + Colors.WHITE + ": " + output);
          }
        }
      } else {
        msgOut.println("400: PLEASE START ANY CONNECTION WITH " + ChatServer.REQ + "/fn");
      }
      socket.close();
      // ChatServer.pushToChat("closed socket");

    } catch (Exception e) {
      e.printStackTrace(msgOut);
      if (!isPing) {
        leave();
        p.nl("Lost connection to user: "******"[WARNING]" + Colors.WHITE + " Lost connection to user: "******"got pinged by: " + socket);
        ChatServer.pushToChat(Colors.YELLOW + "[INFO]" + Colors.WHITE + " Pinged by: " + socket);
      }
      // ChatServer.updateUsers();
      // ChatServer.removeClient(ChatServer.getClientID(name)); //figure out what to do if the
      // client exits without doing /logout
    }
  }
  /**
   * close: close the socket and stream
   *
   * @return: true for success, false for error
   */
  protected boolean close() {
    _dlog("Do main thread closing...");
    if (_sock != null && !_sock.isClosed()) {
      _dlog("Closing the receiving thread");
      try {
        _sock.close();
        _in.close();
        _out.close();

        _sock = null;
        _in = null;
        _out = null;
        _dlog("Success!");
      } catch (IOException e) {
        if (!_server.noException()) {
          _elog(e.toString());
        }
        if (_server.debugMode()) {
          e.printStackTrace();
        }
        return false;
      }
    }
    _sock = null;
    _in = null;
    _out = null;

    _dlog("Cancel the user input thread");
    if (_userNet != null) {
      _userNet.stop();
      _userNet.join(); // guaranteed to be closed
      _userNet = null;
    }
    if (_userSock != null && !_userSock.isClosed()) {
      _dlog("Closing the user input thread");
      // Stop the user thread
      try {
        _userSock.close();
        _userIn.close();
        _userOut.close();
        _userSock = null;
        _userIn = null;
        _userOut = null;
        _dlog("Success!");
      } catch (IOException e) {
        if (!_server.noException()) {
          _elog(e.toString());
        }
        if (_server.debugMode()) {
          e.printStackTrace();
        }
        return false;
      }
    }
    _userSock = null;
    _userIn = null;
    _userOut = null;

    /* Cancel all client nodes */

    _dlog("Finished");
    return true;

    /* CAUTION: _server is never cleared */
  }