11 import gateway_msgs.msg
as gateway_msgs
12 import gateway_msgs.srv
as gateway_srvs
13 import rocon_gateway_utils
14 import rocon_python_comms
16 from .exceptions
import GatewaySampleRuntimeError
17 from .utils
import connection_types
23 _gateway_namespace =
'/gateway' 32 Slowly loop (and block) until the gateway is connected to a hub. 34 gateway_info_service = rocon_python_comms.SubscriberProxy(ns +
'/gateway_info', gateway_msgs.GatewayInfo)
35 start_time = rospy.Time.now()
36 while not rospy.is_shutdown():
37 gateway_info = gateway_info_service()
38 if gateway_info.connected:
40 if rospy.Time.now() - start_time > timeout:
41 raise GatewaySampleRuntimeError(
"timed out waiting for the gateway to connect to a hub")
47 Slowly loop (and block) until remote the gateway is visible on our hub. 49 rospy.wait_for_service(ns +
'/remote_gateway_info')
50 remote_gateway_info_service = rospy.ServiceProxy(ns +
'/remote_gateway_info', gateway_srvs.RemoteGatewayInfo)
51 req = gateway_srvs.RemoteGatewayInfoRequest()
52 start_time = rospy.Time.now()
53 while not rospy.is_shutdown():
54 req = gateway_srvs.RemoteGatewayInfoRequest()
56 resp = remote_gateway_info_service(req)
58 if rospy.Time.now() - start_time > timeout:
59 raise GatewaySampleRuntimeError(
"timed out waiting for a remote gateway to appear")
60 for gateway
in resp.gateways:
61 if remote_gateway_name == gateway.name:
64 remote_gateway_basename = rocon_gateway_utils.gateway_basename(gateway.name)
65 print(
"Samples: gateway comparison [%s][%s]" % (remote_gateway_basename, remote_gateway_name))
66 if remote_gateway_name == remote_gateway_basename:
75 Parses the remote gateway list to find a gateway to use for testing. 77 It's a dumb hack to make testing quite convenient. 79 @return gateway string name 82 rospy.wait_for_service(ns +
'/remote_gateway_info')
83 remote_gateway_info = rospy.ServiceProxy(ns +
'/remote_gateway_info', gateway_srvs.RemoteGatewayInfo)
84 start_time = rospy.Time.now()
85 while not rospy.is_shutdown():
86 req = gateway_srvs.RemoteGatewayInfoRequest()
88 resp = remote_gateway_info(req)
89 if rospy.Time.now() - start_time > timeout:
90 raise GatewaySampleRuntimeError(
"timed out waiting for a remote gateway to appear")
91 if len(resp.gateways) > 0:
94 return resp.gateways[0].name
100 if use_regex_patterns:
102 gateway_msgs.ConnectionType.PUBLISHER:
'.*ter',
103 gateway_msgs.ConnectionType.SUBSCRIBER:
'.*ter',
104 gateway_msgs.ConnectionType.SERVICE:
'/add_.*_ints',
105 gateway_msgs.ConnectionType.ACTION_CLIENT:
'/fibonacci/cli.*',
106 gateway_msgs.ConnectionType.ACTION_SERVER:
'/fibonacci/ser.*' 109 gateway_msgs.ConnectionType.PUBLISHER:
'/t.*er',
110 gateway_msgs.ConnectionType.SUBSCRIBER:
'',
111 gateway_msgs.ConnectionType.SERVICE:
'',
112 gateway_msgs.ConnectionType.ACTION_CLIENT:
'',
113 gateway_msgs.ConnectionType.ACTION_SERVER:
'' 117 gateway_msgs.ConnectionType.PUBLISHER:
'/chatter',
118 gateway_msgs.ConnectionType.SUBSCRIBER:
'/chatter',
119 gateway_msgs.ConnectionType.SERVICE:
'/add_two_ints',
120 gateway_msgs.ConnectionType.ACTION_CLIENT:
'/fibonacci/client',
121 gateway_msgs.ConnectionType.ACTION_SERVER:
'/fibonacci/server' 124 gateway_msgs.ConnectionType.PUBLISHER:
'',
125 gateway_msgs.ConnectionType.SUBSCRIBER:
'',
126 gateway_msgs.ConnectionType.SERVICE:
'',
127 gateway_msgs.ConnectionType.ACTION_CLIENT:
'',
128 gateway_msgs.ConnectionType.ACTION_SERVER:
'' 144 text =
"cancelling" if cancel
else msg
150 Sends a rule for advertising everything except the default blacklist. 152 rospy.wait_for_service(ns +
'/advertise_all')
153 advertise_all = rospy.ServiceProxy(ns +
'/advertise_all', gateway_srvs.AdvertiseAll)
154 req = gateway_srvs.AdvertiseAllRequest()
157 rospy.loginfo(
"Advertise All : %s all." %
_action_text(cancel,
'advertising'))
159 if resp.result == gateway_msgs.ErrorCodes.ADVERTISEMENT_EXISTS:
161 elif resp.result != 0:
162 raise GatewaySampleRuntimeError(
"failed to advertise all (todo: no error message yet)")
166 rospy.wait_for_service(ns +
'/advertise')
167 advertise = rospy.ServiceProxy(ns +
'/advertise', gateway_srvs.Advertise)
168 req = gateway_srvs.AdvertiseRequest()
170 rule = gateway_msgs.Rule()
175 for connection_type
in connection_types:
177 rule.name = names[connection_type]
178 rule.type = connection_type
179 rule.node = nodes[connection_type]
180 rospy.loginfo(
"Advertise : %s [%s,%s,%s]." %
181 (
_action_text(cancel,
'advertising'), rule.type, rule.name, rule.node
or 'None'))
182 req.rules.append(rule)
183 resp = advertise(req)
184 if resp.result == gateway_msgs.ErrorCodes.ADVERTISEMENT_EXISTS:
186 elif resp.result != 0:
187 raise GatewaySampleRuntimeError(
"failed to advertise %s [%s]" % (rule.name, resp.error_message))
190 def pull_all(remote_gateway_name=None, cancel=False, ns=_gateway_namespace):
192 Sends a rule for pulling everything from the specified remote gateway. 194 rospy.wait_for_service(ns +
'/pull_all')
195 pull_all = rospy.ServiceProxy(ns +
'/pull_all', gateway_srvs.RemoteAll)
196 if not remote_gateway_name:
198 req = gateway_srvs.RemoteAllRequest()
199 req.gateway = remote_gateway_name
202 rospy.loginfo(
"Pull All : %s." %
_action_text(cancel,
'sending pull rule for all to the gateway'))
204 if resp.result == gateway_msgs.ErrorCodes.PULL_RULE_ALREADY_EXISTS:
206 elif resp.result != 0:
207 raise GatewaySampleRuntimeError(
"failed to pull all from %s [%s]" % (remote_gateway_name, resp.error_message))
210 def pull_tutorials(remote_gateway_name=None, cancel=False, regex_patterns=False, ns=_gateway_namespace):
211 rospy.wait_for_service(ns +
'/pull')
212 pull = rospy.ServiceProxy(ns +
'/pull', gateway_srvs.Remote)
213 if not remote_gateway_name:
215 req = gateway_srvs.RemoteRequest()
222 for connection_type
in connection_types:
223 rule = gateway_msgs.Rule()
224 rule.name = names[connection_type]
225 rule.type = connection_type
226 rule.node = nodes[connection_type]
228 "Pull : %s [%s,%s,%s][%s]." %
231 'sending pull rule to the gateway'),
235 remote_gateway_name))
236 req.remotes.append(gateway_msgs.RemoteRule(remote_gateway_name, rule))
238 if resp.result == gateway_msgs.ErrorCodes.PULL_RULE_ALREADY_EXISTS:
240 elif resp.result != 0:
241 raise GatewaySampleRuntimeError(
"failed to pull %s [%s]" % (rule.name, resp.error_message))
244 def flip_all(remote_gateway_name=None, cancel=False, ns=_gateway_namespace):
246 Sends a rule for flipping everything to the specified remote gateway. 248 rospy.wait_for_service(ns +
'/flip_all')
249 flip_all = rospy.ServiceProxy(ns +
'/flip_all', gateway_srvs.RemoteAll)
250 if not remote_gateway_name:
252 req = gateway_srvs.RemoteAllRequest()
253 req.gateway = remote_gateway_name
256 rospy.loginfo(
"Flip All : %s [%s]." %
257 (
_action_text(cancel,
'sending flip rule for all to the gateway'), remote_gateway_name))
259 if resp.result == gateway_msgs.ErrorCodes.FLIP_RULE_ALREADY_EXISTS:
261 elif resp.result != 0:
262 raise GatewaySampleRuntimeError(
"failed to flip all to %s [%s]" % (remote_gateway_name, resp.error_message))
265 def flip_tutorials(remote_gateway_name=None, cancel=False, regex_patterns=False, ns=_gateway_namespace):
266 rospy.wait_for_service(ns +
'/flip')
267 flip = rospy.ServiceProxy(ns +
'/flip', gateway_srvs.Remote)
268 if not remote_gateway_name:
270 req = gateway_srvs.RemoteRequest()
277 for connection_type
in connection_types:
278 rule = gateway_msgs.Rule()
279 rule.name = names[connection_type]
280 rule.type = connection_type
281 rule.node = nodes[connection_type]
283 "Flip : %s [%s,%s,%s][%s]." %
286 'requesting flip to gateway'),
290 remote_gateway_name))
291 req.remotes.append(gateway_msgs.RemoteRule(remote_gateway_name, rule))
293 if resp.result == gateway_msgs.ErrorCodes.FLIP_RULE_ALREADY_EXISTS:
295 elif resp.result != 0:
296 raise GatewaySampleRuntimeError(
"failed to flip %s [%s]" % (rule.name, resp.error_message))
300 service_name = ns +
"/connect_hub" 301 rospy.wait_for_service(service_name, 1.0)
302 connect = rospy.ServiceProxy(service_name, gateway_srvs.ConnectHub)
304 req = gateway_srvs.ConnectHubRequest()
305 req.uri =
"http://localhost:6380" 307 rospy.loginfo(
"Request:\n\n%s\n" % req)
311 rospy.loginfo(
"Response:\n\n%s\n" % resp)
313 if resp.result != gateway_msgs.ErrorCodes.SUCCESS:
314 raise GatewaySampleRuntimeError(
"failed to connect to hub [%s][%s]" % (req.uri, resp.error_message))
315 return resp.result, resp.error_message
def connect_hub_by_service(ns=_gateway_namespace, raise_exception=True)
def wait_for_remote_gateway(remote_gateway_name, ns=_gateway_namespace, timeout=rospy.Duration(5.0))
def _action_text(cancel=False, msg='acting')
Methods.
def flip_all(remote_gateway_name=None, cancel=False, ns=_gateway_namespace)
def pull_tutorials(remote_gateway_name=None, cancel=False, regex_patterns=False, ns=_gateway_namespace)
def advertise_tutorials(cancel=False, regex_patterns=False, ns=_gateway_namespace)
def wait_for_gateway(ns=_gateway_namespace, timeout=rospy.Duration(5.0))
Utility functions.
def pull_all(remote_gateway_name=None, cancel=False, ns=_gateway_namespace)
def flip_tutorials(remote_gateway_name=None, cancel=False, regex_patterns=False, ns=_gateway_namespace)
def create_tutorial_dictionaries(use_regex_patterns=False)
def find_first_remote_gateway(ns=_gateway_namespace, timeout=rospy.Duration(15.0))
def advertise_all(cancel=False, ns=_gateway_namespace)