CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
psyclone_client.client.PsycloneClient Class Reference

Public Member Functions

 __init__ (self, transport, device, options)
 udp (cls, host, port, device=None, **options)
 tcp (cls, host, port, device=None, **options)
 tls (cls, host, port, device=None, **options)
 websocket (cls, url, device=None, **options)
 transport (self)
 connected (self)
 stats (self)
 server_features (self)
 on_trigger (self, trigger, handler)
 on_any_message (self, handler)
 on_text (self, handler)
 on_state_changed (self, listener)
 remove_handler (self, handler)
 post (self, post, msg=None)
 send_text (self, text)
 request_async (self, post, msg=None, timeout=5.0)
 request (self, post, msg=None, timeout=5.0)
 wait_connected (self, timeout)
 wait_server (self, timeout)
 close (self)
 __enter__ (self)
 __exit__ (self, *exc)

Public Attributes

 device = device
 options = options
int sent = 0
int received = 0
int bad = 0
bool packed_active = PackedMode.PACKED
bool server_answers_keepalive = False
 connected

Protected Member Functions

 _on_bytes (self, data)
 _on_text_frame (self, text)
 _on_state (self, connected, error)
 _heard (self, n)
 _state_changed (self, transport_up, error)
 _keepalive_reply (self, msg)
 _keepalive_loop (self)
 _tick (self)
 _send_keepalive (self)
 _receive (self, msg)
 _run_callback (self, fn)
 _dispatch (self, calls, raw=False)

Protected Attributes

 _transport = transport
dict _handlers = {}
list _any = []
list _text = []
list _state = []
dict _pending = {}
list _modules = []
 _lock = threading.Lock()
 _seq = itertools.count(1)
str _prefix = "r%x-" % (int.from_bytes(os.urandom(6), "little"))
bool _closed = False
 _counters = StatsCounters()
# a new connection may reach another server negotiate again _stream = not isinstance(transport, UdpTransport)
 _ka_seq = itertools.count(1)
bool _link_dead = False
 _state_lock = threading.Lock()
bool _last_up = False
 _wake = threading.Event()
 _ka_thread = threading.Thread(target=self._keepalive_loop, name="psyclone-keepalive", daemon=True)

Detailed Description

A client for a Psyclone ``<service>``, over UDP, TCP or WebSocket.

::

    psy = PsycloneClient.websocket("wss://host:8443/phone", device="phone-1")
    psy.on_trigger("Playout", lambda msg: play(msg.get_data("audio")))   # the system pushes to us
    psy.post("AudioFrame", DataMessage().set_data("audio", opus))        # fire-and-forget into it
    answer = psy.request("Question", DataMessage().set_string("Text", "hi"))  # reply, or None
    psy.close()

Every message sent names one of the service's posts in ``PsyPost`` (the service applies that post's
type; a client never sets a type) and carries ``PsyDevice`` when ``device`` is set. Every message
received carries ``PsyTrigger``. ``request()`` adds ``PsyClientRequestID`` and waits for the reply
carrying it back: a responder that copies the request (the built-in ``Simple`` crank) does; one that
builds a new message must copy ``PsyClientRequestID`` and ``PsyServiceConnection``.

Every ``keepalive_interval`` (and right after connecting) the client sends a keepalive
(``PsyPost=PsyKeepAlive``, ``PsyKeepAlive=<seq>``). A Psyclone 2.3.2+ service answers it without posting
anything (round-trip time, ``PsyPacked``, ``PsyVersion``; 2.3.2+ also ``PsyFeatures``); an older one
drops it after refreshing the peer's address, which still keeps a UDP path (NAT) open, and keeps a
2.3.2+ service from forgetting a silent UDP peer. Once a service has answered keepalives, a connection
that stays silent for ``keepalive_miss_limit`` intervals is treated as dead: TCP/WebSocket reconnect,
UDP reports the link down and opens a new socket.

Handlers run on the reader thread unless ``callback`` is given. TCP and WebSocket reconnect by
themselves, with a doubling, jittered back-off; nothing is queued while a pipe is down (``post()``
returns False). The service learns a UDP peer's address from its first datagram (the first keepalive
does this at once).

Definition at line 103 of file client.py.

