6 check the existence of the ros nodes and publish the result to diagnostics 11 from xmlrpc.client
import ServerProxy
13 from xmlrpclib
import ServerProxy
18 import diagnostic_updater
19 import diagnostic_msgs
20 from sound_play.msg
import SoundRequest
25 raise ROSNodeException(
"remote call failed: %s"%msg)
28 def ping(node_name, max_count=None, verbose=False):
30 Test connectivity to node by calling its XMLRPC API 31 @param node_name: name of node to ping 33 @param max_count: number of ping requests to make 35 @param verbose: print ping information to screen 37 @return: True if node pinged 39 @raise ROSNodeIOException: if unable to communicate with master 41 master = rosgraph.Master(ID)
42 node_api = get_api_uri(master,node_name)
50 print(
"pinging %s with a timeout of %ss"%(node_name, timeout))
51 socket.setdefaulttimeout(timeout)
52 node = ServerProxy(node_api)
64 dur = (end-start)*1000.
69 print(
"xmlrpc reply from %s\ttime=%fms"%(node_api, dur))
71 except socket.error
as e:
77 p = urlparse.urlparse(node_api)
79 elif errnum == errno.ECONNREFUSED:
81 new_node_api = get_api_uri(master,node_name, skip_cache=
True)
85 if new_node_api != node_api:
87 print(
"node url has changed from [%s] to [%s], retrying to ping"%(node_api, new_node_api))
88 node_api = new_node_api
89 node = ServerProxy(node_api)
97 print(
"unknown network error contacting node: %s"%(str(e)))
98 if max_count
and count >= max_count:
101 except KeyboardInterrupt:
104 if verbose
and count > 1:
105 print(
"ping average: %fms"%(acc/count))
109 global nodes, speak_text
113 res =
ping(n, max_count = 1, verbose =
False)
118 stat.summary(diagnostic_msgs.msg.DiagnosticStatus.ERROR,
119 "dead nodes: " +
", ".join([n
for (n, res)
120 in result.items()
if not res]))
122 sound = SoundRequest()
123 sound.sound = SoundRequest.SAY
124 sound.command = SoundRequest.PLAY_ONCE
126 sound.arg = speak_text
128 sound.arg =
" ".join(nodes).replace(
"/",
"").replace(
"_",
" ") +
" is dead" 129 g_robotsound_pub.publish(sound)
131 stat.summary(diagnostic_msgs.msg.DiagnosticStatus.OK,
132 "every node is alive")
133 for n, res
in result.items():
137 if __name__ ==
"__main__":
138 rospy.init_node(
'rosping_existence')
140 updater.setHardwareID(rospy.get_name())
141 updater.add(
"node existence", checkNodeExistence)
142 argv = rospy.myargv()
148 speak = rospy.get_param(
"~speak",
False)
149 speak_text = rospy.get_param(
"~speak_text",
"")
151 g_robotsound_pub = rospy.Publisher(
"/robotsound", SoundRequest)
153 while not rospy.is_shutdown():
def ping(node_name, max_count=None, verbose=False)
def checkNodeExistence(stat)