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._BaseObject
The
DxlClient
class 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
DxlClient
instance 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 theDxlClient
dxlclient.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.Event
messages.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.DxlClientConfig
object containing the configuration settings for the client.-
add_event_callback
(topic, event_callback, subscribe_to_topic=True)¶ Adds a
dxlclient.callbacks.EventCallback
to the client for the specified topic. The callback will be invoked whendxlclient.message.Event
messages are received by the client on the specified topic. A topic ofNone
indicates that the callback should receivedxlclient.message.Event
messages for all topics (no filtering).Parameters: - topic -- The topic to receive
dxlclient.message.Event
messages on. A topic ofNone
indicates that the callback should receivedxlclient.message.Event
messages for all topics (no filtering). - event_callback -- The
dxlclient.callbacks.EventCallback
to be invoked when adxlclient.message.Event
message 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. SpecifyFalse
to prevent subscribing to the topic.
- topic -- The topic to receive
-
add_request_callback
(topic, request_callback)¶ Adds a
dxlclient.callbacks.RequestCallback
to the client for the specified topic. The callback will be invoked whendxlclient.message.Request
messages are received by the client on the specified topic. A topic ofNone
indicates that the callback should receivedxlclient.message.Request
messages for all topics (no filtering).NOTE: Usage of this method is quite rare due to the fact that registration of
dxlclient.callbacks.RequestCallback
instances with the client occurs automatically when registering a service. See moduledxlclient.service
for more information on DXL services.Parameters: - topic -- The topic to receive
dxlclient.message.Request
messages on. A topic ofNone
indicates that the callback should receivedxlclient.message.Request
messages for all topics (no filtering). - request_callback -- The
dxlclient.callbacks.RequestCallback
to be invoked when adxlclient.message.Request
message is received on the specified topic
- topic -- The topic to receive
-
add_response_callback
(topic, response_callback)¶ Adds a
dxlclient.callbacks.ResponseCallback
to the client for the specified topic. The callback will be invoked whendxlclient.message.Response
messages are received by the client on the specified topic. A topic ofNone
indicates that the callback should receivedxlclient.message.Response
messages for all topics (no filtering).NOTE: Usage of this method is quite rare due to the fact that the use of
dxlclient.callbacks.ResponseCallback
instances are typically limited to invoking a remote DXL service via theasync_request()
method.Parameters: - topic -- The topic to receive
dxlclient.message.Request
messages on. A topic ofNone
indicates that the callback should receivedxlclient.message.Request
messages for all topics (no filtering). - request_callback -- The
dxlclient.callbacks.RequestCallback
to be invoked when adxlclient.message.Request
message is received on the specified topic
- topic -- The topic to receive
-
async_request
(request, response_callback=None)¶ Sends a
dxlclient.message.Request
message 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.Request
message to the DXL fabric (It does not wait for the correspondingdxlclient.message.Response
to be received).An optional
dxlclient.callbacks.ResponseCallback
can be specified. This callback will be invoked when the correspondingdxlclient.message.Response
message is received by the client.See module
dxlclient.service
for more information on DXL services.Parameters: - request -- The
dxlclient.message.Request
message to send to a remote DXL service - response_callback -- An optional
dxlclient.callbacks.ResponseCallback
that will be invoked when the correspondingdxlclient.message.Response
message is received by the client.
- request -- The
-
config
¶ The
dxlclient.client_config.DxlClientConfig
instance that was specified when the client was constructed.See
dxlclient.client_config
for 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.Broker
specified in thedxlclient.client_config.DxlClientConfig
dxlclient.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.Broker
that the client is currently connected to.None
is 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.ServiceRegistrationInfo
instance 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.service
for more information on DXL services.Parameters: service_reg_info -- A dxlclient.service.ServiceRegistrationInfo
instance 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.ServiceRegistrationInfo
instance 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.service
for more information on DXL services.Parameters: - service_reg_info -- A
dxlclient.service.ServiceRegistrationInfo
instance 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.EventCallback
from 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.EventCallback
to 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. SpecifyFalse
to prevent unsubscribing to the topic.
-
remove_request_callback
(topic, request_callback)¶ Removes a
dxlclient.callbacks.RequestCallback
from 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.RequestCallback
to be removed for the specified topic
-
remove_response_callback
(topic, response_callback)¶ Removes a
dxlclient.callbacks.ResponseCallback
from 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.ResponseCallback
to be removed for the specified topic
-
send_event
(event)¶ Attempts to deliver the specified
dxlclient.message.Event
message to the DXL fabric.See module
dxlclient.message
for more information on message types, how they are delivered to remote clients, etc.Parameters: event -- The dxlclient.message.Event
to send
-
send_response
(response)¶ Attempts to deliver the specified
dxlclient.message.Response
message 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.message
for more information on message types, how they are delivered to remote clients, etc.See module
dxlclient.service
for more information on DXL services.Parameters: event -- The dxlclient.message.Event
to 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.EventCallback
instances 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_topic
parameter set toFalse
preventing 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.Request
message to a remote DXL service.See module
dxlclient.service
for more information on DXL services.Parameters: - request -- The
dxlclient.message.Request
message to send to a remote DXL service - timeout -- The amount of time (in seconds) to wait for the
dxlclient.message.Response
to the request. If the timeout is exceeded an exception will be raised. Defaults to3600
seconds (1 hour)
- request -- The
-
unregister_service_async
(service_reg_info)¶ Unregisters (removes) a DXL service with from the fabric asynchronously. The specified
dxlclient.service.ServiceRegistrationInfo
instance 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.service
for more information on DXL services.Parameters: service_reg_info -- A dxlclient.service.ServiceRegistrationInfo
instance 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.ServiceRegistrationInfo
instance 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.service
for more information on DXL services.Parameters: - service_reg_info -- A
dxlclient.service.ServiceRegistrationInfo
instance 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