Constructor & Destructor Documentation

◆ __init__()

psyclone_client.client.PsycloneClient.__init__ ( self,
transport,
device,
options )

Definition at line 134 of file client.py.

Member Function Documentation

◆ __enter__()

psyclone_client.client.PsycloneClient.__enter__ ( self)

Definition at line 505 of file client.py.

◆ __exit__()

psyclone_client.client.PsycloneClient.__exit__ ( self,
* exc )

Definition at line 508 of file client.py.

References close().

◆ _dispatch()

psyclone_client.client.PsycloneClient._dispatch ( self,
calls,
raw = False )
protected

Definition at line 344 of file client.py.

References cmlabs::ParamInfo.options, and options.

Referenced by _on_text_frame(), _receive(), _run_callback(), and _state_changed().

◆ _heard()

psyclone_client.client.PsycloneClient._heard ( self,
n )
protected
Count what arrived; a UDP link that was judged dead is up again.

Definition at line 228 of file client.py.

References _counters, _link_dead, _state_changed(), and _transport.

Referenced by _on_bytes(), and _on_text_frame().

◆ _keepalive_loop()

psyclone_client.client.PsycloneClient._keepalive_loop ( self)
protected
Every keepalive interval: judge the link, then send a keepalive (also at once after connecting).

Definition at line 275 of file client.py.

References _closed, _counters, _send_keepalive(), _tick(), _wake, cmlabs::ParamInfo.options, and options.

◆ _keepalive_reply()

psyclone_client.client.PsycloneClient._keepalive_reply ( self,
msg )
protected

Definition at line 257 of file client.py.

References _counters, cmlabs::ParamInfo.options, options, packed_active, and server_answers_keepalive.

Referenced by _on_bytes().

◆ _on_bytes()

psyclone_client.client.PsycloneClient._on_bytes ( self,
data )
protected

◆ _on_state()

psyclone_client.client.PsycloneClient._on_state ( self,
connected,
error )
protected

◆ _on_text_frame()

psyclone_client.client.PsycloneClient._on_text_frame ( self,
text )
protected

◆ _receive()

psyclone_client.client.PsycloneClient._receive ( self,
msg )
protected

Definition at line 321 of file client.py.

References _any, _dispatch(), _handlers, _lock, _modules, _pending, and received.

Referenced by _on_bytes(), and _on_text_frame().

◆ _run_callback()

psyclone_client.client.PsycloneClient._run_callback ( self,
fn )
protected
Run fn() where handlers run (the ``callback`` executor, or here).

Definition at line 340 of file client.py.

References _dispatch().

◆ _send_keepalive()

psyclone_client.client.PsycloneClient._send_keepalive ( self)
protected

Definition at line 309 of file client.py.

References _closed, _counters, _ka_seq, _transport, device, and packed_active.

Referenced by _keepalive_loop(), and _tick().

◆ _state_changed()

psyclone_client.client.PsycloneClient._state_changed ( self,
transport_up,
error )
protected

Definition at line 236 of file client.py.

References _closed, _counters, _dispatch(), _last_up, _link_dead, _state, and _state_lock.

Referenced by _heard(), _on_state(), and _tick().

◆ _tick()

psyclone_client.client.PsycloneClient._tick ( self)
protected

◆ close()

psyclone_client.client.PsycloneClient.close ( self)

◆ connected()

psyclone_client.client.PsycloneClient.connected ( self)
Stream transports: while the socket is up. UDP: while its socket is open and (once the service has
answered keepalives) the service keeps answering.

Definition at line 365 of file client.py.

References _link_dead, and _transport.

◆ on_any_message()

psyclone_client.client.PsycloneClient.on_any_message ( self,
handler )
Call handler(msg) for every message received (after trigger handlers), except request replies.

Definition at line 385 of file client.py.

References _any.

◆ on_state_changed()

psyclone_client.client.PsycloneClient.on_state_changed ( self,
listener )
listener(connected, error) when the connection comes up or goes down (error is None for a clean
change); failed reconnect attempts are reported as (False, error) too. UDP: up while its socket is
open and, once the service has answered a keepalive, while it keeps answering.

Definition at line 395 of file client.py.

References _state.

◆ on_text()

