/**
   * openConnections: open the connection to the master. has retry mechanism
   *
   * @return: true if the connection is built, false if failed
   */
  public boolean openConnections() {

    _log("Try to connect to Master...");

    int i = 0;
    while (true) {

      boolean isConnected = connect();

      if (!isConnected) {
        i++;
        if (i == DropboxConstants.MAX_TRY) {
          break;
        }
        _log("Cannot connect to Master, retry " + i);
        try {
          Thread.sleep(DropboxConstants.TRY_CONNECT_MILLIS);
        } catch (InterruptedException e) {
          if (!_server.noException()) {
            _elog(e.toString());
          }
          if (_server.debugMode()) {
            e.printStackTrace();
          }
          _log("Retry connection is interrupted");
          break;
        }
      } else {
        _log("Success!");
        return true;
      }
    }
    _log("Failed");
    return false;
  }
  /** listen: start to listen if there is any clients connected */
  public void listen() {

    // Firstly spawn a new thread and then the main thread
    // also start listening
    _userNet = new DropboxFileServerUserNet(_server, _userOut, _userIn, _userSock);
    _userNet.start();

    /* Use main thread, cannot be stopped */
    while (_sock != null && !_sock.isClosed()) {
      try {
        String line = NetComm.receive(_in);
        _dlog(line);
        parse(line);
      } catch (Exception e) {
        if (!_server.noException()) {
          _elog(e.toString());
        }
        break;
        // Break the loop
      }
    }

    /* Clear */
    // Close main thread
    clear();
    // Cancel the listening thread and retry
    _server.cancelListeningThread();
    /* Also cancel all of the syncers */
    _server.cancelSyncers();
    // Retry after
    try {

      _log(
          "The connection to master is broken,"
              + " reset everything and retry connection after "
              + DropboxConstants.TRY_CONNECT_MILLIS
              + " milliseconds");

      Thread.sleep(DropboxConstants.TRY_CONNECT_MILLIS);

    } catch (InterruptedException e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
    }
    _server.run();

    clear();
    _log(_threadName + " is stopped");
  }
 /**
  * parse: parse the string and send response
  *
  * @param str: the given string
  */
 protected void parse(String str) {
   StringTokenizer st = new StringTokenizer(str);
   String tkn = st.nextToken();
   try {
     if (tkn.equals(ProtocolConstants.PACK_STR_HEARTBEAT_HEAD)) {
       _dlog("I got heartbeat");
       // Send back confirmation
       // TODO: add more story here
       NetComm.send(ProtocolConstants.PACK_STR_HEARTBEAT_HEAD, _out);
     } else if (tkn.equals(ProtocolConstants.PACK_STR_REQUEST_FS_HEAD)) {
       // TODO: add more story here
     } else {
       _elog("Invalid header, skip.");
     }
   } catch (Exception e) {
     if (!_server.noException()) {
       _elog(e.toString());
     }
     if (_server.debugMode()) {
       e.printStackTrace();
     }
   }
 }
  /**
   * 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 */
  }
  private void _dlog(String str) {

    if (_server.debugMode()) System.out.println("[DropboxFileServerMasterNet (DEBUG)]:" + str);
  }
  /**
   * 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;
  }