Commit ad6f5404 authored by Kevin's avatar Kevin
Browse files

parah

parent a88ea56c
......@@ -14,6 +14,7 @@ import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
/*
......@@ -37,6 +38,7 @@ public class ConnectionHandler implements Runnable {
private int serverSocket;
public static Map<String,String> tokenMap;
public static JSONArray serverList;
public ConnectionHandler(){
threadName = "Connection-Handler";
......
......@@ -3,14 +3,20 @@ package Default;
import Controller.ConnectionHandler;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import static Controller.ConnectionHandler.serverList;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
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.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
......@@ -188,9 +194,126 @@ public class EndpointMethod {
return res.toString();
}
public static String sendAccept(JSONObject obj) {
public String SendFind(JSONObject obj) {
JSONObject res = new JSONObject();
JSONObject request = new JSONObject();
JSONArray response = new JSONObject();
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 = "";
try {
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;
}
return null;
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();
}
public static String accept(JSONObject obj) {
......@@ -278,4 +401,15 @@ public class EndpointMethod {
return res.toString();
}
public static String updateServerStatus(JSONObject obj) {
serverList = (JSONArray) obj.get("server");
JSONObject json = new JSONObject();
if (serverList != null) {
json.put("status", "ok");
} else {
json.put("status", "error");
}
return json.toJSONString();
}
}
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