iotile.core.utilities.validating_wsclient

A websocket client that validates messages received and dispatches them.

Module Contents

SuccessfulResponseSchema
FailureResponseSchema
ResponseSchema
class ValidatingWSClient(url, logger_name=__name__)

Bases:ws4py.client.threadedclient.WebSocketClient

A threaded websocket client that validates messages received.

Messages are assumed to be packed using msgpack in a binary format and are decoded and validated against message type schema. Matching messages are dispatched to the appropriate handler and messages that match no attached schema are logged and dropped.

connected
add_message_type(self, validator, callback)

Add a message type that should trigger a callback.

Each validator must be unique, in that a message will be dispatched to the first callback whose validator matches the message.

Args:
validator (Verifier): A schema verifier that will
validate a received message uniquely
callback (callable): The function that should be called
when a message that matches validator is received.
start(self, timeout=5.0)

Synchronously connect to websocket server.

Args:
timeout (float): The maximum amount of time to wait for the
connection to be established. Defaults to 5 seconds
stop(self, timeout=5.0)

Synchronously disconnect from websocket server.

Args:
timeout (float): The maximum amount of time to wait for the
connection to be established. Defaults to 5 seconds
send_message(self, obj)

Send a packed message.

Args:
obj (dict): The message to be sent
send_command(self, command, args, timeout=10.0)

Send a command and synchronously wait for a response.

Args:
command (string): The command name args (dict): Optional arguments timeout (float): The maximum time to wait for a response
send_ping(self, timeout=10.0)

Send a ping message to keep connection alive and to verify if the server is still there.

ponged(self, pong)

Pong message received after a ping was sent

post_command(self, command, args)

Post a command asynchronously and don’t wait for a response.

Args:
command (string): The command name args (dict): Optional arguments
opened(self)

Callback called in another thread when a connection is opened.

closed(self, code, reason)

Callback called in another thread when a connection is closed.

Args:
code (int): A code for the closure reason (string): A reason for the closure
_unpack(self, msg)

Unpack a binary msgpacked message.

classmethod decode_datetime(cls, obj)

Decode a msgpack’ed datetime.

classmethod encode_datetime(cls, obj)

Encode a msgpck’ed datetime.

received_message(self, message)

Callback when a message is received.

The message must be encoded using message_mack

Args:
message (object): The message that was received
_on_response_received(self, resp)