proxy.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2012, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 import fnmatch
35 import socket
36 from rosservice import get_service_list
37 from rosservice import get_service_type as rosservice_get_service_type
38 from rosservice import get_service_node as rosservice_get_service_node
39 from rosservice import get_service_uri
40 from rosservice import rosservice_find
41 from rostopic import find_by_type
42 from rostopic import get_topic_type as rosservice_get_topic_type
43 from rosnode import get_node_names
44 from rosgraph.masterapi import Master
45 
46 from rosapi.msg import TypeDef
47 
48 from .glob_helper import filter_globs, any_match
49 
50 
51 def get_topics_and_types(topics_glob):
52  """ Returns a list of all the active topics in the ROS system """
53  try:
54  # Function getTopicTypes also returns inactive topics and does not
55  # return topics with unknown message types, so it must be compared
56  # to results from getSystemState.
57  master = Master('/rosbridge')
58  topic_types = master.getTopicTypes()
59  publishers, subscribers, services = master.getSystemState()
60  topics = set([x for x, _ in publishers] + [x for x, _ in subscribers])
61 
62  # Filter the list of topics by whether they are public.
63  topics = set(filter_globs(topics_glob, topics))
64  topic_types = [x for x in topic_types if x[0] in topics]
65 
66  # Add topics with unknown type messages.
67  unknown_type = topics.difference([x for x, _ in topic_types])
68  return zip(* topic_types + [[x,''] for x in unknown_type])
69  except:
70  return []
71 
72 
73 def get_topics_for_type(type, topics_glob):
74  # Filter the list of topics by whether they are public before returning.
75  return filter_globs(topics_glob, find_by_type(type))
76 
77 
78 def get_services(services_glob):
79  """ Returns a list of all the services advertised in the ROS system """
80  # Filter the list of services by whether they are public before returning.
81  return filter_globs(services_glob, get_service_list())
82 
83 
84 def get_services_for_type(service_type, services_glob):
85  """ Returns a list of services as specific service type """
86  # Filter the list of services by whether they are public before returning.
87  return filter_globs(services_glob, rosservice_find(service_type))
88 
89 
90 def get_nodes():
91  """ Returns a list of all the nodes registered in the ROS system """
92  return get_node_names()
93 
94 
96  """ Returns a list of topic names that are been published by the specified node """
97  try:
98  publishers, subscribers, services = Master('/rosbridge').getSystemState()
99  toReturn = []
100  for i, v in publishers:
101  if node in v:
102  toReturn.append(i)
103  toReturn.sort()
104  return toReturn
105  except socket.error:
106  return []
107 
108 
110  """ Returns a list of topic names that are been subscribed by the specified node """
111  try:
112  publishers, subscribers, services = Master('/rosbridge').getSystemState()
113  toReturn = []
114  for i, v in subscribers:
115  if node in v:
116  toReturn.append(i)
117  toReturn.sort()
118  return toReturn
119  except socket.error:
120  return []
121 
122 
124  """ Returns a list of service names that are been hosted by the specified node """
125  try:
126  publishers, subscribers, services = Master('/rosbridge').getSystemState()
127  toReturn = []
128  for i, v in services:
129  if node in v:
130  toReturn.append(i)
131  toReturn.sort()
132  return toReturn
133  except socket.error:
134  return []
135 
136 
137 def get_topic_type(topic, topics_glob):
138  """ Returns the type of the specified ROS topic """
139  # Check if the topic is hidden or public.
140  # If all topics are public then the type is returned
141  if any_match(str(topic), topics_glob):
142  # If the topic is published, return its type
143  topic_type, _, _ = rosservice_get_topic_type(topic)
144  if topic_type is None:
145  # Topic isn't published so return an empty string
146  return ""
147  return topic_type
148  else:
149  # Topic is hidden so return an empty string
150  return ""
151 
152 
154  """ Returns a list of action servers """
155  action_servers = []
156  possible_action_server = ''
157  possibility = [0, 0, 0, 0, 0]
158 
159  action_topics = ['cancel', 'feedback', 'goal', 'result', 'status']
160  for topic in sorted(topics):
161  split = topic.split('/')
162  if(len(split) >= 3):
163  topic = split.pop()
164  namespace = '/'.join(split)
165  if(possible_action_server != namespace):
166  possible_action_server = namespace
167  possibility = [0, 0, 0, 0, 0]
168  if possible_action_server == namespace and topic in action_topics:
169  possibility[action_topics.index(topic)] = 1
170  if all(p == 1 for p in possibility):
171  action_servers.append(possible_action_server)
172 
173  return action_servers
174 
175 
176 def get_service_type(service, services_glob):
177  """ Returns the type of the specified ROS service, """
178  # Check if the service is hidden or public.
179  if any_match(str(service), services_glob):
180  try:
181  return rosservice_get_service_type(service)
182  except:
183  return ""
184  else:
185  # Service is hidden so return an empty string.
186  return ""
187 
188 
189 def get_publishers(topic, topics_glob):
190  """ Returns a list of node names that are publishing the specified topic """
191  try:
192  if any_match(str(topic), topics_glob):
193  publishers, subscribers, services = Master('/rosbridge').getSystemState()
194  pubdict = dict(publishers)
195  if topic in pubdict:
196  return pubdict[topic]
197  else:
198  return []
199  else:
200  return []
201  except socket.error:
202  return []
203 
204 
205 def get_subscribers(topic, topics_glob):
206  """ Returns a list of node names that are subscribing to the specified topic """
207  try:
208  if any_match(str(topic), topics_glob):
209  publishers, subscribers, services = Master('/rosbridge').getSystemState()
210  subdict = dict(subscribers)
211  if topic in subdict:
212  return subdict[topic]
213  else:
214  return []
215  else:
216  return []
217  except socket.error:
218  return []
219 
220 
221 def get_service_providers(queried_type, services_glob):
222  """ Returns a list of node names that are advertising a service with the specified type """
223  _, _, services = Master('/rosbridge').getSystemState()
224 
225  service_type_providers = []
226  for service, providers in services:
227  service_type = get_service_type(service, services_glob)
228 
229  if service_type == queried_type:
230  service_type_providers += providers
231  return service_type_providers
232 
233 
234 def get_service_node(service):
235  """ Returns the name of the node that is providing the given service, or empty string """
236  node = rosservice_get_service_node(service)
237  if node == None:
238  node = ""
239  return node
240 
241 
242 def get_service_host(service):
243  """ Returns the name of the machine that is hosting the given service, or empty string """
244  uri = get_service_uri(service)
245  if uri == None:
246  uri = ""
247  else:
248  uri = uri[9:]
249  uri = uri[:uri.find(':')]
250  return uri
def get_service_host(service)
Definition: proxy.py:242
def get_service_providers(queried_type, services_glob)
Definition: proxy.py:221
def get_publishers(topic, topics_glob)
Definition: proxy.py:189
def get_node_subscriptions(node)
Definition: proxy.py:109
def get_service_type(service, services_glob)
Definition: proxy.py:176
def filter_globs(globs, full_list)
Definition: glob_helper.py:30
def any_match(query, globs)
Definition: glob_helper.py:38
def filter_action_servers(topics)
Definition: proxy.py:153
def get_service_node(service)
Definition: proxy.py:234
def get_node_publications(node)
Definition: proxy.py:95
def get_topics_for_type(type, topics_glob)
Definition: proxy.py:73
def get_topic_type(topic, topics_glob)
Definition: proxy.py:137
def get_topics_and_types(topics_glob)
Definition: proxy.py:51
def get_subscribers(topic, topics_glob)
Definition: proxy.py:205
def get_services_for_type(service_type, services_glob)
Definition: proxy.py:84
def get_services(services_glob)
Definition: proxy.py:78
def get_nodes()
Definition: proxy.py:90
def get_node_services(node)
Definition: proxy.py:123


rosapi
Author(s): Jonathan Mace
autogenerated on Fri May 10 2019 02:17:04