Poll Frontend

An MQTT client that demonstrates a select-based polling interface.

A loopback client that subscribes to a topic that it publishes to. Every time a message is published it should be echoed back to the client by the remote MQTT broker.

class examples.frontend_poll.ExampleMqttClient(endpoint)[source]

Bases: haka_mqtt.frontends.event_queue.MqttEventEnqueue, haka_mqtt.frontends.poll.MqttPollClient

A helper class for polling mqtt events.

It is critical that the order of inheritance is correct for this class to work correctly. The MqttEventEnqueue class must appear before MqttPollClient so that its methods are called first.

expect_event(predicate, timeout=None)[source]

Waits any event to occur and returns it if predicate(event) returns True; otherwise raises an exception. If timeout expires before any event is received then returns None.

Parameters:
  • predicate (callable) – A callable that will be passed a single argument that will be of type mqtt_codec.packet.MqttPacketBody or haka_mqtt.frontends.event_queue.MqttConnectionEvent.
  • timeout (float or None) – Maximum amount of time to wait for an event. If None then waits forever.
Raises:

UnexpectedMqttEventError – The first even that occurs predicate(event) returns False.

Returns:

Returns an event matching predicate(e) or None if no such event occurred before timeout.

Return type:

mqtt_codec.packet.MqttPacketBody or haka_mqtt.frontends.event_queue.MqttConnectionEvent or None

poll_until_event(timeout=None)[source]

Polls connection until an event occurs then returns it. If timeout passes without an event occurring returns None.

Parameters:timeout (float or None) – Maximum amount of time to wait for an event. If None then waits forever. Must satisfy condition timeout >= 0.
Returns:None is returned if no event occurs.
Return type:mqtt_codec.packet.MqttPacketBody or MqttConnectionEvent or None
exception examples.frontend_poll.UnexpectedMqttEventError(e)[source]

Bases: exceptions.Exception

examples.frontend_poll.argparse_endpoint(s)[source]

Splits an incoming string into host and port components.

>>> argparse_endpoint('localhost:1883')
('localhost', 1883)
Parameters:s (str) –
Raises:ArgumentTypeError – Raised when port number is out of range 1 <= port <= 65535, when port is not an integer, or when there is more than one colon in the string.
Returns:hostname, port tuple.
Return type:(str, int)
examples.frontend_poll.create_parser()[source]

Creates a command-line argument parser used by the program main method.

Returns:
Return type:ArgumentParser
examples.frontend_poll.main(args=['-T', '-E', '-b', 'readthedocs', '-d', '_build/doctrees-readthedocs', '-D', 'language=en', '.', '_build/html'])[source]

Parses arguments and passes them to run() method. Returns one when an error occurs.

Returns:
Return type:int
examples.frontend_poll.run(client)[source]
Raises:UnexpectedMqttEventError
Parameters:client (ExampleMqttClient) –