Commit 3a328152 authored by Rakhmatullah Yoga Sutrisna's avatar Rakhmatullah Yoga Sutrisna
Browse files

debug server

parent 45571847
......@@ -24,16 +24,20 @@ public class DummyClient {
public static void main(String[] args) {
String hostName = "localhost";
int portNumber = 8000;
int portNumber = 9876;
JSONManager manager = new JSONManager();
Map request = new LinkedHashMap();
List<String> key = new ArrayList<>();
List<String> value = new ArrayList<>();
key.add("method"); value.add("login");
key.add("username"); value.add("rafi");
key.add("password"); value.add("rafi");
request = manager.createJSON(key, value);
// key.add("method"); value.add("sendfind");
// key.add("token"); value.add("139c4e89cdbedaf144d05ca54a12a57b");
// key.add("item"); value.add("2");
request.put("method", "sendfind");
request.put("token", "139c4e89cdbedaf144d05ca54a12a57b");
request.put("item", 2);
// request = manager.createJSON(key, value);
try {
System.out.println("Connecting to "+hostName+" on port "+portNumber);
Socket client = new Socket(hostName, portNumber);
......
......@@ -6,17 +6,26 @@
package grandquestserver;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
/**
......@@ -25,7 +34,7 @@ import org.json.simple.parser.ParseException;
*/
public class ClientHandler extends Thread {
private Socket socketListener;
private ServerSocket server;
private ServerSocket server;
public ClientHandler(ServerSocket sock) throws IOException {
System.out.println("Waiting for client on " + InetAddress.getLocalHost().getHostAddress()+ ":" + sock.getLocalPort() + "...");
......@@ -73,7 +82,51 @@ public class ClientHandler extends Thread {
out.writeUTF(handler.TradeBox(response));
break;
case "sendfind":
out.writeUTF(handler.sendFind(response));
Long item_id = parse.parseIntJSON(response, "item");
List<Object> list = new ArrayList<>();
JSONArray offerList = new JSONArray();
Map sendfindResponse = new LinkedHashMap();
List<Object> result = new ArrayList<>();
List<Offer> offers = new ArrayList<>();
DBHandler db = new DBHandler();
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
System.out.println(TrackerListener.serverList.length());
for(int i=0; i<TrackerListener.serverList.length(); i++) {
JSONObject obj = TrackerListener.serverList.getJSONObject(i);
if(!obj.getString("ip").equals(InetAddress.getLocalHost().getHostAddress())) {
System.out.println("masuk "+obj.getString("ip")+", "+obj.getInt("port"));
Socket p2p = new Socket(obj.getString("ip"), obj.getInt("port"));
p2p.setSoTimeout(3);
String output = JSONValue.toJSONString(parse.findOfferJSON(item_id.intValue()));
p2p.getOutputStream().write(output.getBytes());
BufferedReader input = new BufferedReader(new InputStreamReader(p2p.getInputStream()));
char[] buffer = new char[4096];
int length_buff = 0;
String p2p_response = "{}";
if((length_buff = input.read(buffer)) >= 0) {
p2p_response = new String(buffer).substring(0, length_buff);
if (parse.parseJSON(p2p_response, "status").equals("ok")) {
JSONObject arrayObj = new JSONObject(p2p_response);
offerList.put(arrayObj.getJSONArray("offers"));
}
}
}
}
System.out.println("keluar loop");
String localSearch = handler.sendFindResponse(response);
JSONObject resultlocal = new JSONObject(localSearch);
JSONArray apa[] = new JSONArray[offerList.length()];
for(int i=0; i<offerList.length(); i++) {
apa[i] = offerList.getJSONArray(i);
}
for(int i=0; i<offerList.length(); i++) {
resultlocal.put("offers", apa[i]);
}
System.out.println("hasil sendfind: "+JSONValue.toJSONString(resultlocal));
out.writeUTF(JSONValue.toJSONString(resultlocal));
break;
case "findoffer":
out.writeUTF(handler.findOffer(response));
......
......@@ -26,7 +26,7 @@ public class GrandQuestServer {
* @throws org.json.JSONException
*/
public static void main(String[] args) throws IOException, ParseException, JSONException {
int port = 8000;
int port = 9876;
TrackerListener trackerListen = new TrackerListener();
trackerListen.newServerRequest();
trackerListen.start();
......
......@@ -179,6 +179,13 @@ public class JSONParse {
return response;
}
public Map findOfferJSON(int item_id) {
Map response = new LinkedHashMap();
response.put("method", "findoffer");
response.put("item", item_id);
return response;
}
//Respon JSON untuk sendaccept
public Map sendacceptJSON(boolean valid) {
......
......@@ -461,7 +461,7 @@ public class MethodHandler {
}
//
public String sendFind(String input) throws ParseException {
public String findOffer(String input) throws ParseException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
......@@ -470,10 +470,15 @@ public class MethodHandler {
Map response = new LinkedHashMap();
Long item_id = parse.parseIntJSON(input, "item");
System.out.println(item_id);
if (item_id==2) {
offer.add(5); offer.add(3); offer.add(6);
offer.add(1); offer.add(false); offer.add("we23klf9vv4");
offer.add(5);
offer.add(3);
offer.add(6);
offer.add(1);
offer.add(false);
offer.add("we23klf9vv4");
temp = copyList(offer);
offer.clear();
offers.add(temp);
......@@ -487,7 +492,7 @@ public class MethodHandler {
}
//mencari item yang diinginkan dari server lain
public String findOffer(String input) throws ParseException, SQLException {
public String sendFindResponse(String input) throws ParseException, SQLException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
......
......@@ -28,7 +28,7 @@ public class TrackerListener extends Thread {
private final Socket trackListener;
private final String trackerAddress = "167.205.32.46";
private final int trackerPort = 8000;
private JSONArray serverList;
public static JSONArray serverList;
public boolean ready;
public TrackerListener() throws IOException {
......
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