Package rospy :: Package impl :: Module init

Source Code for Module rospy.impl.init

  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: init.py 12050 2010-11-09 08:13:53Z kwc $ 
 34  """ 
 35  Internal use: rospy initialization. 
 36   
 37  This is mainly routines for initializing the master or slave based on 
 38  the OS environment. 
 39  """ 
 40   
 41  import os 
 42  import sys 
 43  import logging 
 44  import time 
 45  import traceback 
 46   
 47  import roslib.rosenv 
 48  import roslib.xmlrpc 
 49   
 50  from ..names import _set_caller_id 
 51  from ..core import is_shutdown, add_log_handler, signal_shutdown, rospyerr 
 52  from ..rostime import is_wallclock, get_time 
 53   
 54  from .tcpros import init_tcpros 
 55  from .masterslave import ROSHandler 
 56   
 57  DEFAULT_NODE_PORT = 0 #bind to any open port 
 58  DEFAULT_MASTER_PORT=11311 #default port for master's to bind to 
 59  DEFAULT_MASTER_URI = 'http://localhost:%s/'%DEFAULT_MASTER_PORT 
 60   
 61  ################################################### 
 62  # rospy module lower-level initialization 
 63   
64 -def _node_run_error(e):
65 """ 66 If XML-RPC errors out of the run() method, this handler is invoked 67 """ 68 rospyerr(traceback.format_exc()) 69 signal_shutdown('error in XML-RPC server: %s'%(e))
70
71 -def start_node(environ, resolved_name, master_uri=None, port=None):
72 """ 73 Load ROS slave node, initialize from environment variables 74 @param environ: environment variables 75 @type environ: dict 76 @param resolved_name: resolved node name 77 @type resolved_name: str 78 @param master_uri: override ROS_MASTER_URI: XMlRPC URI of central ROS server 79 @type master_uri: str 80 @param port: override ROS_PORT: port of slave xml-rpc node 81 @type port: int 82 @return: node server instance 83 @rtype roslib.xmlrpc.XmlRpcNode 84 @raise ROSInitException: if node has already been started 85 """ 86 init_tcpros() 87 if not master_uri: 88 master_uri = roslib.rosenv.get_master_uri() 89 if not master_uri: 90 master_uri = DEFAULT_MASTER_URI 91 92 # this will go away in future versions of API 93 _set_caller_id(resolved_name) 94 95 handler = ROSHandler(resolved_name, master_uri) 96 node = roslib.xmlrpc.XmlRpcNode(port, handler, on_run_error=_node_run_error) 97 node.start() 98 while not node.uri and not is_shutdown(): 99 time.sleep(0.00001) #poll for XMLRPC init 100 logging.getLogger("rospy.init").info("ROS Slave URI: [%s]", node.uri) 101 102 while not handler._is_registered() and not is_shutdown(): 103 time.sleep(0.1) #poll for master registration 104 logging.getLogger("rospy.init").info("registered with master") 105 return node
106 107 # #2879 108 # resolve sys.stdout/stderr each time through in case testing program or otherwise wants to redirect stream
109 -def _stdout_log(level):
110 def fn(msg): 111 if is_wallclock(): 112 sys.stdout.write("[%s] [WallTime: %f] %s\n"%(level,time.time(), str(msg))) 113 else: 114 sys.stdout.write("[%s] [WallTime: %f] [%f] %s\n"%(level,time.time(), get_time(), str(msg)))
115 return fn
116 -def _stderr_log(level):
117 def fn(msg): 118 if is_wallclock(): 119 sys.stderr.write("[%s] [WallTime: %f] %s\n"%(level,time.time(), str(msg))) 120 else: 121 sys.stderr.write("[%s] [WallTime: %f] [%f] %s\n"%(level,time.time(), get_time(), str(msg)))
122 return fn 123 124 _loggers_initialized = False
125 -def init_log_handlers():
126 global _loggers_initialized 127 if _loggers_initialized: 128 return 129 130 from rosgraph_msgs.msg import Log 131 132 # client logger 133 clogger = logging.getLogger("rosout") 134 add_log_handler(Log.DEBUG, clogger.debug) 135 add_log_handler(Log.INFO, clogger.info) 136 add_log_handler(Log.ERROR, clogger.error) 137 add_log_handler(Log.WARN, clogger.warn) 138 add_log_handler(Log.FATAL, clogger.critical) 139 140 # TODO: make this configurable 141 # INFO -> stdout 142 # ERROR, FATAL -> stderr 143 add_log_handler(Log.INFO, _stdout_log('INFO')) 144 add_log_handler(Log.WARN, _stderr_log('WARN')) 145 add_log_handler(Log.ERROR, _stderr_log('ERROR')) 146 add_log_handler(Log.FATAL, _stderr_log('FATAL'))
147