Commit 47d5056a authored by adwisatya's avatar adwisatya
Browse files

Handling ServerStatus belum

parent ce7aca72
......@@ -88,7 +88,8 @@ public class Client extends Thread{
System.out.println(database.getServers().toString());
break;
case "serverStatus":
cmdServerStatus(cmd);
System.out.println(JSONtoString("server",cmd));
cmdServerStatus(JSONtoString("server",cmd));
break;
/* 1 */
case "join" :
......@@ -155,7 +156,20 @@ public class Client extends Thread{
return jsonObj.getString(param);
}
public void cmdServerStatus(String request){
System.out.println(request);
try {
//servers.clear();
JSONArray objArr = new JSONArray(request);
System.out.println("objArr"+objArr.toString());
for(int i = 0; i<objArr.length();i++){
objArr.getJSONObject(i).get("ip");
objArr.getJSONObject(i).get("port");
servers.add(objArr.toString());
}
} catch (JSONException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("request");
}
public void cmdJoin() throws IOException, JSONException{
......@@ -175,16 +189,6 @@ public class Client extends Thread{
BufferedReader in = new BufferedReader(new InputStreamReader(miniSocket.getInputStream()));
response = org.apache.commons.io.IOUtils.toString(in);
in.close();
JSONObject objResponse = new JSONObject(response);
if(isJSONValid(objResponse.toString())){
JSONArray objArr = new JSONArray(obj.getJSONArray("value"));
for(int i = 0; i<objArr.length();i++){
objArr.getJSONObject(i).get("ip");
objArr.getJSONObject(i).get("port");
tmpServer.add(objArr.toString());
}
}
}
......
......@@ -12,6 +12,8 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import static thealchemist.Client.defPort;
......@@ -27,6 +29,7 @@ public class TCPServer {
ServerSocket mainSocket;
mainSocket = new ServerSocket(portNumber);
System.out.println("Start listening...");
cmdJoin();
while(true){
Socket connectionSocket = mainSocket.accept();
if(connectionSocket != null){
......@@ -36,5 +39,35 @@ public class TCPServer {
}
}
}
public static void cmdJoin() throws IOException, JSONException{
String response = "";
Socket miniSocket = new Socket("167.205.32.46",defPort);
ArrayList<String> tmpServer = new ArrayList<String>();
JSONObject obj = new JSONObject();
obj.put("method", "join");
obj.put("ip", "167.205.34.195");
obj.put("port", new Integer(myPort));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(miniSocket.getOutputStream())));
out.println(obj);
out.println();
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(miniSocket.getInputStream()));
response = org.apache.commons.io.IOUtils.toString(in);
in.close();
System.out.println(response);
//
// JSONObject objResponse = new JSONObject(response);
// if(isJSONValid(objResponse.toString())){
// JSONArray objArr = new JSONArray(obj.getJSONArray("value"));
// for(int i = 0; i<objArr.length();i++){
// objArr.getJSONObject(i).get("ip");
// objArr.getJSONObject(i).get("port");
// tmpServer.add(objArr.toString());
// }
// }
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment