Ejemplo n.º 1
0
    def _invoke_service(self, topic, payload, other_fields=None):
        """
        Invokes a request method on the File Transfer DXL service.

        :param str topic: The topic to send the request to.
        :param payload: The payload to include in the request
        :param dict other_fields: Other fields to include in the request
        :return: Results of the service invocation.
        :rtype: dict
        """
        # Create the DXL request message.
        request = Request(topic)

        # Set the full request parameters.
        request.payload = payload
        request.other_fields = other_fields

        # Perform a synchronous DXL request.
        response = self._dxl_sync_request(request)

        # Convert the JSON payload in the DXL response message to a Python
        # dictionary and return it.
        return MessageUtils.json_payload_to_dict(response)
            # If all of the bytes in the local file have been read, this must
            # be the last segment. Send a 'store' result, file 'name', and
            # 'size' and sha256 'hash' values that the service can use to
            # confirm that the full contents of the file were transmitted
            # properly.
            bytes_read += len(segment)
            if bytes_read == file_size:
                other_fields[FileStoreProp.NAME] = os.path.join(
                    STORE_FILE_DIR, os.path.basename(STORE_FILE_NAME))
                other_fields[FileStoreProp.RESULT] = FileStoreResultProp.STORE
                other_fields[FileStoreProp.SIZE] = str(file_size)
                other_fields[FileStoreProp.HASH_SHA256] = file_hash.hexdigest()

            # Set the full request parameters
            req.other_fields = other_fields
            req.payload = segment

            # Send the file segment request to the DXL fabric. Exit if an
            # error response is received.
            res = client.sync_request(req, timeout=30)
            if res.message_type == Message.MESSAGE_TYPE_ERROR:
                print(
                    "\nError invoking service with topic '{}': {} ({})".format(
                        request_topic, res.error_message, res.error_code))
                exit(1)

            # Update the current percent complete on the console.
            sys.stdout.write("\rPercent complete: {}%".format(
                int((segment_number / total_segments) *
                    100) if total_segments else 100))