plestylib.service.tcp_ip_server#

Classes#

DeviceTCPIPServer

ZeroMQ-based asynchronous device server.

Module Contents#

class plestylib.service.tcp_ip_server.DeviceTCPIPServer(device, wrapper_cls, address='tcp://*:5555')#

ZeroMQ-based asynchronous device server.

This server exposes a synchronous device over TCP/IP using a JSON-based RPC protocol. The device is wrapped using an async wrapper (e.g., AsyncWrapperSafe or AsyncDeviceThread) to ensure safe, serialized access.

The server supports multiple concurrent clients via a ROUTER socket.

Protocol:

Request payload:

{
  "type": "query | write | call | help | describe",
  "timeout": 3.0
}

Response payload:

{"status": "ok", "result": "..."}

Error response payload:

{"status": "error", "error": "message", "type": "ExceptionType"}

Initialize the device server.

Parameters:
  • device – Synchronous device instance.

  • wrapper_cls – Async wrapper class (e.g., AsyncDeviceThread).

  • address – ZMQ bind address.

device#
ctx#
socket#
property address#

Get the server’s bind address.

property is_running#

Check if the server is currently running.

async _execute(coro, timeout=None)#

Execute a coroutine with optional timeout.

Parameters:
  • coro – Coroutine to execute.

  • timeout – Timeout in seconds.

Returns:

Result of coroutine.

Raises:

asyncio.TimeoutError – If timeout is exceeded.

async _handle(request_str)#

Handle a single client request.

Parameters:

request_str – JSON-encoded request string.

Returns:

JSON-serializable response.

Return type:

dict

async _describe()#

Introspect the device and list available methods.

Returns:

Available callable methods.

Return type:

dict

async _help()#
async run()#

Run the server loop indefinitely.

This method listens for incoming requests and processes them sequentially. Each request is handled asynchronously.

async shutdown()#

Gracefully shutdown the server.