Exemple #1
0
 def send_message(self, destination_ip, path, data):
     destination = os.getenv('TEST_IP',
                             format_address(destination_ip, 5000))
     m = Message(format_address(self.ip, 5000), destination, data)
     logger.debug("Request", request=m.to_json())
     ret = requests.post(m.destination + '/' + self.api_version + '/' +
                         path,
                         json=m.to_json(),
                         timeout=5)
     logger.debug("Response", response=ret.content)
     return ret.content
 def send_next_ball(self, destination_ip):
     destination = os.getenv('TEST_IP',
                             format_address(destination_ip, 5001))
     m = Message(format_address(self.ip, 5001), destination, self.next_ball)
     logger.debug('I am sending next ball message.',
                  message=m.to_json(),
                  destination=destination)
     ret = requests.post(m.destination + '/' + self.api_version +
                         '/send-ball',
                         json=m.to_json(),
                         timeout=5)
     return ret.content
Exemple #3
0
def exchange_view(request):
    if request.method == 'POST':

        logger.info("ExchangeView")
        logger.debug("ExchangeView",
                     request=request,
                     partialView=cyclon.partialView)

        # 1) I cast the received json into a PartialView
        logger.debug(
            "ExchangeView: I cast the received json into a PartialView.")
        message = json.loads(request.body)
        received_partial_view = PartialView.from_dict(message.get('data'))

        # 2) I send a subset of my partial view no matter if the source ip is contained in it
        logger.debug(
            "ExchangeView: I send a subset of my partial view no matter if the source ip is contained in it."
        )
        to_send = cyclon.partialView.select_neighbors_for_reply()

        # 3) I merge current partial view with the one just received
        logger.debug(
            "ExchangeView: I merge current partial view with the one just received."
        )
        cyclon.partialView.merge(to_send, received_partial_view)
        logger.debug("ExchangeView", partialView=cyclon.partialView)

        m = Message(format_address(my_ip(), 5000), message.get('source'),
                    to_send)
        logger.debug("ExchangeView",
                     response=m.to_json(),
                     to=message.get('source'))
        return JsonResponse(m.to_json())

    else:
        return JsonResponse(
            {"error": {
                "message": "Only the POST method is allowed."
            }},
            status=403)
Exemple #4
0
    def test_new_message_to_json(self):

        partial_view = PartialView("172.0.1.0")
        partial_view.add_peer_ip("172.0.1.1")
        partial_view.add_peer_ip("172.0.1.2")
        m = Message("source_ip", "destination_ip", partial_view)

        # Transform it into json
        json_path = os.path.join(os.path.dirname(__file__), "message.json")
        with open(json_path) as json_file:
            d = json.load(json_file)
            self.assertEqual(m.to_json(), d)
            json_file.close()
Exemple #5
0
    def respond_to_description_request(self):
        """
        Handles the description request and sends a response.
        :return:
        """

        self.logger.debug("Responding to description request")
        data = self.connection.recv(BUFFER_SIZE).decode('UTF-8')
        # print(self.node.name + " received a description request:\n" + data)

        log_message = "Description request received \n'" + data + "'"
        self.logger.info(log_message)

        with open(self.filename, 'a') as f:
            log_message = str(datetime.now()) + ": " + "Description request received"
            f.write(log_message)
            log_message = "\t\t" + data
            f.write(log_message)
            f.flush()

        if len(data) <= 0:
            return
        try:
            message = Message.to_object(data)
        except ValueError as e:
            self.logger.error(e.args[0])
            # print(e.args)
            return
        # TODO: fix message type checking (message.to_object always assigns "2"!)
        self.logger.debug("Skipping Description request message type check; Responding to request.")
        # Create description message from node info
        response = Message(MessageType.description_response, self.node.name, self.node.address,
                           Timestamp.create_timestamp(), self.node.service_list)

        # Convert message to json
        json_message = json.dumps(response.to_json())

        # Encode message to utf-8 for sending through socket
        data = json_message.encode()

        self.connection.send(data)
        """
Exemple #6
0
    def request_description(self):
        """
        Request description of a node's services.
        Expects name of the node.

        """
        if len(self.received_command) != 2:
            self.logger.error("Wrong argument count! Expected \"describe node_name\".")
            return
        node_name = self.received_command[1]
        address = self.peer_list.get_node_address(node_name)    # TODO: send description request to address in peer_list
        self.logger.info("Sending description request to " + str(address))
        # Create message and send it.
        message = Message(MessageType.description_request, node_name, address, Timestamp.create_timestamp())
        data = message.to_json()
        data_str = json.dumps(data)
        self.output_socket.connect(address[0], address[1])

        self.measurer.start_timer()
        self.output_socket.send(bytes(data_str, 'UTF-8'))
        response = self.output_socket.read()
        self.measurer.stop_timer()
        duration = self.measurer.get_last_duration()
        self.measurer.description_duration(self.node.name, duration)

        self.logger.info("Description request duration: {0}".format(duration))
        response_str = response.decode('UTF-8')
        self.logger.debug(self.node.name + " received description response:\n" + response_str)
        if self.remote_socket:
            self.remote_socket.sendall(response)
        else:
            pass
            #print(response_str)
        update_message = Message.to_object(response_str)
        self.peer_list.update_node(node_name, update_message)
        #message = Message.to_object(response_str)

        return None