Commit 26a69ca0 authored by Timothy Pratama's avatar Timothy Pratama
Browse files

Merge branch 'master' of...

Merge branch 'master' of http://gitlab.informatika.org/timothy.pratama/IF3230-Tugas-Besar-Sister-2015.git
parents 9496ebe1 86f654de
This diff is collapsed.
__author__ = 'Willy'
from pymongo import MongoClient
from inventory import Inventory;
import time
class OfferMarket:
......@@ -18,7 +18,7 @@ class OfferMarket:
class Market:
time = 0
__time = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
__market = MongoClient('localhost', 27017)['sister']\
.get_collection("market")
......@@ -40,6 +40,7 @@ class Market:
'ip': ip, 'port': port})
@staticmethod
def clear(item_id):
Market.__time[item_id] = time.time()
Market.__market.delete_many({'offered_item': item_id})
@staticmethod
......@@ -53,4 +54,8 @@ class Market:
@staticmethod
def exist(token):
return Market.__market.find_one_and_replace({'offer_token': token}, {'availability': False}) != None
\ No newline at end of file
return Market.__market.find({'offer_token': token}, {'availability': False}) != None
@staticmethod
def dirty(item):
return time.time() - Market.__time[item] > 30
\ No newline at end of file
......@@ -3,7 +3,6 @@ import time
import json
import socket as Socket
import select
import codecs
from user import User
from map import Map
from tradebox import Tradebox
......@@ -16,10 +15,10 @@ class ConnectionHandler:
TRACKER_IP = '167.205.32.46'
TRACKER_PORT = 8000
BUFFER_SIZE = 4096
READ_TIMEOUT = 3 # buat ngehandle json yang belum selesai kekirim
CONNECTION_TIMEOUT = 5 # dalam detikkalo client gak ngirim ping apa2 ke server, bisa dianggap timeout
def __init__(self,conn,addr,server_instance):
self._conn = conn #client socket
self._addr = addr #client address
......@@ -99,7 +98,7 @@ class ConnectionHandler:
elif (method == 'sendaccept'):
self.handle_sendaccept(data.get('token',''),data.get('offer_token',''))
elif (method == 'accept'):
self.handle_accept(data.get('token',''))
self.handle_accept(data.get('offer_token',''))
elif (method == 'fetchitem'):
self.handle_fetchitem(data.get('token',''),data.get('offer_token',''))
elif (method == 'canceloffer'):
......@@ -257,24 +256,25 @@ class ConnectionHandler:
# invoke sendfind
def handle_sendfind(self,token,item):
#tambahin server abusive
Market.clear(item)
offer_list = []
thread_list = []
# untuk setiap server lain
for other_server in self._server._server_list:
host = other_server.get('ip')
port = int(other_server.get('port'))
if (not ((host == self._server.__class__.TRACKER_IP) and (port == self._server.__class__.TRACKER_PORT))):
thr = FindOfferThread(host,port,item)
thread_list.append(thr)
thr.start()
for thr in thread_list:
thr.join()
for thr in thread_list:
# print(thr.get_result())
offer_list.extend(thr.get_result())
if True:#Market.dirty(item):
Market.clear(item)
for other_server in self._server._server_list:
host = other_server.get('ip')
port = int(other_server.get('port'))
if (not ((host == self._server.__class__.TRACKER_IP) and (port == self._server.__class__.TRACKER_PORT))):
thr = FindOfferThread(host,port,item)
thread_list.append(thr)
thr.start()
for thr in thread_list:
thr.join()
for thr in thread_list:
# print(thr.get_result())
offer_list.extend(thr.get_result())
offer_list = Market.get_offers(item)
my_offers = Tradebox.get_offers(token)
......@@ -374,6 +374,7 @@ class ConnectionHandler:
if (target_response.get('status') == 'ok'):
user.inventory.items[offer.demanded_id] -= offer.demanded_amount
user.inventory.items[offer.offered_id] += offer.offered_amount
user.save()
data = (json.dumps({'status':'ok'})).encode('ascii')
#call db
elif (target_response.get('status') == 'fail'):
......@@ -389,13 +390,15 @@ class ConnectionHandler:
# invoke map
def handle_fetchitem(self,token,offer_token):
print(token+' '+offer_token)
offer = Tradebox.find_offer_id(offer_token)
if (offer.user_id == token) and (offer.availability):
if (offer.user_id == token) and (not offer.availability):
user = User(token)
user.inventory.items[offer.demanded_id] += offer.demanded_amount
user.save()
offer.availability = False
offer.save()
offer.delete()
data = json.dumps({'status':'ok'})
else:
data = json.dumps({'status':'fail','description': 'FAILURE MESSAGE HERE'})
......
......@@ -57,7 +57,7 @@ class Tradebox():
@staticmethod
def exist(token):
return Tradebox.__market.find_one({'_id': ObjectId(token)}) != None
return Tradebox.__market.find({'_id': ObjectId(token)}).count() > 0
if __name__ == '__main__':
# MongoClient('localhost', 27017)['sister'].create_collection('tradebox')
......
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