Commit 4f990875 authored by Stephen's avatar Stephen
Browse files

Added join, serverStatus

parent a3eebe93
compile.on.save=true
<<<<<<< HEAD
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
=======
<<<<<<< HEAD
=======
>>>>>>> 2e2ad538abfe623e38cc24be02c48f5ad2ee0768
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
>>>>>>> 43e41c8c08f84039568ca701a584f38f9911e548
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
\ No newline at end of file
......@@ -3,7 +3,8 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/E:/semester%206/Sistem%20Paralel%20dan%20Terdistribusi/SISTER/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/MainPage.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/client/src/tcpclient/TCPClient.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/client/src/tcpclient/MainPage.java</file>
</group>
</open-files>
</project-private>
......@@ -368,7 +368,6 @@ public class TCPClient {
return responseJSON;
}
<<<<<<< HEAD
public static void main(String argv[]) throws Exception
{
TCPClient client = new TCPClient("localhost", 6789);
......@@ -473,135 +472,6 @@ public class TCPClient {
// System.out.println();
// } while(true);
// } while(!command.equalsIgnoreCase("exit"));
JSONObject response = client.login("ayam", "ayam");
if(response.getString("status").equals("ok")) {
client.token = response.getString("token");
client.x=response.getInt("x");
client.y=response.getInt("y");
}
System.out.println(response.toString());
response = client.map(client.token);
System.out.println(response.toString());
response = client.field(client.token);
System.out.println(response.toString());
response = client.offer(client.token, 0, 1, 7, 3);
System.out.println(response.toString());
response = client.canceloffer(client.token, "a");
System.out.println(response.toString());
}
=======
// public static void main(String argv[]) throws Exception
// {
// TCPClient client = new TCPClient("localhost", 6789);
//
//// do {
//// BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
//// DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
//// BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
////
//// // Test Register
//// do {
//// System.out.println("Register");
//// System.out.print("Username : "); username = inFromUser.readLine();
//// System.out.print("Password : "); password = inFromUser.readLine();
//// String toSend = registerJSON(username, password).toString();
////
//// outToServer.writeBytes(toSend + '\n');
////
//// String response = inFromServer.readLine();
//// JSONObject responseJSON = new JSONObject(response);
//// responseStatus = responseJSON.getString("status");
//// if(responseStatus.equalsIgnoreCase("ok")) {
//// System.out.println("Registration Success!");
//// } else if(responseStatus.equalsIgnoreCase("fail")) {
//// String reason = responseJSON.getString("description");
//// System.out.println("Registration fail, " + reason);
//// }
//// System.out.println();
//// } while(!responseStatus.equalsIgnoreCase("ok"));
////
//// // Test Login
//// do {
//// System.out.println("Login");
//// System.out.print("Username : "); username = inFromUser.readLine();
//// System.out.print("Password : "); password = inFromUser.readLine();
//// String toSend = loginJSON(username, password).toString();
////
//// outToServer.writeBytes(toSend + '\n');
////
//// String response = inFromServer.readLine();
//// JSONObject responseJSON = new JSONObject(response);
//// responseStatus = responseJSON.getString("status");
//// if(responseStatus.equalsIgnoreCase("ok")) {
//// System.out.println("Login Success!");
//// System.out.println("Token : " + responseJSON.getString("token"));
//// token = responseJSON.getString("token");
//// } else if(responseStatus.equalsIgnoreCase("fail")) {
//// String reason = responseJSON.getString("description");
//// System.out.println("Login fail, " + reason);
//// }
//// System.out.println();
//// } while(!responseStatus.equalsIgnoreCase("ok"));
////
//// // Test Inventory
//// do {
//// do {
//// System.out.print("Command : "); command = inFromUser.readLine();
//// } while(!command.equalsIgnoreCase("inventory") && !command.equalsIgnoreCase("exit"));
////
//// if(command.equalsIgnoreCase("inventory")) {
//// String toSend = inventoryJSON(token).toString();
////
//// outToServer.writeBytes(toSend + '\n');
////
//// String response = inFromServer.readLine();
//// JSONObject responseJSON = new JSONObject(response);
//// responseStatus = responseJSON.getString("status");
//// if(responseStatus.equalsIgnoreCase("ok")) {
//// System.out.println("Your Inventory : ");
//// List inventory = new ArrayList<Integer>();
//// JSONArray jsonarray = responseJSON.getJSONArray("inventory");
//// for(int i=0; i<jsonarray.length(); i++) {
//// inventory.add(jsonarray.getInt(i));
//// }
//// printItemList(inventory);
//// } else if(responseStatus.equalsIgnoreCase("error")) {
//// System.out.println("Error opening inventory");
//// }
//// System.out.println();
//// }
//// } while(!command.equalsIgnoreCase("exit"));
////
//// // Test Mix Item
//// do {
//// System.out.println("Mix Item");
//// System.out.print("Item 1 ID : "); int id1 = Integer.parseInt(inFromUser.readLine());
//// System.out.print("Item 2 ID : "); int id2 = Integer.parseInt(inFromUser.readLine());
//// String toSend = mixitemJSON(token, id1, id2).toString();
////
//// outToServer.writeBytes(toSend + '\n');
////
//// String response = inFromServer.readLine();
//// JSONObject responseJSON = new JSONObject(response);
//// responseStatus = responseJSON.getString("status");
//// if(responseStatus.equalsIgnoreCase("ok")) {
//// System.out.println("Mix item succeded, your new item id : " + responseJSON.getInt("item"));
//// } else if(responseStatus.equalsIgnoreCase("fail")) {
//// System.out.println("Failed to mix item, " + responseJSON.getString("description"));
//// } else if(responseStatus.equalsIgnoreCase("error")) {
//// System.out.println("Error mixing item");
//// }
//// System.out.println();
//// } while(true);
//// } while(!command.equalsIgnoreCase("exit"));
//
// JSONObject response = client.login("ayam", "ayam");
// if(response.getString("status").equals("ok")) {
......@@ -619,6 +489,8 @@ public class TCPClient {
//
// response = client.offer(client.token, 0, 1, 7, 3);
// System.out.println(response.toString());
// }
>>>>>>> 2e2ad538abfe623e38cc24be02c48f5ad2ee0768
//
// response = client.canceloffer(client.token, "a");
// System.out.println(response.toString());
}
}
compile.on.save=true
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
do.depend=false
do.jar=true
javac.debug=true
javadoc.preview=true
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
......@@ -3,7 +3,8 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/E:/semester%206/Sistem%20Paralel%20dan%20Terdistribusi/SISTER/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/TCPServer.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/server/src/tcpserver/TCPServer.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/server/src/tcpserver/MySQLAccess.java</file>
</group>
</open-files>
</project-private>
package tcpserver;
public class Host {
public String ipAddress;
public int port;
public Host(String ipAddress, int port) {
this.ipAddress = ipAddress;
this.port = port;
}
public void print() {
System.out.println("ip: " + ipAddress + ", port: " + port);
}
}
......@@ -3,63 +3,75 @@ package tcpserver;
import java.io.*;
import java.net.*;
import com.google.common.io.CharStreams;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jdk.nashorn.internal.parser.JSONParser;
import org.json.*;
import org.p2psockets.P2PServerSocket;
public class TCPServer {
public ServerSocket serverSocket;
public Socket p2pSocket;
public Socket connectionSocket;
public class TCPServer implements Runnable {
public ServerSocket serverClientSocket;
public ServerSocket serverTrackerSocket;
public Socket clientSocket;
public Socket trackerSocket;
public MySQLAccess sql;
public String request;
public String clientRequest;
public String trackerRequest;
public String token;
public int userID;
public int width;
public int height;
public static String[][] map;
public List<Host> servers;
public TCPServer(int port) throws IOException, JSONException {
System.out.println("Server running!");
serverSocket = new ServerSocket(port);
serverClientSocket = new ServerSocket(port);
servers = new ArrayList<Host>();
// Test join tracker
join(InetAddress.getLocalHost().getHostAddress(), 6789);
join(InetAddress.getLocalHost().getHostAddress(), 6790);
System.out.println("Waiting for connection...");
connectionSocket = serverSocket.accept();
sql = new MySQLAccess();
}
public void printAllServers() {
System.out.println("Server yang terhubung :");
for(Host server : servers) {
server.print();
}
}
public void join(String host, int port) throws JSONException, UnknownHostException, IOException {
public void join(String ip, int port) throws JSONException, UnknownHostException, IOException {
JSONObject joinRequest = new JSONObject();
joinRequest.put("method", "join");
joinRequest.put("ip", host);
joinRequest.put("ip", ip);
joinRequest.put("port", port);
boolean joined = false;
do {
p2pSocket = new Socket("167.205.32.46", 8000);
trackerSocket = new Socket("167.205.32.46", 8000);
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(p2pSocket.getOutputStream());
outToServer = new DataOutputStream(trackerSocket.getOutputStream());
outToServer.writeBytes(joinRequest.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(p2pSocket.getInputStream()));
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(trackerSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject joinResponse = new JSONObject(response);
if(joinResponse.getString("status").equalsIgnoreCase("ok")) {
System.out.println("Success joining tracker!");
JSONArray servers = joinResponse.getJSONArray("value");
for (int i=0; i<servers.length(); i++) {
JSONObject server = new JSONObject(servers.get(i).toString());
System.out.println("IP: " + server.getString("ip") + ", PORT: " + server.getInt("port"));
JSONArray serverarray = joinResponse.getJSONArray("value");
for (int i=0; i<serverarray.length(); i++) {
JSONObject server = new JSONObject(serverarray.get(i).toString());
Host host = new Host(server.getString("ip"), server.getInt("port"));
servers.add(host);
}
printAllServers();
joined=true;
}
} while(!joined);
......@@ -178,72 +190,123 @@ public class TCPServer {
}
}
public JSONObject serverstatus(JSONObject clientObject) throws JSONException {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "ok");
servers.clear();
JSONArray serverarray = clientObject.getJSONArray("server");
for (int i=0; i<serverarray.length(); i++) {
JSONObject server = new JSONObject(serverarray.get(i).toString());
Host host = new Host(server.getString("ip"), server.getInt("port"));
servers.add(host);
}
printAllServers();
return responseJSON;
}
public void run() {
while(true) {
int port = 6790;
try {
while(true) {
// Menunggu koneksi dari tracker
System.out.println("Waiting for tracker to connect..");
serverTrackerSocket = new ServerSocket(port);
trackerSocket = serverTrackerSocket.accept();
System.out.println("Tracker connected!");
// Membaca request dari client
BufferedReader inFromClient =new BufferedReader(new InputStreamReader(trackerSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(trackerSocket.getOutputStream());
trackerRequest = inFromClient.readLine();
// Cek apakah ping dari tracker
if(!trackerRequest.equalsIgnoreCase("")) {
JSONObject clientObject = new JSONObject(trackerRequest);
String method = clientObject.getString("method");
if(method.equalsIgnoreCase("serverStatus")) {
JSONObject responseJSON = serverstatus(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString());
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
int port = 6789;
TCPServer server = new TCPServer(port);
System.out.println("User connected to " + server.serverSocket.getLocalSocketAddress() + ", " + server.connectionSocket.getLocalSocketAddress());
// Starts a thread that listens to tracker
Thread trackerListener =new Thread(server);
trackerListener.start();
System.out.println("Waiting for client to connect..");
server.clientSocket = server.serverClientSocket.accept();
System.out.println("User connected to " + server.serverClientSocket.getLocalSocketAddress() + ", " + server.clientSocket.getLocalSocketAddress());
while(true)
{
// Membaca request dari client
BufferedReader inFromClient =new BufferedReader(new InputStreamReader(server.connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(server.connectionSocket.getOutputStream());
server.request = inFromClient.readLine();
BufferedReader inFromClient =new BufferedReader(new InputStreamReader(server.clientSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(server.clientSocket.getOutputStream());
server.clientRequest = inFromClient.readLine();
System.out.println("Received: " + server.request);
System.out.println("Received: " + server.clientRequest);
// Cek apakah ini ping dari server
if(server.request.equalsIgnoreCase("")) {
} else {
// Mementuk JSON Object dari request client
JSONObject clientObject = new JSONObject(server.request);
String method = clientObject.getString("method");
// Mementuk JSON Object dari request client
JSONObject clientObject = new JSONObject(server.clientRequest);
String method = clientObject.getString("method");
if(method.equalsIgnoreCase("signup")) { // Jika method request adalah "signup"
JSONObject responseJSON = server.signup(clientObject);
System.out.println(responseJSON.toString());
if(responseJSON.getString("status").equalsIgnoreCase("ok")) {
server.sql.createInventory(clientObject.getString("username"));
}
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("login")) { // Jika method request adalah "login"
JSONObject responseJSON = server.login(clientObject);
System.out.println(responseJSON.toString());
if(responseJSON.getString("status").equalsIgnoreCase("ok")) {
server.userID = server.sql.getUserId(clientObject.getString("username"));
server.token = responseJSON.getString("token");
System.out.println("ID : " + server.userID);
System.out.println("token : " + server.token);
}
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("inventory")) {
JSONObject responseJSON = server.inventory(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("mixitem")) {
JSONObject responseJSON = server.mixitem(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("map")) {
JSONObject responseJSON = server.map(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("move")) {
JSONObject responseJSON = server.move(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("field")) {
JSONObject responseJSON = server.field(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("offer")) {
JSONObject responseJSON = server.offer(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
if(method.equalsIgnoreCase("signup")) { // Jika method request adalah "signup"
JSONObject responseJSON = server.signup(clientObject);
System.out.println(responseJSON.toString());
if(responseJSON.getString("status").equalsIgnoreCase("ok")) {
server.sql.createInventory(clientObject.getString("username"));
}
}
else if(method.equalsIgnoreCase("canceloffer")) {
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("login")) { // Jika method request adalah "login"
JSONObject responseJSON = server.login(clientObject);
System.out.println(responseJSON.toString());
if(responseJSON.getString("status").equalsIgnoreCase("ok")) {
server.userID = server.sql.getUserId(clientObject.getString("username"));
server.token = responseJSON.getString("token");
System.out.println("ID : " + server.userID);
System.out.println("token : " + server.token);
}
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("inventory")) {
JSONObject responseJSON = server.inventory(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("mixitem")) {
JSONObject responseJSON = server.mixitem(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("map")) {
JSONObject responseJSON = server.map(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("move")) {
JSONObject responseJSON = server.move(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("field")) {
JSONObject responseJSON = server.field(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("offer")) {
JSONObject responseJSON = server.offer(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("canceloffer")) {
JSONObject responseJSON = server.canceloffer(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
......
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