cgse-common 2024.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
egse/protocol.py ADDED
@@ -0,0 +1,607 @@
1
+ """
2
+ CommandProtocol is a base class for communicating commands with the hardware or
3
+ the control server. This class implements methods to send command messages and
4
+ receive responses.
5
+
6
+ The protocol also knows how to load the commands from the YAML file that contains
7
+ command definitions.
8
+
9
+ """
10
+ from __future__ import annotations
11
+
12
+ import abc
13
+ import inspect
14
+ import logging
15
+ import pickle
16
+
17
+ from prometheus_client import Counter
18
+ from prometheus_client import Summary
19
+
20
+ from egse.command import Command
21
+ from egse.command import CommandExecution
22
+ from egse.control import ControlServer
23
+ from egse.control import Failure
24
+ from egse.device import DeviceConnectionObserver
25
+ from egse.system import format_datetime
26
+ from egse.zmq_ser import bind_address
27
+
28
+ logger = logging.getLogger(__name__)
29
+
30
+ COMMAND_REQUESTS = Counter("cs_command_requests_count", "Count the number of commands", ["target"])
31
+ EXECUTION_TIME = Summary("cs_command_execution_time_seconds", "Time spent executing a command")
32
+
33
+
34
+ def get_method(parent_obj, method_name: str):
35
+ """
36
+ Returns a bound method from a given class instance.
37
+
38
+ Args:
39
+ parent_obj: the class instance that provides the method
40
+ method_name: name of the method that is requested
41
+
42
+ Returns:
43
+ the method [type: method].
44
+
45
+ .. note::
46
+ The method returned is an bound class instance method and therefore
47
+ this method *does not* expects as its first argument the class
48
+ instance, i.e. self, when you call this as a function.
49
+
50
+ """
51
+ if method_name is None or method_name == "None":
52
+ return None
53
+
54
+ if hasattr(parent_obj, method_name):
55
+ method = getattr(parent_obj, method_name)
56
+ if inspect.ismethod(method) or hasattr(method, "__method_wrapper"):
57
+ return method
58
+ logger.warning(f"{method_name} is not a method, type={type(method)}")
59
+ else:
60
+ logger.warning(f"{parent_obj!r} has no method called {method_name}")
61
+
62
+ return None
63
+
64
+
65
+ def get_function(parent_class, method_name: str):
66
+ """
67
+ Returns a function (unbound method) from a given class.
68
+
69
+ Args:
70
+ parent_class: the class that provides the method
71
+ method_name: name of the method that is requested
72
+
73
+ Returns:
74
+ the method [type: function].
75
+
76
+ .. note::
77
+ The function returned is an unbound class instance method and
78
+ therefore this function expects as its first argument the class
79
+ instance, i.e. self, when you call it as a function.
80
+
81
+ """
82
+ if method_name is None or method_name == "None":
83
+ return None
84
+
85
+ if hasattr(parent_class, method_name):
86
+ func = getattr(parent_class, method_name)
87
+ if inspect.isfunction(func):
88
+ return func
89
+ logger.warning(f"{method_name} is not a function, type={type(func)}")
90
+ else:
91
+ logger.warning(
92
+ f"{parent_class.__module__}.{parent_class.__name__} has no method called {method_name}"
93
+ )
94
+
95
+ return None
96
+
97
+
98
+ class BaseCommandProtocol(DeviceConnectionObserver):
99
+
100
+ def __init__(self, control_server: ControlServer):
101
+ super().__init__()
102
+ self.__socket = None
103
+ self.__control_server = control_server
104
+
105
+ def bind(self, socket):
106
+ """Bind to a socket to listen for commands."""
107
+ self.__socket = socket
108
+ self.__socket.bind(self.get_bind_address())
109
+
110
+ def get_bind_address(self):
111
+ """
112
+ Returns a string with the bind address, the endpoint, for accepting connections
113
+ and bind a socket to.
114
+
115
+ This method should be implemented by the sub-class since it contains the protocol
116
+ and port number that are specific for the sub-class.
117
+
118
+ Returns:
119
+ a string with the protocol and port to bind a socket to.
120
+ """
121
+ return bind_address(
122
+ self.__control_server.get_communication_protocol(),
123
+ self.__control_server.get_commanding_port(),
124
+ )
125
+
126
+ def is_alive(self) -> bool:
127
+ """
128
+ This method can be overridden by a sub-class to check whether any Thread or sub-process
129
+ that was started is still alive.
130
+ """
131
+ return True
132
+
133
+ def get_control_server(self):
134
+ return self.__control_server
135
+
136
+ def get_status(self):
137
+ """
138
+ Returns a dictionary with status information for the control server, enhanced by the
139
+ sub-class with device specific status information.
140
+
141
+ This method should be implemented/overridden by the sub-class. The sub-class specific
142
+ method should update the dictionary returned by this super-class method with device
143
+ specific status values.
144
+
145
+ The dict returned by this method includes the following keywords:
146
+
147
+ * timestamp (str): a string representation of the current datetime
148
+ * PID (int): the Process ID for the control server
149
+ * Up (float): the uptime of the control server [s]
150
+ * UUID (uuid1): a UUID for the control server
151
+ * RSS (int): the 'Resident Set Size', this is the non-swapped physical memory a process
152
+ has used [byte]
153
+ * USS (int): the 'Unique Set Size', this is the memory which is unique to a process [byte]
154
+ * CPU User (float): time spent in user mode [s]
155
+ * CPU System (float): time spent in kernel mode [s]
156
+ * CPU% (float): the process CPU utilization as a percentage [%]
157
+
158
+ Check the documentation for `psutil.Process` for more in-depth information about the
159
+ dict keys.
160
+
161
+ Returns:
162
+ a dictionary with status information.
163
+ """
164
+ status = {
165
+ "timestamp": format_datetime(),
166
+ "delay": self.__control_server.delay,
167
+ }
168
+ status.update(self.__control_server.get_process_status())
169
+ return status
170
+
171
+ def get_housekeeping(self) -> dict:
172
+ """Returns a dictionary with housekeeping information about the device."""
173
+ raise NotImplementedError(
174
+ f"The get_housekeeping() method shall be implemented for {self.__class__.__name__}."
175
+ )
176
+
177
+ def get_device(self):
178
+ """Returns the device object for the device that is controlled by this protocol."""
179
+ raise NotImplementedError
180
+
181
+ def send(self, data):
182
+ """
183
+ Send a message to the ControlServer. The message shall be fully populated
184
+ and is only serialized before sending over the ZeroMQ socket.
185
+
186
+ FIXME: We need to add error handling here, e.g. what if the send() fails? Do we need
187
+ to implement retries as with Proxy?
188
+ """
189
+ pickle_string = pickle.dumps(data)
190
+ self.__socket.send(pickle_string)
191
+
192
+ def receive(self):
193
+ """
194
+ Receive a serialized message from the ControlServer. The message will not
195
+ be decoded/de-serialized, but is returned as it was sent. Decoding shall
196
+ be handled by the calling method.
197
+ """
198
+ pickle_string = self.__socket.recv()
199
+ return pickle.loads(pickle_string)
200
+
201
+ # FIXME:
202
+ # We might want to reconsider how commands are send over the ZeroMQ sockets.
203
+ # it can be very useful to use multipart messages here with the type and
204
+ # origin etc. to ease the if..else.. constructs.
205
+
206
+ @EXECUTION_TIME.time()
207
+ def execute(self):
208
+ cs = self.get_control_server() # FIXME: 'cs' not used!
209
+ data = self.receive()
210
+ cmd = None
211
+ args = kwargs = None
212
+ if isinstance(data, CommandExecution):
213
+ cmd = data.get_cmd()
214
+ cmd_name = cmd.get_name()
215
+ args = data.get_args()
216
+ kwargs = data.get_kwargs()
217
+ elif isinstance(data, dict):
218
+ cmd_name = data.get("cmd")
219
+ args = data.get("args")
220
+ kwargs = data.get("kwargs")
221
+ elif isinstance(data, str):
222
+ cmd_name = data
223
+ else:
224
+ cmd_name = None
225
+
226
+ logger.log(0, f"cmd_name = {cmd_name}")
227
+
228
+ # Server availability request - Ping-Pong
229
+
230
+ if cmd_name == "Ping":
231
+ COMMAND_REQUESTS.labels(target="ping").inc()
232
+ self.send("Pong")
233
+ elif cmd_name == "send_commands":
234
+ logger.warning("send_commands was commanded for a DynamicCommandProtocol!")
235
+ elif cmd_name == "get_service_port":
236
+ self.send(self.__control_server.get_service_port())
237
+ elif cmd_name == "get_monitoring_port":
238
+ self.send(self.__control_server.get_monitoring_port())
239
+ elif cmd_name == "get_commanding_port":
240
+ self.send(self.__control_server.get_commanding_port())
241
+ elif cmd_name == "get_ip_address":
242
+ self.send(self.__control_server.get_ip_address())
243
+ elif cmd:
244
+ COMMAND_REQUESTS.labels(target="device").inc()
245
+ cmd.server_call(self, *args, **kwargs)
246
+ else:
247
+ COMMAND_REQUESTS.labels(target="invalid").inc()
248
+ logger.warning(f"Invalid command received: {cmd_name}")
249
+ self.send(Failure(f"Invalid command: {cmd_name}"))
250
+
251
+ def quit(self):
252
+ """
253
+ This method can be overridden by a sub-class to cleanup and stop threads that it
254
+ started.
255
+ """
256
+
257
+ logger.info("quit() method called on Protocol base class.")
258
+
259
+
260
+
261
+ class DynamicCommandProtocol(BaseCommandProtocol, metaclass=abc.ABCMeta):
262
+ def __init__(self, control_server: ControlServer):
263
+ super().__init__(control_server)
264
+
265
+ def handle_device_method(self, cmd: Command, *args, **kwargs):
266
+ """
267
+ Call the device method with the given arguments.
268
+
269
+ Args:
270
+ cmd: the devices command class that knows which device command shall be called
271
+ args: the arguments that will be passed on to the device command
272
+ kwargs: the keyword arguments that will be passed on to the device command
273
+ """
274
+ # The lookup table contains object (bound) methods, so we do not have to
275
+ # provide the 'self' argument anymore.
276
+
277
+ method_name = cmd.get_device_method_name()
278
+ method = get_method(self.get_device(), method_name)
279
+
280
+ # We treat the get_response function special as it needs to send the ``cmd`` string
281
+ # to the device we need to pass the processed cmd string into the method.
282
+
283
+ try:
284
+ if method_name == "get_response":
285
+ device_cmd_string = cmd.get_cmd_string(*args, *kwargs)
286
+ logger.log(5, f"Executing method {method.__name__}({device_cmd_string})")
287
+ response = method(device_cmd_string)
288
+ else:
289
+ logger.log(5, f"Executing method {method.__name__}({args}, {kwargs})")
290
+ response = method(*args, **kwargs)
291
+ except Exception as exc:
292
+ logger.exception(f"Executing {method_name} failed.")
293
+ # Pass the exception on to the client as a Failure message
294
+ response = Failure(f"Executing {method_name} failed: ", exc)
295
+
296
+ # Enable the following message only when debugging, because this log message can become
297
+ # very long for data storage commands.
298
+ # logger.debug(f"handle_device_method: {device_name}({args}, {kwargs}) -> {response!s}")
299
+
300
+ self.send(response)
301
+
302
+
303
+ # TODO (rik): The CommandProtocol shall also inherit from the BaseCommandProtocol
304
+
305
+ class CommandProtocol(DeviceConnectionObserver, metaclass=abc.ABCMeta):
306
+ """
307
+ This class is the glue between the control servers and the hardware
308
+ controllers on one side, and between the control server and the connected
309
+ proxy classes on the other side.
310
+
311
+ The connection with the hardware controllers is when the ``execute()`` method
312
+ calls the ``server_call()`` method of the command class.
313
+
314
+ The connection with the proxy classes is when the ``client_call()`` method is added to the
315
+ interface of the Proxy subclass (by the ``_add_commands()`` method).
316
+
317
+ FIXME: Protocol is not used at the client side, i.e. the Proxy class.
318
+ """
319
+
320
+ def __init__(self):
321
+ super().__init__()
322
+ self.__socket = None
323
+ self._commands = {} # variable is used by sub classes
324
+ self.control_server: ControlServer | None = None # variable set by the sub-class
325
+ self._method_lookup = {} # lookup table for device methods
326
+
327
+ def bind(self, socket):
328
+ """Bind to a socket to listen for commands."""
329
+ self.__socket = socket
330
+
331
+ bind_address = self.get_bind_address()
332
+ logger.info(f"Binding to {bind_address}")
333
+
334
+ self.__socket.bind(bind_address)
335
+
336
+ # FIXME:
337
+ # We might want to reconsider how commands are send over the ZeroMQ sockets.
338
+ # it can be very useful to use multipart messages here with the type and
339
+ # origin etc. to ease the if..else.. constructs.
340
+
341
+ @EXECUTION_TIME.time()
342
+ def execute(self):
343
+ data = self.receive()
344
+ cmd = None
345
+ args = kwargs = None
346
+ if isinstance(data, CommandExecution):
347
+ cmd = data.get_cmd()
348
+ cmd_name = cmd.get_name()
349
+ args = data.get_args()
350
+ kwargs = data.get_kwargs()
351
+ elif isinstance(data, dict):
352
+ cmd_name = data.get("cmd")
353
+ args = data.get("args")
354
+ kwargs = data.get("kwargs")
355
+ elif isinstance(data, str):
356
+ cmd_name = data
357
+ else:
358
+ cmd_name = None
359
+
360
+ logger.log(0, f"cmd_name = {cmd_name}")
361
+
362
+ # Server availability request - Ping-Pong
363
+
364
+ if cmd_name == "Ping":
365
+ COMMAND_REQUESTS.labels(target="ping").inc()
366
+ self.send("Pong")
367
+ elif cmd_name == "send_commands":
368
+ self.send_commands()
369
+ elif cmd_name == "get_service_port":
370
+ self.send(self.control_server.get_service_port())
371
+ elif cmd_name == "get_monitoring_port":
372
+ self.send(self.control_server.get_monitoring_port())
373
+ elif cmd_name == "get_commanding_port":
374
+ self.send(self.control_server.get_commanding_port())
375
+ elif cmd_name == "get_ip_address":
376
+ self.send(self.control_server.get_ip_address())
377
+ elif cmd_name == "get_storage_mnemonic":
378
+ self.send(self.control_server.get_storage_mnemonic())
379
+ elif cmd:
380
+ COMMAND_REQUESTS.labels(target="device").inc()
381
+ cmd.server_call(self, *args, **kwargs)
382
+ else:
383
+ COMMAND_REQUESTS.labels(target="invalid").inc()
384
+ logger.warning(f"Invalid command received: {cmd_name}")
385
+ self.send(Failure(f"Invalid command: {cmd_name}"))
386
+
387
+ def quit(self):
388
+ """
389
+ This method can be overridden by a sub-class to cleanup and stop threads that it
390
+ started.
391
+ """
392
+
393
+ logger.info("quit() method called on Protocol base class.")
394
+
395
+ def is_alive(self) -> bool:
396
+ """
397
+ This method can be overridden by a sub-class to check whether any Thread or sub-process
398
+ that was started is still alive.
399
+ """
400
+ return True
401
+
402
+ @abc.abstractmethod
403
+ def get_bind_address(self):
404
+ """
405
+ Returns a string with the bind address, the endpoint, for accepting connections
406
+ and bind a socket to.
407
+
408
+ This method should be implemented by the sub-class since it contains the protocol
409
+ and port number that are specific for the sub-class.
410
+
411
+ Returns:
412
+ a string with the protocol and port to bind a socket to.
413
+ """
414
+ pass
415
+
416
+ @abc.abstractmethod
417
+ def get_status(self):
418
+ """
419
+ Returns a dictionary with status information for the control server, enhanced by the
420
+ sub-class with device specific status information.
421
+
422
+ This method should be implemented/overridden by the sub-class. The sub-class specific
423
+ method should update the dictionary returned by this super-class method with device
424
+ specific status values.
425
+
426
+ The dict returned by this method includes the following keywords:
427
+
428
+ * timestamp (str): a string representation of the current datetime
429
+ * PID (int): the Process ID for the control server
430
+ * Up (float): the uptime of the control server [s]
431
+ * UUID (uuid1): a UUID for the control server
432
+ * RSS (int): the 'Resident Set Size', this is the non-swapped physical memory a process
433
+ has used [byte]
434
+ * USS (int): the 'Unique Set Size', this is the memory which is unique to a process [byte]
435
+ * CPU User (float): time spent in user mode [s]
436
+ * CPU System (float): time spent in kernel mode [s]
437
+ * CPU% (float): the process CPU utilization as a percentage [%]
438
+
439
+ Check the documentation for `psutil.Process` for more in-depth information about the
440
+ dict keys.
441
+
442
+ Returns:
443
+ a dictionary with status information.
444
+ """
445
+ status = {
446
+ "timestamp": format_datetime(),
447
+ "delay": self.control_server.delay,
448
+ }
449
+ status.update(self.control_server.get_process_status())
450
+ return status
451
+
452
+ def get_housekeeping(self) -> dict:
453
+ """Returns a dictionary with housekeeping information about the device."""
454
+ raise NotImplementedError(
455
+ f"The get_housekeeping() method shall be implemented for {self.__class__.__name__}."
456
+ )
457
+
458
+ def send(self, data):
459
+ """
460
+ Send a message to the ControlServer. The message shall be fully populated
461
+ and is only serialized before sending over the ZeroMQ socket.
462
+
463
+ FIXME: We need to add error handling here, e.g. what if the send() fails? Do we need
464
+ to implement retries as with Proxy?
465
+ """
466
+ pickle_string = pickle.dumps(data)
467
+ self.__socket.send(pickle_string)
468
+
469
+ def receive(self):
470
+ """
471
+ Receive a serialized message from the ControlServer. The message will not
472
+ be decoded/de-serialized, but is returned as it was sent. Decoding shall
473
+ be handled by the calling method.
474
+ """
475
+ pickle_string = self.__socket.recv()
476
+ data = pickle.loads(pickle_string)
477
+ return data
478
+
479
+ def send_commands(self):
480
+ """
481
+ Send the command definitions that were loaded for the specific device.
482
+ """
483
+ self.send(self._commands)
484
+
485
+ def load_commands(self, command_settings, command_class, device_class):
486
+ """
487
+ Loads the command definitions from the given ``command_settings`` and builds an internal
488
+ dictionary containing the command names as keys and the corresponding ``Command`` class
489
+ objects as values.
490
+
491
+ The ``command_settings`` is usually loaded from a YAML configuration file containing the
492
+ command definitions for the device.
493
+
494
+ Args:
495
+ command_settings: a dictionary containing the command definitions for this device
496
+ command_class: the type of command to create, a subclass of Command
497
+ device_class: the type of the base device class from which the methods are loaded
498
+ """
499
+ for name in command_settings:
500
+ command_settings_name = command_settings[name]
501
+ if "cmd" in command_settings_name:
502
+ cmd = command_settings_name["cmd"]
503
+ else:
504
+ cmd = ""
505
+
506
+ if "description" in command_settings_name:
507
+ description = command_settings_name["description"]
508
+ else:
509
+ description = None
510
+
511
+ # The response field is the name of a function from the CommandProtocol class or a
512
+ # sub-class. This function shall send a response back to the client (Proxy). That's
513
+ # why this field is called response.
514
+ # By convention we like that this method name would start with `handle_` so the we
515
+ # can make a distinction between response commands and normal methods in Protocol.
516
+ # Remember that response methods should send a reply back to the client (which will
517
+ # be waiting for it..).
518
+ # If no response field is given, then the `handle_device_method` will be called.
519
+
520
+ if "response" in command_settings_name:
521
+ response_method = get_function(self.__class__, command_settings_name["response"])
522
+ else:
523
+ response_method = get_function(self.__class__, "handle_device_method")
524
+
525
+ # The device_method field is used in the `handle_device_method` to call the method on
526
+ # the device class. That is the class that implements the DeviceInterface and is
527
+ # usually called a Controller or a Simulator.
528
+ #
529
+ # If no device_name field is given, the name from the command_settings is used.
530
+
531
+ if "device_method" in command_settings_name:
532
+ device_method_name = command_settings_name["device_method"]
533
+ else:
534
+ device_method_name = name
535
+
536
+ # check if the device_method exists in the device base class
537
+
538
+ if device_method_name == "None":
539
+ device_method = None
540
+ else:
541
+ device_method = get_function(device_class, device_method_name)
542
+
543
+ logger.log(
544
+ 0,
545
+ f"Creating {command_class.__module__}.{command_class.__name__}(name='{name}', "
546
+ f"cmd='{cmd}', "
547
+ f"response={response_method}, device_method={device_method})",
548
+ )
549
+ logger.debug(f"Creating {command_class.__name__} command with {name=}, {cmd=}, {device_method=}")
550
+
551
+ self._commands[name] = command_class(
552
+ name=name,
553
+ cmd=cmd,
554
+ response=response_method,
555
+ description=description,
556
+ device_method=device_method,
557
+ )
558
+
559
+ def build_device_method_lookup_table(self, device_obj):
560
+ """
561
+ Fill the lookup table with device command methods that are bound to the device object.
562
+
563
+ Args:
564
+ device_obj: instance of a device command class
565
+ """
566
+ for cmd in self._commands.values():
567
+ method_name = cmd.get_device_method_name()
568
+ method = get_method(device_obj, method_name)
569
+ if method is not None:
570
+ self._method_lookup[method_name] = method
571
+
572
+ def handle_device_method(self, cmd: Command, *args, **kwargs):
573
+ """
574
+ Call the device method with the given arguments.
575
+
576
+ Args:
577
+ cmd: the devices command class that knows which device command shall be called
578
+ args: the arguments that will be passed on to the device command
579
+ kwargs: the keyword arguments that will be passed on to the device command
580
+ """
581
+ # The lookup table contains object (bound) methods, so we do not have to
582
+ # provide the 'self' argument anymore.
583
+
584
+ device_name = cmd.get_device_method_name()
585
+ method = self._method_lookup[device_name]
586
+
587
+ # We treat the get_response function special as it needs to send the ``cmd`` string
588
+ # to the device we need to pass the processed cmd string into the method.
589
+
590
+ try:
591
+ if device_name == "get_response":
592
+ device_cmd_string = cmd.get_cmd_string(*args, *kwargs)
593
+ logger.log(5, f"Executing method {method.__name__}({device_cmd_string})")
594
+ response = method(device_cmd_string)
595
+ else:
596
+ logger.log(5, f"Executing method {method.__name__}({args}, {kwargs})")
597
+ response = method(*args, **kwargs)
598
+ except Exception as exc:
599
+ logger.exception(f"Executing {device_name} failed.")
600
+ # Pass the exception on to the client as a Failure message
601
+ response = Failure(f"Executing {device_name} failed: ", exc)
602
+
603
+ # Enable the following message only when debugging, because this log message can become
604
+ # very long for data storage commands.
605
+ # logger.debug(f"handle_device_method: {device_name}({args}, {kwargs}) -> {response!s}")
606
+
607
+ self.send(response)