diff --git a/client_client.py b/client_client.py index 2fb5c9d..8a7a9ec 100644 --- a/client_client.py +++ b/client_client.py @@ -10,6 +10,7 @@ import socket import struct import config +import sctp import json import threading import random @@ -20,6 +21,27 @@ def __init__(self): self.client_list = dict() self.username = '' + @staticmethod + def sctp_handler(client_port: int): + # SCTP socket creation + sock = sctp.sctpsocket_udp(socket.AF_INET) + # get notifications on assoc state + tmp = sctp.event_subscribe(sock) + tmp.set_association(1) + tmp.set_data_io(1) + sock.autoclose = 0 + sock.bind(('', client_port)) + + while True: + try: + fromaddr, flags, msgret, notif = sock.sctp_recv(2048) + if notif.state == 3: + print(f'Client {sock.getpaddr(notif.assoc_id)} is down.') + elif notif.state == 0: + print.info(f'Client {sock.getpaddr(notif.assoc_id)} is up.') + except: + pass + @staticmethod def multicast_handler(client_port: int): # create the datagram socket diff --git a/config.py b/config.py index 3014427..5c94354 100644 --- a/config.py +++ b/config.py @@ -1,4 +1,5 @@ MULTICAST_PORT = 10001 MULTICAST_IP = '224.0.0.1' TCP_PORT = 10001 +SCTP_PORT = 10001 SERVER_IP = '127.0.0.1' \ No newline at end of file diff --git a/daemon_file.py b/daemon_file.py index 2b19dae..4b458a6 100644 --- a/daemon_file.py +++ b/daemon_file.py @@ -2,6 +2,7 @@ import socket import config import json +import sctp import struct import threading import os @@ -70,6 +71,29 @@ def tcp_handler(address): finally: sock.close() + @staticmethod + def sctp_handler(address): + logger.info(f'Starting STCP session for {address}') + # SCTP socket creation + sock = sctp.sctpsocket_udp(socket.AF_INET) + # get notifications on assoc state + tmp = sctp.event_subscribe(sock) + tmp.set_association(1) + tmp.set_data_io(1) + sock.autoclose = 0 + sock.bind(('', config.SCTP_PORT)) + sock.sctp_send(msg=b'0', to=address) + + while True: + try: + fromaddr, flags, msgret, notif = sock.sctp_recv(2048) + if notif.state == 3: + logger.info(f'Client {sock.getpaddr(notif.assoc_id)} is down.') + elif notif.state == 0: + logger.info(f'Client {sock.getpaddr(notif.assoc_id)} is up.') + except: + pass + def multicast_handler(self): server_address = ('', config.MULTICAST_PORT) @@ -94,6 +118,10 @@ def multicast_handler(self): thread = threading.Thread(target=self.tcp_handler, args=(address,), daemon=True) # run thread in the background as daemon thread.start() + # call SCTP session + _thread = threading.Thread(target=self.sctp_handler, args=(address,), daemon=True) + # run thread in the background as daemon + _thread.start() if __name__ == '__main__': diff --git a/sctp_server.py b/sctp_server.py index c531d9c..2c690e7 100644 --- a/sctp_server.py +++ b/sctp_server.py @@ -15,10 +15,9 @@ srv.listen(5) while True: - #srv_to_cli, _addr_client = srv.accept() try: fromaddr, flags, msgret, notif = srv.sctp_recv(2048) - _ = "up" if notif.state==0 else "down" + _ = "up" if notif.state == 0 else "down" print(f"assoc: {notif.assoc_id} -> {_}") print('===============================================') except: