Commit 5b30e317 authored by Stephen's avatar Stephen
Browse files

Fixed sendfind

parent 1ce989a2
......@@ -12,6 +12,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;
import jdk.nashorn.internal.parser.JSONParser;
import org.json.JSONArray;
import org.json.JSONException;
......@@ -571,6 +572,37 @@ public class MySQLAccess {
return response;
}
public JSONObject sendfind(int id_item) throws JSONException{
open();
JSONObject response = new JSONObject();
JSONArray offers = new JSONArray();
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offered_item=" + id_item + ";");
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
JSONArray offer = new JSONArray();
offer.put(resultSet.getInt("offered_item"));
offer.put(resultSet.getInt("n1"));
offer.put(resultSet.getInt("demanded_item"));
offer.put(resultSet.getInt("n2"));
offer.put(resultSet.getBoolean("available"));
offer.put(resultSet.getString("offer_token"));
offers.put(offer);
}
response.put("status", "ok");
response.put("offers", offers);
} catch (SQLException e) {
response.put("status", "error");
e.printStackTrace();
}
close();
return response;
}
public JSONObject canceloffer(int userID, String offer_token) throws JSONException
{
open();
......@@ -620,6 +652,38 @@ public class MySQLAccess {
return response;
}
public void insertOffer(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;");
preparedStatement.executeUpdate();
// Tambahkan ke basis data semua offer baru dari server lain
if(offers.length()>0) {
for(int i=0; i<offers.length(); i++) {
JSONArray offer = (JSONArray) offers.get(i);
preparedStatement = connect.prepareStatement("INSERT INTO offer "
+ "VALUES(NULL, 0, ?, ?, ?, ?, ?, ?, 1, ?)");
preparedStatement.setInt(1, offer.getInt(0));
preparedStatement.setInt(2, offer.getInt(1));
preparedStatement.setInt(3, offer.getInt(2));
preparedStatement.setInt(4, offer.getInt(3));
preparedStatement.setString(5, offerHost.get(i).ipAddress);
preparedStatement.setInt(6, offerHost.get(i).port);
preparedStatement.setString(7, offer.getString(5));
preparedStatement.executeUpdate();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
}
public void createInventory(String username) {
open();
try {
......
......@@ -9,6 +9,7 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import jdk.nashorn.internal.parser.JSONParser;
......@@ -31,7 +32,7 @@ public class TCPServer implements Runnable {
public static String[][] map;
public List<Host> servers;
public String findOfferTime;
public String findOfferTime = "";
public TCPServer() throws IOException, JSONException {
System.out.println("Server running!");
......@@ -83,7 +84,12 @@ public class TCPServer implements Runnable {
printAllServers();
joined=true;
} else if(joinResponse.getString("status").equalsIgnoreCase("error")){
System.out.println("Error joining tracker, " + joinResponse.getString("description"));
System.out.println("Error joining tracker, " + joinResponse.getString("description") + ", retrying in 3 secs");
try {
Thread.sleep(3000);
} catch(Exception ex) {
ex.printStackTrace();
}
}
} while(!joined);
}
......@@ -265,56 +271,82 @@ public class TCPServer implements Runnable {
}
}
public boolean moreThan5Minutes(String d1, String d2) {
boolean more = false;
return more;
}
public JSONObject sendfind(JSONObject clientObject) throws JSONException, UnknownHostException, IOException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
// isi findOfferTime dengan waktu sekarang
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
findOfferTime = dateFormat.format(date);
System.out.println(dateFormat.format(date));
boolean firsttime = true;
String currTime = dateFormat.format(date);
if(!findOfferTime.equalsIgnoreCase("")) {
firsttime = false;
} else {
findOfferTime = dateFormat.format(date);
}
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "ok");
JSONArray offers = new JSONArray();
for(Host server : servers) {
System.out.println(server.ipAddress + " " + server.port);
if(server.ipAddress.equalsIgnoreCase("167.205.32.46") && server.port==8000) {
// do nothing
} else if(server.ipAddress.equalsIgnoreCase(thisIP) && server.port==serverPort) {
// do nothing
} else {
JSONObject tempRequest = new JSONObject();
tempRequest.put("method", "findoffer");
tempRequest.put("item", clientObject.getInt("item"));
Socket tempsocket = new Socket(server.ipAddress, server.port);
tempsocket.setSoTimeout(5000);
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(tempsocket.getOutputStream());
outToServer.write(tempRequest.toString().getBytes("UTF-8"));
System.out.println(tempRequest.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
if(firsttime || moreThan5Minutes(findOfferTime, currTime)) {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "ok");
JSONArray offers = new JSONArray();
Vector offerHost = new Vector<Host>();
for(Host server : servers) {
System.out.println(server.ipAddress + " " + server.port);
if(server.ipAddress.equalsIgnoreCase("167.205.32.46") && server.port==8000) {
// do nothing
} else if(server.ipAddress.equalsIgnoreCase(thisIP) && server.port==serverPort) {
// do nothing
} else if(server.ipAddress.equalsIgnoreCase("167.205.32.46")){ // Masih cuma pake kakaknya punya
JSONObject tempRequest = new JSONObject();
tempRequest.put("method", "findoffer");
tempRequest.put("item", clientObject.getInt("item"));
Socket tempsocket = new Socket(server.ipAddress, server.port);
tempsocket.setSoTimeout(5000);
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(tempsocket.getOutputStream());
outToServer.write(tempRequest.toString().getBytes("UTF-8"));
System.out.println(tempRequest.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
DataInputStream inFromServer = new DataInputStream(tempsocket.getInputStream());
byte b[] = new byte[4096];
inFromServer.read(b);
String response = new String(b, "UTF-8");
System.out.println(response);
JSONObject tempResponse = new JSONObject(response);
if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
offers.put(tempResponse.getJSONArray("offers"));
}
DataInputStream inFromServer = new DataInputStream(tempsocket.getInputStream());
tempsocket.close();
byte b[] = new byte[4096];
inFromServer.read(b);
String response = new String(b, "UTF-8");
System.out.println(response);
JSONObject tempResponse = new JSONObject(response);
if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
if(tempResponse.getJSONArray("offers").length()>0) {
for(int i=0; i<tempResponse.getJSONArray("offers").length(); i++) {
offers.put(tempResponse.getJSONArray("offers").get(i));
offerHost.add(new Host(server.ipAddress, server.port));
}
}
}
tempsocket.close();
}
}
responseJSON.put("offers", offers);
sql.insertOffer(responseJSON, offerHost);
date = new Date();
findOfferTime = dateFormat.format(date);
return responseJSON;
} else {
return sql.sendfind(clientObject.getInt("item"));
}
responseJSON.put("offers", offers);
return responseJSON;
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
......@@ -323,13 +355,7 @@ public class TCPServer implements Runnable {
}
public JSONObject findoffer(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.findOffer(clientObject.getInt("item"));
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
return responseJSON;
}
}
public JSONObject serverstatus(JSONObject clientObject) throws JSONException {
......@@ -369,6 +395,7 @@ public class TCPServer implements Runnable {
while(true)
{
System.out.println("======================================================================");
System.out.println("Waiting for client to connect..");
server.clientSocket = server.serverClientSocket.accept();
......@@ -379,7 +406,7 @@ public class TCPServer implements Runnable {
inFromClient.read(b);
server.clientRequest = new String(b, "UTF-8");
System.out.println("Received: " + server.clientRequest);
System.out.println("Received: " + server.clientRequest + "aaaaaaa");
if(!server.clientRequest.equalsIgnoreCase("")) {
// Mementuk JSON Object dari request client
......
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