Commit 86a77b34 authored by Muhammad Yafi's avatar Muhammad Yafi
Browse files

add sendfind

parent 6b46b87a
......@@ -14,8 +14,8 @@ except Exception as msg:
command = '{"method": "signup","username": "dummy","password": "dummy"}'
s.send(command.encode('ascii'))
print(s.recv(4096))
time.sleep(3)
command = '{"method": "signup","username": "dummy","password": "dummy"}'
time.sleep(1)
command = '{"method": "sendfind","item": 1}'
s.send(command.encode('ascii'))
print(s.recv(4096))
......
......@@ -10,6 +10,9 @@ from ssocketio import SocketIO
class ConnectionHandler:
"""Kelas untuk melakukan request/response dengan client"""
TRACKER_IP = '167.205.32.46'
TRACKER_PORT = 8000
BUFFER_SIZE = 4096
READ_TIMEOUT = 3 # buat ngehandle json yang belum selesai kekirim
CONNECTION_TIMEOUT = 5 # dalam detikkalo client gak ngirim ping apa2 ke server, bisa dianggap timeout
......@@ -33,7 +36,8 @@ class ConnectionHandler:
# connection lost
if ((len(recv) == 0)):
if (time.time() - last_read_time > ConnectionHandler.CONNECTION_TIMEOUT):
print('connection lost')
# karena requestnya per create connection satu kali request aja berarti gak usah ngecek kalo clientnya dc
# print('connection lost')
break
else:
last_read_time = time.time()
......@@ -166,16 +170,43 @@ class ConnectionHandler:
# invoke sendfind
def do_sendfind(self,token,item):
# do something
data = json.dumps({'status':'ok','description':'stub sendfind'})
offer_list = []
# untuk setiap server lain
for other_server in self._server._server_list:
#bikin koneksi server
socket = Socket.socket();
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)
socket.connect((host,port))
socketio = SocketIO(socket)
socketio.write_to_socket((json.dumps({'method':'findoffer','item':item})).encode('ascii'))
print('read offering from ',host,':',port)
data = socketio.read_from_socket()
if (data != None):
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)
data = json.dumps({'status':'ok','offers':offer_list})
try:
self._socketio.write_to_socket(data.encode('ascii'))
except Exception as msg:
print(msg)
# invoke findoffer
def do_findoffer(self,item):
# do something
# lakukan query ke database untuk mendapatkan daftar offerings
data = json.dumps({'status':'ok','description':'stub findoffer'})
try:
self._socketio.write_to_socket(data.encode('ascii'))
......@@ -209,6 +240,9 @@ class ConnectionHandler:
except Exception as msg:
print(msg)
def do_unknownmethod(self,method):
print('unknown method: ',method)
# baca data dari socket
# be sure to handle socket exception!
# json hasil bacaan BELUM TENTU VALID COK
......
......@@ -70,6 +70,22 @@ class Server:
print('tracker connected')
self.do_join(self._ip,self._port,socket)
tracker_io = SocketIO(socket)
# listen apakah ada server baru
while True:
data = tracker_io.read_from_socket()
# cek ping atau bukan
if (data != b''):
response = json.loads(data.decode('utf-8'))
if (response.get('method') == 'serverStatus'):
self._server_list = response.get('server')
tracker_io.write_to_socket(json.dumps({'status':'ok'}).encode('ascii'))
else:
print('Unknown data from tracker: ',data.decode('utf-8'))
tracker_io.write_to_socket(json.dumps({'status':'error'}).encode('ascii'))
# nodong ke tracker
......
# JANGAN DIUBAH-UBAH, PUNYA YAFI BUAT NGETES SERVER SISTER DOANG
import socket as Socket
from ssocketio import SocketIO
import time
import json
s = Socket.socket() # Create a socket object
host = '167.205.32.46'
port = 8025 # Reserve a port for your service.
s.connect((host, port))
findoffer = '{"method": "findoffers","item": 1}'
command = findoffer
# s.send(command.encode('ascii'))
print(s.recv(4096))
findoffer = '{"method": "sendfind","item": 1}'
command = findoffer
# s.send(command.encode('ascii'))
print(s.recv(4096))
# bikin user baru
s.connect((host, port))
socketio = SocketIO(s)
print('try to signup')
socketio.write_to_socket((json.dumps({'method':'signup','username':'dummy','password':'dummy'})).encode('ascii'))
data = socketio.read_from_socket()
print(data)
s.close()
# coba login
s = Socket.socket()
s.connect((host,port))
socketio = SocketIO(s)
print('try to login')
socketio.write_to_socket((json.dumps({'method':'login','username':'dummy','password':'dummy'})).encode('ascii'))
data = b''
while (data == b''):
data = socketio.read_from_socket()
print(data)
s.close
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