00001 # Copyright (c) 2013, Oregon State University 00002 # All rights reserved. 00003 00004 # Redistribution and use in source and binary forms, with or without 00005 # modification, are permitted provided that the following conditions are met: 00006 # * Redistributions of source code must retain the above copyright 00007 # notice, this list of conditions and the following disclaimer. 00008 # * Redistributions in binary form must reproduce the above copyright 00009 # notice, this list of conditions and the following disclaimer in the 00010 # documentation and/or other materials provided with the distribution. 00011 # * Neither the name of the Oregon State University nor the 00012 # names of its contributors may be used to endorse or promote products 00013 # derived from this software without specific prior written permission. 00014 00015 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00016 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00018 # DISCLAIMED. IN NO EVENT SHALL OREGON STATE UNIVERSITY BE LIABLE FOR ANY 00019 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00020 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00021 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00022 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00023 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00024 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 00026 # Author Dan Lazewatsky/lazewatd@engr.orst.edu 00027 00028 import roslib; roslib.load_manifest('rostop_gui') 00029 import rosnode 00030 import rospy 00031 import xmlrpclib 00032 import psutil 00033 00034 ID = '/NODEINFO' 00035 00036 class NodeInfo(object): 00037 nodes = dict() 00038 def get_node_info(self, node_name): 00039 node_api = rosnode.get_api_uri(rospy.get_master(), node_name) 00040 code, msg, pid = xmlrpclib.ServerProxy(node_api[2]).getPid(ID) 00041 if node_name in self.nodes: 00042 return self.nodes[node_name] 00043 else: 00044 try: 00045 p = psutil.Process(pid) 00046 self.nodes[node_name] = p 00047 return p 00048 except: 00049 return False 00050 00051 00052 def get_all_node_info(self): 00053 infos = [] 00054 for node_name in rosnode.get_node_names(): 00055 info = self.get_node_info(node_name) 00056 if info is not False: infos.append((node_name, info)) 00057 return infos 00058 00059 def get_all_node_fields(self, fields): 00060 # import pdb; pdb.set_trace() 00061 processes = self.get_all_node_info() 00062 infos = [] 00063 for name, p in processes: 00064 infos.append(p.as_dict(fields + ['cmdline', 'get_memory_info'])) 00065 infos[-1]['node_name'] = name 00066 return infos 00067 00068 def kill_node(self, node_name): 00069 success, fail = rosnode.kill_nodes([node_name]) 00070 return node_name in success