Commit 5463bea6 authored by Muhammad Yafi's avatar Muhammad Yafi
Browse files

change sendfind to multi thread

parent ec8eca4b
......@@ -6,6 +6,7 @@ import select
import codecs
from user import User
from map import Map
from threading import Thread
from ssocketio import SocketIO
......@@ -211,47 +212,56 @@ class ConnectionHandler:
def handle_sendfind(self,token,item):
#tambahin server abusive
offer_list = []
thread_list = []
# untuk setiap server lain
for other_server in self._server._server_list:
#bikin koneksi server
socket = Socket.socket()
socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
host = other_server.get('ip')
port = other_server.get('port')
# kalo ternyata tracker jangan direquest
if ((host == self._server.__class__.TRACKER_IP) and (port == self._server.__class__.TRACKER_PORT)):
continue
print('connecting to ',host,':',port)
socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
try:
socket.connect((host,port))
except Exception as e:
print('Error connecting to',host,':',port)
print(e)
continue
socketio = SocketIO(socket)
socketio.write_to_socket((json.dumps({'method':'findoffer','item':item})).encode('ascii'))
print('read offering from ',host,':',port)
#catch connection timeout
try:
data = socketio.read_from_socket()
except Exception as e:
print(e)
continue
if (data != None):
try:
response = json.loads(data.decode('utf-8'))
server_offer_list = response.get('offers',[])
print('Result : ',end='')
print(server_offer_list)
offer_list.extend(server_offer_list)
except ValueError as ve:
i = 1 # do nothing
port = int(other_server.get('port'))
thr = FindOfferThread(host,port,item)
thread_list.append(thr)
thr.run()
# #bikin koneksi server
# socket = Socket.socket()
# socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
# host = other_server.get('ip')
# port = other_server.get('port')
# # kalo ternyata tracker jangan direquest
# if ((host == self._server.__class__.TRACKER_IP) and (port == self._server.__class__.TRACKER_PORT)):
# continue
# print('connecting to ',host,':',port)
# socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
# try:
# socket.connect((host,port))
# except Exception as e:
# print('Error connecting to',host,':',port)
# print(e)
# continue
# socketio = SocketIO(socket)
# socketio.write_to_socket((json.dumps({'method':'findoffer','item':item})).encode('ascii'))
# print('read offering from ',host,':',port)
# #catch connection timeout
# try:
# data = socketio.read_from_socket()
# except Exception as e:
# print(e)
# continue
# if (data != None):
# try:
# response = json.loads(data.decode('utf-8'))
# server_offer_list = response.get('offers',[])
# print('Result : ',end='')
# print(server_offer_list)
# offer_list.extend(server_offer_list)
# except ValueError as ve:
# i = 1 # do nothing
for thr in thread_list:
thr.join()
for thr in thread_list:
offer_list.extends(thr.get_result())
data = json.dumps({'status':'ok','offers':offer_list})
try:
......@@ -334,4 +344,48 @@ class ConnectionHandler:
# # tulis data ke socket
# # be sure to handle socket exception
# def write_to_socket(self,data):
# recv = self._conn.send(data + b'\n')
\ No newline at end of file
# 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)
self._result = []
def run(self):
try:
self.socket.connect((host,port))
except Exception as e:
print('Error connecting to',host,':',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)
#catch connection timeout
try:
data = socketio.read_from_socket()
except Exception as e:
print(e)
return
if (data != None):
try:
response = json.loads(data.decode('utf-8'))
server_offer_list = response.get('offers',[])
print('Result : ',end='')
print(server_offer_list)
self._result = []
except ValueError as ve:
return
def get_result(self):
return self._result
......@@ -77,7 +77,7 @@ class Server:
print('Tracker error')
print(e)
return
print('tracker connected')
tracker_io = SocketIO(socket)
tracker_io.write_to_socket((json.dumps({'method':'join','ip':ip_address,'port':port})).encode('ascii'))
......
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