import logging import time import hashlib from message import Message from message import BROADCAST class Broadcast(Message): def __init__(self, message, state): message['command'] = BROADCAST message['bounces'] = message.get('bounces', 0) super(Broadcast, self).__init__(message, state) def send(self): if not self.speaker: logging.error("aborting message send due speaker not being set") return # if we are not rebroadcasting we need to set the timestamp self.timestamp = int(time.time()) target_peer = None self.message_bytes = self.get_message_bytes(target_peer) self.message_hash = hashlib.sha256(self.message_bytes).digest() self.long_buffer.intern(self) self.state.update_broadcast_self_chain(self.message_hash) self.state.update_net_chain(self.message_hash) for peer in self.state.get_keyed_peers(exclude_addressless=True): signed_packet_bytes = self.pack(peer, self.command, self.bounces, self.message_bytes) peer.send(signed_packet_bytes) self.log_outgoing(peer) # we already have message bytes here since this message came from the long buffer def retry(self, requesting_peer): 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) def forward(self): if not self.speaker: logging.error("aborting message send due speaker not being set") return reporting_peer_ids = map(lambda p: p.peer_id, self.reporting_peers) for peer in self.state.get_keyed_peers(exclude_addressless=True, exclude_ids=reporting_peer_ids): # we don't want to send a broadcast back to the originator if self.peer and (peer.peer_id == self.peer.peer_id): continue signed_packet_bytes = Message.pack(peer, self.command, self.bounces, self.message_bytes) peer.send(signed_packet_bytes) self.log_outgoing(peer)