Commit fab7b70f authored by Tony's avatar Tony
Browse files

Update offer_token integrity (server-server) (not tested)

parent febacbf9
......@@ -35,7 +35,8 @@ server = db.server
market = db.market
PORT = 8000
IP = "100.100.101.16"
#IP = "100.100.101.16"
IP = 'localhost'
sendAccept = True
accept = True
......@@ -75,12 +76,11 @@ def join():
try:
sendOK = True
print("Sending join request..")
message = {"method": "join", "ip": IP, "port": PORT}
sock.sendall(bytes(json.dumps(message), 'UTF-8'))
except socket.error:
print('Failed to send')
sendOK = False
message = {"method": "join", "ip": IP, "port": PORT}
sendOK = False
#receiving reply
recvOK = False
......@@ -109,7 +109,10 @@ def join():
server.remove({})
print(server.find().count())
for serv in decoded['value']:
server.insert({"ip": str(serv['ip']), "port": str(serv['port'])}) #Insert to database
if (str(serv['ip']) == '0.0.0.0' or str(serv['ip']) == '127.0.0.1'):
print("Kocak")
else:
server.insert({"ip": str(serv['ip']), "port": serv['port']}) #Insert to database
elif (decoded['status'] == "error"): #join failed
print ("Join error")
......@@ -137,13 +140,22 @@ def clientThread(conn):
if not json_input: #empty data
break
else: #data not empty
decoded = json.loads(json_input.decode('UTF-8'))
try:
decoded = json.loads(json_input.decode('UTF-8'))
except ValueError: # decode error
reply = {"status": "error"}
conn.sendall(bytes(json.dumps(reply), 'UTF-8'))
break
print("request: " + str(decoded))
#ServerStatus(tracker)
if (decoded['method'] == "serverStatus"):
server.remove({})
for serv in decoded['server']:
server.insert({"ip": str(serv['ip']), "port": str(serv['port'])}) #Insert to database
if (str(serv['ip']) == '0.0.0.0' or str(serv['ip']) == '127.0.0.1'):
print("Invalid IP")
else:
server.insert({"ip": str(serv['ip']), "port": serv['port']}) #Insert to database
reply = {"status": "ok"}
conn.sendall(bytes(json.dumps(reply), 'UTF-8'))
......@@ -155,10 +167,7 @@ def clientThread(conn):
if (userQuery == None): #user available
#Assign Map for new user
mapQuery = list(map.find())
randomMap = random.randrange(nMap)
for i in range(0,nMap):
if (i == randomMap):
selectedMap = mapQuery[i]
selectedMap = mapQuery[random.randrange(nMap)]
#Randoming items
randomItems = [2,4,8,12]
......@@ -263,7 +272,7 @@ def clientThread(conn):
reply = {"status": "fail", "description": "You are already here"}
else:
if (decoded['x'] < mapQuery["width"] and decoded['x'] >= 0 and
decoded['x'] < mapQuery["height"] and decoded['y'] >= 0): #move OK
decoded['y'] < mapQuery["height"] and decoded['y'] >= 0): #move OK
jarak = abs(decoded['x'] - userQuery['position']['x']) + abs(decoded['y'] - userQuery['position']['y'])
user.update({"_id": userQuery['_id']}, {"$set": {"time": int(timeNow + 7 * jarak), "position.x": int(decoded['x']), "position.y": int(decoded['y']), "collected": False}})
......@@ -350,7 +359,7 @@ def clientThread(conn):
connectOK = True
try:
print("Connecting to: " + str(serve['ip']) + ":" + str(serve['port']))
sock_serv.connect((serve['ip'] , serve['port']))
sock_serv.connect((str(serve['ip']) , serve['port']))
except socket.error: #Socket fail to connect
connectOK = False
print('Failed to connect to another server')
......@@ -385,13 +394,19 @@ def clientThread(conn):
if (market.count() > 0):
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],
market.update(
{"offer_token": off[5]},
{
"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": int(serve['port'])}})
"server": {"ip": str(serve['ip']),"port": int(serve['port'])}
},
{"$upsert": True}
)
elif (res['status'] == 'error'): #sendfind error
print("Error response from server "+ str(serve['ip']) + ":" + str(serve['port']))
......@@ -433,6 +448,7 @@ def clientThread(conn):
while(not sendAccept):
print("WAITING")
sendAccept = False
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
reply = {"status": "error"}
......@@ -535,6 +551,7 @@ def clientThread(conn):
global accept
while(not accept):
print("WAITING")
accept = False
off = offer.find_one({"offer_token": decoded['offer_token']})
if (offer == None) : #offer token not found
......
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