Metadata-Version: 2.4
Name: psyclone-client
Version: 2.3.1
Summary: Client for Psyclone systems: post messages by post name and receive triggers over UDP, TCP or WebSocket (CMSDK DataMessage, pure Python)
Author-email: "CMLabs (Communicative Machines)" <support@cmlabs.com>
License: EXTENDED BSD License
        
        Redistribution and use in source and binary forms, with or without
        modification, is permitted provided that the following conditions 
        are met:
        
        - Redistributions of source code must retain the above copyright 
          and collaboration notice, this list of conditions and the following 
          disclaimer.
        
        - Redistributions in binary form must reproduce the above copyright 
          notice, this list of conditions and the following disclaimer in the 
          documentation and/or other materials provided with the distribution.
        
        - Neither the name of its copyright holders nor the names of its 
          contributors may be used to endorse or promote products derived from 
          this software without specific prior written permission.
        
        - CADIA Clause: The license granted in and to the software under this 
          agreement is a limited-use license. The software may not be used in 
          furtherance of: (i) intentionally causing bodily injury or severe 
          emotional harm to any person; (ii) invading the personal privacy or 
          violating the human rights of any person; or (iii) committing or 
          preparing for any act of war. 
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
        "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
        PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
        OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
        EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
        PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
        PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
        LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
        NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
        SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: Homepage, https://cmlabs.com/psyclone
Project-URL: Documentation, https://psyclone.cmlabs.com/userguide/11-services-interfaces.html
Keywords: psyclone,cmsdk,datamessage,multi-agent,messaging,websocket,udp
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: System :: Networking
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# psyclone-client (Python)

Talk to a running [Psyclone](https://cmlabs.com/psyclone) system from outside it: post messages into
the system by **post name** and receive what its triggers send out by **trigger name**, over UDP, TCP
or WebSocket. Messages are CMSDK `DataMessage`s, byte-identical with the C++ library (full or packed
form). Pure Python, standard library only: one wheel for Python 3.8+ on every OS.

```
pip install psyclone-client
```

```python
from psyclone_client import PsycloneClient, DataMessage

with PsycloneClient.websocket("ws://host:18600/phone", device="py-1") as psy:   # or .udp(host, port), .tcp(...)
    psy.on_trigger("Playout", lambda m: print("got", m.get_data("audio")))      # pushed by the system
    psy.post("AudioFrame", DataMessage().set_data("audio", b"..."))             # into the system
    reply = psy.request("Question", DataMessage().set_string("Text", "hi"), timeout=3)   # or None
```

The service it talks to (any Psyclone 2.3+ system):

```xml
<service name="Phone">
  <post name="AudioFrame" type="Mimir.Audio.Frame"/>
  <trigger name="Playout" type="Mimir.Playout.Chunk"/>
</service>
<interface name="PhoneTCP" port="18600" protocol="HTTP" service="Phone"/>           <!-- HTTP + WebSocket -->
<interface name="PhoneUDP" port="18600" ip="UDP" protocol="Message" service="Phone"/>
```

## DataMessage

```python
m = DataMessage().set_string("Name", "x").set_int("N", 42).set_double("Gain", 0.7).set_data("pcm", b"\0\1")
m["Other"] = 5                     # by Python type: str, int, float, bytes, DataMessage
m.to_bytes(); m.to_packed_bytes()  # the two wire forms
DataMessage.from_bytes(wire)       # either form
```

`setString`/`getInt`/`getBytes`/`fromBytes`... (the names of the C++ binding `cmsdk3`) work too, so code
moves between the two. Header fields are attributes (`time`, `serial`, `type`, `tag`, ...).

## Behaviour

- `post(name, msg)` sets `PsyPost` (and `PsyDevice`) on `msg` and sends it; False when the pipe is down
  (nothing is queued; reliability belongs in the modules, as with every Psyclone service).
- Received messages go to the `on_trigger(name, handler)` handlers by their `PsyTrigger`, then to
  `on_any_message`. Handlers run on the reader thread; pass `callback=loop.call_soon_threadsafe` (or any
  callable that runs a function) to move them, e.g. into asyncio.
- `request()` / `request_async()` add `PsyClientRequestID` and wait 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`.
- TCP and WebSocket reconnect by themselves (0.5 s doubling to 10 s, `on_state_changed`); WebSocket pings
  every 20 s. A UDP service learns a peer's address from its first datagram; with `device` set it follows
  the peer across address changes.
- `packed=True` sends the packed form (about 3x less overhead); Psyclone answers in the form a peer used.
- WebSocket text frames: `send_text()`, `on_text(handler)`.
- `PsycloneHttp(base_url).request(path, post, fields)` for a service's HTTP interface.

Not to be confused with `cmsdk3`, the C++ binding that runs Python **cranks inside** Psyclone; this
package is for programs outside it. Licence: BSD with the CADIA clause (CMSDK).

## Tests

```
python3 -m unittest discover -s tests        # codec conformance (C++ golden messages) + local transports
# live, against bin/<arch>/Psyclone spec=CMSDK/tools/client-test.xml port=18700:
PSYCLONE_LIVE=127.0.0.1 python3 -m unittest tests.test_client
```
