master.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 '''
00004 Created on 08/08/2011
00005 
00006 @author: Daniel Stonier
00007 '''
00008 ##############################################################################
00009 # Imports
00010 ##############################################################################
00011 
00012 import xmlrpclib
00013 
00014 # Ros imports
00015 import roslib; roslib.load_manifest('concert_master')
00016 import rosgraph
00017 import rospy
00018 import zeroconf_msgs
00019 from zeroconf_msgs.srv import AddService
00020 
00021 ##############################################################################
00022 # Functions
00023 ##############################################################################
00024 
00025 try:
00026     import urllib.parse as urlparse
00027 except ImportError:
00028     import urlparse
00029 
00030 def parse_http_host_and_port(url):     
00031     """     
00032     Convenience routine to handle parsing and validation of HTTP URL     
00033     port due to the fact that Python only provides easy accessors in     
00034     Python 2.5 and later. Validation checks that the protocol and host     
00035     are set.     
00036      
00037     @param url: URL to parse     
00038     @type  url: str     
00039     @return: hostname and port number in URL or 80 (default).     
00040     @rtype: (str, int)     
00041     @raise ValueError: if the url does not validate     
00042     """     
00043     # can't use p.port because that's only available in Python 2.5     
00044     if not url:     
00045         raise ValueError('not a valid URL')     
00046     p = urlparse.urlparse(url)     
00047     if not p[0] or not p[1]: #protocol and host     
00048         raise ValueError('not a valid URL')     
00049     if ':' in p[1]:     
00050         hostname, port = p[1].split(':')     
00051         port = int(port)     
00052     else:     
00053         hostname, port = p[1], 80     
00054     return hostname, port
00055 
00056 ##############################################################################
00057 # Main
00058 ##############################################################################
00059 '''
00060   This currently serves the following purposes:
00061   
00062   1) Checks that the zeroconf node has come up
00063   2) Retrieves some parameters and advertises the master
00064   
00065   Parameters:
00066     ~name : name for the concert master [Concert Master].
00067     ~domain : zeroconf domain to advertise on [local]. 
00068 '''
00069 def main():
00070     rospy.init_node('concert_master', log_level=rospy.DEBUG)
00071     request = zeroconf_msgs.srv.AddServiceRequest()
00072     request.service.name = rospy.get_param("name","Concert Master")
00073     request.service.type = "_concert-master._tcp"
00074     request.service.domain = rospy.get_param("domain","local")
00075     request.service.port = rosgraph.network.parse_http_host_and_port(rosgraph.rosenv.get_master_uri())[1]
00076 
00077     # Make sure the zeroconf is up and running first
00078     try:
00079         rospy.wait_for_service('add_service', 30.0)
00080         advertise_concert_master = rospy.ServiceProxy('add_service', zeroconf_msgs.srv.AddService)
00081         response = advertise_concert_master(request)
00082         if response.result:
00083             rospy.loginfo("Concert Master: advertising zeroconf information [%s][%s][%s]"%(request.service.name, request.service.domain, request.service.port))
00084         else:
00085             rospy.logwarn("Concert Master : failed to advertise this concert master on the zeroconf node")
00086     except rospy.ROSException:
00087         rospy.logerr("Concert Master: timed out waiting for the zeroconf node to become available.")
00088         return
00089 
00090     # Find useful things to add here later.
00091     rospy.spin()
00092 
00093 if __name__ == '__main__':
00094     main()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


graveyard_concert_master
Author(s): Daniel Stonier
autogenerated on Wed Jan 23 2013 13:41:59