from Packets.Packet import Packet from Daisy.Daisy import Daisy import msgpack class Header(Packet): """ Metadata packet for messages `🔗 Source `__ 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 3 digit (maximum) pAction ID for mapping precise actions within a protocol (TODO) """ def __init__( self, packetsID: int, packetCount: int, sender: int, senderDisplayName: int, sourceNode: int, recipient: int, recipientNode: int, nonce, subpacket: bool = False, wantFullResponse: bool = False, packetsClass: int = 0, pAction: int = -1, ): super().__init__( "", packetsID=packetsID, packetCount=packetCount, packetsClass=packetsClass ) self.sender = sender self.senderDisplayName = senderDisplayName self.recipient = recipient self.recipientNode = recipientNode self.subpacket = subpacket self.wantFullResponse = wantFullResponse self.pAction = pAction self.sourceNode = sourceNode self.nonce = nonce def usePreset(self, path: str): """ Add preset fields to the packet """ preset = Daisy(path) for key in preset.get().keys(): self.msg[key] = preset.get()[key] def dump(self): """ Dump packet to msgpack encoded binary for transmission """ res = msgpack.loads(super().dump()) res["sender"] = self.sender res["senderDisplayName"] = self.senderDisplayName res["sourceNode"] = self.sourceNode res["recipient"] = self.recipient res["recipientNode"] = self.recipientNode res["subpacket"] = self.subpacket res["wantFullResponse"] = self.wantFullResponse res["packetsClass"] = self.packetsClass res["pAction"] = self.pAction res["nonce"] = self.nonce return msgpack.dumps(res)