13 import gateway_msgs.msg
as gateway_msgs
14 import gateway_msgs.srv
as gateway_srvs
15 import std_msgs.msg
as std_msgs
16 import std_srvs.srv
as std_srvs
17 from urlparse
import urlparse
18 import rocon_hub_client
21 from .
import hub_manager
31 Currently this just provides getup and go for the gateway. 38 self.
_param = rocon_gateway.setup_ros_parameters()
39 if self.
_param[
'disable_uuids']:
41 rospy.logwarn(
"Gateway : uuid's disabled, using possibly non-unique name [%s]" % self.
_unique_name)
47 rospy.loginfo(
"Gateway : generated unique hash name [%s]" % self.
_unique_name)
50 gateway_msgs.ErrorCodes.HUB_CONNECTION_BLACKLISTED,
51 gateway_msgs.ErrorCodes.HUB_NAME_NOT_FOUND,
57 hub_whitelist=self.
_param[
'hub_whitelist'],
58 hub_blacklist=self.
_param[
'hub_blacklist']
68 direct_hub_uri_list = [self.
_param[
'hub_uri']]
if self.
_param[
'hub_uri'] !=
'' else []
82 Clears this gateway's information from the redis server. 84 rospy.loginfo(
"Gateway : shutting down.")
91 self._hub_discovery_thread.shutdown()
94 except Exception
as e:
95 rospy.logerr(
"Gateway : unknown error on shutdown [%s][%s]" % (str(e), type(e)))
104 Called when the hub discovery can ping a hub 111 uri = ip +
':' + str(port)
112 if uri
in self._disallowed_hubs.keys():
116 hub, error_code, error_code_str = self._hub_manager.is_connected_to_hub(ip, port)
117 if error_code == gateway_msgs.ErrorCodes.NO_HUB_CONNECTION:
120 if error_code == gateway_msgs.ErrorCodes.SUCCESS:
122 elif error_code == gateway_msgs.ErrorCodes.HUB_CONNECTION_ALREADY_EXISTS:
127 "Gateway : failed to register gateway with the hub [%s][%s][%s]" % (uri, error_code, error_code_str))
128 elif error_code == gateway_msgs.ErrorCodes.HUB_CONNECTION_FAILED:
130 "Gateway : failed to connect to the hub [%s][%s][%s]" % (uri, error_code, error_code_str))
131 elif error_code == gateway_msgs.ErrorCodes.HUB_CONNECTION_UNRESOLVABLE:
135 "Gateway : failed to register gateway with the hub [%s][%s][%s]" % (uri, error_code, error_code_str))
138 "Gateway : Unknown error code when insuring gateway connection with the hub [%s][%s][%s]" %
139 (uri, error_code, error_code_str))
141 return error_code, error_code_str
145 Called when either the hub discovery module finds a hub 146 or a request to connect via ros service is made. 148 It starts the actual redis connection with the hub and also 149 registers the appropriate information about the gateway on 152 Note, the return type is only really used by the service callback 153 (ros_service_connect_hub). 155 @return error code and message 156 @rtype gateway_msgs.ErrorCodes, string 158 @sa hub_discovery.HubDiscovery 161 existing_advertisements = self._gateway.public_interface.getConnections()
162 hub, error_code, error_code_str = \
163 self._hub_manager.connect_to_hub(
169 existing_advertisements
172 rospy.loginfo(
"Gateway : registering on the hub [%s]" % hub.name)
175 return error_code, error_code_str
179 Called whenever gateway_hub detects the connection to the hub has been 182 This function informs the hub discovery thread that the hub was lost, 183 which allows the hub_discovery thread to start looking for the hub. 185 @param hub: hub to be disengaged 189 self._hub_discovery_thread.disengage_hub(hub)
191 self._gateway.disengage_hub(hub)
192 except AttributeError:
202 gateway_services = {}
203 gateway_services[
'connect_hub'] = rospy.Service(
205 gateway_services[
'remote_gateway_info'] = rospy.Service(
207 gateway_services[
'advertise'] = rospy.Service(
208 '~advertise', gateway_srvs.Advertise, self._gateway.ros_service_advertise)
209 gateway_services[
'advertise_all'] = rospy.Service(
210 '~advertise_all', gateway_srvs.AdvertiseAll, self._gateway.ros_service_advertise_all)
211 gateway_services[
'flip'] = rospy.Service(
212 '~flip', gateway_srvs.Remote, self._gateway.ros_service_flip)
213 gateway_services[
'flip_all'] = rospy.Service(
214 '~flip_all', gateway_srvs.RemoteAll, self._gateway.ros_service_flip_all)
215 gateway_services[
'pull'] = rospy.Service(
216 '~pull', gateway_srvs.Remote, self._gateway.ros_service_pull)
217 gateway_services[
'pull_all'] = rospy.Service(
218 '~pull_all', gateway_srvs.RemoteAll, self._gateway.ros_service_pull_all)
223 return gateway_services
226 gateway_publishers = {}
227 gateway_publishers[
'gateway_info'] = rospy.Publisher(
'~gateway_info', gateway_msgs.GatewayInfo, latch=
True, queue_size=5)
228 return gateway_publishers
231 gateway_subscribers = {}
234 return gateway_subscribers
242 Handle incoming requests to connect directly to a gateway hub. 244 Requests are of the form of a uri (hostname:port pair) pointing to 247 response = gateway_srvs.ConnectHubResponse()
248 o = urlparse(request.uri)
251 if response.result == gateway_msgs.ErrorCodes.SUCCESS:
252 rospy.loginfo(
"Gateway : made direct connection to hub [%s]" % request.uri)
259 gateway_info = gateway_msgs.GatewayInfo()
261 gateway_info.ip = self._gateway.ip
262 gateway_info.connected = self._gateway.is_connected()
263 gateway_info.hub_names = []
264 gateway_info.hub_uris = []
265 for hub
in self._hub_manager.hubs:
266 gateway_info.hub_names.append(hub.name)
267 gateway_info.hub_uris.append(hub.uri)
268 gateway_info.firewall = self.
_param[
'firewall']
269 gateway_info.flipped_connections = self._gateway.flipped_interface.get_flipped_connections()
270 gateway_info.flipped_in_connections = self._gateway.flipped_interface.getLocalRegistrations()
271 gateway_info.flip_watchlist = self._gateway.flipped_interface.getWatchlist()
272 gateway_info.pulled_connections = self._gateway.pulled_interface.getLocalRegistrations()
273 gateway_info.pull_watchlist = self._gateway.pulled_interface.getWatchlist()
274 gateway_info.public_watchlist = self._gateway.public_interface.getWatchlist()
275 gateway_info.public_interface = self._gateway.public_interface.getInterface()
277 except AttributeError:
282 Sends out to the hubs to get the remote gateway information for either the specified, 283 or the known list of remote gateways. 285 :todo: can we optimise this so that hub requests go as a group? 287 response = gateway_srvs.RemoteGatewayInfoResponse()
288 requested_gateways = request.gateways
if request.gateways
else self._hub_manager.list_remote_gateway_names()
289 for gateway
in list(set(requested_gateways)):
290 remote_gateway_info = self._hub_manager.remote_gateway_info(gateway)
291 if remote_gateway_info:
292 response.gateways.append(remote_gateway_info)
294 rospy.logwarn(
"Gateway : requested gateway info for unavailable gateway [%s]" % gateway)
def _disengage_hub(self, hub)
_disallowed_hubs_error_codes
def _hub_ensure_connection(self, ip, port)
Hub Discovery & Connection.
Gateway Configuration and Main Loop Class.
def _register_gateway(self, hub)
def __init__(self)
Init & Shutdown.
def _setup_ros_subscribers(self)
def _setup_ros_services(self)
Ros Pubs, Subs and Services.
def ros_service_remote_gateway_info(self, request)
def ros_service_connect_hub(self, request)
Ros Service Callbacks.
def _publish_gateway_info(self)
def _setup_ros_publishers(self)