Commit 0e83946a authored by Jonathan Sudibya's avatar Jonathan Sudibya
Browse files

latest commit

parent eae10bf9
......@@ -5,13 +5,20 @@ import Controller.DBHandler;
import Controller.ConnectionHandler;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/*
......@@ -158,61 +165,6 @@ public final class EndpointMethod {
return res.toString();
}
public static String findOffer(JSONObject obj) throws UnknownHostException {
JSONObject res = new JSONObject();
res.put("status", "error");
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("offered_item", obj.get("item").toString());
DBCursor cursor = DBHandler.offers.find(queryObj);
if (cursor.size() > 0) {
JSONArray offer_list = new JSONArray();
while (cursor.hasNext()) {
BasicDBObject docObj = new BasicDBObject();
docObj = (BasicDBObject) cursor.next();
JSONArray offerJson = new JSONArray();
offerJson.add(docObj.get("offered_item"));
offerJson.add(docObj.get("n1"));
offerJson.add(docObj.get("demanded_item"));
offerJson.add(docObj.get("n2"));
offerJson.add(docObj.get("available"));
offerJson.add(docObj.get("offer_token"));
offer_list.add(offerJson);
}
res.put("status", "ok");
res.put("offers", offer_list);
}
return res.toString();
}
public static String sendAccept(JSONObject obj) {
return null;
}
public static String accept(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("offer_token", obj.get("offer_token").toString());
DBCursor cursor = DBHandler.offers.find(queryObj);
if (cursor.size() > 0) {
JSONArray offer_list = new JSONArray();
while (cursor.hasNext()) {
BasicDBObject docObj = new BasicDBObject();
docObj = (BasicDBObject) cursor.next();
if (docObj.get("available").toString().equalsIgnoreCase("true")) {
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("available", false);
DBHandler.setOfferAttribute(null, null, null);
res.put("status", "ok");
} else {
res.put("status", "fail");
res.put("description", "offer is not available");
}
}
}
return res.toString();
}
public static String fetchItem(JSONObject Obj) {
JSONObject res = new JSONObject();
......@@ -401,9 +353,9 @@ public final class EndpointMethod {
public static String updateServerStatus(JSONObject obj)
{
serverList = (JSONArray) obj.get("value");
serverList = (JSONArray) obj.get("server");
JSONObject json = new JSONObject();
if (serverList == null){
if (serverList != null){
json.put("status","ok");
}
else{
......@@ -411,4 +363,150 @@ public final class EndpointMethod {
}
return json.toJSONString();
}
public static String findOffer(JSONObject obj) throws UnknownHostException {
JSONObject res = new JSONObject();
res.put("status", "error");
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("offered_item", obj.get("item").toString());
DBCursor cursor = DBHandler.offers.find(queryObj);
if (cursor.size() > 0) {
JSONArray offer_list = new JSONArray();
while (cursor.hasNext()) {
BasicDBObject docObj = new BasicDBObject();
docObj = (BasicDBObject) cursor.next();
JSONArray offerJson = new JSONArray();
offerJson.add(docObj.get("offered_item"));
offerJson.add(docObj.get("n1"));
offerJson.add(docObj.get("demanded_item"));
offerJson.add(docObj.get("n2"));
offerJson.add(docObj.get("available"));
offerJson.add(docObj.get("offer_token"));
offer_list.add(offerJson);
}
res.put("status", "ok");
res.put("offers", offer_list);
}
return res.toString();
}
public String SendFind(JSONObject obj) {
JSONObject res = new JSONObject();
JSONObject request = new JSONObject();
JSONArray response = new JSONArray();
JSONArray arr = new JSONArray();
res.put("status", "error");
request.put("method", "findoffer");
String find = obj.get("item").toString();
request.put("item", find);
String Request = request.toString();
try {
response = this.send(Request);
} catch (Exception ex) {
Logger.getLogger(EndpointMethod.class.getName()).log(Level.SEVERE, null, ex);
}
JSONArray Offers = new JSONArray();
Offers = response;
String token = obj.get("token").toString();
String username = ConnectionHandler.tokenMap.get(token);
DBCursor cursor = DBHandler.offers.find();
while (cursor.hasNext()) {
BasicDBObject curObj = new BasicDBObject();
curObj = (BasicDBObject) cursor.next();
if (!curObj.get("username").toString().equalsIgnoreCase(username)) {
JSONArray curArray = new JSONArray();
curArray.add(curObj.get("offered_item"));
curArray.add(curObj.get("n1"));
curArray.add(curObj.get("demanded_item"));
curArray.add(curObj.get("n2"));
curArray.add(curObj.get("available"));
curArray.add(curObj.get("offer_token"));
Offers.add(curArray);
}
}
return res.toString();
}
public JSONArray send(String lalala) throws Exception {
//JSONParser parser = new JSONParser();
String sentence;
JSONArray res = new JSONArray();
InputStreamReader inFromAnotherServer;
DataOutputStream outToAnotherServer;
int buf;
String clientSentence = "";
Socket clientSocket = new Socket("localhost", 6789);
outToAnotherServer = new DataOutputStream(clientSocket.getOutputStream());
inFromAnotherServer = new InputStreamReader(clientSocket.getInputStream());
sentence = lalala;
// System.out.println(lalala);
outToAnotherServer.write(sentence.getBytes(Charset.forName("UTF-8")));
// System.out.println("FROM Another SERVER: " + modifiedSentence);
while ((buf = inFromAnotherServer.read()) != -1) {
char c = (char) buf;
clientSentence += Character.toString(c);
}
if (!clientSentence.equals("")) {
JSONParser parser = new JSONParser();
Object object = parser.parse(clientSentence);
if (object != null) {
JSONObject data = (JSONObject) object;
if (data.get("status").toString().equalsIgnoreCase("ok")) {
JSONArray tmp = new JSONArray();
tmp = (JSONArray) data.get("offers");
res.addAll(tmp);
System.out.println(res.toString());
}
}
}
return res;
}
// public static String sendAccept(JSONObject obj) throws IOException {
// JSONObject res = new JSONObject();
// String token = obj.get("token").toString();
// String username = ConnectionHandler.tokenMap.get(token);
// int countOwnDemand = DBHandler.getUserAttribute(username, demand_item);
// if (countOwnDemand > n2) {
// JSONObject sendJson = new JSONObject();
// sendJson.put("method", "accept");
// sendJson.put("offer_token", obj.get("offer_token").toString());
// Socket _socket = new Socket(ip_address, port);
// DataOutputStream outToAnotherServer = new DataOutputStream(_socket.getOutputStream());
// outToAnotherServer.write(sendJson.toString().getBytes(Charset.forName("UTF-8")));
// InputStreamReader inFromAnotherServer = new InputStreamReader(_socket.getInputStream());
// String clientSentence = "";
// int buf;
// while ((buf = inFromAnotherServer.read()) != -1) {
// char c = (char) buf;
// clientSentence += Character.toString(c);
// }
// if (!clientSentence.equals("")) {
// JSONParser parser = new JSONParser();
// Object object = parser.parse(clientSentence);
// if (object != null) {
// JSONObject data = (JSONObject) object;
// if (data.get("status").toString().equalsIgnoreCase("ok")) {
// DBHandler.getUserAttribute(username, demand_item);
// DBHandler.getUserAttribute(username, offer_item);
// DBHandler.setUserAttribute(username, offer_item, hasil_komputasi);
// DBHandler.setUserAttribute(username, demanded_item, hasil_komputasi);
// res.put("status", "ok");
// } else if (data.get("status").toString().equalsIgnoreCase("fail")) {
// res.put("status", "fail");
// res.put("description", data.get("description").toString());
// } else if (data.get("status").toString().equalsIgnoreCase("error")) {
// res.put("status", "error");
// }
// }
// }
// } else {
// res.put("status", "fail");
// res.put("description", "anda tidak memenuhi persyaratan offer");
// }
// return res.toString();
// }
}
......@@ -6,11 +6,13 @@
package Model;
import Method.EndpointMethod;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
/**
*
......@@ -64,7 +66,13 @@ public final class MessageProcessor {
break;
}
case "tradebox" : {
System.out.println("BELUM!");
try {
response = EndpointMethod.tradebox(data);
} catch (IOException ex) {
Logger.getLogger(MessageProcessor.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(MessageProcessor.class.getName()).log(Level.SEVERE, null, ex);
}
break;
}
case "sendfind" : {
......
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