proxy.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2012, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 import fnmatch
00035 from rosservice import get_service_list
00036 from rosservice import get_service_type as rosservice_get_service_type
00037 from rosservice import get_service_node as rosservice_get_service_node
00038 from rosservice import get_service_uri
00039 from rosservice import rosservice_find
00040 from rostopic import find_by_type
00041 from rostopic import get_topic_type as rosservice_get_topic_type
00042 from ros import rosnode, rosgraph
00043 from rosnode import get_node_names
00044 from rosgraph.masterapi import Master
00045 
00046 from rosapi.msg import TypeDef
00047 
00048 
00049 def get_topics(topics_glob):
00050     """ Returns a list of all the active topics in the ROS system """
00051     try:
00052         publishers, subscribers, services = Master('/rosbridge').getSystemState()
00053         # Filter the list of topics by whether they are public before returning.
00054         return filter(lambda x: any(fnmatch.fnmatch(str(x), glob) for glob in topics_glob), list(set([x for x, _ in publishers] + [x for x, _, in subscribers])))
00055     except:
00056         return []
00057 
00058 def get_topics_types(topics, topics_glob):
00059     try:
00060         types = []
00061         for i in topics:
00062             types.append(get_topic_type(i, topics_glob))
00063         return types
00064     except:
00065         return[]
00066 
00067 
00068 def get_topics_for_type(type, topics_glob):
00069     # Filter the list of topics by whether they are public before returning.
00070     return filter(lambda x: any(fnmatch.fnmatch(str(x), glob) for glob in topics_glob), find_by_type(type))
00071 
00072 
00073 def get_services(services_glob):
00074     """ Returns a list of all the services advertised in the ROS system """
00075     # Filter the list of services by whether they are public before returning.
00076     return filter(lambda x: any(fnmatch.fnmatch(str(x), glob) for glob in services_glob), get_service_list())
00077 
00078 
00079 def get_services_for_type(service_type, services_glob):
00080     """ Returns a list of services as specific service type """
00081     # Filter the list of services by whether they are public before returning.
00082     return filter(lambda x: any(fnmatch.fnmatch(str(x), glob) for glob in services_glob), rosservice_find(service_type))
00083 
00084 
00085 def get_nodes():
00086     """ Returns a list of all the nodes registered in the ROS system """
00087     return rosnode.get_node_names()
00088 
00089 def get_node_publications(node):
00090     """ Returns a list of topic names that are been published by the specified node """
00091     try:
00092       publishers, subscribers, services = Master('/rosbridge').getSystemState()
00093       toReturn = []
00094       for i,v in publishers:
00095         if node in v:
00096           toReturn.append(i)
00097       toReturn.sort()
00098       return toReturn
00099     except socket.error:
00100       return []
00101     
00102 def get_node_subscriptions(node):
00103     """ Returns a list of topic names that are been subscribed by the specified node """
00104     try:
00105       publishers, subscribers, services = Master('/rosbridge').getSystemState()
00106       toReturn = []
00107       for i,v in subscribers:
00108         if node in v:
00109           toReturn.append(i)
00110       toReturn.sort()
00111       return toReturn
00112     except socket.error:
00113       return []
00114     
00115 def get_node_services(node):
00116     """ Returns a list of service names that are been hosted by the specified node """
00117     try:
00118       publishers, subscribers, services = Master('/rosbridge').getSystemState()
00119       toReturn = []
00120       for i,v in services:
00121         if node in v:
00122           toReturn.append(i)
00123       toReturn.sort()
00124       return toReturn
00125     except socket.error:
00126       return []
00127 
00128 def get_topic_type(topic, topics_glob):
00129     """ Returns the type of the specified ROS topic """
00130     # Check if the topic is hidden or public.
00131     if any(fnmatch.fnmatch(str(topic), glob) for glob in topics_glob):
00132         # If the topic is published, return its type
00133         topic_type, _, _ = rosservice_get_topic_type(topic)
00134         if topic_type is None:
00135             # Topic isn't published so return an empty string
00136             return ""
00137         return topic_type
00138     else:
00139         # Topic is hidden so return an empty string
00140         return ""
00141 
00142 
00143 def filter_action_servers(topics):
00144     """ Returns a list of action servers """
00145     action_servers = []
00146     possible_action_server = ''
00147     possibility = [0, 0, 0, 0, 0]
00148     for topic in sorted(topics):
00149         if (len(topic.split('/')) == 3):
00150             [empty, namespace, topic] = topic.split('/')
00151             if(possible_action_server != namespace):
00152                 possible_action_server = namespace
00153                 possibility[0]=0; possibility[1]=0; possibility[2]=0; possibility[3]=0; possibility[4]=0
00154             if(possible_action_server == namespace and topic == "cancel"):  possibility[0] = 1
00155             if(possible_action_server == namespace and topic == "feedback"):possibility[1] = 1
00156             if(possible_action_server == namespace and topic == "goal"):    possibility[2] = 1
00157             if(possible_action_server == namespace and topic == "result"):  possibility[3] = 1
00158             if(possible_action_server == namespace and topic == "status"):  possibility[4] = 1
00159         if(possibility[0] == 1 and possibility[1] == 1 and possibility[2] == 1 and possibility[3] == 1 and possibility[4] == 1):
00160             action_servers.append(possible_action_server)
00161 
00162     return action_servers
00163 
00164 def get_service_type(service, services_glob):
00165     """ Returns the type of the specified ROS service, """
00166     # Check if the service is hidden or public.
00167     if any(fnmatch.fnmatch(str(service), glob) for glob in services_glob):
00168         try:
00169             return rosservice_get_service_type(service)
00170         except:
00171             return ""
00172     else:
00173         # Service is hidden so return an empty string.
00174         return ""
00175 
00176 
00177 def get_publishers(topic, topics_glob):
00178     """ Returns a list of node names that are publishing the specified topic """
00179     try:
00180         if any(fnmatch.fnmatch(str(topic), glob) for glob in topics_glob):
00181             publishers, subscribers, services = Master('/rosbridge').getSystemState()
00182             pubdict = dict(publishers)
00183             if topic in pubdict:
00184                 return pubdict[topic]
00185             else:
00186                 return []
00187         else:
00188             return []
00189     except socket.error:
00190         return []
00191 
00192 
00193 def get_subscribers(topic, topics_glob):
00194     """ Returns a list of node names that are subscribing to the specified topic """
00195     try:
00196         if any(fnmatch.fnmatch(str(topic), glob) for glob in topics_glob):
00197             publishers, subscribers, services = Master('/rosbridge').getSystemState()
00198             subdict = dict(subscribers)
00199             if topic in subdict:
00200                 return subdict[topic]
00201             else:
00202                 return []
00203         else:
00204             return []
00205     except socket.error:
00206         return []
00207 
00208 
00209 def get_service_providers(servicetype, services_glob):
00210     """ Returns a list of node names that are advertising a service with the specified type """
00211     try:
00212         if any(fnmatch.fnmatch(str(topic), glob) for glob in services_glob):
00213             publishers, subscribers, services = Master('/rosbridge').getSystemState()
00214             servdict = dict(services)
00215             if servicetype in servdict:
00216                 return servdict[servicetype]
00217             else:
00218                 return []
00219         else:
00220             return []
00221     except socket.error:
00222         return []
00223 
00224 
00225 def get_service_node(service):
00226     """ Returns the name of the node that is providing the given service, or empty string """
00227     node = rosservice_get_service_node(service)
00228     if node == None:
00229         node = ""
00230     return node
00231 
00232 
00233 def get_service_host(service):
00234     """ Returns the name of the machine that is hosting the given service, or empty string """
00235     uri = get_service_uri(service)
00236     if uri == None:
00237         uri = ""
00238     else:
00239         uri = uri[9:]
00240         uri = uri[:uri.find(':')]
00241     return uri


rosapi
Author(s): Jonathan Mace
autogenerated on Wed Sep 13 2017 03:18:17