Commit b1c9f450 authored by Muhammad Yafi's avatar Muhammad Yafi
Browse files

fixing multithread findoffer

parent 5463bea6
......@@ -25,10 +25,10 @@ response = invoke(json.dumps({'method':'login','username':'dummy','password':'du
token = response.get('token')
print(response)
print('')
print('invoking map')
response = invoke(json.dumps({"method":"map","token":token}))
move_list = []
# print('')
# print('invoking map')
# response = invoke(json.dumps({"method":"map","token":token}))
# move_list = []
# for x in range(0,response.get('width')):
# for y in range(0,response.get('height')):
# move_list.append((x,y))
......@@ -41,12 +41,12 @@ move_list = []
# response = invoke(json.dumps({'method':'move','token':token,'x':x,'y':y}))
# print(response)
print('')
print('invoking field')
response = invoke(json.dumps({"method": "field","token":token}))
print(response)
response = invoke(json.dumps({"method": "field","token":token}))
print(response)
# print('')
# print('invoking field')
# response = invoke(json.dumps({"method": "field","token":token}))
# print(response)
# response = invoke(json.dumps({"method": "field","token":token}))
# print(response)
print('')
print('invoking sendfind')
......@@ -66,7 +66,7 @@ print(response)
print('')
print('invoking offer')
response = invoke(json.dumps({"method": "offer","token": token,"offered_item": 2,"n1": 1,"demanded_item": 1,"n2": 1}))
response = invoke(json.dumps({"method": "offer","token": token,"offered_item": 2,"n1": 1,"demanded_item": 2,"n2": 1}))
print(response)
#invoke abusive client
......
......@@ -218,9 +218,10 @@ class ConnectionHandler:
for other_server in self._server._server_list:
host = other_server.get('ip')
port = int(other_server.get('port'))
thr = FindOfferThread(host,port,item)
thread_list.append(thr)
thr.run()
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()
# #bikin koneksi server
# socket = Socket.socket()
# socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
......@@ -261,8 +262,11 @@ class ConnectionHandler:
for thr in thread_list:
thr.join()
for thr in thread_list:
offer_list.extends(thr.get_result())
print('hello')
# print(thr.get_result())
offer_list.extend(thr.get_result())
print('data')
print(offer_list)
data = json.dumps({'status':'ok','offers':offer_list})
try:
self._socketio.write_to_socket(data.encode('ascii'))
......@@ -347,25 +351,28 @@ class ConnectionHandler:
# recv = self._conn.send(data + b'\n')
class FindOfferThread(Thread):
def __init__(self,target_ip,target_port,item_id):
self.socket = Socket.socket()
self.socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
host = target_ip
port = target_port
self.socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
def __init__(self,target_ip,target_port,item):
Thread.__init__(self)
self._host = target_ip
self._port = target_port
self._item = item
self._result = []
def run(self):
socket = Socket.socket()
socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
self._result = []
print('Connect to ',self._host,':',self._port)
try:
self.socket.connect((host,port))
socket.connect((self._host,self._port))
except Exception as e:
print('Error connecting to',host,':',port)
print('Error connecting to',self._host,':',self._port)
print(e)
return
socketio = SocketIO(socket)
socketio.write_to_socket((json.dumps({'method':'findoffer','item':item})).encode('ascii'))
print('read offering from ',host,':',port)
socketio.write_to_socket((json.dumps({'method':'findoffer','item':self._item})).encode('ascii'))
print('read offering from ',self._host,':',self._port)
#catch connection timeout
try:
data = socketio.read_from_socket()
......@@ -377,9 +384,8 @@ class FindOfferThread(Thread):
try:
response = json.loads(data.decode('utf-8'))
server_offer_list = response.get('offers',[])
print('Result : ',end='')
print(server_offer_list)
self._result = []
self._result = server_offer_list
print(self._result)
except ValueError as ve:
return
......
......@@ -79,11 +79,11 @@ class Server:
return
print('tracker connected')
tracker_io = SocketIO(socket)
tracker_io.write_to_socket((json.dumps({'method':'join','ip':ip_address,'port':port})).encode('ascii'))
data = tracker_io.read_from_socket()
if (data == b''):
print('join result empty')
return None
response = json.loads(data.decode('utf-8'))
......@@ -91,7 +91,7 @@ class Server:
self._server_list = response.get('value')
print(self._server_list)
else:
print(response.get('description'))
print(response)
return None
# nanya ke server lain
......
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