예제 #1
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
    }
  }
  /**
   * connect: connect to the master
   *
   * @return: true for success and false for failure
   */
  protected boolean connect() {

    boolean connected = true;
    try {
      _sock = new Socket(_serverIP, _serverPort);
      _out = new PrintWriter(_sock.getOutputStream(), true);
      _in = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
      String reply = sendAll();
      if (!reply.equals(ProtocolConstants.PACK_STR_CONFIRM_HEAD)) {
        /* Tokenize */
        StringTokenizer st = new StringTokenizer(reply);
        if (st.nextToken().equals(ProtocolConstants.PACK_STR_ERRMES_HEAD)) {
          _elog(st.nextToken());
          return false;
        }
      }

      _userSock = new Socket(_serverIP, _serverPort);
      _userOut = new PrintWriter(_userSock.getOutputStream(), true);
      _userIn = new BufferedReader(new InputStreamReader(_userSock.getInputStream()));
      String replyUser = sendUserID();
      if (!replyUser.equals(ProtocolConstants.PACK_STR_CONFIRM_HEAD)) {
        throw new Exception("Not confirmed");
      }
    } catch (UnknownHostException e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
      connected = false;
    } catch (IOException e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
      connected = false;
    } catch (Exception e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
      connected = false;
    }
    if (!connected) {
      return connected;
    }

    if (!connected) {
      /* Remove the pair of socket */
      _sock = null;
      _in = null;
      _out = null;

      _userSock = null;
      _userOut = null;
      _userIn = null;
    }
    return connected;
  }