RoboidControl-python/SiteServer.py
Pascal Serrarens e218e0ea51 Event handling
2025-01-20 19:26:30 +01:00

46 lines
1.6 KiB
Python

from .Participant import Participant
from .ClientMsg import ClientMsg
from .NetworkIdMsg import NetworkIdMsg
import socket
import threading
class SiteServer(Participant):
name = "Site Server"
def __init__(self, ip_address="0.0.0.0", port=7681, remote=False, udp_socket = None):
self.ip_address = ip_address
self.port = port
self.publishInterval = 0
self.others = []
self.network_id = 0
self.buffer = bytearray(256)
if remote == False:
self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.udp_socket.bind(("0.0.0.0", port))
self.AddParticipant(self.ip_address, self.port)
self.thread = threading.Thread(target = self.Receiver)
self.thread.daemon = True
self.thread.start()
else:
self.udp_socket = udp_socket
def AddParticipant(self, ip_address, port):
# print(f'{self.name} Add site participant {ip_address} {port}')
remote_participant = SiteServer(ip_address, port, remote=True, udp_socket=self.udp_socket)
remote_participant.network_id = len(self.others)
self.others.append(remote_participant)
return remote_participant
def ProcessClientMsg(self, sender, msg):
if msg.network_id == 0:
print(f'{self.name} received New Client -> {sender.network_id}')
sender.Send(NetworkIdMsg(sender.network_id))
# else:
# print(f'{self.name} Client')
def ProcessNetworkId(self, msg):
pass