13 import gateway_msgs.msg
as gateway_msgs
14 import rocon_hub_client
16 from .exceptions
import GatewayUnavailableError
17 from .
import gateway_hub
27 :ivar hubs: list of gateway hub instances 28 :vartype hubs: [rocon_gateway.GatewayHub] 35 def __init__(self, hub_whitelist, hub_blacklist):
37 self.
_param[
'hub_whitelist'] = hub_whitelist
38 self.
_param[
'hub_blacklist'] = hub_blacklist
43 return True if self.
hubs else False 51 Parse all the hubs and retrieve the list of remote gateway names. 53 Note: not sure where is most convenient, here or in gateway class. 55 @return list of remote gateway names (with hashes), e.g. gateway345ae2c... 58 remote_gateway_names = []
59 self._hub_lock.acquire()
61 remote_gateway_names.extend(hub.list_remote_gateway_names())
62 self._hub_lock.release()
64 return list(set(remote_gateway_names))
68 Utility function to parse all hubs for the remote gateways and 69 create a dictionary of the type: 71 dic['remote_gateway_name'] = ['hub1', 'hub2'] 73 where the hub list is a list of actual hub object references. 76 self._hub_lock.acquire()
78 for remote_gateway
in hub.list_remote_gateway_names():
79 if remote_gateway
in dic:
80 dic[remote_gateway].append(hub)
82 dic[remote_gateway] = [hub]
83 self._hub_lock.release()
88 Returns all unblocked flip requests received by this hub 90 @return list of flip registration requests 91 @rtype list of utils.Registration 94 self._hub_lock.acquire()
96 registrations.extend(hub.get_unblocked_flipped_in_connections())
97 self._hub_lock.release()
102 Return information that a remote gateway has posted on the hub(s). 104 @param remote_gateway_name : the hash name for the remote gateway 107 @return remote gateway information 108 @rtype gateway_msgs.RemotGateway or None 110 remote_gateway_info =
None 111 self._hub_lock.acquire()
112 for hub
in self.
hubs:
113 if remote_gateway_name
in hub.list_remote_gateway_names():
115 remote_gateway_info = hub.remote_gateway_info(remote_gateway_name)
116 if remote_gateway_info
is not None:
118 self._hub_lock.release()
119 return remote_gateway_info
123 Return information that a remote gateway has posted on the hub(s). 125 @param remote_gateway_name : the hash name for the remote gateway 128 @return True, false if the flag is set or not, None if remote 129 gateway information cannot found 133 self._hub_lock.acquire()
134 for hub
in self.
hubs:
135 if remote_gateway_name
in hub.list_remote_gateway_names():
138 firewall_flag = hub.get_remote_gateway_firewall_flag(remote_gateway_name)
140 except GatewayUnavailableError:
142 self._hub_lock.release()
147 Send an unflip request to the specified gateway through all available 150 Doesn't raise GatewayUnavailableError if nothing got sent as the higher level 151 doesn't need any logic there yet (only called from gateway.shutdown). 153 @param remote_gateway_name : the hash name for the remote gateway 156 @param remote_rule : the remote rule to unflip 157 @type gateway_msgs.RemoteRule 159 self._hub_lock.acquire()
160 for hub
in self.
hubs:
161 if remote_gateway_name
in hub.list_remote_gateway_names():
163 if hub.send_unflip_request(remote_gateway_name, remote_rule):
164 self._hub_lock.release()
166 except GatewayUnavailableError:
168 self._hub_lock.release()
178 gateway_disengage_hub,
180 existing_advertisements
183 Attempts to make a connection and register the gateway with a hub. 184 This is called from the gateway node's _register_gateway method. 189 @param gateway_unique_name 190 @param remote_gateway_request_callbacks 191 @type method : Gateway.remote_gateway_request_callbacks() 192 @param gateway_disengage_hub : this is the hub connection lost hook 193 @type method : Gateway.disengage_hub() 195 @param existing advertisements 196 @type { utils.ConnectionTypes : utils.Connection[] } 198 @return an integer indicating error (important for the service call) 199 @rtype gateway_msgs.ErrorCodes 203 self._hub_lock.acquire()
205 new_hub.register_gateway(firewall_flag,
207 gateway_disengage_hub,
210 for connection_type
in utils.connection_types:
211 for advertisement
in existing_advertisements[connection_type]:
212 new_hub.advertise(advertisement)
215 if new_hub
in self.
hubs:
216 self.hubs.remove(new_hub)
218 self.hubs.append(new_hub)
219 except rocon_hub_client.HubError
as e:
220 return None, e.id, str(e)
222 self._hub_lock.release()
223 return new_hub, gateway_msgs.ErrorCodes.SUCCESS,
"success" 227 Check if the gateway is properly connected to the hub. 232 if h.ip == ip
and h.port == port:
239 except rocon_hub_client.HubError
as e:
240 return None, e.id, str(e)
242 self._hub_lock.acquire()
243 registered = hub.is_gateway_registered()
244 self._hub_lock.release()
246 return hub, gateway_msgs.ErrorCodes.HUB_CONNECTION_ALREADY_EXISTS,
"already connected to this hub" 248 return hub, gateway_msgs.ErrorCodes.NO_HUB_CONNECTION,
"not connected to this hub" 252 Disengages a hub. Make sure all necessary connections 253 are cleaned up before calling this (Gateway.disengage_hub). 255 @param hub_to_be_disengaged 259 hub_to_be_disengaged.disconnect()
260 self._hub_lock.acquire()
261 if hub_to_be_disengaged
in self.
hubs:
262 rospy.loginfo(
"Gateway : disengaged connection with the hub [%s][%s]" % (
263 hub_to_be_disengaged.name, hub_to_be_disengaged.uri))
264 self.
hubs[:] = [hub
for hub
in self.
hubs if hub != hub_to_be_disengaged]
265 self._hub_lock.release()
268 self._hub_lock.acquire()
269 for hub
in self.
hubs:
270 hub.advertise(connection)
271 self._hub_lock.release()
274 self._hub_lock.acquire()
275 for hub
in self.
hubs:
276 hub.unadvertise(connection)
277 self._hub_lock.release()
281 Parses the hub lists looking for strong (identical) and 282 weak (matches the name without the uuid hash) matches. 286 self._hub_lock.acquire()
287 for hub
in self.
hubs:
288 matches.extend(hub.matches_remote_gateway_name(remote_gateway_name))
289 weak_matches.extend(hub.matches_remote_gateway_basename(remote_gateway_name))
290 self._hub_lock.release()
292 matches = list(set(matches))
293 weak_matches = list(set(weak_matches))
294 return matches, weak_matches
298 Publish network statistics to every hub this gateway is connected to. 301 @type gateway_msgs.ConnectionStatistics 303 self._hub_lock.acquire()
304 for hub
in self.
hubs:
305 hub.publish_network_statistics(statistics)
306 self._hub_lock.release()
def match_remote_gateway_name(self, remote_gateway_name)
def advertise(self, connection)
def disengage_hub(self, hub_to_be_disengaged)
def send_unflip_request(self, remote_gateway_name, remote_rule)
def unadvertise(self, connection)
def get_remote_gateway_firewall_flag(self, remote_gateway_name)
def connect_to_hub(self, new_hub, firewall_flag, gateway_unique_name, gateway_disengage_hub, gateway_ip, existing_advertisements)
Hub Connections.
def is_connected_to_hub(self, ip, port)
def publish_network_statistics(self, statistics)
def list_remote_gateway_names(self)
Introspection.
def create_remote_gateway_hub_index(self)
def __init__(self, hub_whitelist, hub_blacklist)
Init & Shutdown.
def get_flip_requests(self)
def remote_gateway_info(self, remote_gateway_name)