zeroconf.py
Go to the documentation of this file.
00001 '''
00002 Created on 09/08/2011
00003 
00004 @author: snorri
00005 '''
00006 
00007 ##############################################################################
00008 # Imports
00009 ##############################################################################
00010 
00011 # Ros imports
00012 import roslib; roslib.load_manifest('rocon_conductor')
00013 import rospy
00014 
00015 # Rocon imports
00016 import zeroconf_avahi
00017 import zeroconf_msgs
00018 from zeroconf_msgs.msg import *
00019 from zeroconf_msgs.srv import AddListener
00020 
00021 ##############################################################################
00022 # Functions
00023 ##############################################################################
00024 
00025 def listen_for_app_managers():
00026     '''
00027         Tell the concert master's zeroconf node to listen for app managers.
00028     '''
00029     try:
00030         rospy.wait_for_service('add_listener', 30.0)
00031         add_listener=rospy.ServiceProxy('add_listener', AddListener)
00032         request = zeroconf_msgs.srv.AddListenerRequest()
00033         request.service_type = '_app-manager._tcp'
00034         response = add_listener(request)
00035         if response.result == False:
00036             rospy.logerr("Conductor: couldn't add a listener to the concert master's zeroconf node.")
00037             return False
00038     except rospy.ROSException:
00039         rospy.logerr("Conductor: timed out waiting for the concert master to advertise itself.")
00040         return False
00041     return True
00042 
00043 def concert_master_name():
00044     '''
00045     Retrieve the concert master's zeroconf info. This will have automatically been started along
00046     with the conductor, so we just grab from the published services.
00047     
00048     Note: we assume that there is only one concert master service published on 
00049     this ros master!
00050     '''
00051     rospy.sleep(1.0)
00052     rospy.wait_for_service('list_published_services')
00053     try:
00054         try_count = 0
00055         while (try_count != 10):
00056             service_info = rospy.ServiceProxy('list_published_services', zeroconf_msgs.srv.ListPublishedServices)
00057             request = zeroconf_msgs.srv.ListPublishedServicesRequest()
00058             request.service_type = "_concert-master._tcp"
00059             response = service_info(request)
00060             if len(response.services) > 0:
00061                 for service in response.services:
00062                     return service.name
00063             ++try_count
00064             rospy.sleep(1.0)
00065     except rospy.ServiceException, error:
00066         rospy.logwarn("Conductor : could not get this concert master's zeroconf details [%s]"%error)
00067     return None
00068 
00069 def discover_concert_clients():
00070     '''
00071     This is a one-shot call to discover concert clients. After this we rely on the new_connections/
00072     lost_connections subscriber callbacks.
00073 
00074     Assumptions:
00075       1) Clients are only valid on ipv4 protocols (local lan)
00076     '''
00077     try:
00078         rospy.wait_for_service('list_discovered_services', timeout=5.0 )
00079         rospy.loginfo("Conductor: the concert master zero-configuration has been published.")
00080     except rospy.ROSException,e:
00081         rospy.logerr("Conductor: timed out waiting for the concert master's zero-configuration [%s]"%e)
00082         
00083     concert_clients = []
00084     try:
00085         discover_clients = rospy.ServiceProxy('list_discovered_services', zeroconf_msgs.srv.ListDiscoveredServices)
00086         request = zeroconf_msgs.srv.ListDiscoveredServicesRequest()
00087         request.service_type = "_app-manager._tcp"
00088         response = discover_clients(request)
00089         # Ignore ipv6 clients
00090         for service in response.services:
00091             if len(service.ipv4_addresses) > 0:
00092                 concert_clients.append(service)
00093         return concert_clients
00094     except rospy.ServiceException, error:
00095         rospy.logwarn("Conductor : could not get the list of concert clients via zeroconf [%s]"%error)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


graveyard_rocon_conductor
Author(s): Daniel Stonier
autogenerated on Wed Jan 23 2013 13:42:04