def test_deserializing_multiple_messages_bytes_stream(self): mess = putils.create_find_node_message(self.sender, self.receiver, 55) mess2 = putils.create_find_node_message(self.sender, self.receiver, 33) serialized = putils.serialize_message(mess) serialized2 = putils.serialize_message(mess2) self.assertEqual(mess2, putils.deserialize_message(serialized + serialized2))
async def join_network(self, bootstrap_node): """ Join network that the bootstrap_node belongs to :param bootstrap_node: Bootstrap node we will be asking for information about network :return: SUCCESS or FAILURE """ sever_status = await self.start_server() if sever_status is StatusMessage.FAILURE: print("Cancel network joining, could not start the server") return sever_status if bootstrap_node: log.debug("Joining network, bootstrap node: {}".format(bootstrap_node)) peer_to_ask = Peer(None, bootstrap_node[0], bootstrap_node[1], False) await self.lower_layer.add_peer(peer_to_ask) await self.ping(peer_to_ask.id) log.debug("Waiting for boot node to respond") await asyncio.sleep(4) peer_to_ask = await self.lower_layer.get_peer_by_id(peer_to_ask.id) if peer_to_ask is None: log.warning("Bootstrap node is not responding. Failed to bootstrap") await self.stop_server() return StatusMessage.FAILURE message = putils.create_find_node_message(sender=self.get_myself(), receiver=peer_to_ask, guid=self.get_myself().id) status = await self._put_message_on_lower(message) if status is StatusMessage.FAILURE: log.warning("Could not send find node message to bootstrap node") await self.stop_server() return status return StatusMessage.SUCCESS
async def find_node(self, guid, id_of_peer_to_ask): """ Create find_node message with given guid to find and peer_to_ask as a receiver of our query and pass it on :param guid: id of peer to ask :param id_of_peer_to_ask: peer to query about wanted peer :return: SUCCESS or FAILURE """ peer_to_ask = await self.lower_layer.get_peer_by_id(id_of_peer_to_ask) if peer_to_ask is None: return StatusMessage.FAILURE message = putils.create_find_node_message(sender=self.get_myself(), receiver=peer_to_ask, guid=guid) status = await self._put_message_on_lower(message) return status
def test_find_node(self): """ Business layer creates find node message using protobuf_utils and passes it on to the lower layer. """ peer = Peer(2, "127.33.21.22", 3233, False) _run(self.business_layer.lower_layer._routing_table.insert(peer)) status = _run(self.business_layer.find_node(2332, 2)) self.assertIs(status, StatusMessage.SUCCESS) message = _run(self.lower[1].get()) self.assertIsInstance(message, Message) self.assertEqual(message.type, Message.FIND_NODE) find_node_message = putils.create_find_node_message( sender=self.business_layer.get_myself(), receiver=peer, guid=2332) find_node_message.uuid = message.uuid self.assertEqual(message, find_node_message) status = _run( self.business_layer.find_node(guid=266, id_of_peer_to_ask=88)) self.assertIs(status, StatusMessage.FAILURE)
def test_creation_of_find_node_message(self): mess = putils.create_find_node_message(self.sender, self.receiver, 22) self.assertEqual(mess.type, Message.FIND_NODE) self.assertEqual(mess.findNode.guid, '22')