Commit 687d7e5c authored by Tony's avatar Tony
Browse files

Server to server Done(SendFind & Accept)

parent 89549d24
......@@ -35,7 +35,7 @@ server = db.server
market = db.market
PORT = 8000
IP = str(socket.gethostbyname(socket.gethostname()))
IP = "100.100.101.16"
#Join procedure
def join():
......@@ -302,7 +302,7 @@ def clientThread(conn):
if (userQuery == None): #Invalid Request, token mismatch
reply = {"status": "error"}
else: #valid request
serverList = server.find({"ip": {"$ne": IP}, "port": {"$ne": PORT}})
serverList = server.find({"$or": [{"ip": {"$ne": str(IP)}}, {"port": {"$ne": 8000}}]})
#Request all offer in another servers
for serve in serverList:
......@@ -335,7 +335,7 @@ def clientThread(conn):
if (res['status'] == 'ok'): #sendfind ok!
#update market
if (market.count() > 0):
market.remove({"offered_item": decoded['item'], "ip": str(serve['ip']), "port": int(serve['port'])})
market.remove({"offered_item": decoded['item'], "server.ip": serve['ip'], "server.port": serve['port']})
for off in res['offers']:
market.insert({ "offer_token": off[5],
"offered_item": off[0],
......@@ -367,7 +367,7 @@ def clientThread(conn):
#FindOffer(server)
elif (decoded['method'] == 'findoffer'):
allOffer = offer.find({"offered_item": decoded['item']})
allOffer = offer.find({"offered_item": decoded['item'], "availability": True})
offers = []
for off in allOffer:
singleOffer = [int(off['offered_item']),int(off['n_offered_item']),int(off['demanded_item']),int(off['n_demanded_item']),off['availability'],str(off['offer_token'])]
......@@ -404,8 +404,7 @@ def clientThread(conn):
print ('Socket Created')
#connect socket
sock_serv.connect((str(marketOff['ip']) , int(marketOff['port'])))
sock_serv.connect((str(marketOff['server']['ip']) , marketOff['server']['port']))
request = {"method": "accept", "offer_token": decoded['offer_token']}
try :
sock_serv.sendall(bytes(json.dumps(request), 'UTF-8'))
......@@ -422,12 +421,11 @@ def clientThread(conn):
#closing socket
sock_serv.close()
#Update database
if (market.count() > 0):
market.remove({'offer_token': decoded['offer_token']})
user.update({'_id': ObjectId(decoded['token'])},{"$inc": {'inventory.'+str(off['demanded_item']): -off['n_demanded_item'],
'inventory.'+str(off['offered_item']): off['n_offered_item']}})
user.update({'_id': ObjectId(decoded['token'])},{"$inc": {'inventory.'+str(marketOff['demanded_item']): -marketOff['n_demanded_item'],
'inventory.'+str(marketOff['offered_item']): marketOff['n_offered_item']}})
market.remove({'offer_token': marketOff['offer_token']})
reply = {"status": "ok"}
else: #insufficient item
reply = {"status": "fail", "description": "you have insufficient item"}
......@@ -551,7 +549,7 @@ if __name__ == '__main__':
#Bind socket to local host and port
try:
sock.bind(('localhost', PORT))
sock.bind((IP, PORT))
except (socket.error):
print ('Bind failed')
sys.exit()
......
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