raw
9982-getdata            1 import logging
9982-getdata 2 import time
9982-getdata 3 import hashlib
9982-getdata 4 from message import Message
9982-getdata 5 from message import BROADCAST
9982-getdata 6
9982-getdata 7
9982-getdata 8 class Broadcast(Message):
9982-getdata 9 def __init__(self, message, state):
9982-getdata 10 message['command'] = BROADCAST
9980-preserve-bou... 11 message['bounces'] = message.get('bounces', 0)
9982-getdata 12 super(Broadcast, self).__init__(message, state)
9982-getdata 13
9982-getdata 14 def send(self):
9982-getdata 15 if not self.speaker:
9982-getdata 16 logging.error("aborting message send due speaker not being set")
9982-getdata 17 return
9982-getdata 18
9982-getdata 19 # if we are not rebroadcasting we need to set the timestamp
9982-getdata 20 self.timestamp = int(time.time())
9982-getdata 21
9982-getdata 22 target_peer = None
9982-getdata 23
9982-getdata 24 self.message_bytes = self.get_message_bytes(target_peer)
9982-getdata 25 self.message_hash = hashlib.sha256(self.message_bytes).digest()
9982-getdata 26
9982-getdata 27 self.long_buffer.intern(self)
9982-getdata 28
9982-getdata 29 self.state.update_broadcast_self_chain(self.message_hash)
9982-getdata 30 self.state.update_net_chain(self.message_hash)
9982-getdata 31 for peer in self.state.get_keyed_peers(exclude_addressless=True):
9982-getdata 32 signed_packet_bytes = self.pack(peer, self.command, self.bounces, self.message_bytes)
9982-getdata 33 peer.send(signed_packet_bytes)
9982-getdata 34 self.log_outgoing(peer)
9982-getdata 35
9982-getdata 36 # we already have message bytes here since this message came from the long buffer
9982-getdata 37 def retry(self, requesting_peer):
9982-getdata 38 signed_packet_bytes = self.pack(requesting_peer, self.command, self.bounces, self.message_bytes)
9982-getdata 39 requesting_peer.send(signed_packet_bytes)
9982-getdata 40 self.log_outgoing(requesting_peer)
9982-getdata 41
9982-getdata 42 def forward(self):
9982-getdata 43 if not self.speaker:
9982-getdata 44 logging.error("aborting message send due speaker not being set")
9982-getdata 45 return
9982-getdata 46
9982-getdata 47 reporting_peer_ids = map(lambda p: p.peer_id, self.reporting_peers)
9982-getdata 48 for peer in self.state.get_keyed_peers(exclude_addressless=True, exclude_ids=reporting_peer_ids):
9982-getdata 49 # we don't want to send a broadcast back to the originator
9982-getdata 50 if self.peer and (peer.peer_id == self.peer.peer_id):
9982-getdata 51 continue
9982-getdata 52
9982-getdata 53 signed_packet_bytes = Message.pack(peer, self.command, self.bounces, self.message_bytes)
9982-getdata 54 peer.send(signed_packet_bytes)
9982-getdata 55 self.log_outgoing(peer)