RoboidControl-python/Messages/ParticipantMsg.py

72 lines
2.3 KiB
Python

from Messages.Messages import IMessage
class ParticipantMsg(IMessage):
"""! A participant messages notifies other participants of its presence.
When received by another participant, it can be followed by a NetworkIdMsg
to announce that participant to this client such that it can join privately
"""
## The message ID
id: int = 0xA0
## The length of the message
length: int = 2
## The network ID known by the participant.
network_id: int = None
def __init__(self, network_id: int):
"""! Create a new message for sending
@param network_id The network ID known by the participant. Use 0 if it is unknown
"""
if isinstance(network_id, int):
self.network_id = network_id
elif isinstance(network_id, bytes):
self.network_id = network_id[1]
def Serialize(self, buffer_ref):
"""! Serialize the message into a byte array.
@param buffer_ref The buffer to serialize into
@return The length of the message in the buffer
"""
if buffer_ref is None or self.network_id is None:
return 0
buffer: bytearray = buffer_ref[0]
buffer[0:ParticipantMsg.length] = [
ParticipantMsg.id,
self.network_id
]
return ParticipantMsg.length
# @staticmethod
# def Serialized(buffer_ref, network_id):
# if buffer_ref is None or network_id is None:
# return 0
# buffer: bytearray = buffer_ref[0]
# buffer[0:ClientMsg.length] = [
# ClientMsg.id,
# network_id
# ]
# return ClientMsg.length
## Send the network_id to the participant
# def SendTo(participant, network_id):
# if participant is None or network_id is None:
# return False
# participant.buffer[0:2] = [
# ClientMsg.id,
# network_id
# ]
# participant.SendBuffer(ClientMsg.length)
## Publish the network_id
# def Publish(participant, network_id):
# if participant is None or network_id is None:
# return False
# participant.buffer[0:2] = [
# ClientMsg.id,
# network_id
# ]
# participant.PublishBuffer(ClientMsg.length)