psyclone_client.client.PsycloneClient.on_text ( self,
handler )
WebSocket text frames: handler(str). Without one they arrive as messages with a ``Text`` entry.

Definition at line 390 of file client.py.

References _text.

◆ on_trigger()

psyclone_client.client.PsycloneClient.on_trigger ( self,
trigger,
handler )
Call handler(msg) for every message the service trigger named ``trigger`` sends.

Definition at line 380 of file client.py.

References _handlers.

◆ post()

psyclone_client.client.PsycloneClient.post ( self,
post,
msg = None )
Post ``msg`` (sets ``PsyPost`` and ``PsyDevice`` on it) as the service post named ``post``.
Returns False when the pipe is down (nothing is queued).

Definition at line 410 of file client.py.

References _closed, _counters, _transport, device, packed_active, and sent.

Referenced by test_remote_module.Probe.machine_key(), and request_async().

◆ remove_handler()

psyclone_client.client.PsycloneClient.remove_handler ( self,
handler )
Remove ``handler`` from on_any_message and every on_trigger list.

Definition at line 402 of file client.py.

References _any, and _handlers.

◆ request()

psyclone_client.client.PsycloneClient.request ( self,
post,
msg = None,
timeout = 5.0 )
Blocking request: the reply DataMessage, or None on timeout or when the pipe is down.

Definition at line 467 of file client.py.

References request_async().

◆ request_async()

psyclone_client.client.PsycloneClient.request_async ( self,
post,
msg = None,
timeout = 5.0 )
Post and return a concurrent.futures.Future for the reply (TimeoutError after ``timeout`` s).

Definition at line 439 of file client.py.

References _counters, _lock, _pending, _prefix, _seq, and post().

Referenced by request().

◆ send_text()

