def initiate_ballot(self): self.last_tried = self.last_tried + 1 message = Packet(self, "NextBallot({})".format(self.last_tried), None) self.slip.num_participants = self.router.get_num_endpoints() self.slip.quorum = int(self.slip.num_participants/2)+1 self.queue.append(message) self.slip.num_received_last_vote = 0
def process_next_ballot(self, packet): pattern = re.compile("NextBallot\(([0-9]+)\)") match = pattern.match(packet.get_message()) if match: ballot = int(match.group(1)) if ballot > self.next_bal: self.queue.append(Packet(self, "LastVote({},{})".format(ballot, self.prev_vote), packet.get_source())) self.next_bal = ballot
def broadcast(self, packet): source_index = self.get_source_index(packet) assert source_index >= 0 for endpoint_index, endpoint in enumerate(self.get_endpoints()): self.__queue.append( Packet(source_index, packet.get_message(), endpoint_index)) self.log( "Broadcasting: '{}' to {} endpoints.".format( packet.get_message(), self.get_num_endpoints()), source_index)
def process_next_ballot(self, packet): pattern = re.compile("NextBallot\(([0-9]+)\)") match = pattern.match(packet.get_message()) if match: ballot = int(match.group(1)) self.queue.append( Packet(self, "LastVote({},{})".format(ballot, -1), packet.get_source())) self.__max_ballot_received = ballot
def test_send(self): sender = Mock() receiver = Mock() message = Packet(sender, "Hello world", 1) self.router.add_endpoint(sender) self.router.add_endpoint(receiver) self.router.send(message) self.router.distribute() receiver.receive.assert_called_once()
def process_begin_ballot(self, packet): pattern = re.compile("BeginBallot\(([0-9]+),([\-0-9]+)\)") match = pattern.match(packet.get_message()) if match: ballot = int(match.group(1)) decree = int(match.group(2)) if ballot == self.next_bal: self.prev_vote = ballot self.queue.append(Packet(self, "Voted({})".format(ballot), packet.get_source()))
def process_voted(self, packet): pattern = re.compile("Voted\(([0-9]+)\)") match = pattern.match(packet.get_message()) if match: ballot = int(match.group(1)) if ballot == self.last_tried: self.slip.received_votes.append(packet.get_source()) if len(self.slip.received_votes) == self.slip.quorum: self.decree = self.slip.proposed_decree self.queue.append(Packet(self, "Success({})".format(self.slip.proposed_decree), None))
def process_last_vote(self, packet): pattern = re.compile("LastVote\(([0-9]+),([\-0-9]+)\)") match = pattern.match(packet.get_message()) if match: ballot = int(match.group(1)) last_vote = int(match.group(2)) if ballot == self.last_tried: self.slip.num_received_last_vote += 1 if self.slip.num_received_last_vote == self.slip.num_participants: self.slip.proposed_decree = 23 send = Packet(self, "BeginBallot({},{})".format(self.last_tried, self.slip.proposed_decree), None) self.queue.append(send)
def test_broadcast(self): receiver1 = Mock() receiver2 = Mock() sender = Mock() self.router.add_endpoint(receiver1) self.router.add_endpoint(receiver2) self.router.add_endpoint(sender) message_sent = Packet(sender, "Hello world", None) self.router.broadcast(message_sent) self.router.distribute() receiver1.receive.assert_called_once() self.router.distribute() receiver2.receive.assert_called_once()
def test_receive(self): message = Packet(0, "Hello world", 1) self.priest.receive(message)
def test_send_not_broadcast(self): message = Packet(0, "Hello world", None) self.assertRaises(AssertionError, self.router.send, message)
def receive(ext): print('Waiting for message') print('Reading public key from {}'.format(CONFIG_DIR)) print('Using configuration: file_ext = {}'.format(ext)) with open(os.path.join(CONFIG_DIR, 'public_key'), 'r') as f: public_key = RSA.import_key(f.read()) if not os.path.exists(RECEIVER_DIR): print('Creating directory: '.format(RECEIVER_DIR)) os.makedirs(RECEIVER_DIR) messages_dict = dict() # type: Dict[str, MessageBuilder] print('Looking for files in {}'.format(SENDER_DIR)) for filename in os.listdir(SENDER_DIR): _, file_ext = os.path.splitext(filename) if file_ext[1:] != ext: print(' Skipping file with extension {}: {}'.format( file_ext, filename)) continue print(' Reading packet from file: {}'.format(filename)) filepath = os.path.join(SENDER_DIR, filename) try: if file_ext == '.txt': content = extract_content_from_text_file(filepath) elif file_ext == '.png': content = extract_content_from_image(filepath) except Exception as ex: print(' Error parsing contents, skipping file {}'.format( filename)) continue # noinspection PyUnboundLocalVariable packet = Packet.deserialize(content) if packet.message_hash not in messages_dict: messages_dict[packet.message_hash] = MessageBuilder( packet.message_hash, packet.message_signature, packet.total_chunks, public_key) messages_dict[packet.message_hash].add_packet(packet) print('Looking for complete messages from the packets received') for message_hash, builder in messages_dict.items(): if not builder.is_complete(): print( "Skipping message '{}' because not all packets have been received" .format(message_hash)) continue message = builder.build() outfile = os.path.join(RECEIVER_DIR, message_hash) print("Writing message '{}' to {}".format(message_hash, outfile)) with open(outfile, 'w') as f: f.write(message.message)