Commit 4f13966c authored by Khoirunnisa Afifah's avatar Khoirunnisa Afifah
Browse files

Merge branch 'master' of...

Merge branch 'master' of http://gitlab.informatika.org/sinambelajunita/IF3230-Tugas-Besar-Sister-2015
parents 989f6019 8144ed99
......@@ -61,21 +61,21 @@ public class Server {
Socket trackerSocket = null;
try {
trackerSocket = new Socket("167.205.32.46", 8000);
ServerLogger.log("SERVER_THREAD", "Sending request join");
ServerLogger.log("MAIN_THREAD", "Sending request join");
while(!joiningTracker(trackerSocket)){
ServerLogger.log("SERVER_THREAD", "Sending request join (again)");
ServerLogger.log("MAIN_THREAD", "Sending request join (again)");
}
ServerSocket welcomeSocket = null;
Socket clientSocket = null;
welcomeSocket = new ServerSocket(6789);
ServerLogger.log("SERVER_THREAD", "Start listening to new connection");
ServerLogger.log("MAIN_THREAD", "Start listening to new connection");
while(true){
clientSocket = welcomeSocket.accept();
ServerLogger.log("SERVER_THREAD", "New connection accepted");
ServerLogger.log("MAIN_THREAD", "New connection accepted");
new Thread(new ClientHandler(clientSocket)).start();
}
} catch (IOException | JSONException ex) {
ServerLogger.log("SERVER_THREAD", "Failed");
ServerLogger.log("MAIN_THREAD", "Failed");
System.out.println(ex.getMessage());
ex.printStackTrace();
}
......
......@@ -40,7 +40,9 @@ public class ServerHandler{
request = new String(byt);
if(response != null) {
// request = request.substring(0,count);
listResponses.add(response);
synchronized(this){
addResponse(response);
}
}
s.close();
} catch (IOException ex) {
......@@ -49,6 +51,9 @@ public class ServerHandler{
ServerLogger.log("SERVER_THREAD","Terminated");
}
}
public static void addResponse(String response){
listResponses.add(response);
}
/**
* Create thread to handle request to other server and return expected response that will be given to client in string
* @param request
......
......@@ -95,7 +95,9 @@ public class Database {
// System.out.println(saveOfferToTradeBox("553b6adc3c892b2edffa877c", 4, 1, 5, 3));
// getTradebox("553b64d23c892b2d5b772c74");
changeLocalTradeboxToFalse("553c36143c892b1bbf7723d6");
// changeLocalTradeboxToFalse("553c36143c892b1bbf7723d6");
System.out.println(isExist("sijc"));
}
/**
......@@ -391,7 +393,7 @@ public class Database {
{
String oneOfferString = cursor.next().toJson();
JSONObject jsonObj = new JSONObject(oneOfferString);
if(jsonObj.getString("usertoken").equals(usertoken)
if(!jsonObj.getString("usertoken").equals(usertoken)
&& (jsonObj.getInt("offered_item") == itemIdx))
{
oneoffer = new ArrayList<>();
......@@ -416,6 +418,40 @@ public class Database {
return allOffer;
}
public static ArrayList<ArrayList<Object>> getTradebox()
{
MongoCollection<Document> collection = database.getCollection("tradebox");
MongoCursor<Document> cursor = collection.find().iterator();
ArrayList<ArrayList<Object>> allOffer;
ArrayList<Object> oneoffer;
try{
allOffer = new ArrayList<>();
while(cursor.hasNext())
{
String oneOfferString = cursor.next().toJson();
JSONObject jsonObj = new JSONObject(oneOfferString);
oneoffer = new ArrayList<>();
oneoffer.add(jsonObj.getInt("offered_item"));
oneoffer.add(jsonObj.getInt("n1"));
oneoffer.add(jsonObj.getInt("demanded_item"));
oneoffer.add(jsonObj.getInt("n2"));
oneoffer.add(jsonObj.getBoolean("availability"));
oneoffer.add(getJsonObjId(jsonObj.toString()));
allOffer.add(oneoffer);
}
if(allOffer.isEmpty())
return null;
} catch (JSONException | NullPointerException ex) {
return null;
}
finally
{
cursor.close();
}
return allOffer;
}
public static ArrayList<ArrayList<Object>> getTradebox(int itemIdx)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
......@@ -470,6 +506,20 @@ public class Database {
}
}
public static boolean isExist(String offer_token)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
try{
Document offerDoc = collection.find(eq("_id", new ObjectId(offer_token))).first();
return offerDoc != null;
}
catch(Exception ex)
{
return false;
}
}
/**
* Dipanggil oleh acceptAcceptFromAnotherServer
* @param offer_token
......
......@@ -581,7 +581,7 @@ public class Message {
String response;
JSONObject jsonAllOffers;
try{
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox(token);
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox();
jsonAllOffers = new JSONObject();
if(allOffers != null)
{
......@@ -616,6 +616,11 @@ public class Message {
JSONObject jsonAllOffers;
JSONObject jsonObj;
try{
jsonObj = new JSONObject();
jsonObj.put("method", "findoffer");
jsonObj.put("item", itemId);
response = ServerHandler.broadcast(jsonObj.toString());
//buat json b
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox(token,itemId);
jsonAllOffers = new JSONObject();
if(allOffers != null)
......@@ -626,11 +631,7 @@ public class Message {
}
else
{
jsonObj = new JSONObject();
jsonObj.put("method", "findoffer");
jsonObj.put("item", itemId);
response = ServerHandler.broadcast(jsonObj.toString());
//tangani kalau mau dimasukin ke database
response = error();
}
}
......@@ -643,13 +644,19 @@ public class Message {
return response;
}
//offer
//tradebox
//sendfind
/**
* Accept offer sesuai dengan item id yang diinginkan. Dikirim oleh client.
* @param token
* @param offer_token
* @return
*/
public static String sendAccept(String token, String offer_token) //String token, int itemId) //11
{
String response = null;
if(log)
System.out.println(response);
return response;
}
/**
* Fungsi untuk catch method find offer dari server lain
* @param itemIdx
......@@ -664,41 +671,13 @@ public class Message {
jsonObj.put("status", "ok");
jsonObj.put("offers", Database.getTradebox(itemIdx));
response = jsonObj.toString();
}catch(JSONException | NullPointerException ex)
}catch(JSONException ex)
{
System.out.println("getFindOffer() Error: " + jsonObj.toString());
response = error();
}
if(log)
{
System.out.println(response);
}
return response;
}
public static String sendAccept() //String token, int itemId) //11
{
String response = null;
if(log)
System.out.println(response);
return response;
}
/**
*
* @param token Token dari user yang mengirim sendAccept
* @param offer_token Offer-token yang diaccept oleh user pemilik token
* @return
*/
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
......@@ -708,9 +687,11 @@ public class Message {
public static String accept(String offerToken) //14
{
String response;
JSONObject jsonObj;
//Ubah status tradebox (dengan id = offerToken) jadi false
if(Database.isAvailable(offerToken)) //Jika tradebox dengan id=offerToken
{
jsonObj = new JSONObject();
if(Database.changeLocalTradeboxToFalse(offerToken))
{
response = statusOKOnly();
......
......@@ -6,11 +6,7 @@
package server.helper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
/**
*
......@@ -65,7 +61,7 @@ public class Store {
* Update servers list from tracker
* @param list
*/
public static void updateServers(List<IPAddress> list){
public static synchronized void updateServers(List<IPAddress> list){
listServer = list;
}
/**
......
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