PneumaticsServerSimulator¶
- class lsst.ts.atpneumaticssimulator.PneumaticsServerSimulator(host: str | None, port: int | None, log: Logger, dispatch_callback: Callable[[BaseClientOrServer], Union[None, Awaitable[None]]], connect_callback: Optional[Callable[[BaseClientOrServer], Union[None, Awaitable[None]]]] = None, name: str = '', **kwargs: Any)¶
Bases:
OneClientReadLoopServer
- An implementation of OneClientJsonServer that simulates a Pneumatics
server.
- Parameters:
- host
str
orNone
IP address for this server; typically
LOCALHOST_IPV4
for IPV4 orLOCALHOST_IPV6
for IPV6. IfNone
then bind to all network interfaces (e.g. listen on an IPv4 socket and an IPv6 socket). None can cause trouble with port=0; seeport
in the Attributes section for more information.- port
int
IP port for this server. If 0 then randomly pick an available port (or ports, if listening on multiple sockets). 0 is strongly recommended for unit tests.
- log
logging.Logger
Logger.
- dispatch_callbackcallable
Asynchronous function to call when data are read and dispatched.
- connect_callbackcallable or
None
, optional Asynchronous or (deprecated) synchronous function to call when when a client connects or disconnects. If the other end (client) closes the connection, it may take
monitor_connection_interval
seconds or longer to notice. The function receives one argument: thisOneClientServer
.- name
str
, optional Name used for log messages, e.g. “Commands” or “Telemetry”.
- **kwargs
dict
[str
,typing.Any
] Additional keyword arguments for
asyncio.start_server
, beyond host and port.
- host
Attributes Summary
Return True if self.reader and self.writer are connected.
Methods Summary
Close the connected client socket, if any.
A client has connected or disconnected.
close
()Close socket server and client socket, and set done_task done.
close_client
([cancel_read_loop_task])Stop the read loop and close the client.
read
(n)Read up to n bytes.
Read, parse, and dispatch one item of data.
read_into
(struct)Read binary data from a stream reader into a
ctypes.Structure
.Read JSON data.
Read incoming data and handle them.
read_str
()Read data ending in
TERMINATOR
.readexactly
(n)Read exactly n bytes.
readline
()Read a sequence of bytes ending with
\n
.readuntil
([separator])Read one line, where “line” is a sequence of bytes ending with
start
(**kwargs)Start the TCP/IP server.
write
(data)Write data and call
drain
.write_from
(*structs)Write binary data from one or more
ctypes.Structure
s.write_json
(data)Write data in JSON format.
write_str
(line)Write a string of data.
writelines
(lines)Write an iterable of bytes and call
drain
.Attributes Documentation
- connected¶
Return True if self.reader and self.writer are connected.
Note: if the other end drops the connection and if you are not trying to read data (e.g. in a background loop), then it takes the operating system awhile to realize the connection is lost. So this can return true for some unknown time after the connection has been dropped.
Methods Documentation
- async basic_close_client() None ¶
Close the connected client socket, if any.
Also:
Reset
self.connected_task
to a new Future.Call connect_callback, if a client was connected.
Unlike
close_client
, this does not touchself.should_be_connected
.Always safe to call.
- async close() None ¶
Close socket server and client socket, and set done_task done.
Call connect_callback if a client was connected.
Always safe to call.
After calling the super stop method, an async event is cleared so clients can be ensured that the server has been closed.
- async close_client(cancel_read_loop_task: bool = True) None ¶
Stop the read loop and close the client.
- Parameters:
- cancel_read_loop_task
bool
Cancel the read loop task or not? Defaults to True and should be False when called from the read loop task itself.
- cancel_read_loop_task
- async read(n: int) bytes ¶
Read up to n bytes.
- Parameters:
- n
int
The number of bytes to read. If -1 then block until the other end closes its writer, then return all data seen.
- n
- Raises:
ConnectionError
If the connection is lost before, or while, reading.
- async read_and_dispatch() None ¶
Read, parse, and dispatch one item of data.
Subclasses need to implement this method such that it reads and parses data and then dispatches handling the data to a method suitable for the subclass. Methods that might be helpful include:
- async read_into(struct: Structure) None ¶
Read binary data from a stream reader into a
ctypes.Structure
.- Parameters:
- struct
ctypes.Structure
Structure to set.
- struct
- Raises:
ConnectionError
If the connection is lost before, or while, reading.
asyncio.IncompleteReadError
If EOF is reached before
n
bytes can be read. Use theIncompleteReadError.partial
attribute to get the partially read data.
- async read_json() Any ¶
Read JSON data.
Before returning the data, it is decoded using JSON.
- Returns:
- str:
Data decoded from JSON.
- Raises:
ConnectionError
If the connection is lost before, or while, reading.
asyncio.IncompleteReadError
If EOF is reached before the complete separator is found and the internal buffer is reset.
LimitOverrunError
If the amount of data read exceeds the configured stream lmit. The data is left in the internal buffer and can be read again.
TypeError
If the data are of a type that cannot be decoded from JSON.
json.JSONDecodeError
If the data cannot be decoded from JSON.
- async read_loop() None ¶
Read incoming data and handle them.
The actual reading is deferred to the
read_and_dispatch
method and needs to be implemented by subclasses.
- async read_str() str ¶
Read data ending in
TERMINATOR
.Before returning the data, the TERMINATOR is stripped and the data is decoded into an UTF-8 string.
- Returns:
- str:
Data decoded into a string.
- Raises:
ConnectionError
If the connection is lost before, or while, reading.
asyncio.IncompleteReadError
If EOF is reached before the complete separator is found and the internal buffer is reset.
LimitOverrunError
If the amount of data read exceeds the configured stream lmit. The data is left in the internal buffer and can be read again.
UnicodeError
If decoding fails.
- async readexactly(n: int) bytes ¶
Read exactly n bytes.
- Parameters:
- n
int
The number of bytes to read.
- n
- Raises:
ConnectionError
If the connection is lost before, or while, reading.
asyncio.IncompleteReadError
If EOF is reached before
n
bytes can be read. Use theIncompleteReadError.partial
attribute to get the partially read data.
- async readline() bytes ¶
Read a sequence of bytes ending with
\n
.If EOF is received and
\n
was not found, the method returns partially read data.- Raises:
ConnectionError
If the connection is lost before, or while, reading.
- async readuntil(separator: bytes = b'\n') bytes ¶
Read one line, where “line” is a sequence of bytes ending with .
Read data from the stream until separator is found.
On success, the data and separator will be removed from the internal buffer (consumed). Returned data will include the separator at the end.
- Raises:
ConnectionError
If the connection is lost before, or while, reading.
asyncio.IncompleteReadError
If EOF is reached before the complete separator is found and the internal buffer is reset.
LimitOverrunError
If the amount of data read exceeds the configured stream lmit. The data is left in the internal buffer and can be read again.
- async start(**kwargs: Any) None ¶
Start the TCP/IP server.
This is called automatically by the constructor, and is not intended to be called by the user. It is a public method so that subclasses can override it.
After calling the super start method, an async event is set so clients can be ensured that the server has been started.
- Parameters:
- **kwargs
dict
[str
,typing.Any
] Additional keyword arguments for
asyncio.start_server
, beyond host and port.
- **kwargs
- Raises:
RuntimeError
If start has already been called and has successfully constructed a server.
- async write(data: bytes) None ¶
Write data and call
drain
.- Parameters:
- data
bytes
The data to write.
- data
- Raises:
ConnectionError
If
self.connected
false before writing begins.
- async write_from(*structs: Structure) None ¶
Write binary data from one or more
ctypes.Structure
s.- Parameters:
- structs
list
[ctypes.Structure
] Structures to write.
- structs
- Raises:
ConnectionError
If
self.connected
false before writing begins.
- async write_json(data: Any) None ¶
Write data in JSON format.
Before writing, the data is encoded using JSON and the TERMINATOR is appended.
- Parameters:
- data
any
The data to be written.
- data
- async write_str(line: str) None ¶
Write a string of data.
Before writing, the data first gets UTF-8 encoded and the
TERMINATOR
gets appended.- Parameters:
- line
str
The line of data to be written.
- line
- Raises:
ConnectionError
If the connection is lost before, or while, reading.
UnicodeError
If encoding fails.
- async writelines(lines: Iterable) None ¶
Write an iterable of bytes and call
drain
.- Parameters:
- lines
collections.abc.Iterable
[bytes
] The data to write, as an iterable collection of
bytes
.
- lines
- Raises:
ConnectionError
If
self.connected
false before writing begins.