52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
from ParticipantUDP import ParticipantUDP
|
|
from Messages.ParticipantMsg import ParticipantMsg
|
|
from Messages.NetworkIdMsg import NetworkIdMsg
|
|
from Things.TemperatureSensor import TemperatureSensor
|
|
from Thing import Thing
|
|
|
|
import socket
|
|
import threading
|
|
|
|
class SiteServer(ParticipantUDP):
|
|
"""! A site server is a participant which provides a shared simulated environment
|
|
"""
|
|
|
|
name = "Site Server"
|
|
|
|
def __init__(self, port=7681):
|
|
"""! Create a new site server
|
|
@param port The UDP port on which communication is received
|
|
"""
|
|
super().__init__(ip_address = "127.0.0.1", port = port)
|
|
# self.ip_address = "0.0.0.0"
|
|
# self.port = port
|
|
# self.local_port = port
|
|
self.isolated = False # site servers are never isolated
|
|
self.remote_site = None # site servers never have remote sites
|
|
|
|
# self.others = []
|
|
# self.network_id = 0
|
|
# self.buffer = bytearray(256)
|
|
# self.thing_msg_processors = {}
|
|
# self.new_thing_handlers = []
|
|
self.publishInterval = 0
|
|
|
|
# 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", self.local_port))
|
|
|
|
# self.thread = threading.Thread(target = self.Receiver)
|
|
# self.thread.daemon = True
|
|
# self.thread.start()
|
|
|
|
self.Register(TemperatureSensor, Thing.Type.TemperatureSensor)
|
|
|
|
def ProcessParticipantMsg(self, sender, msg):
|
|
if msg.network_id == 0:
|
|
print(f'{self.name} received New Client -> {sender.network_id}')
|
|
self.Send(sender, NetworkIdMsg(sender.network_id))
|
|
# else:
|
|
# print(f'{self.name} Client')
|
|
|
|
def ProcessNetworkId(self, msg):
|
|
pass |