Two different clients are provided: the BlockingClient for synchronous communication with a server and the CallbackClient for asynchronous communication. Both clients raise KatcpClientError when exceptions occur.
The DeviceClient base class is provided as a foundation for those wishing to implement their own clients.
Implement blocking requests on top of DeviceClient.
This client will use message IDs if the server supports them.
| Parameters : | host : string
port : int
tb_limit : int
logger : object
auto_reconnect : bool
timeout : float in seconds
|
|---|
Examples
>>> c = BlockingClient('localhost', 10000)
>>> c.start()
>>> reply, informs = c.blocking_request(katcp.Message.request('myreq'))
>>> print reply
>>> print [str(msg) for msg in informs]
>>> c.stop()
>>> c.join()
Methods
| BlockingClient.blocking_request(msg[, ...]) | Send a request messsage. |
| BlockingClient.handle_inform(msg) | Handle inform messages related to any current requests. |
| BlockingClient.handle_message(msg) | Handle a message from the server. |
| BlockingClient.handle_reply(msg) | Handle a reply message related to the current request. |
| BlockingClient.handle_request(msg) | Dispatch a request message to the appropriate method. |
| BlockingClient.inform_build_state(msg) | Handle katcp v4 and below build-state inform |
| BlockingClient.inform_version(msg) | Handle katcp v4 and below version inform |
| BlockingClient.inform_version_connect(msg) | Process a #version-connect message. |
| BlockingClient.is_connected() | Check if the socket is currently connected. |
| BlockingClient.join([timeout]) | Rejoin the client thread. |
| BlockingClient.notify_connected(connected) | Event handler that is called whenever the connection status changes. |
| BlockingClient.preset_protocol_flags(...) | Preset server protocol flags. |
| BlockingClient.request(msg[, use_mid, timeout]) | Send a request message, automatically assign a message ID if requested |
| BlockingClient.run() | Process reply and inform messages from the server. |
| BlockingClient.running() | Whether the client is running. |
| BlockingClient.send_message(msg[, timeout]) | Send any kind of message. |
| BlockingClient.send_request(msg[, timeout]) | Send a request messsage. |
| BlockingClient.start([timeout, daemon, ...]) | Start the client in a new thread. |
| BlockingClient.stop([timeout]) | Stop a running client (from another thread). |
| BlockingClient.unhandled_inform(msg) | Fallback method for inform messages without a registered handler |
| BlockingClient.unhandled_reply(msg) | Fallback method for reply messages without a registered handler |
| BlockingClient.unhandled_request(msg) | Fallback method for requests without a registered handler |
| BlockingClient.wait_connected([timeout]) | Wait until the client is connected. |
| BlockingClient.wait_protocol([timeout]) | Wait until katcp protocol information has been received from the client. |
Send a request messsage.
| Parameters : | msg : Message object
timeout : float in seconds
keepalive : boolean, optional
use_mid : boolean, optional
|
|---|---|
| Returns : | reply : Message object
informs : list of Message objects
|
Handle inform messages related to any current requests.
Inform messages not related to the current request go up to the base class method.
| Parameters : | msg : Message object
|
|---|
Handle a message from the server.
| Parameters : | msg : Message object
|
|---|
Handle a reply message related to the current request.
Reply messages not related to the current request go up to the base class method.
| Parameters : | msg : Message object
|
|---|
Dispatch a request message to the appropriate method.
| Parameters : | msg : Message object
|
|---|
Handle katcp v4 and below build-state inform
Handle katcp v4 and below version inform
Process a #version-connect message.
Check if the socket is currently connected.
| Returns : | connected : bool
|
|---|
Rejoin the client thread.
| Parameters : | timeout : float in seconds
|
|---|
Event handler that is called whenever the connection status changes.
Override in derived class for desired behaviour.
Note
This function should never block. Doing so will cause the client to cease processing data from the server until notify_connected completes.
| Parameters : | connected : bool
|
|---|
Preset server protocol flags.
Sets the assumed server protocol flags and disables automatic server version detection.
| Parameters : | protocol_flags : katcp.core.ProtocolFlags instance |
|---|
Send a request message, automatically assign a message ID if requested
| Parameters : | msg : katcp.Message request message use_mid : bool or None, default=None timeout : float or None, default=None
|
|---|---|
| Returns : | mid : string or None
If use_mid is None and the server supports msg ids, or if use_mid is : True a message ID will automatically be assigned msg.mid is None. : if msg.mid has a value, and the server supports msg ids, that value will : be used. If the server does not support msg ids, KatcpVersionError will : be raised : |
Process reply and inform messages from the server.
Whether the client is running.
| Returns : | running : bool
|
|---|
Send any kind of message.
| Parameters : | msg : Message object
timeout : float or None, default=None
|
|---|
Send a request messsage.
| Parameters : | msg : Message object
timeout : float or None, default=None
|
|---|
Start the client in a new thread.
| Parameters : | timeout : float in seconds
daemon : boolean
excepthook : function
|
|---|
Stop a running client (from another thread).
| Parameters : | timeout : float in seconds
|
|---|
Fallback method for inform messages without a registered handler
| Parameters : | msg : Message object
|
|---|
Fallback method for reply messages without a registered handler
| Parameters : | msg : Message object
|
|---|
Fallback method for requests without a registered handler
| Parameters : | msg : Message object
|
|---|
Wait until the client is connected.
| Parameters : | timeout : float in seconds
|
|---|---|
| Returns : | connected : bool
|
Wait until katcp protocol information has been received from the client.
| Parameters : | timeout : float in seconds
|
|---|---|
| Returns : | received : bool
If this method returns True, the server’s protocol information is : available in the ProtocolFlags instance self.protocol_flags. : |
Implement callback-based requests on top of DeviceClient.
This client will use message IDs if the server supports them.
| Parameters : | host : string
port : int
tb_limit : int, optional
logger : object, optional
auto_reconnect : bool, optional
timeout : float in seconds, optional
|
|---|
Examples
>>> def reply_cb(msg):
... print "Reply:", msg
...
>>> def inform_cb(msg):
... print "Inform:", msg
...
>>> c = CallbackClient('localhost', 10000)
>>> c.start()
>>> c.callback_request(
... katcp.Message.request('myreq'),
... reply_cb=reply_cb,
... inform_cb=inform_cb,
... )
...
>>> # expect reply to be printed here
>>> # stop the client once we're finished with it
>>> c.stop()
>>> c.join()
Methods
| CallbackClient.blocking_request(msg[, ...]) | Send a request messsage. |
| CallbackClient.callback_request(msg[, ...]) | Send a request messsage. |
| CallbackClient.handle_inform(msg) | Handle inform messages related to any current requests. |
| CallbackClient.handle_message(msg) | Handle a message from the server. |
| CallbackClient.handle_reply(msg) | Handle a reply message related to the current request. |
| CallbackClient.handle_request(msg) | Dispatch a request message to the appropriate method. |
| CallbackClient.inform_build_state(msg) | Handle katcp v4 and below build-state inform |
| CallbackClient.inform_version(msg) | Handle katcp v4 and below version inform |
| CallbackClient.inform_version_connect(msg) | Process a #version-connect message. |
| CallbackClient.is_connected() | Check if the socket is currently connected. |
| CallbackClient.join([timeout]) | |
| CallbackClient.notify_connected(connected) | Event handler that is called whenever the connection status changes. |
| CallbackClient.preset_protocol_flags(...) | Preset server protocol flags. |
| CallbackClient.request(msg[, use_mid, timeout]) | Send a request message, automatically assign a message ID if requested |
| CallbackClient.run() | Process reply and inform messages from the server. |
| CallbackClient.running() | Whether the client is running. |
| CallbackClient.send_message(msg[, timeout]) | Send any kind of message. |
| CallbackClient.send_request(msg[, timeout]) | Send a request messsage. |
| CallbackClient.start([timeout, daemon, ...]) | Start the client in a new thread. |
| CallbackClient.stop(*args, **kwargs) | |
| CallbackClient.unhandled_inform(msg) | Fallback method for inform messages without a registered handler |
| CallbackClient.unhandled_reply(msg) | Fallback method for reply messages without a registered handler |
| CallbackClient.unhandled_request(msg) | Fallback method for requests without a registered handler |
| CallbackClient.wait_connected([timeout]) | Wait until the client is connected. |
| CallbackClient.wait_protocol([timeout]) | Wait until katcp protocol information has been received from the client. |
Send a request messsage.
| Parameters : | msg : Message object
timeout : float in seconds
use_mid : boolean, optional
|
|---|---|
| Returns : | reply : Message object
informs : list of Message objects
|
Send a request messsage.
| Parameters : | msg : Message object
reply_cb : function
inform_cb : function
user_data : tuple
timeout : float in seconds
use_mid : boolean, optional
|
|---|
Handle inform messages related to any current requests.
Inform messages not related to the current request go up to the base class method.
| Parameters : | msg : Message object
|
|---|
Handle a message from the server.
| Parameters : | msg : Message object
|
|---|
Handle a reply message related to the current request.
Reply messages not related to the current request go up to the base class method.
| Parameters : | msg : Message object
|
|---|
Dispatch a request message to the appropriate method.
| Parameters : | msg : Message object
|
|---|
Handle katcp v4 and below build-state inform
Handle katcp v4 and below version inform
Process a #version-connect message.
Check if the socket is currently connected.
| Returns : | connected : bool
|
|---|
Event handler that is called whenever the connection status changes.
Override in derived class for desired behaviour.
Note
This function should never block. Doing so will cause the client to cease processing data from the server until notify_connected completes.
| Parameters : | connected : bool
|
|---|
Preset server protocol flags.
Sets the assumed server protocol flags and disables automatic server version detection.
| Parameters : | protocol_flags : katcp.core.ProtocolFlags instance |
|---|
Send a request message, automatically assign a message ID if requested
| Parameters : | msg : katcp.Message request message use_mid : bool or None, default=None timeout : float or None, default=None
|
|---|---|
| Returns : | mid : string or None
If use_mid is None and the server supports msg ids, or if use_mid is : True a message ID will automatically be assigned msg.mid is None. : if msg.mid has a value, and the server supports msg ids, that value will : be used. If the server does not support msg ids, KatcpVersionError will : be raised : |
Process reply and inform messages from the server.
Whether the client is running.
| Returns : | running : bool
|
|---|
Send any kind of message.
| Parameters : | msg : Message object
timeout : float or None, default=None
|
|---|
Send a request messsage.
| Parameters : | msg : Message object
timeout : float or None, default=None
|
|---|
Start the client in a new thread.
| Parameters : | timeout : float in seconds
daemon : boolean
excepthook : function
|
|---|
Fallback method for inform messages without a registered handler
| Parameters : | msg : Message object
|
|---|
Fallback method for reply messages without a registered handler
| Parameters : | msg : Message object
|
|---|
Fallback method for requests without a registered handler
| Parameters : | msg : Message object
|
|---|
Wait until the client is connected.
| Parameters : | timeout : float in seconds
|
|---|---|
| Returns : | connected : bool
|
Wait until katcp protocol information has been received from the client.
| Parameters : | timeout : float in seconds
|
|---|---|
| Returns : | received : bool
If this method returns True, the server’s protocol information is : available in the ProtocolFlags instance self.protocol_flags. : |
Device client proxy.
Subclasses should implement .reply_*, .inform_* and send_request_* methods to take actions when messages arrive, and implement unhandled_inform, unhandled_reply and unhandled_request to provide fallbacks for messages for which there is no handler.
Request messages can be sent by calling .send_request().
| Parameters : | host : string
port : int
tb_limit : int
logger : object
auto_reconnect : bool
|
|---|
Examples
>>> MyClient(DeviceClient):
... def reply_myreq(self, msg):
... print str(msg)
...
>>> c = MyClient('localhost', 10000)
>>> c.start()
>>> c.send_request(katcp.Message.request('myreq'))
>>> # expect reply to be printed here
>>> # stop the client once we're finished with it
>>> c.stop()
>>> c.join()
Methods
| DeviceClient.handle_inform(msg) | Dispatch an inform message to the appropriate method. |
| DeviceClient.handle_message(msg) | Handle a message from the server. |
| DeviceClient.handle_reply(msg) | Dispatch a reply message to the appropriate method. |
| DeviceClient.handle_request(msg) | Dispatch a request message to the appropriate method. |
| DeviceClient.inform_build_state(msg) | Handle katcp v4 and below build-state inform |
| DeviceClient.inform_version(msg) | Handle katcp v4 and below version inform |
| DeviceClient.inform_version_connect(msg) | Process a #version-connect message. |
| DeviceClient.is_connected() | Check if the socket is currently connected. |
| DeviceClient.join([timeout]) | Rejoin the client thread. |
| DeviceClient.notify_connected(connected) | Event handler that is called whenever the connection status changes. |
| DeviceClient.preset_protocol_flags(...) | Preset server protocol flags. |
| DeviceClient.request(msg[, use_mid, timeout]) | Send a request message, automatically assign a message ID if requested |
| DeviceClient.run() | Process reply and inform messages from the server. |
| DeviceClient.running() | Whether the client is running. |
| DeviceClient.send_message(msg[, timeout]) | Send any kind of message. |
| DeviceClient.send_request(msg[, timeout]) | Send a request messsage. |
| DeviceClient.start([timeout, daemon, excepthook]) | Start the client in a new thread. |
| DeviceClient.stop([timeout]) | Stop a running client (from another thread). |
| DeviceClient.unhandled_inform(msg) | Fallback method for inform messages without a registered handler |
| DeviceClient.unhandled_reply(msg) | Fallback method for reply messages without a registered handler |
| DeviceClient.unhandled_request(msg) | Fallback method for requests without a registered handler |
| DeviceClient.wait_connected([timeout]) | Wait until the client is connected. |
| DeviceClient.wait_protocol([timeout]) | Wait until katcp protocol information has been received from the client. |
Dispatch an inform message to the appropriate method.
| Parameters : | msg : Message object
|
|---|
Handle a message from the server.
| Parameters : | msg : Message object
|
|---|
Dispatch a reply message to the appropriate method.
| Parameters : | msg : Message object
|
|---|
Dispatch a request message to the appropriate method.
| Parameters : | msg : Message object
|
|---|
Handle katcp v4 and below build-state inform
Handle katcp v4 and below version inform
Process a #version-connect message.
Check if the socket is currently connected.
| Returns : | connected : bool
|
|---|
Rejoin the client thread.
| Parameters : | timeout : float in seconds
|
|---|
Event handler that is called whenever the connection status changes.
Override in derived class for desired behaviour.
Note
This function should never block. Doing so will cause the client to cease processing data from the server until notify_connected completes.
| Parameters : | connected : bool
|
|---|
Preset server protocol flags.
Sets the assumed server protocol flags and disables automatic server version detection.
| Parameters : | protocol_flags : katcp.core.ProtocolFlags instance |
|---|
Send a request message, automatically assign a message ID if requested
| Parameters : | msg : katcp.Message request message use_mid : bool or None, default=None timeout : float or None, default=None
|
|---|---|
| Returns : | mid : string or None
If use_mid is None and the server supports msg ids, or if use_mid is : True a message ID will automatically be assigned msg.mid is None. : if msg.mid has a value, and the server supports msg ids, that value will : be used. If the server does not support msg ids, KatcpVersionError will : be raised : |
Process reply and inform messages from the server.
Whether the client is running.
| Returns : | running : bool
|
|---|
Send any kind of message.
| Parameters : | msg : Message object
timeout : float or None, default=None
|
|---|
Send a request messsage.
| Parameters : | msg : Message object
timeout : float or None, default=None
|
|---|
Start the client in a new thread.
| Parameters : | timeout : float in seconds
daemon : boolean
excepthook : function
|
|---|
Stop a running client (from another thread).
| Parameters : | timeout : float in seconds
|
|---|
Fallback method for inform messages without a registered handler
| Parameters : | msg : Message object
|
|---|
Fallback method for reply messages without a registered handler
| Parameters : | msg : Message object
|
|---|
Fallback method for requests without a registered handler
| Parameters : | msg : Message object
|
|---|
Wait until the client is connected.
| Parameters : | timeout : float in seconds
|
|---|---|
| Returns : | connected : bool
|
Wait until katcp protocol information has been received from the client.
| Parameters : | timeout : float in seconds
|
|---|---|
| Returns : | received : bool
If this method returns True, the server’s protocol information is : available in the ProtocolFlags instance self.protocol_flags. : |
Implements some standard messages on top of DeviceServerBase.
Inform messages handled are:
- version (sent on connect)
- build-state (sent on connect)
- log (via self.log.warn(...), etc)
- disconnect
- client-connected
Requests handled are:
- halt
- help
- log-level
- restart [1]
- client-list
- sensor-list
- sensor-sampling
- sensor-value
- watchdog
- version-list (only standard in KATCP v5 or later)
- sensor-sampling-clear (non-standard)
| [1] | Restart relies on .set_restart_queue() being used to register a restart queue with the device. When the device needs to be restarted, it will be added to the restart queue. The queue should be a Python Queue.Queue object without a maximum size. |
Unhandled standard requests are:
- configure
- mode
Subclasses can define the tuple VERSION_INFO to set the interface name, major and minor version numbers. The BUILD_INFO tuple can be defined to give a string describing a particular interface instance and may have a fourth element containing additional version information (e.g. rc1).
Subclasses may manipulate the versions returned by the ?version-list command by editing .extra_versions which is a dictionary mapping role or component names to (version, build_state_or_serial_no) tuples. The build_state_or_serial_no may be None.
Subclasses must override the .setup_sensors() method. If they have no sensors to register, the method should just be a pass.
Methods
| DeviceServer.add_sensor(sensor) | Add a sensor to the device. |
| DeviceServer.build_state() | Return a build state string in the form |
| DeviceServer.clear_strategies(client_conn[, ...]) | Clear the sensor strategies of a client connection |
| DeviceServer.get_sensor(sensor_name) | Fetch the sensor with the given name. |
| DeviceServer.get_sensors() | Fetch a list of all sensors |
| DeviceServer.get_sockets() | Return the complete list of current client socket. |
| DeviceServer.handle_inform(connection, msg) | Dispatch an inform message to the appropriate method. |
| DeviceServer.handle_message(client_conn, msg) | Handle messages of all types from clients. |
| DeviceServer.handle_reply(connection, msg) | Dispatch a reply message to the appropriate method. |
| DeviceServer.handle_request(connection, msg) | Dispatch a request message to the appropriate method. |
| DeviceServer.has_sensor(sensor_name) | Whether a sensor_name is known. |
| DeviceServer.inform(connection, msg) | Send an inform message to a particular client. |
| DeviceServer.join([timeout]) | Rejoin the server thread. |
| DeviceServer.mass_inform(msg) | Send an inform message to all clients. |
| DeviceServer.on_client_connect(client_conn) | Inform client of build state and version on connect. |
| DeviceServer.on_client_disconnect(...) | Inform client it is about to be disconnected. |
| DeviceServer.remove_sensor(sensor) | Remove a sensor from the device. |
| DeviceServer.reply(connection, reply, orig_req) | Send an asynchronous reply to an earlier request. |
| DeviceServer.reply_inform(connection, ...) | Send an inform as part of the reply to an earlier request. |
| DeviceServer.request_client_list(req, msg) | Request the list of connected clients. |
| DeviceServer.request_halt(req, msg) | Halt the device server. |
| DeviceServer.request_help(req, msg) | Return help on the available requests. |
| DeviceServer.request_log_level(req, msg) | Query or set the current logging level. |
| DeviceServer.request_restart(req, msg) | Restart the device server. |
| DeviceServer.request_sensor_list(req, msg) | Request the list of sensors. |
| DeviceServer.request_sensor_sampling(req, msg) | Configure or query the way a sensor is sampled. |
| DeviceServer.request_sensor_sampling_clear(*args) | Set all sampling strategies for this client to none. |
| DeviceServer.request_sensor_value(req, msg) | Request the value of a sensor or sensors. |
| DeviceServer.request_version_list(req, msg) | Request the list of versions of roles and subcomponents. |
| DeviceServer.request_watchdog(req, msg) | Check that the server is still alive. |
| DeviceServer.run() | Override DeviceServerBase.run() to ensure that the reactor thread is |
| DeviceServer.running() | Whether the server is running. |
| DeviceServer.set_restart_queue(restart_queue) | Set the restart queue. |
| DeviceServer.setup_sensors() | Populate the dictionary of sensors. |
| DeviceServer.start([timeout, daemon, excepthook]) | Start the server in a new thread. |
| DeviceServer.stop([timeout]) | Stop a running server (from another thread). |
| DeviceServer.tcp_inform(sock, msg) | Send an inform message to a particular TCP client. |
| DeviceServer.tcp_reply(sock, reply, orig_req) | Send an asynchronous reply to an earlier request for a tcp socket. |
| DeviceServer.tcp_reply_inform(sock, inform, ...) | Send an inform as part of the reply to an earlier request. |
| DeviceServer.version() | Return a version string of the form type-major.minor. |
| DeviceServer.wait_running([timeout]) | Wait until the server is running |
Add a sensor to the device.
Usually called inside .setup_sensors() but may be called from elsewhere.
| Parameters : | sensor : Sensor object
|
|---|
Return a build state string in the form name-major.minor[(a|b|rc)n]
Clear the sensor strategies of a client connection
| Parameters : | client_connection : ClientConnectionTCP instance
remove_client : bool, default=False
|
|---|
Fetch the sensor with the given name.
| Parameters : | sensor_name : str
|
|---|---|
| Returns : | sensor : Sensor object
|
Fetch a list of all sensors
| Returns : | sensors : list of Sensor objects
|
|---|
Return the complete list of current client socket.
| Returns : | sockets : list of socket.socket objects
|
|---|
Dispatch an inform message to the appropriate method.
| Parameters : | connection : ClientConnectionTCP object
msg : Message object
|
|---|
Handle messages of all types from clients.
| Parameters : | client_conn : ClientConnectionTCP object
msg : Message object
|
|---|
Dispatch a reply message to the appropriate method.
| Parameters : | connection : ClientConnectionTCP object
msg : Message object
|
|---|
Dispatch a request message to the appropriate method.
| Parameters : | connection : ClientConnectionTCP object
msg : Message object
|
|---|
Whether a sensor_name is known.
Send an inform message to a particular client.
Should only be used for asynchronous informs. Informs that are part of the response to a request should use reply_inform() so that the message identifier from the original request can be attached to the inform.
| Parameters : | connection : ClientConnectionTCP object
msg : Message object
|
|---|
Rejoin the server thread.
| Parameters : | timeout : float in seconds
|
|---|
Send an inform message to all clients.
| Parameters : | msg : Message object
|
|---|
Inform client of build state and version on connect.
| Parameters : | client_conn : ClientConnectionTCP object
|
|---|
Inform client it is about to be disconnected.
| Parameters : | client_conn : ClientConnectionTCP object
msg : str
connection_valid : boolean
|
|---|
Remove a sensor from the device.
Also deregisters all clients observing the sensor.
| Parameters : | sensor : Sensor object or name string
|
|---|
Send an asynchronous reply to an earlier request.
| Parameters : | connection : ClientConnectionTCP object
reply : Message object
orig_req : Message object
|
|---|
Send an inform as part of the reply to an earlier request.
| Parameters : | connection : ClientConnectionTCP object
inform : Message object
orig_req : Message object
|
|---|
Request the list of connected clients.
The list of clients is sent as a sequence of #client-list informs.
| Informs : | addr : str
|
|---|---|
| Returns : | success : {‘ok’, ‘fail’}
informs : int
|
Examples
?client-list
#client-list 127.0.0.1:53600
!client-list ok 1
Halt the device server.
| Returns : | success : {‘ok’, ‘fail’}
|
|---|
Examples
?halt
!halt ok
Return help on the available requests.
Return a description of the available requests using a seqeunce of #help informs.
| Parameters : | request : str, optional
|
|---|---|
| Informs : | request : str
description : str
|
| Returns : | success : {‘ok’, ‘fail’}
informs : int
|
Examples
?help
#help halt ...description...
#help help ...description...
...
!help ok 5
?help halt
#help halt ...description...
!help ok 1
Query or set the current logging level.
| Parameters : | level : {‘all’, ‘trace’, ‘debug’, ‘info’, ‘warn’, ‘error’, ‘fatal’, ‘off’}, optional
|
|---|---|
| Returns : | success : {‘ok’, ‘fail’}
level : {‘all’, ‘trace’, ‘debug’, ‘info’, ‘warn’, ‘error’, ‘fatal’, ‘off’}
|
Examples
?log-level
!log-level ok warn
?log-level info
!log-level ok info
Restart the device server.
| Returns : | success : {‘ok’, ‘fail’}
|
|---|
Examples
?restart
!restart ok
Request the list of sensors.
The list of sensors is sent as a sequence of #sensor-list informs.
| Parameters : | name : str, optional
|
|---|---|
| Informs : | name : str
description : str
units : str
type : str
params : list of str, optional
|
| Returns : | success : {‘ok’, ‘fail’}
informs : int
|
Examples
?sensor-list
#sensor-list psu.voltage PSU\_voltage. V float 0.0 5.0
#sensor-list cpu.status CPU\_status. \@ discrete on off error
...
!sensor-list ok 5
?sensor-list cpu.power.on
#sensor-list cpu.power.on Whether\_CPU\_hase\_power. \@ boolean
!sensor-list ok 1
?sensor-list /voltage/
#sensor-list psu.voltage PSU\_voltage. V float 0.0 5.0
#sensor-list cpu.voltage CPU\_voltage. V float 0.0 3.0
!sensor-list ok 2
Configure or query the way a sensor is sampled.
Sampled values are reported asynchronously using the #sensor-status message.
| Parameters : | name : str
strategy : {‘none’, ‘auto’, ‘event’, ‘differential’, ‘period’}, optional
params : list of str, optional
|
|---|---|
| Returns : | success : {‘ok’, ‘fail’}
name : str
strategy : {‘none’, ‘auto’, ‘event’, ‘differential’, ‘period’}
params : list of str
|
Examples
?sensor-sampling cpu.power.on
!sensor-sampling ok cpu.power.on none
?sensor-sampling cpu.power.on period 500
!sensor-sampling ok cpu.power.on period 500
Set all sampling strategies for this client to none.
| Returns : | success : {‘ok’, ‘fail’}
|
|---|
Examples
?sensor-sampling-clear !sensor-sampling-clear ok
Request the value of a sensor or sensors.
A list of sensor values as a sequence of #sensor-value informs.
| Parameters : | name : str, optional
|
|---|---|
| Informs : | timestamp : float
count : {1}
name : str
value : object
|
| Returns : | success : {‘ok’, ‘fail’}
informs : int
|
Examples
?sensor-value
#sensor-value 1244631611.415231 1 psu.voltage 4.5
#sensor-value 1244631611.415200 1 cpu.status off
...
!sensor-value ok 5
?sensor-value cpu.power.on
#sensor-value 1244631611.415231 1 cpu.power.on 0
!sensor-value ok 1
Request the list of versions of roles and subcomponents.
| Informs : | name : str
version : str
build_state_or_serial_number : str
|
|---|---|
| Returns : | success : {‘ok’, ‘fail’}
informs : int
|
Examples
?version-list
#version-list katcp-protocol 5.0-MI
#version-list katcp-library katcp-python-0.4 katcp-python-0.4.1-py2
#version-list katcp-device foodevice-1.0 foodevice-1.0.0rc1
!version-list ok 3
Check that the server is still alive.
| Returns : | success : {‘ok’} |
|---|
Examples
?watchdog
!watchdog ok
Override DeviceServerBase.run() to ensure that the reactor thread is running at the same time.
Whether the server is running.
Set the restart queue.
When the device server should be restarted, it will be added to the queue.
| Parameters : | restart_queue : Queue.Queue object
|
|---|
Populate the dictionary of sensors.
Unimplemented by default – subclasses should add their sensors here or pass if there are no sensors.
Examples
>>> class MyDevice(DeviceServer):
... def setup_sensors(self):
... self.add_sensor(Sensor(...))
... self.add_sensor(Sensor(...))
...
Start the server in a new thread.
| Parameters : | timeout : float in seconds
daemon : boolean
excepthook : function
|
|---|
Stop a running server (from another thread).
| Parameters : | timeout : float in seconds
|
|---|
Send an inform message to a particular TCP client.
Should only be used for asynchronous informs. Informs that are part of the response to a request should use reply_inform() so that the message identifier from the original request can be attached to the inform.
| Parameters : | sock : socket.socket object
msg : Message object
|
|---|
Send an asynchronous reply to an earlier request for a tcp socket.
| Parameters : | sock : socket.socket object
reply : Message object
orig_req : Message object
|
|---|
Send an inform as part of the reply to an earlier request.
| Parameters : | sock : socket.socket object
inform : Message object
orig_req : Message object
|
|---|
Return a version string of the form type-major.minor.
Wait until the server is running
Base class for device servers.
Subclasses should add .request_* methods for dealing with request messages. These methods each take the client request connection and msg objects as arguments and should return the reply message or raise an exception as a result.
Subclasses can also add .inform_* and reply_* methods to handle those types of messages.
Should a subclass need to generate inform messages it should do so using either the .inform() or .mass_inform() methods.
Finally, this class should probably not be subclassed directly but rather via subclassing DeviceServer itself which implements common .request_* methods.
| Parameters : | host : str
port : int
tb_limit : int
logger : logging.Logger object
|
|---|
Methods
| DeviceServerBase.get_sockets() | Return the complete list of current client socket. |
| DeviceServerBase.handle_inform(connection, msg) | Dispatch an inform message to the appropriate method. |
| DeviceServerBase.handle_message(client_conn, msg) | Handle messages of all types from clients. |
| DeviceServerBase.handle_reply(connection, msg) | Dispatch a reply message to the appropriate method. |
| DeviceServerBase.handle_request(connection, msg) | Dispatch a request message to the appropriate method. |
| DeviceServerBase.inform(connection, msg) | Send an inform message to a particular client. |
| DeviceServerBase.join([timeout]) | Rejoin the server thread. |
| DeviceServerBase.mass_inform(msg) | Send an inform message to all clients. |
| DeviceServerBase.on_client_connect(conn) | Called after client connection is established. |
| DeviceServerBase.on_client_disconnect(conn, ...) | Called before a client connection is closed. |
| DeviceServerBase.reply(connection, reply, ...) | Send an asynchronous reply to an earlier request. |
| DeviceServerBase.reply_inform(connection, ...) | Send an inform as part of the reply to an earlier request. |
| DeviceServerBase.run() | Listen for clients and process their requests. |
| DeviceServerBase.running() | Whether the server is running. |
| DeviceServerBase.start([timeout, daemon, ...]) | Start the server in a new thread. |
| DeviceServerBase.stop([timeout]) | Stop a running server (from another thread). |
| DeviceServerBase.tcp_inform(sock, msg) | Send an inform message to a particular TCP client. |
| DeviceServerBase.tcp_reply(sock, reply, orig_req) | Send an asynchronous reply to an earlier request for a tcp socket. |
| DeviceServerBase.tcp_reply_inform(sock, ...) | Send an inform as part of the reply to an earlier request. |
| DeviceServerBase.wait_running([timeout]) | Wait until the server is running |
Return the complete list of current client socket.
| Returns : | sockets : list of socket.socket objects
|
|---|
Dispatch an inform message to the appropriate method.
| Parameters : | connection : ClientConnectionTCP object
msg : Message object
|
|---|
Handle messages of all types from clients.
| Parameters : | client_conn : ClientConnectionTCP object
msg : Message object
|
|---|
Dispatch a reply message to the appropriate method.
| Parameters : | connection : ClientConnectionTCP object
msg : Message object
|
|---|
Dispatch a request message to the appropriate method.
| Parameters : | connection : ClientConnectionTCP object
msg : Message object
|
|---|
Send an inform message to a particular client.
Should only be used for asynchronous informs. Informs that are part of the response to a request should use reply_inform() so that the message identifier from the original request can be attached to the inform.
| Parameters : | connection : ClientConnectionTCP object
msg : Message object
|
|---|
Rejoin the server thread.
| Parameters : | timeout : float in seconds
|
|---|
Send an inform message to all clients.
| Parameters : | msg : Message object
|
|---|
Called after client connection is established.
Subclasses should override if they wish to send clients message or perform house-keeping at this point.
| Parameters : | conn : ClientConnectionTCP object
|
|---|
Called before a client connection is closed.
Subclasses should override if they wish to send clients message or perform house-keeping at this point. The server cannot guarantee this will be called (for example, the client might drop the connection). The message parameter contains the reason for the disconnection.
| Parameters : | conn : ClientConnectionTCP object
msg : str
connection_valid : boolean
|
|---|
Send an asynchronous reply to an earlier request.
| Parameters : | connection : ClientConnectionTCP object
reply : Message object
orig_req : Message object
|
|---|
Send an inform as part of the reply to an earlier request.
| Parameters : | connection : ClientConnectionTCP object
inform : Message object
orig_req : Message object
|
|---|
Listen for clients and process their requests.
Whether the server is running.
Start the server in a new thread.
| Parameters : | timeout : float in seconds
daemon : boolean
excepthook : function
|
|---|
Stop a running server (from another thread).
| Parameters : | timeout : float in seconds
|
|---|
Send an inform message to a particular TCP client.
Should only be used for asynchronous informs. Informs that are part of the response to a request should use reply_inform() so that the message identifier from the original request can be attached to the inform.
| Parameters : | sock : socket.socket object
msg : Message object
|
|---|
Send an asynchronous reply to an earlier request for a tcp socket.
| Parameters : | sock : socket.socket object
reply : Message object
orig_req : Message object
|
|---|
Send an inform as part of the reply to an earlier request.
| Parameters : | sock : socket.socket object
inform : Message object
orig_req : Message object
|
|---|
Wait until the server is running
Object for logging messages from a DeviceServer.
Log messages are logged at a particular level and under a particular name. Names use dotted notation to form a virtual hierarchy of loggers with the device.
| Parameters : | device_server : DeviceServerBase object
root_logger : str
|
|---|
Methods
| DeviceLogger.debug(msg, *args, **kwargs) | Log a debug message. |
| DeviceLogger.error(msg, *args, **kwargs) | Log an error message. |
| DeviceLogger.fatal(msg, *args, **kwargs) | Log a fatal error message. |
| DeviceLogger.info(msg, *args, **kwargs) | Log an info message. |
| DeviceLogger.level_from_name(level_name) | Return the level constant for a given name. |
| DeviceLogger.level_name([level]) | Return the name of the given level value. |
| DeviceLogger.log(level, msg, *args, **kwargs) | Log a message and inform all clients. |
| DeviceLogger.log_to_python(logger, msg) | Log a KATCP logging message to a Python logger. |
| DeviceLogger.set_log_level(level) | Set the logging level. |
| DeviceLogger.set_log_level_by_name(level_name) | Set the logging level using a level name. |
| DeviceLogger.trace(msg, *args, **kwargs) | Log a trace message. |
| DeviceLogger.warn(msg, *args, **kwargs) | Log an warning message. |
Log a debug message.
Log an error message.
Log a fatal error message.
Log an info message.
Return the level constant for a given name.
If the level_name is not known, raise a ValueError.
| Parameters : | level_name : str
|
|---|---|
| Returns : | level : logging level constant
|
Return the name of the given level value.
If level is None, return the name of the current level.
| Parameters : | level : logging level constant
|
|---|---|
| Returns : | level_name : str
|
Log a message and inform all clients.
| Parameters : | level : logging level constant
msg : str
args : list of objects
kwargs : additional keyword parameters
|
|---|
Log a KATCP logging message to a Python logger.
| Parameters : | logger : logging.Logger object
msg : Message object
|
|---|
Set the logging level.
| Parameters : | level : logging level constant
|
|---|
Set the logging level using a level name.
| Parameters : | level_name : str
|
|---|
Log a trace message.
Log an warning message.
Instantiate a new sensor object.
Subclasses will usually pass in a fixed sensor_type which should be one of the sensor type constants. The list params if set will have its values formatter by the type formatter for the given sensor type.
Note
The LRU sensor type was deprecated in katcp 0.4.
Note
The ADDRESS sensor type was added in katcp 0.4.
| Parameters : | sensor_type : Sensor type constant
name : str
description : str
units : str
params : list
default : object
|
|---|
Methods
| Sensor.address(name[, description, unit, ...]) | Instantiate a new IP address sensor object. |
| Sensor.attach(observer) | Attach an observer to this sensor. |
| Sensor.boolean(name[, description, unit, ...]) | Instantiate a new boolean sensor object. |
| Sensor.detach(observer) | Detach an observer from this sensor. |
| Sensor.discrete(name[, description, unit, ...]) | Instantiate a new discrete sensor object. |
| Sensor.float(name[, description, unit, ...]) | Instantiate a new float sensor object. |
| Sensor.integer(name[, description, unit, ...]) | Instantiate a new integer sensor object. |
| Sensor.lru(name[, description, unit, default]) | Instantiate a new lru sensor object. |
| Sensor.notify() | Notify all observers of changes to this sensor. |
| Sensor.parse_params(sensor_type, ...[, major]) | Parse KATCP formatted parameters into Python values. |
| Sensor.parse_type(type_string) | Parse KATCP formatted type code into Sensor type constant. |
| Sensor.parse_value(s_value[, katcp_major]) | Parse a value from a string. |
| Sensor.read() | Read the sensor and return a timestamp, status, value tuple. |
| Sensor.read_formatted([major]) | Read the sensor and return a timestamp, status, value tuple. |
| Sensor.set(timestamp, status, value) | Set the current value of the sensor. |
| Sensor.set_formatted(raw_timestamp, ...[, major]) | Set the current value of the sensor. |
| Sensor.set_value(value[, status, timestamp, ...]) | Check and then set the value of the sensor. |
| Sensor.string(name[, description, unit, default]) | Instantiate a new string sensor object. |
| Sensor.timestamp(name[, description, unit, ...]) | Instantiate a new timestamp sensor object. |
| Sensor.value() | Read the current sensor value. |
Instantiate a new IP address sensor object.
Attach an observer to this sensor.
The observer must support a call to observer.update(sensor).
| Parameters : | observer : object
|
|---|
Instantiate a new boolean sensor object.
Detach an observer from this sensor.
| Parameters : | observer : object
|
|---|
Instantiate a new discrete sensor object.
Instantiate a new float sensor object.
Instantiate a new integer sensor object.
Instantiate a new lru sensor object.
Notify all observers of changes to this sensor.
Parse KATCP formatted parameters into Python values.
| Parameters : | sensor_type : Sensor type constant
formatted_params : list of strings
major : int. Defaults to latest implemented KATCP version (5)
|
|---|---|
| Returns : | params : list of objects
|
Parse KATCP formatted type code into Sensor type constant.
| Parameters : | type_string : str
|
|---|---|
| Returns : | sensor_type : Sensor type constant
|
Parse a value from a string.
| Parameters : | s_value : str
|
|---|---|
| Returns : | value : object
|
Read the sensor and return a timestamp, status, value tuple.
| Returns : | timestamp : float in seconds
status : Sensor status constant
value : object
|
|---|
Read the sensor and return a timestamp, status, value tuple.
All values are strings formatted as specified in the Sensor Type Formats in the katcp specification.
| Parameters : | major : int. Defaults to latest implemented KATCP version (5)
|
|---|---|
| Returns : | timestamp : str
status : str
value : str
|
Set the current value of the sensor.
| Parameters : | timestamp : float in seconds
status : Sensor status constant
value : object
|
|---|
Set the current value of the sensor.
| Parameters : | timestamp : str
status : str
value : str
major : int, default = 5
|
|---|
Check and then set the value of the sensor.
| Parameters : | value : object
status : Sensor status constant
timestamp : float in seconds or None
major : int. Defaults to latest implemented KATCP version (5)
|
|---|
Instantiate a new string sensor object.
Instantiate a new timestamp sensor object.
Read the current sensor value.
| Returns : | value : object
|
|---|
Raised by request handlers to indicate a failure.
A custom exception which, when thrown in a request handler, causes DeviceServerBase to send a fail reply with the specified fail message, bypassing the generic exception handling, which would send a fail reply with a full traceback.
Examples
>>> class MyDevice(DeviceServer):
... def request_myreq(self, req, msg):
... raise FailReply("This request always fails.")
...
Raised by a request handlers to indicate it will reply later.
A custom exception which, when thrown in a request handler, indicates to DeviceServerBase that no reply has been returned by the handler but that the handler has arranged for a reply message to be sent at a later time.
Examples
>>> class MyDevice(DeviceServer):
... def request_myreq(self, req, msg):
... self.callback_client.request(
... Message.request("otherreq"),
... reply_cb=self._send_reply,
... )
... raise AsyncReply()
...
Raised by KATCP servers when errors occur.
Changed in version 0.1: Deprecated in 0.1. Servers should not raise errors if communication with a client fails – errors are simply logged instead.
Represents a KAT device control language message.
| Parameters : | mtype : Message type constant
name : str
arguments : list of strings
mid : str, digits only
|
|---|
Methods
| Message.copy() | Return a shallow copy of the message object and its arguments. |
| Message.inform(name, *args, **kwargs) | Helper method for creating inform messages. |
| Message.reply(name, *args, **kwargs) | Helper method for creating reply messages. |
| Message.reply_inform(req_msg, *args) | Helper method for creating inform messages in reply to a request. |
| Message.reply_ok() | Return True if this is a reply and its first argument is ‘ok’. |
| Message.reply_to_request(req_msg, *args) | Helper method for creating reply messages to a specific request. |
| Message.request(name, *args, **kwargs) | Helper method for creating request messages. |
Return a shallow copy of the message object and its arguments.
| Returns : | msg : Message
|
|---|
Helper method for creating inform messages.
| Parameters : | name : str
args : list of strings
|
|---|
Helper method for creating reply messages.
| Parameters : | name : str
args : list of strings
|
|---|
Helper method for creating inform messages in reply to a request.
Copies the message name and message identifier from the request message
| Parameters : | req_msg : katcp.core.Message instance
args : list of strings
|
|---|
Return True if this is a reply and its first argument is ‘ok’.
Helper method for creating reply messages to a specific request.
Copies the message name and message identifier from the request message
| Parameters : | req_msg : katcp.core.Message instance
args : list of strings
|
|---|
Helper method for creating request messages.
| Parameters : | name : str
args : list of strings
|
|---|
Parses lines into Message objects.
Methods
| MessageParser.parse(line) | Parse a line, return a Message. |
Parse a line, return a Message.
| Parameters : | line : str
|
|---|---|
| Returns : | msg : Message object
|
Metaclass for DeviceServer and DeviceClient classes.
Collects up methods named request_* and adds them to a dictionary of supported methods on the class. All request_* methods must have a doc string so that help can be generated. The same is done for inform_* and reply_* methods.
Methods
| DeviceMetaclass.mro(() -> list) | return a type’s method resolution order |
A custom Thread class that provides an exception hook.
Exceptions are passed up to an excepthook callable that functions like sys.excepthook.
| Parameters : | excepthook : callable
args : additional arguments
kwargs: additional keyword arguments :
|
|---|
Methods
| ExcepthookThread.getName() | |
| ExcepthookThread.isAlive() | |
| ExcepthookThread.isDaemon() | |
| ExcepthookThread.is_alive() | |
| ExcepthookThread.join([timeout]) | |
| ExcepthookThread.run() | |
| ExcepthookThread.setDaemon(daemonic) | |
| ExcepthookThread.setName(name) | |
| ExcepthookThread.start() |