/** * 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(); } } }
/** * 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; }