From cdb031a5b4cf639fe43a1dda5c9a8f622e46d01b Mon Sep 17 00:00:00 2001 From: Alexander Tiderko Date: Wed, 29 Nov 2023 13:51:37 +0100 Subject: [PATCH] added daemon version to crossbar --- .../crossbar/runtime_interface.py | 13 +++++++++ .../fkie_node_manager_daemon/server.py | 6 ++++ .../version_servicer.py | 28 +++++++++++-------- .../src/fkie_node_manager_daemon/server.py | 9 +++++- .../version_servicer.py | 18 ++++++++++-- 5 files changed, 58 insertions(+), 16 deletions(-) diff --git a/fkie_multimaster_pylib/fkie_multimaster_pylib/crossbar/runtime_interface.py b/fkie_multimaster_pylib/fkie_multimaster_pylib/crossbar/runtime_interface.py index dd42ba53..9c6d3a60 100644 --- a/fkie_multimaster_pylib/fkie_multimaster_pylib/crossbar/runtime_interface.py +++ b/fkie_multimaster_pylib/fkie_multimaster_pylib/crossbar/runtime_interface.py @@ -367,3 +367,16 @@ def __init__(self, self.size = size self.size_min = size_min self.size_max = size_max + +class DaemonVersion: + ''' + Version of the daemon node. + :param str version: version string. + :param str date: Date string. + ''' + + def __init__(self, + version: str, + date: str) -> None: + self.version = version + self.date = date diff --git a/fkie_node_manager_daemon/fkie_node_manager_daemon/server.py b/fkie_node_manager_daemon/fkie_node_manager_daemon/server.py index 1bc2de27..36671827 100644 --- a/fkie_node_manager_daemon/fkie_node_manager_daemon/server.py +++ b/fkie_node_manager_daemon/fkie_node_manager_daemon/server.py @@ -47,6 +47,7 @@ from fkie_node_manager_daemon.parameter_servicer import ParameterServicer from fkie_node_manager_daemon.rosstate_servicer import RosStateServicer from fkie_node_manager_daemon.screen_servicer import ScreenServicer +from fkie_node_manager_daemon.version_servicer import VersionServicer class Server: @@ -74,6 +75,8 @@ def __init__(self, rosnode, *, default_domain_id=-1): self.crossbar_loop, self.crossbar_realm, self.crossbar_port) self.launch_servicer = LaunchServicer( self.crossbar_loop, self.crossbar_realm, self.crossbar_port, ros_domain_id=self.ros_domain_id) + self.version_servicer = VersionServicer( + self.crossbar_loop, self.crossbar_realm, self.crossbar_port) rosnode.create_service(LoadLaunch, '~/start_launch', self._rosservice_start_launch) @@ -93,6 +96,7 @@ def __init__(self, rosnode, *, default_domain_id=-1): def __del__(self): self.crossbar_loop.stop() + self.version_servicer = None self.launch_servicer = None self.monitor_servicer = None self.screen_servicer = None @@ -131,6 +135,7 @@ def _crossbar_notify_if_regsitered(self): registration_finished &= self.screen_servicer.crossbar_registered registration_finished &= self.rosstate_servicer.crossbar_registered registration_finished &= self.parameter_servicer.crossbar_registered + registration_finished &= self.version_servicer.crossbar_registered time.sleep(0.5) self.publish_daemon_state(True) self._crossbar_send_status(True) @@ -154,6 +159,7 @@ def shutdown(self): WAIT_TIMEOUT = 3 self.publish_daemon_state(False) self._crossbar_send_status(False) + self.version_servicer.stop() self.screen_servicer.stop() self.launch_servicer.stop() self.file_servicer.stop() diff --git a/fkie_node_manager_daemon/fkie_node_manager_daemon/version_servicer.py b/fkie_node_manager_daemon/fkie_node_manager_daemon/version_servicer.py index 12219dc5..a17a4632 100644 --- a/fkie_node_manager_daemon/fkie_node_manager_daemon/version_servicer.py +++ b/fkie_node_manager_daemon/fkie_node_manager_daemon/version_servicer.py @@ -16,23 +16,27 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -import fkie_node_manager_daemon.grpc_proto.version_pb2_grpc as vgrpc -import fkie_node_manager_daemon.grpc_proto.version_pb2 as vmsg +import asyncio +from autobahn import wamp +import json +from fkie_multimaster_pylib.crossbar.base_session import CrossbarBaseSession +from fkie_multimaster_pylib.crossbar.base_session import SelfEncoder +from fkie_multimaster_pylib.crossbar.runtime_interface import DaemonVersion +from fkie_multimaster_pylib.logging.logging import Log import fkie_node_manager_daemon as nmd from . import version -class VersionServicer(vgrpc.VersionServiceServicer): +class VersionServicer(CrossbarBaseSession): - def __init__(self): - nmd.ros_node.get_logger().info("Create version servicer") - vgrpc.VersionServiceServicer.__init__(self) + def __init__(self, loop: asyncio.AbstractEventLoop, realm: str = 'ros', port: int = 11911): + Log.info("Create ROS2 version servicer") + CrossbarBaseSession.__init__(self, loop, realm, port) self._version, self._date = version.detect_version( nmd.ros_node, 'fkie_node_manager_daemon') - def GetVersion(self, request, context): - reply = vmsg.Version() - reply.version = self._version - reply.date = self._date - return reply + @wamp.register('ros.daemon.get_version') + def get_version(self) -> DaemonVersion: + Log.info(f"{self.__class__.__name__}: get daemon version ") + reply = DaemonVersion(f"{self._version}", f"{self._date}") + return json.dumps(reply, cls=SelfEncoder) diff --git a/fkie_node_manager_daemon/src/fkie_node_manager_daemon/server.py b/fkie_node_manager_daemon/src/fkie_node_manager_daemon/server.py index a6a58470..128284ba 100644 --- a/fkie_node_manager_daemon/src/fkie_node_manager_daemon/server.py +++ b/fkie_node_manager_daemon/src/fkie_node_manager_daemon/server.py @@ -94,6 +94,8 @@ def __init__(self, test_env=False): self.crossbar_loop, self.crossbar_realm, self.crossbar_port, test_env=self._test_env) self.screen_servicer = ScreenServicer( self.crossbar_loop, self.crossbar_realm, self.crossbar_port, test_env=self._test_env) + self.version_servicer = VersionServicer( + self.crossbar_loop, self.crossbar_realm, self.crossbar_port, test_env=self._test_env) rospy.Service('~start_launch', LoadLaunch, self._rosservice_start_launch) @@ -102,6 +104,7 @@ def __init__(self, test_env=False): rospy.Service('~list_nodes', ListNodes, self._rosservice_list_nodes) def __del__(self): + self.version_servicer = None self.launch_servicer = None self.monitor_servicer = None self.settings_servicer = None @@ -151,6 +154,8 @@ def restart(self): self.crossbar_loop, self.crossbar_realm, self.crossbar_port, test_env=self._test_env) self.screen_servicer = ScreenServicer( self.crossbar_loop, self.crossbar_realm, self.crossbar_port, test_env=self._test_env) + self.version_servicer = VersionServicer( + self.crossbar_loop, self.crossbar_realm, self.crossbar_port, test_env=self._test_env) self.start(self._launch_url) def start(self, url='[::]:12311'): @@ -184,7 +189,7 @@ def start(self, url='[::]:12311'): stgrpc.add_SettingsServiceServicer_to_server( self.settings_servicer, self.server) vgrpc.add_VersionServiceServicer_to_server( - VersionServicer(), self.server) + self.version_servicer, self.server) self.server.start() Log.info("Server at '%s' started!" % url) #Log.info(f"Connect to crossbar server @ ws://localhost:{self.crossbar_port}/ws, realm: {self.crossbar_realm}") @@ -207,6 +212,7 @@ def _crossbar_notify_if_regsitered(self): registration_finished &= self.screen_servicer.crossbar_registered registration_finished &= self.file_servicer.crossbar_registered registration_finished &= self.parameter_servicer.crossbar_registered + registration_finished &= self.version_servicer.crossbar_registered time.sleep(0.5) self._crossbar_send_status(True) @@ -219,6 +225,7 @@ def shutdown(self): self._crossbar_send_status(False) shutdown_task = self.crossbar_loop.create_task( self.crossbar_loop.shutdown_asyncgens()) + self.version_servicer.stop() self.launch_servicer.stop() self.monitor_servicer.stop() self.parameter_servicer.shutdown() diff --git a/fkie_node_manager_daemon/src/fkie_node_manager_daemon/version_servicer.py b/fkie_node_manager_daemon/src/fkie_node_manager_daemon/version_servicer.py index 764a0f81..8dfa4de8 100644 --- a/fkie_node_manager_daemon/src/fkie_node_manager_daemon/version_servicer.py +++ b/fkie_node_manager_daemon/src/fkie_node_manager_daemon/version_servicer.py @@ -30,18 +30,24 @@ # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. - +import asyncio +from autobahn import wamp +import json +from fkie_multimaster_pylib.crossbar.base_session import CrossbarBaseSession +from fkie_multimaster_pylib.crossbar.base_session import SelfEncoder +from fkie_multimaster_pylib.crossbar.runtime_interface import DaemonVersion import fkie_multimaster_msgs.grpc.version_pb2_grpc as vgrpc import fkie_multimaster_msgs.grpc.version_pb2 as vmsg from . import version from fkie_multimaster_pylib.logging.logging import Log -class VersionServicer(vgrpc.VersionServiceServicer): +class VersionServicer(vgrpc.VersionServiceServicer, CrossbarBaseSession): - def __init__(self): + def __init__(self, loop: asyncio.AbstractEventLoop, realm: str = 'ros', port: int = 11911, test_env=False): Log.info("Create version servicer") vgrpc.VersionServiceServicer.__init__(self) + CrossbarBaseSession.__init__(self, loop, realm, port, test_env=test_env) self._version, self._date = version.detect_version( 'fkie_node_manager_daemon') @@ -50,3 +56,9 @@ def GetVersion(self, request, context): reply.version = self._version reply.date = self._date return reply + + @wamp.register('ros.daemon.get_version') + def get_version(self) -> DaemonVersion: + Log.info(f"{self.__class__.__name__}: get daemon version ") + reply = DaemonVersion(f"{self._version}", f"{self._date}") + return json.dumps(reply, cls=SelfEncoder)