Skip to content

Commit

Permalink
fixed communication protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
duguying committed Feb 1, 2015
1 parent 5cb6037 commit a66c93b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
45 changes: 29 additions & 16 deletions src/net/duguying/goj/JudgerTCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
public class JudgerTCP {
private Socket conn;
private String host;
private String judger_os;
private int port;
private char mark = '#';

public JudgerTCP(String host, int port, String password) throws IOException {
this.host = host;
Expand All @@ -24,8 +26,15 @@ public JudgerTCP(String host, int port, String password) throws IOException {
this.conn = new Socket(this.host, this.port);

// read out the response message from server
String msg = this.read();
System.out.println(msg);
String header = this.Read();
System.out.println(header);
int idx = header.indexOf(this.mark);
if (idx > 0){
idx = idx - 1;
}else{
idx = 0;
}
this.mark = header.charAt(idx);

// prepare login data
Map<String,Object> map = new HashMap<String,Object>();
Expand All @@ -35,8 +44,7 @@ public JudgerTCP(String host, int port, String password) throws IOException {
// send login
try {
Map resp = this.Request(map);
String os = resp.get("os").toString();
System.out.println("os: "+os);
this.judger_os = resp.get("os").toString();
} catch (JSONException e) {
e.printStackTrace();
System.out.println("send request error!");
Expand All @@ -51,29 +59,34 @@ public JudgerTCP(String host, int port, String password) throws IOException {
* @throws JSONException json parse exception
*/
public Map Request(Map<String,Object> requst) throws IOException, JSONException {
String content = this.msgPack(requst);
String content = this.MsgPack(requst);
DataOutputStream dOut = new DataOutputStream(this.conn.getOutputStream());
dOut.write(content.getBytes());
dOut.flush();
String resp = this.read();
String resp = this.Read();

return toMap(new JSONObject(resp.toString()));
}

// reading from server
private String read() throws IOException {
private String Read() throws IOException {
Reader reader = new InputStreamReader(this.conn.getInputStream());
char chars[] = new char[10240];
char chars[] = new char[10];
int len;
StringBuilder sb = new StringBuilder();
String temp;
len=reader.read(chars);
temp = new String(chars, 0, len);
int index = temp.indexOf("eof");
if (index != -1) {//遇到eof时就结束接收
return "";

while (true) {
len = reader.read(chars);
temp = new String(chars, 0, len);
sb.append(temp);

int index = temp.indexOf(this.mark);
if (index != -1) {//遇到mark时就结束接收
break;
}

}
sb.append(temp);

return sb.toString();
}
Expand Down Expand Up @@ -117,9 +130,9 @@ else if(value instanceof JSONObject) {
}

// pack message
private String msgPack(Map<String,Object> map){
private String MsgPack(Map<String,Object> map){
JSONObject json = new JSONObject(map);
return json.toString()+"\003";
return json.toString() + this.mark;
}

protected void finalize(){
Expand Down
6 changes: 1 addition & 5 deletions test/net/duguying/goj/JudgerHTTPTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
*/
public class JudgerHTTPTest extends TestCase {
protected void setUp() {
System.out.println("setUp");
}

public void testJudgerHTTP() {
JudgerHTTP j = new JudgerHTTP();
j = null;
System.out.println("HTTP");
System.gc();

}
}

0 comments on commit a66c93b

Please sign in to comment.