dxlclient.client module¶
Contains the DxlClient class, which is responsible for all
communication with the Data Exchange Layer (DXL) fabric.
-
class
dxlclient.client.DxlClient(config)¶ Bases:
dxlclient._BaseObjectThe
DxlClientclass is responsible for all communication with the Data Exchange Layer (DXL) fabric (it can be thought of as the "main" class). All other classes exist to support the functionality provided by the client.The following example demonstrates the configuration of a
DxlClientinstance and connecting it to the fabric:from dxlclient.broker import Broker from dxlclient.client import DxlClient from dxlclient.client_config import DxlClientConfig # Create the client configuration config = DxlClientConfig( broker_ca_bundle="c:\\certs\\brokercerts.crt", cert_file="c:\\certs\\client.crt", private_key="c:\\certs\\client.key", brokers=[Broker.parse("ssl://192.168.189.12")]) # Create the DXL client with DxlClient(config) as dxl_client: # Connect to the fabric dxl_client.connect()
NOTE: The preferred way to construct the client is via the Python "with" statement as shown above. The "with" statement ensures that resources associated with the client are properly cleaned up when the block is exited.
The following modules support the client:
dxlclient.client_config: See this module for information on configuring theDxlClientdxlclient.message: See this module for information on the different types of messages that can be exchanged over the DXL fabricdxlclient.callbacks: See this module for information on registering "callbacks" that are used to receive messages via theDxlClient. This module also includes an example that demonstrate how to senddxlclient.message.Eventmessages.dxlclient.service: See this module for information on registering "services" with the DXL fabric. This module also includes an example that demonstrates how to invoke a DXL service via theDxlClient.
Constructor parameters:
Parameters: config -- The dxlclient.client_config.DxlClientConfigobject containing the configuration settings for the client.-
add_event_callback(topic, event_callback, subscribe_to_topic=True)¶ Adds a
dxlclient.callbacks.EventCallbackto the client for the specified topic. The callback will be invoked whendxlclient.message.Eventmessages are received by the client on the specified topic. A topic ofNoneindicates that the callback should receivedxlclient.message.Eventmessages for all topics (no filtering).Parameters: - topic -- The topic to receive
dxlclient.message.Eventmessages on. A topic ofNoneindicates that the callback should receivedxlclient.message.Eventmessages for all topics (no filtering). - event_callback -- The
dxlclient.callbacks.EventCallbackto be invoked when adxlclient.message.Eventmessage is received on the specified topic - subscribe_to_topic -- Optional parameter to indicate if the client should subscribe
(
dxlclient.client.DxlClient.subscribe()) to the topic. By default the client will subscribe to the topic. SpecifyFalseto prevent subscribing to the topic.
- topic -- The topic to receive
-
add_request_callback(topic, request_callback)¶ Adds a
dxlclient.callbacks.RequestCallbackto the client for the specified topic. The callback will be invoked whendxlclient.message.Requestmessages are received by the client on the specified topic. A topic ofNoneindicates that the callback should receivedxlclient.message.Requestmessages for all topics (no filtering).NOTE: Usage of this method is quite rare due to the fact that registration of
dxlclient.callbacks.RequestCallbackinstances with the client occurs automatically when registering a service. See moduledxlclient.servicefor more information on DXL services.Parameters: - topic -- The topic to receive
dxlclient.message.Requestmessages on. A topic ofNoneindicates that the callback should receivedxlclient.message.Requestmessages for all topics (no filtering). - request_callback -- The
dxlclient.callbacks.RequestCallbackto be invoked when adxlclient.message.Requestmessage is received on the specified topic
- topic -- The topic to receive
-
add_response_callback(topic, response_callback)¶ Adds a
dxlclient.callbacks.ResponseCallbackto the client for the specified topic. The callback will be invoked whendxlclient.message.Responsemessages are received by the client on the specified topic. A topic ofNoneindicates that the callback should receivedxlclient.message.Responsemessages for all topics (no filtering).NOTE: Usage of this method is quite rare due to the fact that the use of
dxlclient.callbacks.ResponseCallbackinstances are typically limited to invoking a remote DXL service via theasync_request()method.Parameters: - topic -- The topic to receive
dxlclient.message.Requestmessages on. A topic ofNoneindicates that the callback should receivedxlclient.message.Requestmessages for all topics (no filtering). - request_callback -- The
dxlclient.callbacks.RequestCallbackto be invoked when adxlclient.message.Requestmessage is received on the specified topic
- topic -- The topic to receive
-
async_request(request, response_callback=None)¶ Sends a
dxlclient.message.Requestmessage to a remote DXL service asynchronously. This method differs fromsync_request()due to the fact that it returns to the caller immediately after delivering thedxlclient.message.Requestmessage to the DXL fabric (It does not wait for the correspondingdxlclient.message.Responseto be received).An optional
dxlclient.callbacks.ResponseCallbackcan be specified. This callback will be invoked when the correspondingdxlclient.message.Responsemessage is received by the client.See module
dxlclient.servicefor more information on DXL services.Parameters: - request -- The
dxlclient.message.Requestmessage to send to a remote DXL service - response_callback -- An optional
dxlclient.callbacks.ResponseCallbackthat will be invoked when the correspondingdxlclient.message.Responsemessage is received by the client.
- request -- The
-
config¶ The
dxlclient.client_config.DxlClientConfiginstance that was specified when the client was constructed.See
dxlclient.client_configfor more information on configuring the client.
-
connect()¶ Attempts to connect the client to the DXL fabric.
This method does not return until either the client has connected to the fabric or it has exhausted the number of retries configured for the client causing an exception to be raised.
Several attributes are available for controlling the client retry behavior:
dxlclient.client_config.DxlClientConfig.connect_retries: The maximum number of connection attempts for eachdxlclient.broker.Brokerspecified in thedxlclient.client_config.DxlClientConfigdxlclient.client_config.DxlClientConfig.reconnect_delay: The initial delay between retry attempts. The delay increases ("backs off") as subsequent connection attempts are made.dxlclient.client_config.DxlClientConfig.reconnect_back_off_multiplier: Multiples the current reconnect delay by this value on subsequent connect retries. For example, a current delay of 3 seconds with a multiplier of 2 would result in the next retry attempt being in 6 seconds.dxlclient.client_config.DxlClientConfig.reconnect_delay_random: A randomness delay percentage (between 0.0 and 1.0) that is used to increase the current retry delay by a random amount for the purpose of preventing multiple clients from having the same retry patterndxlclient.client_config.DxlClientConfig.reconnect_delay_max: The maximum delay between retry attempts
-
connected¶ Whether the client is currently connected to the DXL fabric.
-
current_broker¶ The
dxlclient.broker.Brokerthat the client is currently connected to.Noneis returned if the client is not currently connected to adxlclient.broker.Broker.
-
destroy()¶ Destroys the client (releases all associated resources).
NOTE: Once the method has been invoked, no other calls should be made to the client.
Also note that this method should rarely be called directly. Instead, the preferred usage of the client is via a Python "with" statement as shown below:
# Create the DXL client with DxlClient(config) as dxl_client: # Connect to the fabric dxl_client.connect()
The "with" statement ensures that resources associated with the client are properly cleaned up when the block is exited (the
destroy()method is invoked).
-
disconnect()¶ Attempts to disconnect the client from the DXL fabric.
-
register_service_async(service_reg_info)¶ Registers a DXL service with the fabric asynchronously. The specified
dxlclient.service.ServiceRegistrationInfoinstance contains information about the service that is to be registered.This method differs from
register_service_sync()due to the fact that it returns to the caller immediately after sending the registration message to the DXL fabric (It does not wait for registration confirmation before returning).See
dxlclient.servicefor more information on DXL services.Parameters: service_reg_info -- A dxlclient.service.ServiceRegistrationInfoinstance containing information about the service that is to be registered.
-
register_service_sync(service_req_info, timeout)¶ Registers a DXL service with the fabric. The specified
dxlclient.service.ServiceRegistrationInfoinstance contains information about the service that is to be registered.This method will wait for confirmation of the service registration for up to the specified timeout in seconds. If the timeout is exceeded an exception will be raised.
See
dxlclient.servicefor more information on DXL services.Parameters: - service_reg_info -- A
dxlclient.service.ServiceRegistrationInfoinstance containing information about the service that is to be registered. - timeout -- The amount of time (in seconds) to wait for confirmation of the service registration. If the timeout is exceeded an exception will be raised.
- service_reg_info -- A
-
remove_event_callback(topic, event_callback, unsubscribe_from_topic=True)¶ Removes a
dxlclient.callbacks.EventCallbackfrom the client for the specified topic. This method must be invoked with the same arguments as when the callback was originally registered viaadd_event_callback().Parameters: - topic -- The topic to remove the callback for
- event_callback -- The
dxlclient.callbacks.EventCallbackto be removed for the specified topic - unsubscribe_from_topic -- Optional parameter to indicate if the client should also unsubscribe
(
dxlclient.client.DxlClient.unsubscribe()) from the topic. By default the client will unsubscribe from the topic. SpecifyFalseto prevent unsubscribing to the topic.
-
remove_request_callback(topic, request_callback)¶ Removes a
dxlclient.callbacks.RequestCallbackfrom the client for the specified topic. This method must be invoked with the same arguments as when the callback was originally registered viaadd_request_callback().Parameters: - topic -- The topic to remove the callback for
- request_callback -- The
dxlclient.callbacks.RequestCallbackto be removed for the specified topic
-
remove_response_callback(topic, response_callback)¶ Removes a
dxlclient.callbacks.ResponseCallbackfrom the client for the specified topic. This method must be invoked with the same arguments as when the callback was originally registered viaadd_response_callback().Parameters: - topic -- The topic to remove the callback for
- response_callback -- The
dxlclient.callbacks.ResponseCallbackto be removed for the specified topic
-
send_event(event)¶ Attempts to deliver the specified
dxlclient.message.Eventmessage to the DXL fabric.See module
dxlclient.messagefor more information on message types, how they are delivered to remote clients, etc.Parameters: event -- The dxlclient.message.Eventto send
-
send_response(response)¶ Attempts to deliver the specified
dxlclient.message.Responsemessage to the DXL fabric. The fabric will in turn attempt to deliver the response back to the client who sent the correspondingdxlclient.message.Request.See module
dxlclient.messagefor more information on message types, how they are delivered to remote clients, etc.See module
dxlclient.servicefor more information on DXL services.Parameters: event -- The dxlclient.message.Eventto send
-
subscribe(topic)¶ Subscribes to the specified topic on the DXL fabric. This method is typically used in conjunction with the registration of
dxlclient.callbacks.EventCallbackinstances via theadd_event_callback()method.The following is a simple example of using this:
from dxlclient.callbacks import EventCallback class MyEventCallback(EventCallback): def on_event(self, event): print("Received event! " + event.source_client_id) dxl_client.add_event_callback("/testeventtopic", MyEventCallback(), False) dxl_client.subscribe("/testeventtopic")
NOTE: By default when registering an event callback the client will automatically subscribe to the topic. In this example the
dxlclient.client.DxlClient.add_event_callback()method is invoked with thesubscribe_to_topicparameter set toFalsepreventing the automatic subscription.Parameters: topic -- The topic to subscribe to
-
subscriptions¶ A tuple containing the topics that the client is currently subscribed to
See
subscribe()for more information on adding subscriptions
-
sync_request(request, timeout=3600)¶ Sends a
dxlclient.message.Requestmessage to a remote DXL service.See module
dxlclient.servicefor more information on DXL services.Parameters: - request -- The
dxlclient.message.Requestmessage to send to a remote DXL service - timeout -- The amount of time (in seconds) to wait for the
dxlclient.message.Responseto the request. If the timeout is exceeded an exception will be raised. Defaults to3600seconds (1 hour)
- request -- The
-
unregister_service_async(service_reg_info)¶ Unregisters (removes) a DXL service with from the fabric asynchronously. The specified
dxlclient.service.ServiceRegistrationInfoinstance contains information about the service that is to be removed.This method differs from
unregister_service_sync()due to the fact that it returns to the caller immediately after sending the unregistration message to the DXL fabric (It does not wait for unregistration confirmation before returning).See
dxlclient.servicefor more information on DXL services.Parameters: service_reg_info -- A dxlclient.service.ServiceRegistrationInfoinstance containing information about the service that is to be unregistered.
-
unregister_service_sync(service_req_info, timeout)¶ Unregisters (removes) a DXL service from the fabric. The specified
dxlclient.service.ServiceRegistrationInfoinstance contains information about the service that is to be removed.This method will wait for confirmation of the service unregistration for up to the specified timeout in seconds. If the timeout is exceeded an exception will be raised.
See
dxlclient.servicefor more information on DXL services.Parameters: - service_reg_info -- A
dxlclient.service.ServiceRegistrationInfoinstance containing information about the service that is to be removed. - timeout -- The amount of time (in seconds) to wait for confirmation of the service unregistration. If the timeout is exceeded an exception will be raised.
- service_reg_info -- A
-
unsubscribe(topic)¶ Unsubscribes from the specified topic on the DXL fabric.
See the
subscribe()method for more information on subscriptions.Parameters: topic -- The topic to unsubscribe from