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/proxy.py ADDED
@@ -0,0 +1,522 @@
1
+ """
2
+ The Proxy module provides the base class for the Proxy objects for each device
3
+ controller.
4
+
5
+ The module also provides the connection state interface and classes for
6
+ maintaining the state of the Proxy connection to the control server.
7
+ """
8
+ import logging
9
+ import pickle
10
+ import types
11
+ from types import MethodType
12
+
13
+ import zmq
14
+
15
+ from egse.decorators import dynamic_interface
16
+ from egse.mixin import DynamicClientCommandMixin
17
+ from egse.system import AttributeDict
18
+ from egse.zmq_ser import split_address
19
+
20
+
21
+ def set_docstring(func, cmd):
22
+ """Decorator to set the docstring of the command on the dynamic method / function."""
23
+
24
+ def wrap_func(*args, **kwargs):
25
+ return func(*args, **kwargs)
26
+
27
+ wrap_func.__doc__ = cmd.__doc__
28
+ wrap_func.__name__ = f"{cmd.get_name()}"
29
+ return wrap_func
30
+
31
+
32
+ REQUEST_TIMEOUT = 30 * 1000 # timeout in millisecond
33
+ REQUEST_RETRIES = 0
34
+
35
+
36
+ class ControlServerConnectionInterface:
37
+ """This interface defines the connection commands for control servers.
38
+
39
+ This interface shall be implemented by the Proxy class and guarantees that connection commands
40
+ do not interfere with the commands defined in the `DeviceConnectionInterface` (which will be
41
+ loaded from the control server).
42
+ """
43
+
44
+ def __enter__(self):
45
+ self.connect_cs()
46
+ return self
47
+
48
+ def __exit__(self, exc_type, exc_val, exc_tb):
49
+ self.disconnect_cs()
50
+
51
+ @dynamic_interface
52
+ def connect_cs(self):
53
+ """Connect to the control server.
54
+
55
+ Raises:
56
+ ConnectionError: when the connection can not be established.
57
+ """
58
+ raise NotImplementedError
59
+
60
+ @dynamic_interface
61
+ def reconnect_cs(self):
62
+ """Reconnect the control server after it has been disconnected.
63
+
64
+ Raises:
65
+ ConnectionError: when the connection can not be established.
66
+ """
67
+ raise NotImplementedError
68
+
69
+ @dynamic_interface
70
+ def disconnect_cs(self):
71
+ """Disconnect from the control server.
72
+
73
+ Raises:
74
+ ConnectionError: when the connection can not be closed.
75
+ """
76
+ raise NotImplementedError
77
+
78
+ @dynamic_interface
79
+ def reset_cs_connection(self):
80
+ """Resets the connection to the control server."""
81
+ raise NotImplementedError
82
+
83
+ @dynamic_interface
84
+ def is_cs_connected(self) -> bool:
85
+ """Check if the control server is connected.
86
+
87
+ Returns:
88
+ True if the device is connected and responds to a command, False otherwise.
89
+ """
90
+ raise NotImplementedError
91
+
92
+
93
+ class BaseProxy(ControlServerConnectionInterface):
94
+ def __init__(self, endpoint, timeout: int = REQUEST_TIMEOUT):
95
+ """
96
+ The `timeout` argument specifies the number of milliseconds to wait for a reply from the
97
+ control server.
98
+ """
99
+
100
+ self._logger = logging.getLogger(self.__class__.__name__)
101
+
102
+ self._ctx = zmq.Context.instance()
103
+ self._poller = zmq.Poller()
104
+ self._socket = None
105
+ self._endpoint = endpoint
106
+ self._timeout = timeout
107
+
108
+ self.connect_cs()
109
+
110
+ def __enter__(self):
111
+ if not self.ping():
112
+ raise ConnectionError(f"Proxy is not connected to endpoint ({self._endpoint}) when entering the context.")
113
+
114
+ return self
115
+
116
+ def __exit__(self, exc_type, exc_val, exc_tb):
117
+ if not self._socket.closed:
118
+ self.disconnect_cs()
119
+
120
+ def connect_cs(self):
121
+ self._logger.log(0, f"Trying to connect {self.__class__.__name__} to {self._endpoint}")
122
+
123
+ self._socket = self._ctx.socket(zmq.REQ)
124
+ self._socket.connect(self._endpoint)
125
+ self._poller.register(self._socket, zmq.POLLIN)
126
+
127
+ def disconnect_cs(self):
128
+ self._socket.setsockopt(zmq.LINGER, 0)
129
+ self._socket.close()
130
+ self._poller.unregister(self._socket)
131
+
132
+ def reconnect_cs(self):
133
+ self._logger.log(20, f"Trying to reconnect {self.__class__.__name__} to {self._endpoint}")
134
+
135
+ if not self._socket.closed:
136
+ self._socket.close(linger=0)
137
+
138
+ self._socket = self._ctx.socket(zmq.REQ)
139
+ self._socket.connect(self._endpoint)
140
+ self._poller.register(self._socket, zmq.POLLIN)
141
+
142
+ def reset_cs_connection(self):
143
+ self._logger.log(
144
+ 10, f"Trying to reset the connection from {self.__class__.__name__} to {self._endpoint}"
145
+ )
146
+
147
+ self.disconnect_cs()
148
+ self.connect_cs()
149
+
150
+ def is_cs_connected(self) -> bool:
151
+ return self.ping()
152
+
153
+ def send(self, data, retries: int = REQUEST_RETRIES, timeout: int = None):
154
+ """
155
+ Sends a command to the control server and waits for a response.
156
+
157
+ When not connected to the control server or when a timeout occurs, the
158
+ ``send()`` command retries a number of times to send the command.
159
+
160
+ The number of retries is hardcoded and currently set to '2', the request
161
+ timeout is set to 2.5 seconds.
162
+
163
+ The command data will be pickled before sending. Make sure the ``data``
164
+ argument can be dumped by pickle.
165
+
166
+ Args:
167
+ data (str): the command that is sent to the control server, usually a
168
+ string, but that is not enforced.
169
+ timeout (int): the time to wait for a reply [in milliseconds]
170
+ retries (int): the number of time we should retry to send the message
171
+
172
+ Returns:
173
+ response: the response from the control server or ``None`` when there was
174
+ a problem or a timeout.
175
+ """
176
+ timeout = timeout or self._timeout
177
+
178
+ pickle_string = pickle.dumps(data)
179
+
180
+ retries_left = retries
181
+
182
+ # When we enter this method, we assume the Proxy has been connected. It
183
+ # might be the server is not responding, but that is handled by the
184
+ # algorithm below where we have a number of retries to receive the response
185
+ # of the sent command. Remember that we are using ZeroMQ where the connect
186
+ # method returns gracefully even when no server is available.
187
+
188
+ if self._socket.closed:
189
+ self.reconnect_cs()
190
+
191
+ self._logger.log(0, f"Sending '{data}'")
192
+ self._socket.send(pickle_string)
193
+
194
+ while True:
195
+ socks = dict(self._poller.poll(timeout))
196
+
197
+ if self._socket in socks and socks[self._socket] == zmq.POLLIN:
198
+ pickle_string = self._socket.recv()
199
+ if not pickle_string:
200
+ break
201
+ response = pickle.loads(pickle_string)
202
+ self._logger.log(0, f"Receiving response: {response}")
203
+ return response
204
+ else:
205
+ # timeout - server unavailable
206
+
207
+ # We should disconnect here because socket is possibly confused.
208
+ # Close the socket and remove from the poller.
209
+
210
+ self.disconnect_cs()
211
+
212
+ if retries_left == 0:
213
+ self._logger.critical("Control Server seems to be off-line, abandoning")
214
+ return None
215
+ retries_left -= 1
216
+
217
+ self._logger.log(logging.CRITICAL, f"Reconnecting {self.__class__.__name__}, {retries_left=}")
218
+
219
+ self.reconnect_cs()
220
+
221
+ # Now try to send the request again
222
+
223
+ self._socket.send(pickle_string)
224
+
225
+ def ping(self):
226
+ return_code = self.send("Ping", retries=0, timeout=1000)
227
+ self._logger.log(0, f"Check if control server is available: Ping - {return_code}")
228
+ return return_code == "Pong"
229
+
230
+ def get_endpoint(self):
231
+ """ Returns the endpoint."""
232
+ return self._endpoint
233
+
234
+ def get_monitoring_port(self) -> int:
235
+ """ Returns the monitoring port. """
236
+ return self.send("get_monitoring_port")
237
+
238
+ def get_commanding_port(self) -> int:
239
+ """ Returns the commanding port."""
240
+ return self.send("get_commanding_port")
241
+
242
+ def get_service_port(self) -> int:
243
+ """ Returns the service port. """
244
+ return self.send("get_service_port")
245
+
246
+ def get_ip_address(self) -> int:
247
+ """ Returns the hostname of the control server."""
248
+ return self.send("get_ip_address")
249
+
250
+ def get_service_proxy(self):
251
+ """Return a ServiceProxy for the control server of this proxy object."""
252
+ from egse.services import ServiceProxy # prevent circular import problem
253
+
254
+ transport, address, _ = split_address(self._endpoint)
255
+
256
+ port = self.send("get_service_port")
257
+
258
+ return ServiceProxy(
259
+ AttributeDict({"PROTOCOL": transport, "HOSTNAME": address, "SERVICE_PORT": port})
260
+ )
261
+
262
+
263
+ class DynamicProxy(BaseProxy, DynamicClientCommandMixin):
264
+ def __init__(self, *args, **kwargs):
265
+ super().__init__(*args, **kwargs)
266
+
267
+
268
+ # TODO (rik): remove all methods from Proxy that are also define in the BaseProxy
269
+
270
+ class Proxy(BaseProxy, ControlServerConnectionInterface):
271
+ """
272
+ A Proxy object will forward CommandExecutions to the connected control server
273
+ and wait for a response. When the Proxy can not connect to its control server
274
+ during initialization, a ConnectionError will be raised.
275
+ """
276
+
277
+ def __init__(self, endpoint, timeout: int = REQUEST_TIMEOUT):
278
+ """
279
+ During initialization, the Proxy will connect to the control server and send a
280
+ handshaking `Ping` command. When that succeeds the Proxy will request and load the
281
+ available commands from the control server. When the connection with the control server
282
+ fails, no commands are loaded and the Proxy is left in a 'disconnected' state. The caller
283
+ can fix the problem with the control server and call `connect_cs()`, followed by a call to
284
+ `load_commands()`.
285
+
286
+ The `timeout` argument specifies the number of milliseconds
287
+ """
288
+
289
+ super().__init__(endpoint, timeout)
290
+
291
+ self._commands = {}
292
+
293
+ if self.ping():
294
+ self.load_commands()
295
+ else:
296
+ self._logger.warning(
297
+ f"{self.__class__.__name__} could not connect to its control server at {endpoint}. "
298
+ f"No commands have been loaded."
299
+ )
300
+
301
+ def __enter__(self):
302
+ if not self.ping():
303
+ raise ConnectionError("Proxy is not connected when entering the context.")
304
+
305
+ # The following check is here because a CS might have come alive between the __init__
306
+ # and __enter__ calls, and while the ping() will reconnect, the Proxy will have no
307
+ # commands loaded.
308
+
309
+ if not self.has_commands():
310
+ self.load_commands()
311
+
312
+ return self
313
+
314
+ def __exit__(self, exc_type, exc_val, exc_tb):
315
+ if not self._socket.closed:
316
+ self.disconnect_cs()
317
+
318
+ def connect_cs(self):
319
+ self._logger.log(0, f"Trying to connect {self.__class__.__name__} to {self._endpoint}")
320
+
321
+ self._socket = self._ctx.socket(zmq.REQ)
322
+ self._socket.connect(self._endpoint)
323
+ self._poller.register(self._socket, zmq.POLLIN)
324
+
325
+ def disconnect_cs(self):
326
+ self._socket.setsockopt(zmq.LINGER, 0)
327
+ self._socket.close()
328
+ self._poller.unregister(self._socket)
329
+
330
+ def reconnect_cs(self):
331
+ self._logger.log(20, f"Trying to reconnect {self.__class__.__name__} to {self._endpoint}")
332
+
333
+ if not self._socket.closed:
334
+ self._socket.close(linger=0)
335
+
336
+ self._socket = self._ctx.socket(zmq.REQ)
337
+ self._socket.connect(self._endpoint)
338
+ self._poller.register(self._socket, zmq.POLLIN)
339
+
340
+ def reset_cs_connection(self):
341
+ self._logger.log(
342
+ 10, f"Trying to reset the connection from {self.__class__.__name__} to {self._endpoint}"
343
+ )
344
+
345
+ self.disconnect_cs()
346
+ self.connect_cs()
347
+
348
+ def is_cs_connected(self) -> bool:
349
+ return self.ping()
350
+
351
+ def send(self, data, retries: int = REQUEST_RETRIES, timeout: int = None):
352
+ """
353
+ Sends a command to the control server and waits for a response.
354
+
355
+ When not connected to the control server or when a timeout occurs, the
356
+ ``send()`` command retries a number of times to send the command.
357
+
358
+ The number of retries is hardcoded and currently set to '2', the request
359
+ timeout is set to 2.5 seconds.
360
+
361
+ The command data will be pickled before sending. Make sure the ``data``
362
+ argument can be dumped by pickle.
363
+
364
+ Args:
365
+ data (str): the command that is sent to the control server, usually a
366
+ string, but that is not enforced.
367
+ timeout (int): the time to wait for a reply [in milliseconds]
368
+ retries (int): the number of time we should retry to send the message
369
+
370
+ Returns:
371
+ response: the response from the control server or ``None`` when there was
372
+ a problem or a timeout.
373
+ """
374
+ timeout = timeout or self._timeout
375
+
376
+ pickle_string = pickle.dumps(data)
377
+
378
+ retries_left = retries
379
+
380
+ # When we enter this method, we assume the Proxy has been connected. It
381
+ # might be the server is not responding, but that is handled by the
382
+ # algorithm below where we have a number of retries to receive the response
383
+ # of the sent command. Remember that we are using ZeroMQ where the connect
384
+ # method returns gracefully even when no server is available.
385
+
386
+ if self._socket.closed:
387
+ self.reconnect_cs()
388
+
389
+ self._logger.log(0, f"Sending '{data}'")
390
+ self._socket.send(pickle_string)
391
+
392
+ while True:
393
+ socks = dict(self._poller.poll(timeout))
394
+
395
+ if self._socket in socks and socks[self._socket] == zmq.POLLIN:
396
+ pickle_string = self._socket.recv()
397
+ if not pickle_string:
398
+ break
399
+ response = pickle.loads(pickle_string)
400
+ self._logger.log(0, f"Receiving response: {response}")
401
+ return response
402
+ else:
403
+ # timeout - server unavailable
404
+
405
+ # We should disconnect here because socket is possibly confused.
406
+ # Close the socket and remove from the poller.
407
+
408
+ self.disconnect_cs()
409
+
410
+ if retries_left == 0:
411
+ self._logger.critical(f"Control Server seems to be off-line, abandoning ({data})")
412
+ return None
413
+ retries_left -= 1
414
+
415
+ self._logger.log(logging.CRITICAL, f"Reconnecting {self.__class__.__name__}, {retries_left=}")
416
+
417
+ self.reconnect_cs()
418
+
419
+ # Now try to send the request again
420
+
421
+ self._socket.send(pickle_string)
422
+
423
+ def _request_commands(self):
424
+ self._commands = self.send("send_commands")
425
+
426
+ def _add_commands(self):
427
+ for key in self._commands:
428
+ if hasattr(self, key):
429
+ attribute = getattr(self, key)
430
+ if isinstance(attribute, types.MethodType) and not hasattr(
431
+ attribute, "__dynamic_interface"
432
+ ):
433
+ self._logger.warning(
434
+ f"{self.__class__.__name__} already has an attribute '{key}', "
435
+ f"not overwriting."
436
+ )
437
+ continue
438
+ command = self._commands[key]
439
+ new_method = MethodType(command.client_call, self)
440
+ new_method = set_docstring(new_method, command)
441
+ setattr(self, key, new_method)
442
+
443
+ def get_service_proxy(self):
444
+ """Return a ServiceProxy for the control server of this proxy object."""
445
+ from egse.services import ServiceProxy # prevent circular import problem
446
+
447
+ transport, address, _ = split_address(self._endpoint)
448
+
449
+ port = self.send("get_service_port")
450
+
451
+ return ServiceProxy(
452
+ AttributeDict({"PROTOCOL": transport, "HOSTNAME": address, "SERVICE_PORT": port})
453
+ )
454
+
455
+ def load_commands(self):
456
+ """
457
+ Requests all available commands from the control server and adds them to
458
+ the Proxy public interface, i.e. each command will become a method for
459
+ this Proxy.
460
+
461
+ A warning will be issued when a command will overwrite an existing method
462
+ of the Proxy class. The original method will not be overwritten and the
463
+ behavior of the Proxy command will not be what is expected.
464
+ """
465
+ # bind the client_call method from each Command to this Proxy object
466
+ # TODO(rik): what will happen when the _request_commands() fails?
467
+ if self.is_cs_connected():
468
+ self._request_commands()
469
+ self._add_commands()
470
+ return True
471
+ else:
472
+ self._logger.warning(f"{self.__class__.__name__} is not connected, try to reconnect.")
473
+ return False
474
+
475
+ def get_commands(self):
476
+ """
477
+ Returns a list of command names that can be send to the device or the
478
+ control server.
479
+
480
+ The commands are defined in the YAML settings file of the device.
481
+ Special commands are available for the ServiceProxy which configure and
482
+ control the control servers.
483
+ """
484
+ return list(self._commands.keys())
485
+
486
+ def has_commands(self):
487
+ """Return `True` if commands have been loaded."""
488
+ return bool(self._commands)
489
+
490
+ def ping(self):
491
+ return_code = self.send("Ping", retries=0, timeout=1000)
492
+ self._logger.debug(f"Check if control server is available: Ping - {return_code}")
493
+ return return_code == "Pong"
494
+
495
+ def get_endpoint(self):
496
+ """ Returns the endpoint.
497
+
498
+ Returns:
499
+ - Endpoint.
500
+ """
501
+
502
+ return self._endpoint
503
+
504
+ def get_monitoring_port(self) -> int:
505
+ """ Returns the monitoring port. """
506
+
507
+ return self.send("get_monitoring_port")
508
+
509
+ def get_commanding_port(self) -> int:
510
+ """ Returns the commanding port."""
511
+
512
+ return self.send("get_commanding_port")
513
+
514
+ def get_service_port(self) -> int:
515
+ """ Returns the service port. """
516
+
517
+ return self.send("get_service_port")
518
+
519
+ def get_ip_address(self) -> int:
520
+ """ Returns the hostname of the control server."""
521
+
522
+ return self.send("get_ip_address")
egse/reload.py ADDED
@@ -0,0 +1,122 @@
1
+ """
2
+ A slightly better approach to reloading modules and function than the standard importlib.reload()
3
+ function. The functions in this module are for interactive used in a Python REPL.
4
+
5
+ * reload_module(module): reloads the given module and all its parent modules
6
+ * reload_function(func): reloads and returns the given function
7
+
8
+ NOTE: It might be possible that after the module or function has been reloaded that an extra
9
+ import is needed to import the proper module attributes in your namespace.
10
+
11
+ Module dependency
12
+
13
+ When you make a change in a module or function that you are not calling directly, but call
14
+ through another function from another module, you need to reload the module where you made
15
+ the change and, after that, reload the function that calls that module.
16
+
17
+ Example:
18
+
19
+ * module x.a contains function func_a
20
+ * module x.b contains function func_b which calls func_a
21
+
22
+ when you make a change in func_a you have to relaod the module x.a and then reload
23
+ the function func_b:
24
+
25
+ >>> from x.b import func_b
26
+ >>> func_b()
27
+ # make some changes in func_a
28
+ >>> reload_module('x.a')
29
+ >>> func_b = reload_function(func_b)
30
+ >>> func_b() # will show the changes done in func_a
31
+
32
+ """
33
+ import importlib
34
+ import itertools
35
+ import typing
36
+ import types
37
+
38
+ import rich
39
+
40
+ from egse.exceptions import Abort
41
+
42
+
43
+ def reload_module(module: typing.Union[types.ModuleType, str], walk: bool = True):
44
+ """
45
+ Reloads the given module and all its parent modules. The modules will be reloaded starting
46
+ from their top-level module. Reloading the 'egse.hexapod.symetry.puna' module will reload
47
+ 'egse', 'egse.hexapod', 'egse.hexapod.symetry', and 'egse.hexapod.symetry.puna' in that order.
48
+
49
+ NOTE: If you pass the module argument as a module, make sure the top level module is
50
+ imported in your session, or you will get a NameError. To prevent this (if you don't want
51
+ to import the top-level module, pass the module as a string.
52
+
53
+ Usage:
54
+ >>> import egse
55
+ >>> reload_module(egse.system)
56
+ or
57
+ >>> reload_module('egse.system')
58
+
59
+ Args:
60
+ module: The module that needs to be reloaded
61
+ walk: walk up the module hierarchy and import all modules [default=True]
62
+
63
+ """
64
+ full_module_name = module.__name__ if isinstance(module, types.ModuleType) else module
65
+
66
+ module_names = itertools.accumulate(full_module_name.split('.'),
67
+ lambda x, y: f"{x}.{y}") if walk else [full_module_name]
68
+
69
+ for module_name in module_names:
70
+ try:
71
+ module = importlib.import_module(module_name)
72
+ importlib.reload(module)
73
+ except (Exception,) as exc:
74
+ rich.print(f'[red]Failed to reload {module_name}[/red], {exc=}')
75
+
76
+
77
+ def reload_function(func: types.FunctionType):
78
+ """
79
+ Reloads and returns the given function. In order for this to work, you should catch the
80
+ return value to replace the given function with its reloaded counterpart.
81
+
82
+ This will also work if you import the function again instead of catching it.
83
+
84
+ NOTE: that this mechanism doesn't work for functions that were defined in the __main__ module.
85
+
86
+ Usage:
87
+ func = reload_function(func)
88
+
89
+ reload_function(func)
90
+ from egse.some_module import func
91
+
92
+ Args:
93
+ func: the function that needs to be reloaded
94
+
95
+ Returns:
96
+ The reloaded function.
97
+
98
+ Raises:
99
+ An Abort when the function is not the proper type or when the function is defined
100
+ in the __main__ module.
101
+ """
102
+
103
+ # Why do I raise an Abort instead of just printing a message and returning?
104
+ #
105
+ # The function is usually called catching the return value and replacing the same function with
106
+ # its reloaded counterpart. If we would just return (None) when an error occurs, the original
107
+ # function will be overwritten with None. Raising an Exception leaves the original as it was
108
+ # before the call.
109
+
110
+ if not isinstance(func, types.FunctionType):
111
+ raise Abort(f"The 'func' argument shall be a function, not {type(func)}")
112
+
113
+ module_name = func.__module__
114
+ func_name = func.__name__
115
+
116
+ if module_name == '__main__':
117
+ raise Abort("Cannot reload a function that is defined in the __main__ module.")
118
+
119
+ reload_module(module_name)
120
+
121
+ module = __import__(module_name, fromlist=[func_name])
122
+ return getattr(module, func_name)