import socket from infosec import Infosec import sys import binascii class Peer(object): def __init__(self, server, peer_entry): self.handles = peer_entry["handles"] self.keys = peer_entry["keys"] self.peer_id = peer_entry["peer_id"] self.server = server self.address = peer_entry["address"] self.port = peer_entry["port"] self.socket = self.server.udp_server_socket self.infosec = Infosec(server) def get_key(self): if len(self.keys) > 0: return self.keys[0] else: return None def send(self, msg): try: full_message = str.encode(msg["body"]) signed_packet_bytes = self.infosec.pack(self, msg) self.server.print_debug("packing message: %s" % full_message) self.socket.sendto(signed_packet_bytes, (self.address, self.port)) self.server.print_debug("[%s:%d] <- %s" % (self.address, self.port, binascii.hexlify(signed_packet_bytes)[0:16])) except Exception as ex: print("Exception while attempting to encode message: %s" % ex)