Skip to main content

Hakoniwa PDU communication library for Python

Project description

hakoniwa-pdu-python

tests

Python PDU communication library for the Hakoniwa simulator. Provides a unified transport layer where RPC and Pub/Sub (topics) run seamlessly over WebSocket. For high-speed use cases, a Shared Memory (SHM) backend is also available. The architecture is extensible to Zenoh, enabling scalable and distributed systems. Binary โ‡” JSON โ‡” Python type conversion is built-in, reducing boilerplate to a minimum.


โœจ Features

  • Unified layer: RPC and Pub/Sub integrated on top of WebSocket
  • Automatic type conversion: safely convert between binary, JSON, and Python types with offset definitions
  • Transport flexibility: choose between WebSocket, Shared Memory (SHM), and extensible backends such as Zenoh
  • Explicit & secure connections: WebSocket URIs (ws://...) clearly define communication scope
  • Event-driven & polling support: register handlers or poll buffers as needed
  • Ready-to-run samples: minimal examples for Twist (topic) and AddTwoInts (RPC) included

๐Ÿ“ฆ Installation

pip install hakoniwa-pdu
pip show hakoniwa-pdu   # check version

Environment Variables

Specify the directory containing .offset files for PDU conversion:

export HAKO_BINARY_PATH=/your/path/to/offset

Default path if unset:

/usr/local/lib/hakoniwa/hako_binary/offset

๐Ÿš€ Quick Start (3 commands)

Example 1: WebSocket Topic (geometry_msgs/Twist publish โ†’ subscribe)

  1. Publisher (server)
python examples/topic/websocket/remote_publisher.py \
  --uri ws://localhost:8080 \
  --pdu-config examples/pdu_config.json \
  --service-config examples/service.json
  1. Subscriber (client)
python examples/topic/websocket/remote_subscriber.py \
  --uri ws://localhost:8080 \
  --pdu-config examples/pdu_config.json \
  --service-config examples/service.json
  1. Output
[INFO] Received Twist: linear.x=0 angular.z=0
[INFO] Received Twist: linear.x=1 angular.z=1

Example 2: WebSocket RPC (AddTwoInts service)

  1. RPC Server
python examples/rpc/websocket/remote_rpc_server.py \
  --uri ws://localhost:8080 \
  --pdu-config examples/pdu_config.json \
  --service-config examples/service.json
  1. RPC Client
python examples/rpc/websocket/remote_rpc_client.py \
  --uri ws://localhost:8080 \
  --pdu-config examples/pdu_config.json \
  --service-config examples/service.json
  1. Output
Response: 3

๐Ÿ“ก Event-Driven PDU Handling

Server:

server_manager.register_handler_pdu_data(on_pdu)

def on_pdu(client_id, packet):
    ...

Client:

client_manager.register_handler_pdu_data(on_pdu)

def on_pdu(packet):
    ...

Polling via contains_buffer() / get_buffer() is also available.


๐Ÿ“ Project Structure

hakoniwa_pdu/
โ”œโ”€โ”€ pdu_manager.py
โ”œโ”€โ”€ impl/
โ”‚   โ”œโ”€โ”€ icommunication_service.py
โ”‚   โ”œโ”€โ”€ websocket_communication_service.py
โ”‚   โ”œโ”€โ”€ websocket_server_communication_service.py
โ”‚   โ”œโ”€โ”€ shm_communication_service.py
โ”‚   โ”œโ”€โ”€ pdu_convertor.py
โ”‚   โ””โ”€โ”€ hako_binary/
โ”œโ”€โ”€ rpc/
โ”‚   โ”œโ”€โ”€ ipdu_service_manager.py
โ”‚   โ”œโ”€โ”€ protocol_client.py
โ”‚   โ”œโ”€โ”€ protocol_server.py
โ”‚   โ”œโ”€โ”€ auto_wire.py
โ”‚   โ”œโ”€โ”€ remote/
โ”‚   โ””โ”€โ”€ shm/
โ”œโ”€โ”€ resources/
โ”‚   โ””โ”€โ”€ offset/
โ””โ”€โ”€ examples/

๐Ÿงญ Class Overview

PduManager

  • Orchestrates PDU buffers and delegates to a transport (ICommunicationService).
  • Direct I/O: declare_pdu_for_read/write โ†’ flush_pdu_raw_data() / read_pdu_raw_data().
  • For RPC: extended via rpc.IPduServiceManager (handles register_client, start_rpc_service, etc.).

Transport Implementations (impl/)

  • ICommunicationService defines the transport API.
  • WebSocketCommunicationService / WebSocketServerCommunicationService: WebSocket backend (explicit URI-based connection, simple & secure).
  • ShmCommunicationService: high-speed shared memory backend.
  • Pluggable design: additional transports (e.g., Zenoh) can be integrated without changing application code.

RPC Layer (rpc/)

  • IPduServiceManager family provides RPC APIs (client/server).
  • protocol_client.py / protocol_server.py: user-friendly helpers.
  • auto_wire.py: auto-loads generated converters.
  • remote/: WebSocket managers.
  • shm/: SHM managers.

๐Ÿงฉ Class Diagram (Mermaid)

classDiagram
    class PduManager
    PduManager --> ICommunicationService : uses
    PduManager --> CommunicationBuffer
    PduManager --> PduConvertor
    PduManager --> PduChannelConfig

    class ICommunicationService {
        <<interface>>
    }
    class WebSocketCommunicationService
    class WebSocketServerCommunicationService
    class ShmCommunicationService
    ICommunicationService <|.. WebSocketCommunicationService
    ICommunicationService <|.. WebSocketServerCommunicationService
    ICommunicationService <|.. ShmCommunicationService

    class IPduServiceManager {
        <<abstract>>
    }
    PduManager <|-- IPduServiceManager
    class IPduServiceClientManager
    class IPduServiceServerManager
    IPduServiceManager <|-- IPduServiceClientManager
    IPduServiceManager <|-- IPduServiceServerManager

    class RemotePduServiceBaseManager
    RemotePduServiceBaseManager <|-- RemotePduServiceClientManager
    RemotePduServiceBaseManager <|-- RemotePduServiceServerManager
    IPduServiceManager <|-- RemotePduServiceBaseManager

    class ShmPduServiceBaseManager
    ShmPduServiceBaseManager <|-- ShmPduServiceClientManager
    ShmPduServiceBaseManager <|-- ShmPduServiceServerManager
    IPduServiceManager <|-- ShmPduServiceBaseManager

๐Ÿ”— Links


๐Ÿ“š Documentation

For detailed API usage: โžก๏ธ API Reference (api-doc.md)


๐Ÿ“œ License

MIT License โ€” see LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hakoniwa_pdu-1.0.3.tar.gz (192.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hakoniwa_pdu-1.0.3-py3-none-any.whl (555.7 kB view details)

Uploaded Python 3

File details

Details for the file hakoniwa_pdu-1.0.3.tar.gz.

File metadata

  • Download URL: hakoniwa_pdu-1.0.3.tar.gz
  • Upload date:
  • Size: 192.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for hakoniwa_pdu-1.0.3.tar.gz
Algorithm Hash digest
SHA256 7b0587ad15335b1c4fd1498992181f6ead2c0071c3d43b5d53ec26086fee600a
MD5 40f2d406b9a51a2562e9f801e70cad6e
BLAKE2b-256 0eb7a7215257eb65be89bae21a431396dae799af4e7d96115e796e72cf5bd6a9

See more details on using hashes here.

File details

Details for the file hakoniwa_pdu-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: hakoniwa_pdu-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 555.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for hakoniwa_pdu-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7205c94d260a4c6328c32e8cf2856523439d2e883534672dc26396f301b0ea73
MD5 f23e9493c9779b2f796548b234a2d3b3
BLAKE2b-256 4354b1b6f489ad1bfb3485bd4fe2280e67d91a09eb26f1e57683fc15de51ef93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page