Commit f2554465 authored by Stephen's avatar Stephen
Browse files

Final version

parent 034855a0
......@@ -1974,7 +1974,7 @@ public class MainPage extends javax.swing.JFrame {
prepareToMixItem();
}
else if(response.get("status").equals("fail")) {
showMessage("Fail to Mix Item");
showMessage(response.getString("description"));
}
}
catch (Exception e) {
......@@ -2128,7 +2128,7 @@ public class MainPage extends javax.swing.JFrame {
try {
System.out.println((Integer) OfferedAmount.getValue());
if(OfferedItemId!=-1 && DemandedItemId!=-1) {
if(((Integer) OfferedAmount.getValue() >= 0) && ((Integer) DemandedAmount.getValue() >=0)) {
if(((Integer) OfferedAmount.getValue() > 0) && ((Integer) DemandedAmount.getValue() > 0)) {
JSONObject response = client.offer(client.token, OfferedItemId,(Integer) OfferedAmount.getValue(), DemandedItemId,(Integer) DemandedAmount.getValue());
if(response.get("status").equals("ok")) {
showMessage("Offer was made");
......
......@@ -245,7 +245,6 @@ public class MySQLAccess {
+ "position_x=" + x + ", "
+ "position_y=" + y + " WHERE "
+ "id="+ userID + ";");
System.out.println(preparedStatement.toString());
preparedStatement.executeUpdate();
response.put("status", "ok");
response.put("time", System.currentTimeMillis()/1000 + (5*abs(curr_x-x)) + (5*abs(curr_y-y)));
......@@ -277,7 +276,6 @@ public class MySQLAccess {
if(resultSet.first()) {
int curr_x = resultSet.getInt("position_x");
int curr_y = resultSet.getInt("position_y");
//System.out.println("nih map :"+TCPServer.map[curr_x][curr_y]);
String id_other = TCPServer.map[curr_x][curr_y];
preparedStatement = connect.prepareStatement("SELECT * FROM items WHERE "+ "id_other=\"" + id_other + "\";");
......
......@@ -3,6 +3,7 @@ package tcpserver;
import java.io.*;
import java.net.*;
import com.google.common.io.CharStreams;
import com.sun.corba.se.impl.orbutil.concurrent.Mutex;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
......@@ -32,6 +33,7 @@ public class TCPServer implements Runnable {
public static String[][] map;
public List<Host> servers;
public String[] findOfferTimes;
public boolean lock = false;
public String findOfferTime = "";
......@@ -240,6 +242,7 @@ public class TCPServer implements Runnable {
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")) {
responseJSON.put("status", "ok");
......@@ -300,7 +303,7 @@ 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) > 30) {
if((totalSec1 - totalSec2) > 60) {
more = true;
}
return more;
......@@ -323,6 +326,7 @@ public class TCPServer implements Runnable {
System.out.println("Current Time : " + currTime);
System.out.println("Last Find Time: " + findOfferTimes[clientObject.getInt("item")]);
Vector<Thread> threads = new Vector<Thread>();
if(firsttime || isTimeOut(currTime, findOfferTimes[clientObject.getInt("item")])) {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "ok");
......@@ -330,52 +334,76 @@ public class TCPServer implements Runnable {
Vector offerHost = new Vector<Host>();
for(Host server : servers) {
if(server.ipAddress.equalsIgnoreCase("167.205.32.46") && server.port==8000) {
// do nothing
// do nothing if it is the tracker ip
} else if(server.ipAddress.equalsIgnoreCase(thisIP) && server.port==serverPort) {
// do nothing
} else{ // Masih cuma pake kakaknya punya
System.out.println("Searching offer from : " + server.ipAddress + ":" + server.port);
JSONObject tempRequest = new JSONObject();
tempRequest.put("method", "findoffer");
tempRequest.put("item", clientObject.getInt("item"));
try {
Socket tempsocket = new Socket(server.ipAddress, server.port);
// do nothing if it is our own ip
} else{
// Start thread to find offer from that server
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();
}
Thread thread = new Thread(){
@Override
public void run(){
System.out.println("Searching offer from : " + server.ipAddress + ":" + server.port);
JSONObject tempRequest = new JSONObject();
try {
tempRequest.put("method", "findoffer");
tempRequest.put("item", clientObject.getInt("item"));
} catch(Exception e) {
System.out.println("Error creating JSON request for " + server.ipAddress + ":" + server.port);
}
try {
Socket tempsocket = new Socket(server.ipAddress, server.port);
DataInputStream inFromServer = new DataInputStream(tempsocket.getInputStream());
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(tempsocket.getOutputStream());
outToServer.write(tempRequest.toString().getBytes("UTF-8"));
} catch (IOException ex) {
ex.printStackTrace();
}
byte b[] = new byte[4096];
inFromServer.read(b);
String response = new String(b, "UTF-8");
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")) {
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));
System.out.println("Response: " + response);
JSONObject tempResponse = new JSONObject(response);
if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
if(tempResponse.getJSONArray("offers").length()>0) {
while(lock) { }
lock = true;
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));
}
lock = false;
}
}
tempsocket.close();
} catch(Exception e) {
System.out.println("Cannot connect to " + server.ipAddress + ":" + server.port);
}
}
};
tempsocket.close();
} catch(Exception e) {
System.out.println("Cannot connect to " + server.ipAddress + ":" + server.port);
try {
thread.start();
threads.add(thread);
} catch (Exception e) {
System.out.println("interrupted");
}
}
}
for(int i=0; i<threads.size(); i++) {
try {
threads.get(i).join();
} catch(Exception e) { }
}
responseJSON.put("offers", offers);
sql.insertOffer(clientObject.getInt("item"), responseJSON, offerHost);
date = new Date();
......
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