Commit ff9555b3 authored by Junita Sinambela's avatar Junita Sinambela
Browse files

~~~~~~~~

parent 48ce6ff0
......@@ -63,7 +63,7 @@ javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=server.Server
main.class=server.helper.Message
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
......
......@@ -55,11 +55,11 @@ public class ServerHandler{
listResponses.add(response);
}
/**
* Create thread to handle request to other server and return expected response that will be given to client in string
* Create thread to handle request to other server and return list of responses from other server
* @param request
* @return
*/
public static String broadcast(String request){
public static List<String> broadcast(String request){
List<Thread> listThread = new ArrayList<>();
listResponses = new ArrayList<>();
for(int i = 0; i < Store.getServers().size(); i++){
......@@ -73,8 +73,6 @@ public class ServerHandler{
ServerLogger.log("SERVER_THREAD", "Interrupted");
}
}
//handle all the response
String responses = null;
return responses;
return listResponses;
}
}
......@@ -12,6 +12,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import objek.Person;
......@@ -69,8 +70,9 @@ public class Message {
// System.out.println(move("553b17283c892b1a8d00961f", 2, 5));
getTradeBox("553b64d23c892b2d5b772c74");
sendFind("553b64d23c892b2d5b772c74", 4);
// getTradeBox("553b64d23c892b2d5b772c74");
// sendFind("553b64d23c892b2d5b772c74", 4);
sendFind("553b64d23c892b2d5b772c74", 2);
}
public static String callURL(String myURL) {
......@@ -581,7 +583,7 @@ public class Message {
String response;
JSONObject jsonAllOffers;
try{
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox();
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox(token);
jsonAllOffers = new JSONObject();
if(allOffers != null)
{
......@@ -613,15 +615,33 @@ public class Message {
public static String sendFind(String token, int itemId) //11
{
String response;
List<String> listResponse = new ArrayList<>();
JSONObject jsonAllOffers;
JSONObject jsonObj;
try{
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox(token,itemId);
jsonObj = new JSONObject();
jsonObj.put("method", "findoffer");
jsonObj.put("item", itemId);
response = ServerHandler.broadcast(jsonObj.toString());
//buat json b
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox(token,itemId);
// listResponse = ServerHandler.broadcast(jsonObj.toString());
listResponse.add("{\"status\":\"ok\",\"offers\":[[3,2,4,3,false,\"553c35e53c892b1b9ddb1b86\"],[4,5,6,2,true,\"553c36143c892b1bbf7723d6\"],[4,1,5,3,false,\"553c372f3c892b1c27a0e924\"]]}");
for (String temp : listResponse) {
JSONObject tempJSON = new JSONObject(temp);
if(tempJSON.getString("status").equals("ok")){
JSONArray arrayJSON = tempJSON.getJSONArray("offers");
for(int i = 0; i < arrayJSON.length(); i++){
ArrayList<Object> arr = new ArrayList<>();
JSONArray js = arrayJSON.getJSONArray(i);
arr.add(js.get(0));
arr.add(js.get(1));
arr.add(js.get(2));
arr.add(js.get(3));
arr.add(js.get(4));
arr.add(js.get(5));
allOffers.add(arr);
}
}
}
jsonAllOffers = new JSONObject();
if(allOffers != null)
{
......@@ -653,6 +673,35 @@ public class Message {
public static String sendAccept(String token, String offer_token) //String token, int itemId) //11
{
String response = null;
List<String> listResponse = null;
JSONObject jsonObj = null;
JSONObject jsonAllOffers = null;
try{
if(Database.isExist())
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox(offer_token);
jsonObj = new JSONObject();
jsonObj.put("method", "accept");
jsonObj.put("offer_token", offer_token);
listResponse = ServerHandler.broadcast(jsonObj.toString());
listResponse.add("{\"status\":\"ok\",\"offers\":[[3,2,4,3,false,\"553c35e53c892b1b9ddb1b86\"],[4,5,6,2,true,\"553c36143c892b1bbf7723d6\"],[4,1,5,3,false,\"553c372f3c892b1c27a0e924\"]]}");
OUTER:
for (String temp : listResponse) {
JSONObject tempJSON = new JSONObject(temp);
switch (tempJSON.getString("status")) {
case "ok":
response = tempJSON.toString();
break OUTER;
case "fail":
case "error":
response = tempJSON.toString();
break;
}
}
}
catch(JSONException ex)
{
response = error();
}
if(log)
System.out.println(response);
return response;
......
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