Skip to content

Commit

Permalink
SCTP initial integration
Browse files Browse the repository at this point in the history
  • Loading branch information
mmuravytskyi committed Jun 23, 2021
1 parent 1e62e55 commit d5b42ee
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
22 changes: 22 additions & 0 deletions client_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import socket
import struct
import config
import sctp
import json
import threading
import random
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -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'
28 changes: 28 additions & 0 deletions daemon_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import socket
import config
import json
import sctp
import struct
import threading
import os
Expand Down Expand Up @@ -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)

Expand All @@ -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__':
Expand Down
3 changes: 1 addition & 2 deletions sctp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d5b42ee

Please sign in to comment.