|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
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) | |
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).
| psyclone_client.client.PsycloneClient.__init__ | ( | self, | |
| transport, | |||
| device, | |||
| options ) |
| psyclone_client.client.PsycloneClient.__exit__ | ( | self, | |
| * | exc ) |
|
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().
|
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().
|
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.
|
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().
|
protected |
Definition at line 192 of file client.py.
References _heard(), psyclone_client.client._is_packed(), _keepalive_reply(), _receive(), psyclone_client.remote_module.RemoteModule._receive, bad, cmlabs::ParamInfo.options, options, and packed_active.
|
protected |
Definition at line 215 of file client.py.
References _closed, _counters, _link_dead, _state_changed(), _stream, _wake, cmlabs::ParamInfo.options, options, packed_active, and server_answers_keepalive.
|
protected |
Definition at line 207 of file client.py.
References _dispatch(), _heard(), _receive(), psyclone_client.remote_module.RemoteModule._receive, _text, and received.
|
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().
|
protected |
Run fn() where handlers run (the ``callback`` executor, or here).
Definition at line 340 of file client.py.
References _dispatch().
|
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().
|
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().
|
protected |
Definition at line 294 of file client.py.
References _counters, _link_dead, _send_keepalive(), _state_changed(), _stream, _transport, cmlabs::ParamInfo.options, options, and server_answers_keepalive.
Referenced by _keepalive_loop().
| psyclone_client.client.PsycloneClient.close | ( | self | ) |
Definition at line 493 of file client.py.
References _closed, _lock, _pending, _transport, _wake, and close().
Referenced by __exit__(), psyclone_client.remote_module.RemoteModule.__exit__(), and close().
| 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.
| psyclone_client.client.PsycloneClient.on_any_message | ( | self, | |
| handler ) |
| 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.
| psyclone_client.client.PsycloneClient.on_text | ( | self, | |
| handler ) |
| psyclone_client.client.PsycloneClient.on_trigger | ( | self, | |
| trigger, | |||
| handler ) |
| 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().
| psyclone_client.client.PsycloneClient.remove_handler | ( | self, | |
| handler ) |
| 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().
| psyclone_client.client.PsycloneClient.request_async | ( | self, | |
| post, | |||
| msg = None, | |||
| timeout = 5.0 ) |
| 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().
| psyclone_client.client.PsycloneClient.server_features | ( | self | ) |
| psyclone_client.client.PsycloneClient.stats | ( | self | ) |
| psyclone_client.client.PsycloneClient.tcp | ( | cls, | |
| host, | |||
| port, | |||
| device = None, | |||
| ** | options ) |
| psyclone_client.client.PsycloneClient.tls | ( | cls, | |
| host, | |||
| port, | |||
| device = None, | |||
| ** | options ) |
| 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__().
| psyclone_client.client.PsycloneClient.udp | ( | cls, | |
| host, | |||
| port, | |||
| device = None, | |||
| ** | options ) |
| 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.
| 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.
| psyclone_client.client.PsycloneClient.websocket | ( | cls, | |
| url, | |||
| device = None, | |||
| ** | options ) |
|
protected |
Definition at line 139 of file client.py.
Referenced by _receive(), psyclone_client.remote_module.RemoteModule._receive(), on_any_message(), psyclone_client.remote_module.RemoteModule.on_any_trigger(), and remove_handler().
|
protected |
Definition at line 147 of file client.py.
Referenced by _keepalive_loop(), _on_state(), psyclone_client.remote_module.RemoteModule._reattach_later(), psyclone_client.remote_module.RemoteModule._receive(), _send_keepalive(), _state_changed(), psyclone_client.remote_module.RemoteModule._state_changed(), close(), psyclone_client.remote_module.RemoteModule.close(), post(), wait_connected(), and wait_server().
|
protected |
Definition at line 151 of file client.py.
Referenced by _heard(), _keepalive_loop(), _keepalive_reply(), _on_state(), _send_keepalive(), _state_changed(), _tick(), post(), request_async(), send_text(), and server_features().
|
protected |
Definition at line 138 of file client.py.
Referenced by _receive(), psyclone_client.remote_module.RemoteModule._receive(), on_trigger(), psyclone_client.remote_module.RemoteModule.on_trigger(), and remove_handler().
|
protected |
Definition at line 153 of file client.py.
Referenced by _send_keepalive().
|
protected |
|
protected |
Definition at line 160 of file client.py.
Referenced by _state_changed().
|
protected |
Definition at line 158 of file client.py.
Referenced by _heard(), _on_state(), _state_changed(), _tick(), and connected().
|
protected |
Definition at line 144 of file client.py.
Referenced by _receive(), psyclone_client.remote_module.RemoteModule._receive(), psyclone_client.remote_module.RemoteModule._request_op(), close(), and request_async().
|
protected |
Definition at line 143 of file client.py.
Referenced by _receive().
|
protected |
Definition at line 142 of file client.py.
Referenced by _receive(), psyclone_client.remote_module.RemoteModule._receive(), psyclone_client.remote_module.RemoteModule._request_op(), close(), and request_async().
|
protected |
Definition at line 146 of file client.py.
Referenced by psyclone_client.remote_module.RemoteModule._request_op(), and request_async().
|
protected |
Definition at line 145 of file client.py.
Referenced by psyclone_client.remote_module.RemoteModule._request_op(), and request_async().
|
protected |
Definition at line 141 of file client.py.
Referenced by _state_changed(), and on_state_changed().
|
protected |
Definition at line 159 of file client.py.
Referenced by _state_changed().
|
protected |
Definition at line 152 of file client.py.
Referenced by _on_state(), and _tick().
|
protected |
Definition at line 140 of file client.py.
Referenced by _on_text_frame(), and on_text().
|
protected |
Definition at line 135 of file client.py.
Referenced by _heard(), _send_keepalive(), _tick(), close(), connected(), post(), send_text(), and transport().
|
protected |
Definition at line 161 of file client.py.
Referenced by _keepalive_loop(), _on_state(), and close().
| psyclone_client.client.PsycloneClient.bad = 0 |
Definition at line 150 of file client.py.
Referenced by _on_bytes().
| psyclone_client.client.PsycloneClient.connected |
Definition at line 372 of file client.py.
Referenced by psyclone_client.stats.ConnectionStats.__str__(), and wait_connected().
| psyclone_client.client.PsycloneClient.device = device |
Definition at line 136 of file client.py.
Referenced by _send_keepalive(), and post().
| psyclone_client.client.PsycloneClient.options = options |
Definition at line 137 of file client.py.
Referenced by psyclone_client.transports.WebSocketTransport._connect(), _dispatch(), _keepalive_loop(), _keepalive_reply(), _on_bytes(), psyclone_client.transports.WebSocketTransport._on_connected(), _on_state(), psyclone_client.transports.UdpTransport._open(), psyclone_client.transports.UdpTransport._read_loop(), psyclone_client.transports.WebSocketTransport._read_loop(), psyclone_client.transports._StreamTransport._run(), and _tick().
| 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().
| psyclone_client.client.PsycloneClient.received = 0 |
Definition at line 149 of file client.py.
Referenced by _on_text_frame(), and _receive().
| psyclone_client.client.PsycloneClient.sent = 0 |
Definition at line 148 of file client.py.
Referenced by post(), and send_text().
| 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().