Skip to content

Commit

Permalink
moved remote_nm.py and respawn scripts from node_manager to daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
atiderko committed Feb 6, 2024
1 parent 22e0e2a commit 398d97a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 193 deletions.
7 changes: 0 additions & 7 deletions fkie_node_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ catkin_install_python(
nodes/dynamic_reconfigure
nodes/node_manager
nodes/script_runner.py
scripts/remote_nm.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

install(
PROGRAMS
scripts/respawn
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

Expand Down
172 changes: 0 additions & 172 deletions fkie_node_manager/src/fkie_node_manager/reduced_nm.py

This file was deleted.

2 changes: 1 addition & 1 deletion fkie_node_manager/src/fkie_node_manager/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Settings(object):
CURRENT_DIALOG_PATH = os.path.expanduser('~')
LOG_PATH = screen.LOG_PATH
LOG_VIEWER = "/usr/bin/less -fKLnQrSU"
STARTER_SCRIPT = 'rosrun fkie_node_manager remote_nm.py'
STARTER_SCRIPT = 'rosrun fkie_node_manager_daemon remote_nm.py'
''':ivar STARTER_SCRIPT: the script used on remote hosts to start new ROS nodes.'''

LAUNCH_HISTORY_FILE = 'launch.history'
Expand Down
7 changes: 7 additions & 0 deletions fkie_node_manager_daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ generate_version(TARGETS version_TARGETS)
catkin_install_python(
PROGRAMS
nodes/node_manager_daemon
scripts/remote_nm.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

install(
PROGRAMS
scripts/respawn
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,43 @@
from rosgraph.network import get_local_addresses

from fkie_master_discovery.common import masteruri_from_ros
from fkie_master_discovery.common import get_hostname, get_port
from fkie_master_discovery.udp import DiscoverSocket
from fkie_node_manager_daemon import host as nmdhost
from fkie_node_manager_daemon import screen
from fkie_node_manager_daemon.common import isstring
from fkie_node_manager_daemon.settings import RESPAWN_SCRIPT
from fkie_node_manager_daemon.settings import LOG_VIEWER

try:
from fkie_node_manager import get_ros_home
from fkie_node_manager import Settings
from fkie_node_manager import StartHandler
from fkie_node_manager import StartException
except Exception:
from fkie_node_manager.reduced_nm import get_ros_home
from fkie_node_manager.reduced_nm import Settings
from fkie_node_manager.reduced_nm import StartHandler
from fkie_node_manager.reduced_nm import StartException
import xmlrpclib as xmlrpcclient
except ImportError:
import xmlrpc.client as xmlrpcclient


class StartException(Exception):
pass


def get_ros_home():
'''
Returns the ROS HOME depending on ROS distribution API.
:return: ROS HOME path
:rtype: str
'''
try:
import rospkg.distro
distro = rospkg.distro.current_distro_codename()
if distro in ['electric', 'diamondback', 'cturtle']:
import roslib.rosenv
return roslib.rosenv.get_ros_home()
else:
from rospkg import get_ros_home
return get_ros_home()
except Exception:
from roslib import rosenv
return rosenv.get_ros_home()


def _get_optparse():
Expand Down Expand Up @@ -116,7 +138,7 @@ def main(argv=sys.argv):
logfile = screen.get_logfile(node=options['show_screen_log'])
if not os.path.isfile(logfile):
raise Exception('screen logfile not found for: %s' % options['show_screen_log'])
cmd = ' '.join([Settings.LOG_VIEWER, str(logfile)])
cmd = ' '.join([LOG_VIEWER, str(logfile)])
print(cmd)
p = subprocess.Popen(shlex.split(cmd))
p.wait()
Expand All @@ -134,7 +156,7 @@ def main(argv=sys.argv):
logfile = screen.get_ros_logfile(node=options['show_ros_log'])
if not os.path.isfile(logfile):
raise Exception('ros logfile not found for: %s' % options['show_ros_log'])
cmd = ' '.join([Settings.LOG_VIEWER, str(logfile)])
cmd = ' '.join([LOG_VIEWER, str(logfile)])
print(cmd)
p = subprocess.Popen(shlex.split(cmd))
p.wait()
Expand Down Expand Up @@ -201,14 +223,62 @@ def remove_src_binary(cmdlist):
return result


def _prepareROSMaster(masteruri):
if not masteruri:
masteruri = masteruri_from_ros()
# start roscore, if needed
try:
if not os.path.isdir(screen.LOG_PATH):
os.makedirs(screen.LOG_PATH)
socket.setdefaulttimeout(3)
master = xmlrpcclient.ServerProxy(masteruri)
master.getUri(rospy.get_name())
except Exception:
# run a roscore
screen.test_screen()
master_host = get_hostname(masteruri)
if nmdhost.is_local(master_host, True):
print("Start ROS-Master with %s ..." % masteruri)
master_port = get_port(masteruri)
new_env = dict(os.environ)
new_env['ROS_MASTER_URI'] = masteruri
ros_hostname = nmdhost.get_ros_hostname(masteruri)
if ros_hostname:
new_env['ROS_HOSTNAME'] = ros_hostname
cmd_args = '%s roscore --port %d' % (screen.get_cmd('/roscore--%d' % master_port), master_port)
try:
subprocess.Popen(shlex.split(cmd_args), env=new_env)
# wait for roscore to avoid connection problems while init_node
result = -1
count = 1
while result == -1 and count < 11:
try:
print(" retry connect to ROS master %d/10" % count)
master = xmlrpcclient.ServerProxy(masteruri)
result, _, _ = master.getUri(rospy.get_name()) # _:=uri, msg
except Exception:
time.sleep(1)
count += 1
if count >= 11:
raise StartException('Cannot connect to the ROS-Master: ' + str(masteruri))
except Exception as e:
import sys
sys.stderr.write("%s\n" % e)
raise
else:
raise Exception("ROS master '%s' is not reachable" % masteruri)
finally:
socket.setdefaulttimeout(None)


def runNode(package, executable, name, args, prefix='', repawn=False, masteruri=None, loglevel=''):
'''
Runs a ROS node. Starts a roscore if needed.
'''
if not masteruri:
masteruri = masteruri_from_ros()
# start roscore, if needed
StartHandler._prepareROSMaster(masteruri)
_prepareROSMaster(masteruri)
# start node
try:
cmd = roslib.packages.find_node(package, executable)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
GRPC_TIMEOUT = 15.0
''':var GRPC_TIMEOUT: timeout for connection to remote gRPC-server'''

RESPAWN_SCRIPT = 'rosrun fkie_node_manager respawn'
LOG_VIEWER = "/usr/bin/less -fKLnQrSU"

RESPAWN_SCRIPT = 'rosrun fkie_node_manager_daemon respawn'
''':var RESPAWN_SCRIPT: start prefix to launch ROS-Nodes with respawn script'''

LOG_PATH = ''.join([os.environ.get('ROS_LOG_DIR'), os.path.sep]) if os.environ.get('ROS_LOG_DIR') else os.path.join(os.path.expanduser('~'), '.ros/log/')
Expand Down

0 comments on commit 398d97a

Please sign in to comment.