Commit ce7aca72 authored by adwisatya's avatar adwisatya
Browse files

Perbaikan join & response

parent d87a3082
......@@ -35,18 +35,19 @@ public class Client extends Thread{
private static String URL ="167.205.32.46";
public static int defPort = 8000;
public static int dbPort = 8025;
public static int myPort = 6789;
/* Client attributes */
public String token;
public String tmpStr;
public String time;
public ArrayList<String> location = new ArrayList<String>(Arrays.asList("0", "0"));
//public int item;
public int heigth;
public int width;
public String name;
public String inventory;
// public int heigth;
// public int width;
// public String name;
// public String inventory;
public ArrayList<Integer> items;
public ArrayList<String> servers;
public String offers;
Database database = new Database();
......@@ -63,6 +64,7 @@ public class Client extends Thread{
toClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = fromClient.readLine();
toClient.writeBytes("Anda mengirimkan " + clientSentence + "\n");
checkCommand(clientSentence.toString());
}catch(IOException e){
System.out.println(e);
......@@ -78,6 +80,7 @@ public class Client extends Thread{
new DataOutputStream(connectionSocket.getOutputStream()).writeBytes(tmpStr);
}
public void checkCommand(String cmd) throws IOException, NoSuchAlgorithmException, JSONException {
cmdJoin();
System.out.println("Request: " + cmd);
if(isJSONValid(cmd)){
switch (JSONtoString("method",cmd)){
......@@ -89,10 +92,9 @@ public class Client extends Thread{
break;
/* 1 */
case "join" :
System.out.println(tmpStr = cmdJoin());
sendToClient(tmpStr);
cmdJoin();
break;
/* 2 */
case "signup" :
cmdSignUp(JSONtoString("username",cmd),JSONtoString("password",cmd));
break;
......@@ -127,9 +129,11 @@ public class Client extends Thread{
case "tradebox":
cmdTradebox(JSONtoString("token",cmd));
break;
/* 11 */
case "sendfind":
cmdSendFind(JSONtoString("token",cmd), Integer.parseInt(JSONtoString("offered_item",cmd)));
cmdSendFind(JSONtoString("token",cmd), Integer.parseInt(JSONtoString("item",cmd)));
break;
case "12": System.out.println(cmdFindOffer("ece63df163ce1948008064c006f4900d",2));
break;
case "13": System.out.println(cmdSendAccept("ece63df163ce1948008064c006f4900d","ece63df163ce1948008064c006f4900d"));
......@@ -151,17 +155,17 @@ public class Client extends Thread{
return jsonObj.getString(param);
}
public void cmdServerStatus(String request){
System.out.println(request);
}
public String cmdJoin() throws IOException, JSONException{
public void cmdJoin() throws IOException, JSONException{
String response = "";
String request= "";
Socket miniSocket = new Socket(URL,defPort);
ArrayList<String> tmpServer = new ArrayList<String>();
JSONObject obj = new JSONObject();
obj.put("method", "join");
obj.put("ip", "167.205.32.46");
obj.put("port", new Integer(8000));
obj.put("ip", InetAddress.getLocalHost().getHostAddress());
obj.put("port", new Integer(myPort));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(miniSocket.getOutputStream())));
out.println(obj);
......@@ -172,7 +176,16 @@ public class Client extends Thread{
response = org.apache.commons.io.IOUtils.toString(in);
in.close();
return 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());
}
}
}
public void cmdSignUp(String username, String password) throws IOException, NoSuchAlgorithmException, JSONException{
......@@ -345,12 +358,8 @@ public class Client extends Thread{
JSONObject obj = new JSONObject();
try {
JSONArray searchedArray = database.SendFind(token,itemId);
if(searchedArray.isNull(0)){ // masih bingung if statement ny apa
obj.put("status", "ok");
obj.put("offers",searchedArray);
}else{
obj.put("status", "error");
}
obj.put("status", "ok");
obj.put("offers",searchedArray);
} catch (ClassNotFoundException ex) {
obj.put("status", "error");
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
......
......@@ -469,7 +469,7 @@ public class Database {
data.put(rs.getString("n1"));
data.put(rs.getString("demanded_item"));
data.put(rs.getString("n2"));
data.put(rs.getString("available"));
data.put(rs.getBoolean("available"));
data.put(rs.getString("offer_token"));
json.put(data);
}
......@@ -492,11 +492,11 @@ public class Database {
statement = conn.createStatement();
rs = statement.executeQuery("SELECT `id_offer`, `offered_item`, `n1`, `demanded_item`, `n2`, `available` FROM `offers` WHERE `token`<>'"+_token+"' AND `offered_item`="+_item);
while(rs.next()){
data.put(rs.getString("offered_item"));
data.put(rs.getString("n1"));
data.put(rs.getString("demanded_item"));
data.put(rs.getString("n2"));
data.put(rs.getString("available"));
data.put(rs.getInt("offered_item"));
data.put(rs.getInt("n1"));
data.put(rs.getInt("demanded_item"));
data.put(rs.getInt("n2"));
data.put(rs.getBoolean("available"));
data.put(toMD5(_token+rs.getString("id_offer"))); // value of token_offer
json.put(data);
}
......
......@@ -5,7 +5,17 @@
*/
package thealchemist;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;
import org.json.JSONException;
import org.json.JSONObject;
import static thealchemist.Client.defPort;
import static thealchemist.Client.myPort;
/**
*
......@@ -26,4 +36,5 @@ public class TCPServer {
}
}
}
}
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