Commit 965b69e9 authored by Akhmad Fakhoni Listiyan Dede's avatar Akhmad Fakhoni Listiyan Dede
Browse files

accept function ok, not tested, dan misahin fungsi2 send (dari server ke...

accept function ok, not tested, dan misahin fungsi2 send (dari server ke server lain) ke kelas sender
parent 0c8c324f
......@@ -94,7 +94,8 @@ public class Database {
//
// System.out.println(saveOfferToTradeBox("553b6adc3c892b2edffa877c", 4, 1, 5, 3));
getTradebox("553b64d23c892b2d5b772c74");
// getTradebox("553b64d23c892b2d5b772c74");
changeLocalTradeboxToFalse("553c36143c892b1bbf7723d6");
}
/**
......@@ -319,7 +320,7 @@ public class Database {
.append("n1", nOffered)
.append("demanded_item", indexDemandedItem)
.append("n2", nDemanded)
.append("availability", false);
.append("availability", true);
collection.insertOne(offerDoc);
//Update inventory
......@@ -452,7 +453,34 @@ public class Database {
return allOffer;
}
public static boolean isAvailable(String offer_token)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
Document offerDoc = collection.find(eq("_id", new ObjectId(offer_token))).first();
try {
JSONObject jObj = new JSONObject(offerDoc.toJson());
return jObj.getBoolean("availability");
} catch (JSONException ex) {
return false;
}
}
/**
* Dipanggil oleh acceptAcceptFromAnotherServer
* @param offer_token
* @return
*/
public static boolean changeLocalTradeboxToFalse(String offer_token)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
try{
collection.updateOne(eq("_id", new ObjectId(offer_token)), new Document("$set", new Document("availability", false)));
return true;
}catch (MongoException ex)
{
return false;
}
}
/**
* Mengembalikan token, username, dan password dari username yang dimasukkan
* @param username
......
......@@ -181,6 +181,16 @@ public class Message {
response = sendFind(token, item);
break;
}
case "findoffer":
{
int item = jsonObj.getInt("item");
response = findOffer(item);
break;
}
case "sendaccept":
{
break;
}
case "serverStatus":
response = serverStatus(jsonString);
break;
......@@ -594,7 +604,7 @@ public class Message {
}
/**
* Find offer sesuai dengan item id yang diinginkan. Dikirim oleh client.
* Mencari offer sesuai dengan item id yang diinginkan. Dikirim oleh client.
* @param token
* @param itemId
* @return
......@@ -632,50 +642,70 @@ public class Message {
//tradebox
//sendfind
/**
* Send find offer to another server. Untuk menemukan semua offer yang tersedia dari semua server
* @param item
* Fungsi untuk catch method find offer dari server lain
* @param itemIdx
* @return
*/
public static String sendFindOffer(int item) //12
public static String findOffer(int itemIdx) //12
{
JSONObject jsonObj = new JSONObject();
String response;
JSONObject jsonObj = new JSONObject();
try
{
jsonObj.put("method", "sendfind");
jsonObj.put("item", item);
jsonObj.put("status", "ok");
jsonObj.put("offers", Database.getTradebox(itemIdx));
response = jsonObj.toString();
}
catch(JSONException ex)
}catch(JSONException | NullPointerException ex)
{
System.out.println("getFindOffer() Error: " + jsonObj.toString());
response = error();
}
if(log)
{
System.out.println(response);
}
return response;
}
/**
* Fungsi untuk catch method find offer dari server lain
* @param itemIdx
*
* @param token Token dari user yang mengirim sendAccept
* @param offer_token Offer-token yang diaccept oleh user pemilik token
* @return
*/
public static String getFindOffer(int itemIdx) //12
public static String sendAccept(String token, String offer_token) //13
{
//berhasil setelah pengecekan Accept berhasil
//Get offernya
//change offernya pake jadi false
//if(accept ok)
return "";
}
/**
* Fungsi saat server lain kirim Accept ke server ini
* @param offerToken
* @return
*/
public static String accept(String offerToken) //14
{
String response;
JSONObject jsonObj = new JSONObject();
try
//Ubah status tradebox (dengan id = offerToken) jadi false
if(Database.isAvailable(offerToken)) //Jika tradebox dengan id=offerToken
{
jsonObj.put("status", "ok");
jsonObj.put("offers", Database.getTradebox(itemIdx));
response = jsonObj.toString();
}catch(JSONException ex)
if(Database.changeLocalTradeboxToFalse(offerToken))
{
response = statusOKOnly();
}
else
{
response = fail("offer is not available");
}
}
else
{
System.out.println("getFindOffer() Error: " + jsonObj.toString());
response = error();
response = fail("offer is not available");
}
return response;
}
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package server.helper;
import org.json.JSONException;
import org.json.JSONObject;
import static server.helper.Message.error;
/**
*
* @author akhfa
*/
public class Sender {
private static boolean log = false;
/**
* Mengirim findoffer to another server. Untuk menemukan semua offer yang tersedia dari semua server
* @param item
* @return
*/
public static String sendFindOfferToAnotherServer(int item) //12
{
JSONObject jsonObj = new JSONObject();
String response;
try
{
jsonObj.put("method", "findoffer");
jsonObj.put("item", item);
response = jsonObj.toString();
}
catch(JSONException ex)
{
System.out.println("sendFindOfferToAnotherServer Exeption");
response = error();
}
if(log)
{
System.out.println(response);
}
return response;
}
/**
* Fungsi untuk mengirim sinyal Accept ke server dimana offer_token berasal
* @param offerToken
* @return
*/
public static String sendAcceptToAnotherServer(String offerToken)
{
String result;
try{
JSONObject jObj = new JSONObject();
jObj.put("method", "accept");
jObj.put("offer_token", offerToken);
result = jObj.toString();
}
catch(JSONException ex)
{
System.out.println("sendAcceptToAnotherServer Exeption");
result = error();
}
return result;
}
}
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