Commit 8a8d9b00 authored by Stephen's avatar Stephen
Browse files

All method fixed

parent e4457f5f
......@@ -206,7 +206,6 @@ public class TCPClient {
try {
obj.put("method", "accept");
obj.put("token", token);
obj.put("offer_token", offer_token);
} catch(Exception e) {
e.printStackTrace();
......
......@@ -596,13 +596,14 @@ public class MySQLAccess {
return response;
}
public void insertOffer(JSONObject object, Vector<Host> offerHost) throws JSONException{
public void insertOffer(int id,JSONObject object, Vector<Host> offerHost) throws JSONException{
open();
JSONArray offers = object.getJSONArray("offers");
try {
// Hapus dari basis data semua offer milik server lain
preparedStatement = connect.prepareStatement("DELETE FROM offer WHERE "
+ "id_user=0;");
+ "id_user=0 AND offered_item="
+ id + ";");
preparedStatement.executeUpdate();
// Tambahkan ke basis data semua offer baru dari server lain
......@@ -686,6 +687,7 @@ public class MySQLAccess {
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
int id_offer = resultSet.getInt("id_offer");
String offered_item = "", demanded_item = "";
// Ambil nama item yang di offer
......@@ -693,7 +695,7 @@ public class MySQLAccess {
+ "id_item=\"" + resultSet.getInt("offered_item") + "\";");
ResultSet resultSet2 = preparedStatement.executeQuery();
if(resultSet2.first()) {
offered_item = resultSet2.getString("item_name");
offered_item = resultSet2.getString("item_name").replace(' ', '_');
}
// Ambil nama item yang di demand
......@@ -701,7 +703,7 @@ public class MySQLAccess {
+ "id_item=\"" + resultSet.getInt("demanded_item") + "\";");
resultSet2 = preparedStatement.executeQuery();
if(resultSet2.first()) {
demanded_item = resultSet2.getString("item_name");
demanded_item = resultSet2.getString("item_name").replace(' ', '_');
}
// Kurangin inventory user dengan yg demanded, dan tambah dengan yg offered
......@@ -710,6 +712,10 @@ public class MySQLAccess {
+ demanded_item + "=" + demanded_item + "-" + resultSet.getInt("n2") + " WHERE "
+ "id_user=" + userID + ";");
preparedStatement.executeUpdate();
// Hilangkan offer tersebut
preparedStatement = connect.prepareStatement("DELETE FROM offer where id_offer=" + id_offer + ";");
preparedStatement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
......@@ -755,7 +761,7 @@ public class MySQLAccess {
public Host getHostByOfferToken(String offer_token)
{
open();
Host host = null;
Host host = new Host("", 0);
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
......
......@@ -31,6 +31,7 @@ public class TCPServer implements Runnable {
public int height;
public static String[][] map;
public List<Host> servers;
public String[] findOfferTimes;
public String findOfferTime = "";
......@@ -39,8 +40,12 @@ public class TCPServer implements Runnable {
serverPort = 6789;
serverClientSocket = new ServerSocket(serverPort);
servers = new ArrayList<Host>();
thisIP = "167.205.71.56";
thisIP = "167.205.34.248";
sql = new MySQLAccess();
findOfferTimes = new String[10];
for(int i=0; i<10; i++) {
findOfferTimes[i] = "";
}
}
public void printAllServers() {
......@@ -220,10 +225,13 @@ public class TCPServer implements Runnable {
Socket tempsocket = new Socket(host.ipAddress, host.port);
JSONObject responseJSON = new JSONObject();
JSONObject toSend = new JSONObject();
toSend.put("method", "accept");
toSend.put("offer_token", clientObject.getString("offer_token"));
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(tempsocket.getOutputStream());
outToServer.write(clientObject.toString().getBytes("UTF-8"));
outToServer.write(toSend.toString().getBytes("UTF-8"));
} catch (IOException ex) {
ex.printStackTrace();
}
......@@ -284,7 +292,7 @@ public class TCPServer implements Runnable {
}
}
public boolean moreThan5Minutes(String d1, String d2) {
public boolean isTimeOut(String d1, String d2) {
boolean more = false;
String[] d1split = d1.split(":");
String[] d2split = d2.split(":");
......@@ -292,30 +300,30 @@ public class TCPServer implements Runnable {
int totalSec1 = Integer.parseInt(d1split[0])*3600 + Integer.parseInt(d1split[1])*60 + Integer.parseInt(d1split[2]);
int totalSec2 = Integer.parseInt(d2split[0])*3600 + Integer.parseInt(d2split[1])*60 + Integer.parseInt(d2split[2]);
if((totalSec1 - totalSec2) > 300) {
if((totalSec1 - totalSec2) > 30) {
more = true;
}
return more;
}
public JSONObject sendfind(JSONObject clientObject) throws JSONException, UnknownHostException, IOException {
System.out.println("masuk find");
if(clientObject.getString("token").equalsIgnoreCase(token)) {
// isi findOfferTime dengan waktu sekarang
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
boolean firsttime = true;
String currTime = dateFormat.format(date);
if(!findOfferTime.equalsIgnoreCase("")) {
if(!findOfferTimes[clientObject.getInt("item")].equalsIgnoreCase("")) {
firsttime = false;
} else {
findOfferTime = dateFormat.format(date);
findOfferTimes[clientObject.getInt("item")] = dateFormat.format(date);
}
System.out.println("Current Time : " + currTime);
System.out.println("Last Find Time: " + findOfferTime);
System.out.println("Last Find Time: " + findOfferTimes[clientObject.getInt("item")]);
if(firsttime || moreThan5Minutes(currTime, findOfferTime)) {
if(firsttime || isTimeOut(currTime, findOfferTimes[clientObject.getInt("item")])) {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "ok");
JSONArray offers = new JSONArray();
......@@ -363,14 +371,13 @@ public class TCPServer implements Runnable {
tempsocket.close();
} catch(Exception e) {
e.printStackTrace();
System.out.println("Cannot connect to " + server.ipAddress + ":" + server.port);
}
}
}
responseJSON.put("offers", offers);
sql.insertOffer(responseJSON, offerHost);
sql.insertOffer(clientObject.getInt("item"), responseJSON, offerHost);
date = new Date();
findOfferTime = dateFormat.format(date);
return responseJSON;
......@@ -452,7 +459,7 @@ public class TCPServer implements Runnable {
inFromClient.read(b);
server.clientRequest = new String(b, "UTF-8");
System.out.println("Received: " + server.clientRequest + "aaaaaaa");
System.out.println("Received: " + server.clientRequest);
if(!server.clientRequest.equalsIgnoreCase("") && isJSONValid(server.clientRequest)) {
// Mementuk JSON Object dari request client
......@@ -535,6 +542,7 @@ public class TCPServer implements Runnable {
}
}
} catch(Exception e) {
e.printStackTrace();
System.out.println("Error reading client request");
}
}
......
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