piermesh/src/Packets/HeaderPacket.py

105 lines
2.9 KiB
Python
Raw Normal View History

2024-07-28 11:21:15 +00:00
from Packets.Packet import Packet
2024-08-01 01:00:46 +00:00
from Daisy.Daisy import Daisy
2024-07-28 11:21:15 +00:00
import msgpack
class Header(Packet):
2024-08-01 01:00:46 +00:00
"""
Metadata packet for messages
2024-08-01 22:03:59 +00:00
`🔗 Source <https://git.utopic.work/PierMesh/piermesh/src/branch/main/Packets/HeaderPacket.py>`__
2024-08-01 01:00:46 +00:00
Attributes
----------
sender: int
6 digit (maximum) node or peer ID
senderDisplayName: int
3 digit (maximum) ID for mapping display names to a given user
recipient: int
6 digit (maximum) node or peer ID
recipientNode: int
6 digit (maximum) node ID to route the packet to
subpacket: bool
Whether this is a subpacket
wantFullResponse: bool
Whether a response should be sent when the message completes reception (TODO)
pAction: int
2024-11-26 17:43:02 +00:00
3 digit (maximum) pAction ID for mapping precise actions within a protocol
2024-08-01 01:00:46 +00:00
"""
2024-07-28 11:21:15 +00:00
def __init__(
self,
2024-08-01 01:00:46 +00:00
packetsID: int,
packetCount: int,
sender: int,
senderDisplayName: int,
2024-08-12 10:29:58 +00:00
sourceNode: int,
2024-08-01 01:00:46 +00:00
recipient: int,
recipientNode: int,
wantFullResponse: bool = False,
packetsClass: int = 0,
pAction: int = -1,
2024-11-23 03:34:39 +00:00
target=True,
2024-07-28 11:21:15 +00:00
):
2024-11-26 17:43:02 +00:00
"""
Arguments
---------
sourceNode: int
Source of request
packetsClass: int
Integer ID matching the class of the message
target
Whether the message is being sent to a target, if so, where
"""
2024-07-28 11:21:15 +00:00
super().__init__(
2024-11-23 03:34:39 +00:00
b"", packetsID=packetsID, packetCount=packetCount, packetsClass=packetsClass
2024-07-28 11:21:15 +00:00
)
2024-11-23 03:34:39 +00:00
self.target = target
2024-07-28 11:21:15 +00:00
self.sender = sender
self.senderDisplayName = senderDisplayName
2024-11-23 03:34:39 +00:00
if target:
self.recipient = recipient
self.recipientNode = recipientNode
else:
self.recipient = -1
self.recipientNode = -1
self.submessages = []
2024-07-28 11:21:15 +00:00
self.wantFullResponse = wantFullResponse
2024-08-01 01:00:46 +00:00
self.pAction = pAction
2024-08-12 10:29:58 +00:00
self.sourceNode = sourceNode
2024-11-23 03:34:39 +00:00
self.packetCount = packetCount
2024-07-28 11:21:15 +00:00
2024-11-23 03:34:39 +00:00
def usePreset(self, path: str, daisyCryptography):
2024-08-01 01:00:46 +00:00
"""
2024-11-26 17:43:02 +00:00
Add preset fields to the packet, currently unused
2024-08-01 01:00:46 +00:00
"""
2024-11-23 03:34:39 +00:00
preset = Daisy(path, daisyCryptography)
2024-07-28 11:21:15 +00:00
for key in preset.get().keys():
self.msg[key] = preset.get()[key]
def dump(self):
2024-08-01 01:00:46 +00:00
"""
Dump packet to msgpack encoded binary for transmission
"""
2024-07-28 11:21:15 +00:00
res = msgpack.loads(super().dump())
res["sender"] = self.sender
res["senderDisplayName"] = self.senderDisplayName
2024-08-12 10:29:58 +00:00
res["sourceNode"] = self.sourceNode
2024-07-28 11:21:15 +00:00
res["recipient"] = self.recipient
res["recipientNode"] = self.recipientNode
2024-11-23 03:34:39 +00:00
res["submessages"] = self.submessages
2024-07-28 11:21:15 +00:00
res["wantFullResponse"] = self.wantFullResponse
2024-08-01 01:00:46 +00:00
res["packetsClass"] = self.packetsClass
res["pAction"] = self.pAction
2024-11-23 03:34:39 +00:00
res["packetCount"] = self.packetCount
2024-08-01 01:00:46 +00:00
2024-07-28 11:21:15 +00:00
return msgpack.dumps(res)