Commit 89549d24 authored by Tony's avatar Tony
Browse files

Fix sendfind (server to server)

parent f60620e9
...@@ -314,6 +314,7 @@ def clientThread(conn): ...@@ -314,6 +314,7 @@ def clientThread(conn):
print ('Failed to create socket') print ('Failed to create socket')
sock_serv.connect((serve['ip'] , serve['port'])) sock_serv.connect((serve['ip'] , serve['port']))
request = {"method": "findoffer", "item": decoded['item']} request = {"method": "findoffer", "item": decoded['item']}
#Sending request #Sending request
...@@ -322,7 +323,6 @@ def clientThread(conn): ...@@ -322,7 +323,6 @@ def clientThread(conn):
except socket.error: #Send Failed except socket.error: #Send Failed
print ('Send failed') print ('Send failed')
sys.exit() sys.exit()
#Receiving response #Receiving response
try: try:
response = sock_serv.recv(4096) response = sock_serv.recv(4096)
...@@ -331,18 +331,20 @@ def clientThread(conn): ...@@ -331,18 +331,20 @@ def clientThread(conn):
sys.exit() sys.exit()
res = json.loads(response.decode('UTF-8')) res = json.loads(response.decode('UTF-8'))
print(res)
if (res['status'] == 'ok'): #sendfind ok! if (res['status'] == 'ok'): #sendfind ok!
#update market #update market
market.remove({"offered_item": decoded['item'], "ip": serve['ip'], "port": serve['port']}) if (market.count() > 0):
market.remove({"offered_item": decoded['item'], "ip": str(serve['ip']), "port": int(serve['port'])})
for off in res['offers']: for off in res['offers']:
market.insert({ "offer_token": res[off[5]], market.insert({ "offer_token": off[5],
"offered_item": res[off[0]], "offered_item": off[0],
"n_offered_item": res[off[1]], "n_offered_item": off[1],
"demanded_item": res[off[2]], "demanded_item": off[2],
"n_demanded_item": res[off[3]], "n_demanded_item": off[3],
"availability": True, "availability": True,
"server": {"ip": str(serve['ip']),"port": serve['port']}}) "server": {"ip": str(serve['ip']),"port": int(serve['port'])}})
elif (res['status'] == 'error'): #sendfind error elif (res['status'] == 'error'): #sendfind error
print("Error response from server "+ str(serve['ip']) + ":" + str(serve['port'])) print("Error response from server "+ str(serve['ip']) + ":" + str(serve['port']))
...@@ -402,9 +404,9 @@ def clientThread(conn): ...@@ -402,9 +404,9 @@ def clientThread(conn):
print ('Socket Created') print ('Socket Created')
#connect socket #connect socket
sock_serv.connect((marketOff['ip'] , marketOff['port'])) sock_serv.connect((str(marketOff['ip']) , int(marketOff['port'])))
request = {"method": "accept", "offer_token": str(decoded['offer_token'])} request = {"method": "accept", "offer_token": decoded['offer_token']}
try : try :
sock_serv.sendall(bytes(json.dumps(request), 'UTF-8')) sock_serv.sendall(bytes(json.dumps(request), 'UTF-8'))
except socket.error: #send failed except socket.error: #send failed
...@@ -422,7 +424,8 @@ def clientThread(conn): ...@@ -422,7 +424,8 @@ def clientThread(conn):
sock_serv.close() sock_serv.close()
#Update database #Update database
market.remove({'offer_token': decoded['offer_token']}) 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'], 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']}}) 'inventory.'+str(off['offered_item']): off['n_offered_item']}})
reply = {"status": "ok"} reply = {"status": "ok"}
......
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