custom_rosnode.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2015 Airbus
00004 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License");
00007 # you may not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 #   http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS,
00014 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 
00018 import time
00019 import rospy
00020 import rosnode
00021 
00022 
00023 def rosnode_ping_all(verbose=False):
00024     """
00025     Ping all running nodes
00026     @return [str], [str]: pinged nodes, un-pingable nodes
00027     @raise ROSNodeIOException: if unable to communicate with master
00028     """
00029     master = rosnode.rosgraph.Master(rosnode.ID)
00030     try:
00031         state = master.getSystemState()
00032     except rosnode.socket.error:
00033         raise rosnode.ROSNodeIOException("Unable to communicate with master!")
00034 
00035     nodes = []
00036     for s in state:
00037         for t, l in s:
00038             nodes.extend(l)
00039     nodes = list(set(nodes)) #uniq
00040     if verbose:
00041         print("Will ping the following nodes: \n"+''.join([" * %s\n"%n for n in nodes]))
00042     pinged = {}
00043     unpinged = []
00044     for node in nodes:
00045         start = time.time()
00046         status = rosnode.rosnode_ping(node, max_count=1, verbose=verbose)
00047         end = time.time()
00048         dur = (end-start)*1000.
00049         if status:
00050             pinged.update({node:dur})
00051         else:
00052             unpinged.append(node)
00053         
00054     return pinged, unpinged
00055 
00056 def rosnode_cleanup():
00057     """
00058     This is a semi-hidden routine for cleaning up stale node
00059     registration information on the ROS Master. The intent is to
00060     remove this method once Master TTLs are properly implemented.
00061     """
00062     pinged, unpinged = rosnode_ping_all()
00063     if unpinged:
00064         master = rosnode.rosgraph.Master(rosnode.ID)
00065         rosnode.cleanup_master_blacklist(master, unpinged)
00066         
00067     return pinged, unpinged


airbus_plugin_node_manager
Author(s): Matignon Martin
autogenerated on Thu Jun 6 2019 17:59:23