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

Fix sendfind (server to server)

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