node_proxy.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 #
00033 # Author: Isaac Saito
00034 
00035 import time
00036 import threading
00037 
00038 from roslaunch import node_args, nodeprocess
00039 import rospy
00040 
00041 
00042 class Polling(threading.Thread):
00043 
00044     def __init__(self, parent):
00045         super(Polling, self).__init__()
00046         self._parent = parent
00047 
00048     def run(self):
00049         while True:
00050             rospy.logdebug('Proc={} Died? {}'.format(self._parent.get_proc_name(),
00051                                                      self._parent.has_died()))
00052             time.sleep(1.0)
00053 
00054 
00055 class NodeProxy(object):
00056 
00057     '''
00058     Works as a proxy between ROS Node
00059     (more in particular, roslaunch.nodeprocess) & GUI.
00060     '''
00061 
00062     __slots__ = ['_run_id', 'master_uri', 'config', '_process']
00063 
00064     def __init__(self, run_id, master_uri, config):
00065         self._run_id = run_id
00066         self.master_uri = master_uri
00067         self.config = config
00068 
00069         # @type: roslaunch.nodeprocess.LocalProcess
00070         self._process = self.recreate_process()
00071 
00072     # LocalProcess.is_alive() does not do what you would expect
00073     def is_running(self):
00074         rospy.logdebug('BEFORE started={}, stopped={} alive={}'.format(
00075             self._process.started,
00076             self._process.stopped,
00077             self._process.is_alive()))
00078         return self._process.started and not self._process.stopped
00079         # and self._process.is_alive()
00080         #  is_alive() returns False once nodeprocess was stopped.
00081 
00082     def has_died(self):
00083         rospy.logdebug('Proc={} started={}, stopped={}, is_alive={}'.format(
00084             self.get_proc_name(), self._process.started, self._process.stopped,
00085             self._process.is_alive()))
00086 
00087         return (self._process.started and not self._process.stopped
00088                 and not self._process.is_alive())
00089 
00090     def recreate_process(self):
00091         '''
00092         Create and set roslaunch.nodeprocess.LocalProcess to member variable.
00093         @rtype: roslaunch.nodeprocess.LocalProcess
00094         '''
00095         _local_process = nodeprocess.LocalProcess
00096         try:
00097             _local_process = nodeprocess.create_node_process(
00098                 self._run_id, self.config, self.master_uri)
00099         except node_args.NodeParamsException as e:
00100             rospy.logerr('roslaunch failed to load the node. {}'.format(
00101                 e.message))
00102             # TODO: Show msg on GUI that the node wasn't loaded.
00103 
00104         return _local_process
00105 
00106     def start_process(self):
00107         # TODO: add null exception check for _process
00108         self._process.start()
00109 
00110     def stop_process(self):
00111         # TODO: add null exception check for _process
00112 
00113         try:
00114             self._process.stop()
00115         except Exception as e:
00116             # TODO: Change roslaunch to raise exception
00117             rospy.logdebug('Proxy stop_process exception={}'.format(e.message))
00118 
00119     def get_spawn_count(self):
00120         return self._process.spawn_count
00121 
00122     def get_proc_name(self):
00123         return self._process.name
00124 
00125     def get_proc_exit_code(self):
00126         return self._process.exit_code


rqt_launch
Author(s): Isaac Saito, Stuart Glaser
autogenerated on Thu Jun 6 2019 21:28:15