nodeutil.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import os
00004 import socket
00005 import xmlrpclib
00006 
00007 PKG = 'webui' # this package name
00008 import roslib; roslib.load_manifest(PKG) 
00009 import rospy
00010 from roslib import scriptutil
00011 from roslib.names import make_caller_id
00012 
00013 def _remote_call(args):
00014   code, msg, val = args
00015   if code != 1:
00016     raise Exception("remote call failed: %s"%msg)
00017   return val
00018 
00019 _caller_apis = {}
00020 def get_api_uri(master, node_name, caller_id):
00021   caller_api = _caller_apis.get(caller_id, None)
00022   if not caller_api:
00023       try:
00024           code, msg, caller_api = master.lookupNode(caller_id, node_name)
00025           if code != 1:
00026               return None
00027           else:
00028               _caller_apis[caller_id] = caller_api
00029       except socket.error:
00030           raise Exception("Unable to communicate with master!")
00031   return caller_api
00032 
00033 
00034 def node_info(node):
00035   caller_id = make_caller_id('nodeutil-%s'%os.getpid())
00036   node_name = node #scriptutil.script_resolve_name(caller_id, node)
00037   master = scriptutil.get_master()
00038 
00039   # go through the master system state first
00040   try:
00041     state = _remote_call(master.getSystemState(caller_id))
00042   except socket.error:
00043     raise Exception("Unable to communicate with master!")
00044       
00045   pubs = [t for t, l in state[0] if node_name in l]
00046   subs = [t for t, l in state[1] if node_name in l]
00047   srvs = [t for t, l in state[2] if node_name in l]  
00048 
00049   results = {
00050     "publications": pubs,
00051     "subscriptions": subs,
00052     "services": srvs
00053   }
00054   node_api = get_api_uri(master, node_name, caller_id)
00055   if not node_api:
00056     results["error"] = "cannot contact [%s]: unknown node"%node_name
00057 
00058   return results
00059 
00060 def topic_info(topic):
00061   caller_id = make_caller_id('nodeutil-%s'%os.getpid())
00062   master = scriptutil.get_master()
00063 
00064   # go through the master system state first
00065   try:
00066     state = _remote_call(master.getSystemState(caller_id))
00067   except socket.error:
00068     raise IOException("Unable to communicate with master!")
00069       
00070   pubs, subs, _ = state
00071   publists = [publist for t, publist in pubs if t == topic]
00072   sublists = [sublist for t, sublist in subs if t == topic]
00073 
00074   #pub_topics = _succeed(master.getPublishedTopics(caller_id, '/'))
00075 
00076   if publists == []:
00077     return {
00078       "error": "This topic does not appear to be published yet."
00079     }
00080   elif sublists == []: 
00081     return {  
00082         "publishers": publists[0], 
00083         "subscribers": [] 
00084     } 
00085   else:
00086     return {
00087       "publishers": publists[0],
00088       "subscribers": sublists[0]
00089     }
00090 
00091 def main():
00092   print node_info("/fake_wifi_ddwrt")
00093   print "----"
00094   print topic_info("/ddwrt/accesspoint")
00095 
00096 
00097 if __name__ == "__main__":
00098   main()


webui
Author(s): Scott Hassan/hassan@willowgarage.com
autogenerated on Wed Apr 23 2014 10:36:00