Skip to content
Snippets Groups Projects
Commit a44573f2 authored by Josep Marcello's avatar Josep Marcello :disappointed_relieved:
Browse files

fix: udah bisa multithreading


Co-authored-by: default avatarFransiskus Febryan Suryawan <13519124@std.stei.itb.ac.id>
parent 30dfbca8
Branches
Tags
No related merge requests found
......@@ -5,7 +5,7 @@ class FLAGS:
SYN = 0b0000_0010
ACK = 0b0001_0000
WINDOWS_SIZE = 1
WINDOWS_SIZE = 16
SERVER_TIMEOUT = CLIENT_TIMEOUT = 5
HEADER_SIZE = 12 # bytes
......
import socket
from sys import argv, stderr, exit
from const import FLAGS, WINDOWS_SIZE, SERVER_TIMEOUT
from threading import Thread
from threading import Event, Thread
from FilePackets import FilePackets
from Packet import *
from typing import Dict
from typing import Dict, Tuple
from SocketBuffer import SocketBuffer
from time import perf_counter, sleep
from struct import unpack
......@@ -13,7 +13,8 @@ from struct import unpack
def send_handler(
sock: socket.socket,
client: any,
filename: str):
event: Event,
filename: str,):
packets = FilePackets(filename)
sb = SocketBuffer()
......@@ -43,13 +44,16 @@ def send_handler(
can_send = sequence_base <= sequence_current <= sequence_max
init_time = perf_counter()
i = 0
max_try = 4
max_try = 3
while not can_send:
# print(f'Waiting to send packet {sequence_current} to {client[0]}:{client[1]}')
# Nunggu dapet ACK sampe timeout
# Kalo timeout belom go_back_n
sleep(0.2)
# Tunggu sampe dapet request dari klien
event.wait(SERVER_TIMEOUT)
update_ack()
# Tandai request udah di-consume
event.clear()
can_send = sequence_base <= sequence_current <= sequence_max
# masih ga bisa ngirim dan waktu habis
......@@ -89,7 +93,7 @@ def send_handler(
return
def handle_client(sock: socket.socket, client: any, filename: str):
def handle_client(sock: socket.socket, client: any, event: Event, filename: str):
# handle three-way handshake
print(f'Initiating three-way handshake with {client[0]}:{client[1]}')
......@@ -108,7 +112,7 @@ def handle_client(sock: socket.socket, client: any, filename: str):
if got_ack:
print(f'Received ACK from client {client[0]}:{client[1]}')
print(f'Connection established with client {client[0]}:{client[1]}')
send_handler(sock, client, filename)
send_handler(sock, client, event, filename)
break
......@@ -121,23 +125,36 @@ if __name__ == '__main__':
filename = argv[2]
ソケット = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM, proto=socket.IPPROTO_UDP)
ソケット.bind(('0.0.0.0', int(port)))
client_list: Dict[any, Thread] = {}
client_list: Dict[any, Tuple[Thread, Event]] = {}
sb = SocketBuffer()
while True:
# Kasih delay setiap sebelum nerima request
# Untuk multithreading
sleep(0.05)
ダタ, クライアント = ソケット.recvfrom(12)
sb.update(ダタ, クライアント)
フラグ = sb.get_buffer()[8]
if フラグ & FLAGS.SYN:
print(f'Client {クライアント[0]}:{クライアント[1]} sent SYN')
# "register" client
t = client_list[クライアント] = Thread(
target=handle_client,
args=(ソケット, クライアント, filename))
t.start()
event = Event()
event.clear()
t = client_list[クライアント] = (
Thread(
target=handle_client,
args=(ソケット, クライアント, event, filename)
),
event,
)
t[0].start()
if フラグ & FLAGS.FIN: print(f'Client {クライアント[0]}:{クライアント[1]} sent FIN')
if フラグ & FLAGS.ACK: print(f'Client {クライアント[0]}:{クライアント[1]} sent ACK')
# Tandain client `クライアント` dapet request
client_list[クライアント][1].set()
except:
pass
finally:
......@@ -145,4 +162,4 @@ if __name__ == '__main__':
print('Waiting for threads to finish...')
print('^C to force stop.')
for _,v in client_list.items():
v.join()
v[0].join()
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