Commit 6092b816 authored by Stephen's avatar Stephen
Browse files

Server all method implemented, not tested

parent 146cdc31
......@@ -573,7 +573,7 @@ public class TCPClient {
return responseJSON;
}
public JSONObject accept(String token,String offer_token) throws Exception
public JSONObject accept(String token,String offer_token) throws Exception
{
openConnection();
System.out.println("Accept");
......
......@@ -526,7 +526,7 @@ public class MySQLAccess {
return response;
}
public JSONObject accept(int userID, String offer_token) throws JSONException
public JSONObject accept(String offer_token) throws JSONException
{
open();
JSONObject response = new JSONObject();
......@@ -550,9 +550,11 @@ public class MySQLAccess {
{
response.put("status", "fail");
response.put("description", "offer is not available");
}
}
}
} else {
response.put("status", "fail");
response.put("description", "offer is not available");
}
}
catch (SQLException e) {
response.put("status", "error");
......@@ -763,6 +765,92 @@ public class MySQLAccess {
return id;
}
public void deleteOffer(String offer_token) {
open();
try {
preparedStatement = connect.prepareStatement("DELETE FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
close();
}
public void commitAcceptOffer(int userID, String offer_token) {
open();
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
String offered_item = "", demanded_item = "";
// Ambil nama item yang di offer
preparedStatement = connect.prepareStatement("SELECT * FROM items WHERE "
+ "id_item=\"" + resultSet.getInt("offered_item") + "\";");
ResultSet resultSet2 = preparedStatement.executeQuery();
if(resultSet2.first()) {
offered_item = resultSet2.getString("item_name");
}
// Ambil nama item yang di demand
preparedStatement = connect.prepareStatement("SELECT * FROM items WHERE "
+ "id_item=\"" + resultSet.getInt("demanded_item") + "\";");
resultSet2 = preparedStatement.executeQuery();
if(resultSet2.first()) {
demanded_item = resultSet2.getString("item_name");
}
// Kurangin inventory user dengan yg demanded, dan tambah dengan yg offered
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ offered_item + "=" + offered_item + "+" + resultSet.getInt("n1") + ", "
+ demanded_item + "=" + demanded_item + "-" + resultSet.getInt("n2") + " WHERE "
+ "id_user=" + userID + ";");
preparedStatement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
}
public boolean isItemEnough(int userID, String offer_token) {
open();
boolean enough = true;
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
int demanded_count = resultSet.getInt("n2");
preparedStatement = connect.prepareStatement("SELECT * FROM items WHERE "
+ "id_item=" + resultSet.getInt("demanded_item") + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
String demanded_item = resultSet.getString("item_name").toLowerCase().replace(' ', '_');
preparedStatement = connect.prepareStatement("SELECT * FROM inventory WHERE "
+ "id_user=" + userID + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
if(resultSet.getInt(demanded_item) < demanded_count) {
enough = false;
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return enough;
}
public Host getHostByOfferToken(String offer_token)
{
open();
......
......@@ -208,8 +208,55 @@ public class TCPServer implements Runnable {
}
public JSONObject accept(JSONObject clientObject) throws JSONException {
return sql.accept(clientObject.getString("offer_token"));
}
public JSONObject sendaccept(JSONObject clientObject) throws JSONException, UnknownHostException, IOException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.accept(userID, clientObject.getString("offer_token"));
Host host = sql.getHostByOfferToken(clientObject.getString("offer_token"));
if(host!=null) {
if(sql.isItemEnough(userID, clientObject.getString("offer_token"))) {
Socket tempsocket = new Socket(host.ipAddress, host.port);
JSONObject responseJSON = new JSONObject();
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(tempsocket.getOutputStream());
outToServer.write(clientObject.toString().getBytes("UTF-8"));
} 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");
JSONObject tempResponse = new JSONObject(response);
if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
responseJSON.put("status", "ok");
sql.commitAcceptOffer(userID, clientObject.getString("offer_token"));
} else if(tempResponse.getString("status").equalsIgnoreCase("fail")) {
responseJSON.put("status", "fail");
responseJSON.put("description", tempResponse.getString("description"));
sql.deleteOffer(clientObject.getString("offer_token"));
} else if(tempResponse.getString("status").equalsIgnoreCase("error")) {
responseJSON.put("status", "error");
}
tempsocket.close();
return responseJSON;
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "fail");
responseJSON.put("description", "insufficient item");
return responseJSON;
}
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
return responseJSON;
}
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
......@@ -217,40 +264,6 @@ public class TCPServer implements Runnable {
}
}
// public JSONObject sendaccept(JSONObject clientObject) throws JSONException, UnknownHostException, IOException {
// if(clientObject.getString("token").equalsIgnoreCase(token)) {
// Host host = sql.getHostByOfferToken(clientObject.getString("offer_token"));
//
// if(host!=null) {
// Socket tempsocket = new Socket(host.ipAddress, host.port);
//
// JSONObject responseJSON = new JSONObject();
// DataOutputStream outToServer;
// try {
// outToServer = new DataOutputStream(trackerSocket.getOutputStream());
// outToServer.writeBytes(clientObject.toString() + '\n');
// } catch (IOException ex) {
// ex.printStackTrace();
// }
//
// BufferedReader inFromServer = new BufferedReader(new InputStreamReader(trackerSocket.getInputStream()));
// String response = inFromServer.readLine();
// JSONObject tempResponse = new JSONObject(response);
// if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
// responseJSON.put("status", "ok");
// sql.
// }
//
// tempsocket.close();
// }
//
// } else {
// JSONObject responseJSON = new JSONObject();
// responseJSON.put("status", "error");
// return responseJSON;
// }
// }
public JSONObject fetchitem(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.fetchitem(userID, clientObject.getString("offer_token"));
......@@ -498,6 +511,10 @@ public class TCPServer implements Runnable {
JSONObject responseJSON = server.accept(clientObject);
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("sendaccept")) {
JSONObject responseJSON = server.sendaccept(clientObject);
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("serverStatus")) {
JSONObject responseJSON = server.serverstatus(clientObject);
System.out.println("Sent: " + responseJSON.toString());
......
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