Commit 8f547042 authored by Kevin's avatar Kevin
Browse files

aneh dah

parent ca3cabd6
...@@ -21,7 +21,6 @@ import org.json.simple.parser.ParseException; ...@@ -21,7 +21,6 @@ import org.json.simple.parser.ParseException;
class TCPServer { class TCPServer {
public ServerSocket welcomeSocket; public ServerSocket welcomeSocket;
public Vector<Client> clients;
public boolean active; public boolean active;
public int globalID; public int globalID;
public Socket conSocket; public Socket conSocket;
...@@ -32,6 +31,7 @@ class TCPServer { ...@@ -32,6 +31,7 @@ class TCPServer {
globalID = 0; globalID = 0;
try { try {
welcomeSocket = new ServerSocket(2000); welcomeSocket = new ServerSocket(2000);
// welcomeSocket.setSoTimeout(100000);
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(TCPServer.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(TCPServer.class.getName()).log(Level.SEVERE, null, ex);
} }
...@@ -43,15 +43,20 @@ class TCPServer { ...@@ -43,15 +43,20 @@ class TCPServer {
DataOutputStream outToTracker = null; DataOutputStream outToTracker = null;
BufferedReader inFromTracker = null; BufferedReader inFromTracker = null;
System.out.println("Nunggu koneksi"); System.out.println("Nunggu koneksi");
System.out.println("Why???");
conSocket = welcomeSocket.accept(); conSocket = welcomeSocket.accept();
System.out.println("Koneksi sudah ada"); System.out.println("Koneksi sudah ada");
inFromTracker = new BufferedReader(new InputStreamReader(conSocket.getInputStream())); inFromTracker = new BufferedReader(new InputStreamReader(conSocket.getInputStream()));
outToTracker = new DataOutputStream(conSocket.getOutputStream()); outToTracker = new DataOutputStream(conSocket.getOutputStream());
int i = 0;
while (active) { while (active) {
try { try {
trackerSentence = inFromTracker.readLine(); while ((trackerSentence = inFromTracker.readLine()) == null) {
System.out.println(i);
inFromTracker = new BufferedReader(new InputStreamReader(conSocket.getInputStream()));
}
System.out.println("trackerSentence telah terbaca"); System.out.println("trackerSentence telah terbaca");
System.out.println("Received: " + trackerSentence); System.out.println("TrackerSentence: " + trackerSentence);
if (trackerSentence != null) { if (trackerSentence != null) {
System.out.println("TrackerSentence: " + trackerSentence); System.out.println("TrackerSentence: " + trackerSentence);
Object obj = parser.parse(trackerSentence); Object obj = parser.parse(trackerSentence);
...@@ -61,14 +66,15 @@ class TCPServer { ...@@ -61,14 +66,15 @@ class TCPServer {
JSONArray server_list_active = new JSONArray(); JSONArray server_list_active = new JSONArray();
server_list_active = (JSONArray) inJson.get("server"); server_list_active = (JSONArray) inJson.get("server");
System.out.println(server_list_active); System.out.println(server_list_active);
resultJson.put("status", "ok");
outToTracker.write(resultJson.toString().getBytes());
return resultJson.toString();
} }
} }
} else { } else if (trackerSentence == null) {
active = false; active = false;
System.out.println("TrackerSentence NULL");
} }
resultJson.put("status", "ok");
outToTracker.write(resultJson.toString().getBytes());
return resultJson.toString();
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) { } catch (ParseException ex) {
...@@ -97,29 +103,29 @@ class TCPServer { ...@@ -97,29 +103,29 @@ class TCPServer {
newServer.close(); newServer.close();
} }
// public static String serverStatus(Object inObj) { public static void message() throws IOException {
// JSONObject resultJson = new JSONObject(); String sentence;
// if (inObj != null) { String modifiedSentence;
// JSONObject inJson = (JSONObject) inObj; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
// if (inJson.containsKey("method")) { Socket newServer = new Socket("167.205.32.46", 8025);
// if (inJson.get("method").toString().equalsIgnoreCase("serverStatus")) { DataOutputStream outToTracker = new DataOutputStream(newServer.getOutputStream());
// JSONArray server_list_active = new JSONArray(); BufferedReader inFromTracker = new BufferedReader(new InputStreamReader(newServer.getInputStream()));
// server_list_active = (JSONArray) inJson.get("server"); sentence = inFromUser.readLine();
// System.out.println(server_list_active); modifiedSentence = inFromTracker.readLine();
// resultJson.put("status", "ok"); System.out.println("FROM TRACKER: " + modifiedSentence);
// } outToTracker.write(sentence.getBytes());
// } newServer.close();
// } }
// resultJson.put("status", "error");
// return resultJson.toString();
// }
public static void main(String argv[]) throws Exception { public static void main(String argv[]) throws Exception {
// ConnectionHandler CH = new ConnectionHandler(); // ConnectionHandler CH = new ConnectionHandler();
// CH.start(); // CH.start();
String hasil; String hasil;
join(); join();
TCPServer tes = new TCPServer(); TCPServer tes = new TCPServer();
hasil = tes.serverStatus(); hasil = tes.serverStatus();
System.out.println(hasil); System.out.println(hasil);
// message();
} }
} }
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