Communicator

class lsst.ts.MTMount.Communicator(name, client_host, client_port, server_host, server_port, log, read_replies, connect=True, connect_callback=None)

Bases: lsst.ts.MTMount.ClientServerPair

Read and write BaseMessages using Tekniker’s communication protocol.

Data is written to the client socket and read from the (single) server socket.

Parameters:
name : str

Name used for error messages.

client_host : str

IP address for the output client socket.

client_port : int

IP port for the output client socket.

server_host : str or None

IP address for the input socket server. If None then use all interfaces.

server_port : int

IP port for the input socket server. If 0 then use a random port.

log : logging.Logger

Logger.

read_replies : bool

If True then read replies, else read commands, on the server port.

connect : bool, optional

Connect the client at construction time? (The server automatically tries to connect.)

connect_callback : callable, optional

Synchronous function to call when a connection is made or dropped.

Notes

Tekniker’s OperationManager software connects to each component (PXI, EUI and HHD) using two TCP/IP sockets:

  • A client socket for output; data is only written to this socket.
    For the PXI this is used to send commands. For EUI and HDD this is used to send replies.
  • A server socket for input; data is only read from this socket.
    For the PXI this is used to read replies. For EUI and HDD this is used to read commands.

Attributes Summary

client_connected Is the client connected?
connect_retry_interval
connected Are both the client and server connected?
server_connected Is the server connected?
server_host
server_port
server_reader Server reader, an asyncio.StreamReader or None if not connected.
server_writer Server writer, an asyncio.StreamWriter or None if not connected.

Methods Summary

call_connect_callback(*args, **kwargs) Call the connect_callback if it exists.
close() Close both the server and the client, to clean up when finished.
close_client() Close the client.
connect([port]) Connect the client socket and wait for a connection to the server.
monitor_client_reader() Monitor the client reader; if it closes then close the writer.
read() Read and return a message.
wait_server_port() Wait for the server to start, then return the port.
write(message) Write a message.

Attributes Documentation

client_connected

Is the client connected?

connect_retry_interval = 0.1
connected

Are both the client and server connected?

server_connected

Is the server connected?

server_host
server_port
server_reader

Server reader, an asyncio.StreamReader or None if not connected.

server_writer

Server writer, an asyncio.StreamWriter or None if not connected.

Methods Documentation

call_connect_callback(*args, **kwargs)

Call the connect_callback if it exists. Any arguments are ignored.

close()

Close both the server and the client, to clean up when finished.

Set the server done_task done.

close_client()

Close the client.

Warning: does NOT call the connect_callback.

connect(port=None)

Connect the client socket and wait for a connection to the server.

Parameters:
port : int or None, optional

TCP/IP port. If None use the port specified in the constructor. Being able to specify a port here is primarily for unit tests.

Notes

This will wait forever for a connection.

monitor_client_reader()

Monitor the client reader; if it closes then close the writer.

read()

Read and return a message. Waits indefinitely.

Returns:
message : BaseMessage

The message read.

Raises:
RuntimeError

If not connected before reading begins.

ConnectionResetError

If the connection is lost while reading. This also calls connect_callback.

wait_server_port()

Wait for the server to start, then return the port.

Useful when you have specified port=0, e.g. for unit tests.

write(message)

Write a message.

Parameters:
message : BaseMessage

Message to write.

Raises:
RuntimeError

If not connected before writing.

ConnectionResetError

If the connection is lost while writing. This also calls connect_callback.