import logging import hashlib import time import binascii from message import Message from message import DIRECT class Direct(Message): def __init__(self, message, state): message['command'] = DIRECT message['bounces'] = 0 super(Direct, self).__init__(message, state) def send(self): if not self.speaker: logging.error("aborting message send due speaker not being set") return self.timestamp = int(time.time()) target_peer = self.state.get_peer_by_handle(self.handle) if target_peer and not target_peer.get_key(): logging.debug("No key for peer associated with %s" % self.handle) return if target_peer == None: logging.debug("Aborting message: unknown handle: %s" % self.handle) return self.message_bytes = self.get_message_bytes(target_peer) self.message_hash = hashlib.sha256(self.message_bytes).digest() logging.debug("generated message_hash: %s" % binascii.hexlify(self.message_hash)) self.peer = target_peer self.long_buffer.intern(self) signed_packet_bytes = self.pack(target_peer, self.command, self.bounces, self.message_bytes) self.state.update_handle_self_chain(target_peer.handles[0], self.message_hash) target_peer.send(signed_packet_bytes) self.log_outgoing(target_peer) def retry(self, requesting_peer): if requesting_peer == None: logging.debug("Aborting message: unknown peer: %s" % requesting_peer.handles[0]) return if not requesting_peer.get_key(): logging.debug("No key for peer associated with %s" % requesting_peer.handles[0]) return # TODO: Figure out how to verify that the requester was the original intended recipient signed_packet_bytes = self.pack(requesting_peer, self.command, self.bounces, self.message_bytes) requesting_peer.send(signed_packet_bytes) self.log_outgoing(requesting_peer)