Package rosmaster :: Module main

Source Code for Module rosmaster.main

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2008, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id$ 
 34   
 35  """Command-line handler for ROS zenmaster (Python Master)""" 
 36   
 37  import logging 
 38  import os 
 39  import sys 
 40  import time 
 41  import optparse 
 42   
 43  import rosmaster.master 
 44  from rosmaster.master_api import NUM_WORKERS 
 45   
46 -def configure_logging():
47 """ 48 Setup filesystem logging for the master 49 """ 50 filename = 'master.log' 51 # #988 __log command-line remapping argument 52 import rosgraph.names 53 import rosgraph.roslogging 54 mappings = rosgraph.names.load_mappings(sys.argv) 55 if '__log' in mappings: 56 logfilename_remap = mappings['__log'] 57 filename = os.path.abspath(logfilename_remap) 58 _log_filename = rosgraph.roslogging.configure_logging('rosmaster', logging.DEBUG, filename=filename)
59
60 -def rosmaster_main(argv=sys.argv, stdout=sys.stdout, env=os.environ):
61 parser = optparse.OptionParser(usage="usage: zenmaster [options]") 62 parser.add_option("--core", 63 dest="core", action="store_true", default=False, 64 help="run as core") 65 parser.add_option("-p", "--port", 66 dest="port", default=0, 67 help="override port", metavar="PORT") 68 parser.add_option("-w", "--numworkers", 69 dest="num_workers", default=NUM_WORKERS, type=int, 70 help="override number of worker threads", metavar="NUM_WORKERS") 71 parser.add_option("-t", "--timeout", 72 dest="timeout", 73 help="override the socket connection timeout (in seconds).", metavar="TIMEOUT") 74 options, args = parser.parse_args(argv[1:]) 75 76 # only arg that zenmaster supports is __log remapping of logfilename 77 for arg in args: 78 if not arg.startswith('__log:='): 79 parser.error("unrecognized arg: %s"%arg) 80 configure_logging() 81 82 port = rosmaster.master.DEFAULT_MASTER_PORT 83 if options.port: 84 port = int(options.port) 85 86 if not options.core: 87 print(""" 88 89 90 ACHTUNG WARNING ACHTUNG WARNING ACHTUNG 91 WARNING ACHTUNG WARNING ACHTUNG WARNING 92 93 94 Standalone zenmaster has been deprecated, please use 'roscore' instead 95 96 97 ACHTUNG WARNING ACHTUNG WARNING ACHTUNG 98 WARNING ACHTUNG WARNING ACHTUNG WARNING 99 100 101 """) 102 103 logger = logging.getLogger("rosmaster.main") 104 logger.info("initialization complete, waiting for shutdown") 105 106 if options.timeout is not None and float(options.timeout) >= 0.0: 107 logger.info("Setting socket timeout to %s" % options.timeout) 108 import socket 109 socket.setdefaulttimeout(float(options.timeout)) 110 111 try: 112 logger.info("Starting ROS Master Node") 113 master = rosmaster.master.Master(port, options.num_workers) 114 master.start() 115 116 import time 117 while master.ok(): 118 time.sleep(.1) 119 except KeyboardInterrupt: 120 logger.info("keyboard interrupt, will exit") 121 finally: 122 logger.info("stopping master...") 123 master.stop()
124 125 if __name__ == "__main__": 126 main() 127