psyclone_client.client.PsycloneClient.send_text ( self,
text )
Send a WebSocket text frame (posted as a ``Text`` entry to the service's only post).

Definition at line 429 of file client.py.

References _counters, _transport, send_text(), and sent.

Referenced by send_text().

◆ server_features()

psyclone_client.client.PsycloneClient.server_features ( self)
The service's PsyFeatures from its last keepalive reply (a list; empty before Psyclone 2.3.2).

Definition at line 376 of file client.py.

References _counters.

◆ stats()

psyclone_client.client.PsycloneClient.stats ( self)
A ConnectionStats snapshot of this connection's counters, state and round-trip times.

Definition at line 370 of file client.py.

◆ tcp()

psyclone_client.client.PsycloneClient.tcp ( cls,
host,
port,
device = None,
** options )
TCP to an ``<interface protocol="Message">`` (CMSDK Message framing).

Definition at line 174 of file client.py.

◆ tls()

psyclone_client.client.PsycloneClient.tls ( cls,
host,
port,
device = None,
** options )
TCP + TLS to an ``<interface protocol="Message">`` with encryption.

Definition at line 180 of file client.py.

◆ transport()

psyclone_client.client.PsycloneClient.transport ( self)
'udp', 'tcp', 'tls', 'ws' or 'wss'.

Definition at line 360 of file client.py.

References _transport.

Referenced by psyclone_client.stats.ConnectionStats.__str__().

◆ udp()

psyclone_client.client.PsycloneClient.udp ( cls,
host,
port,
device = None,
** options )
UDP to an ``<interface ip="UDP" protocol="Message">``; keep messages under ~1200 bytes on mobile.

Definition at line 168 of file client.py.

◆ wait_connected()

psyclone_client.client.PsycloneClient.wait_connected ( self,
timeout )
Wait until connected (``connected``); True if it is.

Definition at line 474 of file client.py.

References _closed, cmlabs::LLMConnection.connected, and connected.

◆ wait_server()

psyclone_client.client.PsycloneClient.wait_server ( self,
timeout )
Wait until the service has answered a keepalive (a Psyclone 2.3.2+ service; AUTO has then
negotiated the packed form); True if it has.

Definition at line 483 of file client.py.

References _closed, and server_answers_keepalive.

◆ websocket()

psyclone_client.client.PsycloneClient.websocket ( cls,
url,
device = None,
** options )
WebSocket (``ws://`` or ``wss://``) to a service's HTTP interface; any path.

Definition at line 186 of file client.py.

Member Data Documentation

◆ _any

list psyclone_client.client.PsycloneClient._any = []
protected

◆ _closed

◆ _counters

psyclone_client.client.PsycloneClient._counters = StatsCounters()
protected

◆ _handlers

dict psyclone_client.client.PsycloneClient._handlers = {}
protected

◆ _ka_seq

psyclone_client.client.PsycloneClient._ka_seq = itertools.count(1)
protected

Definition at line 153 of file client.py.

Referenced by _send_keepalive().

◆ _ka_thread

psyclone_client.client.PsycloneClient._ka_thread = threading.Thread(target=self._keepalive_loop, name="psyclone-keepalive", daemon=True)
protected

Definition at line 162 of file client.py.

◆ _last_up

bool psyclone_client.client.PsycloneClient._last_up = False
protected

Definition at line 160 of file client.py.

Referenced by _state_changed().

◆ _link_dead

bool psyclone_client.client.PsycloneClient._link_dead = False
protected

Definition at line 158 of file client.py.

Referenced by _heard(), _on_state(), _state_changed(), _tick(), and connected().

◆ _lock

psyclone_client.client.PsycloneClient._lock = threading.Lock()
protected

◆ _modules

psyclone_client.client.PsycloneClient._modules = []
protected

Definition at line 143 of file client.py.

Referenced by _receive().

◆ _pending

dict psyclone_client.client.PsycloneClient._pending = {}
protected

◆ _prefix

str psyclone_client.client.PsycloneClient._prefix = "r%x-" % (int.from_bytes(os.urandom(6), "little"))
protected

◆ _seq

psyclone_client.client.PsycloneClient._seq = itertools.count(1)
protected

◆ _state

list psyclone_client.client.PsycloneClient._state = []
protected

Definition at line 141 of file client.py.

Referenced by _state_changed(), and on_state_changed().

◆ _state_lock

psyclone_client.client.PsycloneClient._state_lock = threading.Lock()
protected

Definition at line 159 of file client.py.

Referenced by _state_changed().

◆ _stream

# a new connection may reach another server negotiate again psyclone_client.client.PsycloneClient._stream = not isinstance(transport, UdpTransport)
protected

Definition at line 152 of file client.py.

Referenced by _on_state(), and _tick().

◆ _text

psyclone_client.client.PsycloneClient._text = []
protected

Definition at line 140 of file client.py.

Referenced by _on_text_frame(), and on_text().

◆ _transport

psyclone_client.client.PsycloneClient._transport = transport
protected

Definition at line 135 of file client.py.

Referenced by _heard(), _send_keepalive(), _tick(), close(), connected(), post(), send_text(), and transport().

◆ _wake

psyclone_client.client.PsycloneClient._wake = threading.Event()
protected

Definition at line 161 of file client.py.

Referenced by _keepalive_loop(), _on_state(), and close().

◆ bad

psyclone_client.client.PsycloneClient.bad = 0

Definition at line 150 of file client.py.

Referenced by _on_bytes().

◆ connected

psyclone_client.client.PsycloneClient.connected

Definition at line 372 of file client.py.

Referenced by psyclone_client.stats.ConnectionStats.__str__(), and wait_connected().

◆ device

psyclone_client.client.PsycloneClient.device = device

Definition at line 136 of file client.py.

Referenced by _send_keepalive(), and post().

◆ options

◆ packed_active

psyclone_client.client.PsycloneClient.packed_active = PackedMode.PACKED

Definition at line 155 of file client.py.

Referenced by _keepalive_reply(), _on_bytes(), _on_state(), _send_keepalive(), and post().

◆ received

psyclone_client.client.PsycloneClient.received = 0

Definition at line 149 of file client.py.

Referenced by _on_text_frame(), and _receive().

◆ sent

psyclone_client.client.PsycloneClient.sent = 0

Definition at line 148 of file client.py.

Referenced by post(), and send_text().

◆ server_answers_keepalive

bool psyclone_client.client.PsycloneClient.server_answers_keepalive = False

Definition at line 157 of file client.py.

Referenced by _keepalive_reply(), _on_state(), _tick(), and wait_server().


The documentation for this class was generated from the following file:
  • python-client/src/psyclone_client/client.py