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 """ def __init__( self, packetsID: int, packetCount: int, sender: int, senderDisplayName: int, sourceNode: int, recipient: int, recipientNode: int, wantFullResponse: bool = False, packetsClass: int = 0, pAction: int = -1, target=True, ): """ 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 """ super().__init__( b"", packetsID=packetsID, packetCount=packetCount, packetsClass=packetsClass ) self.target = target self.sender = sender self.senderDisplayName = senderDisplayName if target: self.recipient = recipient self.recipientNode = recipientNode else: self.recipient = -1 self.recipientNode = -1 self.submessages = [] self.wantFullResponse = wantFullResponse self.pAction = pAction self.sourceNode = sourceNode self.packetCount = packetCount def usePreset(self, path: str, daisyCryptography): """ Add preset fields to the packet, currently unused """ preset = Daisy(path, daisyCryptography) 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["submessages"] = self.submessages res["wantFullResponse"] = self.wantFullResponse res["packetsClass"] = self.packetsClass res["pAction"] = self.pAction res["packetCount"] = self.packetCount return msgpack.dumps(res)