RoboidControl-python/SiteServer.py
Pascal Serrarens fa2895dd81 Refactoring
2025-01-20 20:09:53 +01:00

45 lines
1.6 KiB
Python

from .Participant import Participant
from .Messages import *
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