Commit e4be1102 authored by Darwin's avatar Darwin
Browse files

coba find

parents 0ded8f30 5c393c17
compile.on.save=true
<<<<<<< HEAD
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
<<<<<<< HEAD
=======
......@@ -24,3 +25,6 @@ user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.pro
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
=======
>>>>>>> 7d12ee92a34d06c0f223c1804ac375528b1b069d
=======
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
>>>>>>> 5c393c17c0f1ef1dd5ee2823322f7f8ff51533ad
......@@ -1899,7 +1899,7 @@
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="tradeitemPanel">
<Container class="javax.swing.JPanel" name="FindOfferItemPanel">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignCardLayout" value="org.netbeans.modules.form.compat2.layouts.DesignCardLayout$CardConstraintsDescription">
<CardConstraints cardName="TradeItemCard"/>
......
......@@ -189,7 +189,7 @@ public class MainPage extends javax.swing.JFrame {
FindIncense = new javax.swing.JButton();
FindIcon = new javax.swing.JButton();
Find = new javax.swing.JButton();
tradeitemPanel = new javax.swing.JPanel();
FindOfferItemPanel = new javax.swing.JPanel();
FindOfferItemScrollPane = new javax.swing.JScrollPane();
FindOfferItemTable = new javax.swing.JTable();
......@@ -1495,24 +1495,24 @@ public class MainPage extends javax.swing.JFrame {
FindOfferItemTable.setModel(tradeboxModel);
FindOfferItemScrollPane.setViewportView(FindOfferItemTable);
javax.swing.GroupLayout tradeitemPanelLayout = new javax.swing.GroupLayout(tradeitemPanel);
tradeitemPanel.setLayout(tradeitemPanelLayout);
tradeitemPanelLayout.setHorizontalGroup(
tradeitemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(tradeitemPanelLayout.createSequentialGroup()
javax.swing.GroupLayout FindOfferItemPanelLayout = new javax.swing.GroupLayout(FindOfferItemPanel);
FindOfferItemPanel.setLayout(FindOfferItemPanelLayout);
FindOfferItemPanelLayout.setHorizontalGroup(
FindOfferItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(FindOfferItemPanelLayout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(FindOfferItemScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 847, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(42, Short.MAX_VALUE))
);
tradeitemPanelLayout.setVerticalGroup(
tradeitemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tradeitemPanelLayout.createSequentialGroup()
FindOfferItemPanelLayout.setVerticalGroup(
FindOfferItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, FindOfferItemPanelLayout.createSequentialGroup()
.addContainerGap(20, Short.MAX_VALUE)
.addComponent(FindOfferItemScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(20, 20, 20))
);
MainPanel.add(tradeitemPanel, "TradeItemCard");
MainPanel.add(FindOfferItemPanel, "TradeItemCard");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
......@@ -2355,10 +2355,9 @@ public class MainPage extends javax.swing.JFrame {
private void generateFindOfferTable() {
System.out.println("masuk generate tradebox");
System.out.println("masuk generate find offer table");
try {
//ini pake client.find()
JSONObject response = client.tradebox();
JSONObject response = client.sendFind(client.token, FindId);
if(response.getString("status").equals("ok")) {
JSONArray offer = response.getJSONArray("offers");
......@@ -2411,11 +2410,11 @@ public class MainPage extends javax.swing.JFrame {
} else {
showMessage("Tradebox Error. Try again later..");
showMessage("Find Item Error. Try again later..");
}
} catch (Exception ex) {
showMessage("Server tradebox does not respond. Please try again later..");
System.out.println("Tradebox Exception");
showMessage("Server Find Item does not respond. Please try again later..");
System.out.println("FindItem Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
......@@ -2507,6 +2506,7 @@ public class MainPage extends javax.swing.JFrame {
private javax.swing.JButton FindLifeElixir;
private javax.swing.JButton FindManaCrystal;
private javax.swing.JButton FindMineral;
private javax.swing.JPanel FindOfferItemPanel;
private javax.swing.JScrollPane FindOfferItemScrollPane;
private javax.swing.JTable FindOfferItemTable;
private javax.swing.JButton FindPhilosopherStone;
......@@ -2603,7 +2603,6 @@ public class MainPage extends javax.swing.JFrame {
private javax.swing.JPanel tradeboxPanel;
private javax.swing.JScrollPane tradeboxScrollPane;
private javax.swing.JTable tradeboxTable;
private javax.swing.JPanel tradeitemPanel;
private javax.swing.JLabel usernameLoginLabel;
private javax.swing.JTextField usernameLoginTextField;
private javax.swing.JLabel usernameRegisterLabel;
......
......@@ -202,6 +202,20 @@ public class TCPClient {
return obj;
}
public JSONObject acceptJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "accept");
obj.put("token", token);
obj.put("offer_token", offer_token);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
public JSONObject cancelofferJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
......@@ -536,6 +550,31 @@ public class TCPClient {
return responseJSON;
}
public JSONObject accept(String token,String offer_token) throws Exception
{
openConnection();
System.out.println("Accept");
String toSend = acceptJSON(token, offer_token).toString();
//throw ioexception
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver accept exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject findoffer(String token, int id_item) throws Exception
{
openConnection();
......@@ -609,11 +648,11 @@ public class TCPClient {
closeConnection();
return responseJSON;
}
// public static void main(String argv[]) throws Exception
// {
// TCPClient client = new TCPClient("localhost", 6789);
//
public static void main(String argv[]) throws Exception
{
TCPClient client = new TCPClient("localhost", 6789);
// do {
// BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
// DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
......@@ -715,30 +754,22 @@ public class TCPClient {
// } while(true);
// } while(!command.equalsIgnoreCase("exit"));
//
// JSONObject response = client.login("ayam", "ayam");
// if(response.getString("status").equals("ok")) {
// client.token = response.getString("token");
// client.x=response.getInt("x");
// client.y=response.getInt("y");
// }
// System.out.println(response.toString());
//
// response = client.map();
// System.out.println(response.toString());
//
// response = client.offer(client.token, 0, 1, 7, 3);
// System.out.println(response.toString());
//
// response = client.findoffer(client.token, 2);
// System.out.println(response.toString());
//
// response = client.accept(client.token, "a");
// System.out.println(response.toString());
//
// response = client.tradebox(client.token);
// System.out.println(response.toString());
// }
//
//
//
JSONObject response = client.login("ayam", "ayam");
if(response.getString("status").equals("ok")) {
client.token = response.getString("token");
client.x=response.getInt("x");
client.y=response.getInt("y");
}
System.out.println(response.toString());
response = client.map();
System.out.println(response.toString());
response = client.offer(client.token, 0, 1, 7, 3);
System.out.println(response.toString());
response = client.sendFind(client.token, 2);
System.out.println(response.toString());
}
}
#Tue, 21 Apr 2015 17:07:39 +0700
#Wed, 22 Apr 2015 16:28:23 +0700
C\:\\Users\\Stephen\\Documents\\NetBeansProjects\\TubesSister\\src\\server=
compile.on.save=true
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
......@@ -38,3 +38,6 @@ user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.pro
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
=======
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
>>>>>>> 5c393c17c0f1ef1dd5ee2823322f7f8ff51533ad
......@@ -396,7 +396,7 @@ public class MySQLAccess {
return response;
}
public JSONObject findOffer(int userID,int id_item) throws JSONException {
public JSONObject findOffer(int id_item) throws JSONException {
open();
JSONObject response = new JSONObject();
......@@ -429,6 +429,99 @@ public class MySQLAccess {
return response;
}
public JSONObject accept(int userID, String offer_token) throws JSONException
{
open();
JSONObject response = new JSONObject();
try {
//cek offer dengan offer token yang sesuai
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
boolean available = resultSet.getBoolean("available");
//cek apakah offer tersedia
if(available){
//int id_offer = resultSet.getInt("id_offer");
//simpan variabel offer
int offered_item = resultSet.getInt("offered_item");
int N_offered = resultSet.getInt("n1");
int demanded_item = resultSet.getInt("demanded_item");
int N_demanded = resultSet.getInt("n2");
//mencari item sesuai dengan item yang diinginkan oleh penawar item
preparedStatement = connect.prepareStatement("SELECT item_name FROM items WHERE id_item =" + demanded_item + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first())
{
String itemDemandedName = resultSet.getString("item_name").toLowerCase();
preparedStatement = connect.prepareStatement("SELECT * FROM inventory WHERE id_user="
+ userID + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first())
{
int N_item = resultSet.getInt(itemDemandedName);
//cek kondisi item yang diinginkan penawar apakah cukup di inventory
if(N_item >= N_demanded ){
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ itemDemandedName + "="
+ itemDemandedName + "-" + N_demanded
+ " WHERE id_user=" + userID + ";");
preparedStatement.executeUpdate();
//menambahkan item yang ditawarkan oleh penawar ke dalam inventory user
preparedStatement = connect.prepareStatement("SELECT item_name FROM items WHERE id_item =" + offered_item + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first())
{
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ resultSet.getString("item_name").toLowerCase() + "="
+ resultSet.getString("item_name").toLowerCase() + "+" + N_offered
+ " WHERE id_user=" + userID + ";");
preparedStatement.executeUpdate();
}
//hapus data offer yang akan diterima
//menjadi tidak available
preparedStatement = connect.prepareStatement("UPDATE offer SET available = 0" + " WHERE "
+ "offer_token=\"" + offer_token + "\";");
preparedStatement.executeUpdate();
response.put("status", "ok");
}
else // jumlah item di inventory < dari jumlah yang diinginkan penawar
{
//menjadi available kembali karena fail
preparedStatement = connect.prepareStatement("UPDATE offer SET available = 1" + " WHERE "
+ "offer_token=\"" + offer_token + "\";");
preparedStatement.executeUpdate();
response.put("status", "fail");
response.put("description", "Can not accept, item in inventory not enough");
}
}
}
}
else //not available
{
response.put("status", "fail");
response.put("description", "Can not accept, offer not available");
}
}
else
{
response.put("status", "error");
}
} catch (SQLException e) {
response.put("status", "error");
e.printStackTrace();
}
close();
return response;
}
public JSONObject fetchitem(int userID, String offer_token) throws JSONException
{
open();
......
......@@ -15,21 +15,24 @@ public class TCPServer implements Runnable {
public ServerSocket serverTrackerSocket;
public Socket clientSocket;
public Socket trackerSocket;
public int serverPort;
public MySQLAccess sql;
public String clientRequest;
public String trackerRequest;
public String token;
public String thisIP;
public int userID;
public int width;
public int height;
public static String[][] map;
public List<Host> servers;
public TCPServer(int port) throws IOException, JSONException {
public TCPServer() throws IOException, JSONException {
System.out.println("Server running!");
serverClientSocket = new ServerSocket(port);
serverPort = 6789;
serverClientSocket = new ServerSocket(serverPort);
servers = new ArrayList<Host>();
thisIP = "167.205.71.56";
sql = new MySQLAccess();
}
......@@ -189,9 +192,19 @@ public class TCPServer implements Runnable {
}
}
public JSONObject accept(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.accept(userID, clientObject.getString("offer_token"));
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
return responseJSON;
}
}
public JSONObject findoffer(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.findOffer(userID, clientObject.getInt("item"));
return sql.findOffer(clientObject.getInt("item"));
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
......@@ -219,6 +232,52 @@ public class TCPServer implements Runnable {
}
}
public JSONObject sendfind(JSONObject clientObject) throws JSONException, UnknownHostException, IOException {
System.out.println("masuk find");
if(clientObject.getString("token").equalsIgnoreCase(token)) {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "ok");
JSONArray offers = new JSONArray();
for(Host server : servers) {
if(!server.ipAddress.equalsIgnoreCase("167.205.32.46") && server.port!=8000) {
if(!server.ipAddress.equalsIgnoreCase(thisIP) && server.port!=serverPort) {
System.out.println("a");
JSONObject tempRequest = new JSONObject();
tempRequest.put("method", "findoffer");
tempRequest.put("item", clientObject.getString("item"));
Socket tempsocket = new Socket(server.ipAddress, server.port);
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(trackerSocket.getOutputStream());
outToServer.writeBytes(tempRequest.toString() + '\n');
} catch (IOException ex) {
ex.printStackTrace();
}
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(trackerSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject tempResponse = new JSONObject(response);
System.out.println("hahaha");
if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
System.out.println("abcd");
responseJSON.put("offers", tempResponse.getJSONArray("offers"));
}
tempsocket.close();
}
}
}
return responseJSON;
} else {
System.out.println("error oi");
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
return responseJSON;
}
}
public JSONObject serverstatus(JSONObject clientObject) throws JSONException {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "ok");
......@@ -241,26 +300,26 @@ public class TCPServer implements Runnable {
// Test join tracker, ambil ip dari wifi
boolean joined = false;
int count = 1;
do {
//do {
try {
join("192.168.1.102", 6790);
join(thisIP, serverPort);
joined = true;
} catch(Exception e) {
System.out.println(count + ". Tracker not responding, retrying in 5 seconds");
count++;
try {
Thread.sleep(5000);
} catch (Exception ex) {}
// System.out.println(count + ". Tracker not responding, retrying in 5 seconds");
// count++;
// try {
// Thread.sleep(5000);
// } catch (Exception ex) {}
}
} while(!joined);
//} while(!joined);
while(true) {
int port = 6790;
int port = 6777;
try {
// Menunggu koneksi dari tracker
serverTrackerSocket = new ServerSocket(port);
while(true) {
// Menunggu koneksi dari tracker
System.out.println("Waiting for tracker to connect..");
serverTrackerSocket = new ServerSocket(port);
trackerSocket = serverTrackerSocket.accept();
System.out.println("Tracker connected!");
......@@ -289,8 +348,7 @@ public class TCPServer implements Runnable {
public static void main(String[] args) throws Exception {
int port = 6789;
TCPServer server = new TCPServer(port);
TCPServer server = new TCPServer();
// Starts a thread that listens to tracker
Thread trackerListener =new Thread(server);
......@@ -365,11 +423,22 @@ public class TCPServer implements Runnable {
JSONObject responseJSON = server.fetchitem(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
}
else if(method.equalsIgnoreCase("findoffer")) {
} else if(method.equalsIgnoreCase("findoffer")) {
JSONObject responseJSON = server.findoffer(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("sendfind")) {
JSONObject responseJSON = server.sendfind(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("accept")) {
JSONObject responseJSON = server.accept(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("serverStatus")) {
JSONObject responseJSON = server.serverstatus(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
}
}
}
......
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