raw
9982-getdata            1 import logging
9982-getdata 2 import hashlib
9982-getdata 3 import time
9982-getdata 4 import binascii
9982-getdata 5 from message import Message
9982-getdata 6 from message import DIRECT
9982-getdata 7
9982-getdata 8
9982-getdata 9 class Direct(Message):
9982-getdata 10 def __init__(self, message, state):
9982-getdata 11 message['command'] = DIRECT
9982-getdata 12 message['bounces'] = 0
9982-getdata 13 super(Direct, self).__init__(message, state)
9982-getdata 14
9982-getdata 15 def send(self):
9982-getdata 16 if not self.speaker:
9982-getdata 17 logging.error("aborting message send due speaker not being set")
9982-getdata 18 return
9982-getdata 19
9982-getdata 20 self.timestamp = int(time.time())
9982-getdata 21 target_peer = self.state.get_peer_by_handle(self.handle)
9982-getdata 22 if target_peer and not target_peer.get_key():
9982-getdata 23 logging.debug("No key for peer associated with %s" % self.handle)
9982-getdata 24 return
9982-getdata 25
9982-getdata 26 if target_peer == None:
9982-getdata 27 logging.debug("Aborting message: unknown handle: %s" % self.handle)
9982-getdata 28 return
9982-getdata 29
9982-getdata 30 self.message_bytes = self.get_message_bytes(target_peer)
9982-getdata 31 self.message_hash = hashlib.sha256(self.message_bytes).digest()
9982-getdata 32
9982-getdata 33 logging.debug("generated message_hash: %s" % binascii.hexlify(self.message_hash))
9982-getdata 34
9982-getdata 35 self.peer = target_peer
9982-getdata 36 self.long_buffer.intern(self)
9982-getdata 37
9982-getdata 38 signed_packet_bytes = self.pack(target_peer, self.command, self.bounces, self.message_bytes)
9982-getdata 39 self.state.update_handle_self_chain(target_peer.handles[0], self.message_hash)
9982-getdata 40 target_peer.send(signed_packet_bytes)
9982-getdata 41 self.log_outgoing(target_peer)
9982-getdata 42
9982-getdata 43 def retry(self, requesting_peer):
9982-getdata 44 target_peer = self.state.get_peer_by_handle(self.handle)
9982-getdata 45
9982-getdata 46 if target_peer == None:
9982-getdata 47 logging.debug("Aborting message: unknown handle: %s" % self.handle)
9982-getdata 48 return
9982-getdata 49
9982-getdata 50 if not target_peer.get_key():
9982-getdata 51 logging.debug("No key for peer associated with %s" % self.handle)
9982-getdata 52 return
9982-getdata 53
9982-getdata 54 # TODO: Figure out how to verify that the requester was the original intended recipient
9982-getdata 55 signed_packet_bytes = self.pack(target_peer, self.command, self.bounces, self.message_bytes)
9982-getdata 56 target_peer.send(signed_packet_bytes)
9982-getdata 57 self.log_outgoing(target_peer)
9982-getdata 58