-
+ B73F1A4AFA94CAA17167B0316CE1AD893C33E46F3E5114230B808BABC21E4C1F9A3327576E890114DEBE2F48E2FBC4A16533A02E824D1080CDC80268A39F30BF
blatta/lib/broadcast.py
(0 . 0)(1 . 56)
139 import logging
140 import time
141 import hashlib
142 import binascii
143 from message import Message
144 from message import BROADCAST
145
146
147 class Broadcast(Message):
148 def __init__(self, message, state):
149 message['command'] = BROADCAST
150 message['bounces'] = 0
151 super(Broadcast, self).__init__(message, state)
152
153 def send(self):
154 if not self.speaker:
155 logging.error("aborting message send due speaker not being set")
156 return
157
158 # if we are not rebroadcasting we need to set the timestamp
159 self.timestamp = int(time.time())
160
161 target_peer = None
162
163 self.message_bytes = self.get_message_bytes(target_peer)
164 self.message_hash = hashlib.sha256(self.message_bytes).digest()
165
166 self.long_buffer.intern(self)
167
168 self.state.update_broadcast_self_chain(self.message_hash)
169 self.state.update_net_chain(self.message_hash)
170 for peer in self.state.get_keyed_peers(exclude_addressless=True):
171 signed_packet_bytes = self.pack(peer, self.command, self.bounces, self.message_bytes)
172 peer.send(signed_packet_bytes)
173 self.log_outgoing(peer)
174
175 # we already have message bytes here since this message came from the long buffer
176 def retry(self, requesting_peer):
177 signed_packet_bytes = self.pack(requesting_peer, self.command, self.bounces, self.message_bytes)
178 requesting_peer.send(signed_packet_bytes)
179 self.log_outgoing(requesting_peer)
180
181 def forward(self):
182 if not self.speaker:
183 logging.error("aborting message send due speaker not being set")
184 return
185
186 reporting_peer_ids = map(lambda p: p.peer_id, self.reporting_peers)
187 for peer in self.state.get_keyed_peers(exclude_addressless=True, exclude_ids=reporting_peer_ids):
188 # we don't want to send a broadcast back to the originator
189 if self.peer and (peer.peer_id == self.peer.peer_id):
190 continue
191
192 signed_packet_bytes = Message.pack(peer, self.command, self.bounces, self.message_bytes)
193 peer.send(signed_packet_bytes)
194 self.log_outgoing(peer)