-
+ 6BB6808C15C7BFB7C89860C4565FD28E2ABDA8AB75D1C6928E4EBEBF37CEC6F104A73C595FB4DFD34515E47D77D8EEDCAF5DEC8E7CBB353858281C17C2229CBF
blatta/lib/getdata.py
(0 . 0)(1 . 66)
1043 import time
1044 import binascii
1045 import hashlib
1046 import logging
1047 from message import Message
1048 from message import OUTGOING_MESSAGE_LOGGING_FORMAT
1049 from commands import GETDATA
1050 from commands import DIRECT
1051 from commands import BROADCAST
1052 from commands import COMMAND_LABELS
1053
1054 class GetData(Message):
1055 def __init__(self, original, broken_chain, state=None):
1056 message = {
1057 'command': GETDATA,
1058 'body': original[broken_chain],
1059 'timestamp': int(time.time()),
1060 'speaker': state.get_knob('nick'),
1061 'bounces': 0,
1062 'original': original
1063 }
1064 super(GetData, self).__init__(message, state)
1065
1066 def send(self):
1067 target_peer = (self.state.get_peer_by_handle(self.original['speaker'])
1068 if self.original['command'] == DIRECT
1069 else None)
1070
1071 if self.original['command'] == DIRECT and target_peer == None:
1072 logging.debug("Aborting message: unknown handle: %s" % self.handle)
1073 return
1074
1075 if target_peer and not target_peer.get_key():
1076 logging.debug("No key for peer associated with %s" % self.handle)
1077 return
1078
1079 if self.state.get_knob('nick') is None:
1080 logging.error("unable to pack message due to null speaker value")
1081 return
1082
1083 self.message_bytes = self.get_message_bytes(target_peer)
1084 self.message_hash = hashlib.sha256(self.message_bytes).digest()
1085
1086
1087 if self.original['command'] == DIRECT:
1088 signed_packet_bytes = self.pack(target_peer,
1089 self.command,
1090 self.bounces,
1091 self.message_bytes)
1092 target_peer.send(signed_packet_bytes)
1093 self.log_outgoing(target_peer)
1094
1095 elif self.original['command'] == BROADCAST:
1096 for peer in self.state.get_keyed_peers(exclude_addressless=True):
1097 signed_packet_bytes = self.pack(peer, self.command, self.bounces, self.message_bytes)
1098 peer.send(signed_packet_bytes)
1099 self.log_outgoing(peer)
1100
1101 def log_outgoing(self, peer):
1102 logging.info(OUTGOING_MESSAGE_LOGGING_FORMAT % (peer.address,
1103 peer.port,
1104 peer.handles[0],
1105 COMMAND_LABELS[self.command],
1106 binascii.hexlify(self.body),
1107 self.bounces,
1108 binascii.hexlify(self.message_hash)))