Commit ca3cabd6 authored by Kevin's avatar Kevin
Browse files

nyampah

parent 26ebaaa8
......@@ -4,7 +4,6 @@
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/ConnectionHandler.java</file>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/crudDB/crudDB.java</file>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/Client.java</file>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/TCPServer.java</file>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/EndpointMethod.java</file>
......
......@@ -10,33 +10,101 @@
*/
import java.io.*;
import java.net.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
class TCPServer {
public ServerSocket welcomeSocket;
public Vector<Client> clients;
public boolean active;
public int globalID;
public Socket conSocket;
public String trackerSentence;
public TCPServer() {
active = true;
globalID = 0;
try {
welcomeSocket = new ServerSocket(2000);
} catch (IOException ex) {
Logger.getLogger(TCPServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String serverStatus() throws IOException {
JSONObject resultJson = new JSONObject();
JSONParser parser = new JSONParser();
DataOutputStream outToTracker = null;
BufferedReader inFromTracker = null;
System.out.println("Nunggu koneksi");
conSocket = welcomeSocket.accept();
System.out.println("Koneksi sudah ada");
inFromTracker = new BufferedReader(new InputStreamReader(conSocket.getInputStream()));
outToTracker = new DataOutputStream(conSocket.getOutputStream());
while (active) {
try {
trackerSentence = inFromTracker.readLine();
System.out.println("trackerSentence telah terbaca");
System.out.println("Received: " + trackerSentence);
if (trackerSentence != null) {
System.out.println("TrackerSentence: " + trackerSentence);
Object obj = parser.parse(trackerSentence);
if (obj != null) {
JSONObject inJson = (JSONObject) obj;
if (inJson.get("method").toString().equalsIgnoreCase("serverStatus")) {
JSONArray server_list_active = new JSONArray();
server_list_active = (JSONArray) inJson.get("server");
System.out.println(server_list_active);
resultJson.put("status", "ok");
outToTracker.write(resultJson.toString().getBytes());
return resultJson.toString();
}
}
} else {
active = false;
}
} catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(TCPServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
resultJson.put("status", "error");
return resultJson.toString();
}
public void setActive(boolean _active) {
active = _active;
}
public static void join() throws IOException {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket newServer = new Socket("167.205.32.46", 8000);
DataOutputStream outToServer = new DataOutputStream(newServer.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(newServer.getInputStream()));
DataOutputStream outToTracker = new DataOutputStream(newServer.getOutputStream());
BufferedReader inFromTracker = new BufferedReader(new InputStreamReader(newServer.getInputStream()));
sentence = inFromUser.readLine();
outToServer.write(sentence.getBytes());
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
outToTracker.write(sentence.getBytes());
modifiedSentence = inFromTracker.readLine();
System.out.println("FROM TRACKER: " + modifiedSentence);
newServer.close();
}
// public static String serverStatus(Object inObj) {
// JSONObject resultJson=new JSONObject();
// JSONObject resultJson = new JSONObject();
// if (inObj != null) {
// JSONObject inJson = (JSONObject) inObj;
// if (inJson.containsKey("method")) {
// if (inJson.get("method").toString().equalsIgnoreCase("serverStatus")) {
// JSONArray server_list_active=new JSONArray();
// server_list_active= (JSONArray) inJson.get("server");
// JSONArray server_list_active = new JSONArray();
// server_list_active = (JSONArray) inJson.get("server");
// System.out.println(server_list_active);
// resultJson.put("status", "ok");
// }
......@@ -45,11 +113,13 @@ class TCPServer {
// resultJson.put("status", "error");
// return resultJson.toString();
// }
public static void main(String argv[]) throws Exception {
// ConnectionHandler CH = new ConnectionHandler();
// CH.start();
String hasil;
join();
TCPServer tes = new TCPServer();
hasil = tes.serverStatus();
System.out.println(hasil);
}
}
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