Commit fd7a5d7d authored by Stephen's avatar Stephen
Browse files

Added join tracker

parent a0655911
......@@ -148,25 +148,28 @@
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace min="-2" pref="444" max="-2" attributes="0"/>
<Component id="loginButton" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="161" max="-2" attributes="0"/>
<Component id="loginLabel" min="-2" pref="60" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="52" max="-2" attributes="0"/>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="usernameLoginLabel" min="-2" max="-2" attributes="0"/>
<Component id="passwordLoginLabel" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="75" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="passwordLoginField" max="32767" attributes="0"/>
<Component id="usernameLoginTextField" min="-2" pref="151" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="444" max="-2" attributes="0"/>
<Component id="loginButton" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="161" max="-2" attributes="0"/>
<Component id="loginLabel" min="-2" pref="60" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="52" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="usernameLoginLabel" min="-2" max="-2" attributes="0"/>
<Component id="passwordLoginLabel" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="75" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="passwordLoginField" max="32767" attributes="0"/>
<Component id="usernameLoginTextField" min="-2" pref="151" max="-2" attributes="0"/>
</Group>
</Group>
</Group>
<EmptySpace pref="273" max="32767" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
......@@ -176,9 +179,9 @@
<EmptySpace min="-2" pref="90" max="-2" attributes="0"/>
<Component id="loginLabel" min="-2" pref="25" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="usernameLoginLabel" alignment="3" min="-2" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="usernameLoginTextField" alignment="1" min="-2" max="-2" attributes="0"/>
<Component id="usernameLoginLabel" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
......@@ -480,7 +483,7 @@
<EmptySpace max="32767" attributes="0"/>
<Component id="LoginAndRegisterPanel" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="LogoutMenuButton" linkSize="2" min="-2" max="-2" attributes="0"/>
<Component id="LogoutMenuButton" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
......
......@@ -158,22 +158,23 @@ public class MainPage extends javax.swing.JFrame {
loginPanel.setLayout(loginPanelLayout);
loginPanelLayout.setHorizontalGroup(
loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginPanelLayout.createSequentialGroup()
.addGap(444, 444, 444)
.addComponent(loginButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(loginPanelLayout.createSequentialGroup()
.addGap(161, 161, 161)
.addComponent(loginLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(usernameLoginLabel)
.addComponent(passwordLoginLabel))
.addGap(75, 75, 75)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(passwordLoginField)
.addComponent(usernameLoginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(273, Short.MAX_VALUE))
.addGroup(loginPanelLayout.createSequentialGroup()
.addGap(444, 444, 444)
.addComponent(loginButton))
.addGroup(loginPanelLayout.createSequentialGroup()
.addGap(161, 161, 161)
.addComponent(loginLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(usernameLoginLabel)
.addComponent(passwordLoginLabel))
.addGap(75, 75, 75)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(passwordLoginField)
.addComponent(usernameLoginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
loginPanelLayout.setVerticalGroup(
loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
......@@ -181,9 +182,9 @@ public class MainPage extends javax.swing.JFrame {
.addGap(90, 90, 90)
.addComponent(loginLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(usernameLoginLabel)
.addComponent(usernameLoginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(usernameLoginTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(usernameLoginLabel))
.addGap(18, 18, 18)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(passwordLoginLabel)
......
......@@ -33,13 +33,15 @@ file.reference.guava-11.0.2.jar=lib/guava-11.0.2.jar
file.reference.java-json.jar=lib\\java-json.jar
file.reference.json-simple-1.1.1.jar=lib/json-simple-1.1.1.jar
file.reference.mysql-connector-java-5.1.30-bin.jar=lib\\mysql-connector-java-5.1.30-bin.jar
file.reference.p2psockets-core-1.1.2.jar=lib\\p2psockets-core-1.1.2.jar
includes=**
jar.compress=false
javac.classpath=\
${file.reference.java-json.jar}:\
${file.reference.mysql-connector-java-5.1.30-bin.jar}:\
${file.reference.json-simple-1.1.1.jar}:\
${file.reference.guava-11.0.2.jar}
${file.reference.guava-11.0.2.jar}:\
${file.reference.p2psockets-core-1.1.2.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
......
......@@ -3,11 +3,15 @@ package tcpserver;
import java.io.*;
import java.net.*;
import com.google.common.io.CharStreams;
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 MySQLAccess sql;
public String request;
......@@ -17,13 +21,50 @@ public class TCPServer {
public int height;
public static String[][] map;
public TCPServer(int port) throws IOException {
System.out.println("Server running! Waiting for connection...");
public TCPServer(int port) throws IOException, JSONException {
System.out.println("Server running!");
serverSocket = new ServerSocket(port);
// Test join tracker
join(InetAddress.getLocalHost().getHostAddress(), 6789);
System.out.println("Waiting for connection...");
connectionSocket = serverSocket.accept();
sql = new MySQLAccess();
}
public void join(String host, int port) throws JSONException, UnknownHostException, IOException {
JSONObject joinRequest = new JSONObject();
joinRequest.put("method", "join");
joinRequest.put("ip", host);
joinRequest.put("port", port);
boolean joined = false;
do {
p2pSocket = new Socket("167.205.32.46", 8000);
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(p2pSocket.getOutputStream());
outToServer.writeBytes(joinRequest.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(p2pSocket.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"));
}
joined=true;
}
} while(!joined);
}
public JSONObject signup(JSONObject clientObject) throws JSONException {
String username = clientObject.getString("username");
String password = clientObject.getString("password");
......@@ -132,7 +173,6 @@ public class TCPServer {
TCPServer server = new TCPServer(port);
System.out.println("User connected to " + server.serverSocket.getLocalSocketAddress() + ", " + server.connectionSocket.getLocalSocketAddress());
while(true)
{
// Membaca request dari client
......@@ -142,51 +182,56 @@ public class TCPServer {
System.out.println("Received: " + server.request);
// Mementuk JSON Object dari request client
JSONObject clientObject = new JSONObject(server.request);
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);
// 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");
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");
}
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");
}
}
}
......
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