TcpClient#

class lsst.ts.m2com.TcpClient(host: str, port: int, callback_process_message: Callable[[...], Coroutine], *args: Any, log: Logger | None = None, sequence_generator: Generator | None = None, name: str = 'tcp-client', check_message_rate: bool = False)#

Bases: Client

TCP/IP client.

Parameters#

hoststr

Host address.

portint

Port to connect.

callback_process_messagecoroutine

Function to process the received message. It must has a keyward argument of “message” to receive the message as a dictionary.

*argsargs

Arguments needed in “callback_process_message” function call.

loglogging.Logger or None, optional

A logger. If None, a logger will be instantiated. (the default is None)

sequence_generatorgenerator or None, optional

Sequence generator. (the default is None)

namestr, optional

Name of the tcp-client. Used for logging/debugging purposes. The default is “tcp-client”)

check_message_ratebool, optional

Check the message rate or not. If True, the rate of message will be calculated and recorded in the debug logging message. (the default is False)

Attributes#

loglogging.Logger

A logger.

timeoutfloat

Read timeout in second.

last_sequence_idint

Last sequence ID of command.

Methods Summary

close()

Cancel the task and close the connection.

connect([timeout])

Connect to the server.

write_message(msg_type, msg_name[, ...])

Writes message to the server.

Methods Documentation

async close() None#

Cancel the task and close the connection.

Note: this function is safe to call even though there is no connection.

async connect(timeout: float = 10.0) None#

Connect to the server.

Parameters#

timeoutfloat, optional

Timeout in second. (default is 10.0)

async write_message(msg_type: MsgType, msg_name: str, msg_details: dict | None = None, comp_name: str | None = None) None#

Writes message to the server.

Parameters#

msg_typeenum MsgType

Message type.

msg_namestr

Message name.

msg_detailsdict or None, optional

Message details. (the default is None)

comp_namestr or None, optional

Specific component name used in the event or telemetry. (the default is None)

Raises#

RuntimeError

When there is no TCP/IP connection.

ValueError

If ‘id’ is in the message details already.

ValueError

When the message type is not supported.