From e53cda3fa52e2e62290bff7c9789ec75f8b93156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20M=C5=82ynarczyk?= Date: Mon, 11 Feb 2019 13:03:23 +0100 Subject: [PATCH] rosnode kill support (shut down the node) --- .../java/org/ros/internal/node/DefaultNode.java | 2 +- .../ros/internal/node/server/SlaveServer.java | 16 +++++++++++++++- .../node/MasterSlaveIntegrationTest.java | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/rosjava/src/main/java/org/ros/internal/node/DefaultNode.java b/rosjava/src/main/java/org/ros/internal/node/DefaultNode.java index 9f1a1bdc1..ceab569b2 100644 --- a/rosjava/src/main/java/org/ros/internal/node/DefaultNode.java +++ b/rosjava/src/main/java/org/ros/internal/node/DefaultNode.java @@ -146,7 +146,7 @@ public DefaultNode(NodeConfiguration nodeConfiguration, Collection nodeConfiguration.getTcpRosAdvertiseAddress(), nodeConfiguration.getXmlRpcBindAddress(), nodeConfiguration.getXmlRpcAdvertiseAddress(), masterClient, topicParticipantManager, - serviceManager, parameterManager, scheduledExecutorService); + serviceManager, parameterManager, scheduledExecutorService, this); slaveServer.start(); NodeIdentifier nodeIdentifier = slaveServer.toNodeIdentifier(); diff --git a/rosjava/src/main/java/org/ros/internal/node/server/SlaveServer.java b/rosjava/src/main/java/org/ros/internal/node/server/SlaveServer.java index 34034e15c..83bd579bd 100644 --- a/rosjava/src/main/java/org/ros/internal/node/server/SlaveServer.java +++ b/rosjava/src/main/java/org/ros/internal/node/server/SlaveServer.java @@ -20,6 +20,7 @@ import org.ros.address.AdvertiseAddress; import org.ros.address.BindAddress; +import org.ros.internal.node.DefaultNode; import org.ros.internal.node.client.MasterClient; import org.ros.internal.node.parameter.ParameterManager; import org.ros.internal.node.service.ServiceManager; @@ -52,12 +53,15 @@ public class SlaveServer extends XmlRpcServer { private final TopicParticipantManager topicParticipantManager; private final ParameterManager parameterManager; private final TcpRosServer tcpRosServer; + private final DefaultNode defaultNode; + private boolean shutdownStarted; public SlaveServer(GraphName nodeName, BindAddress tcpRosBindAddress, AdvertiseAddress tcpRosAdvertiseAddress, BindAddress xmlRpcBindAddress, AdvertiseAddress xmlRpcAdvertiseAddress, MasterClient master, TopicParticipantManager topicParticipantManager, ServiceManager serviceManager, - ParameterManager parameterManager, ScheduledExecutorService executorService) { + ParameterManager parameterManager, ScheduledExecutorService executorService, + DefaultNode defaultNode) { super(xmlRpcBindAddress, xmlRpcAdvertiseAddress); this.nodeName = nodeName; this.masterClient = master; @@ -66,6 +70,7 @@ public SlaveServer(GraphName nodeName, BindAddress tcpRosBindAddress, this.tcpRosServer = new TcpRosServer(tcpRosBindAddress, tcpRosAdvertiseAddress, topicParticipantManager, serviceManager, executorService); + this.defaultNode = defaultNode; } public AdvertiseAddress getTcpRosAdvertiseAddress() { @@ -81,13 +86,22 @@ public void start() { super.start(org.ros.internal.node.xmlrpc.SlaveXmlRpcEndpointImpl.class, new SlaveXmlRpcEndpointImpl(this)); tcpRosServer.start(); + shutdownStarted = false; } // TODO(damonkohler): This should also shut down the Node. @Override public void shutdown() { + // prevent recursive call of this method + if (shutdownStarted) { + return; + } + shutdownStarted = true; super.shutdown(); tcpRosServer.shutdown(); + if (defaultNode != null) { + defaultNode.shutdown(); + } } public List getBusStats(String callerId) { diff --git a/rosjava/src/test/java/org/ros/internal/node/MasterSlaveIntegrationTest.java b/rosjava/src/test/java/org/ros/internal/node/MasterSlaveIntegrationTest.java index 895f33355..cdf6085bb 100644 --- a/rosjava/src/test/java/org/ros/internal/node/MasterSlaveIntegrationTest.java +++ b/rosjava/src/test/java/org/ros/internal/node/MasterSlaveIntegrationTest.java @@ -62,7 +62,7 @@ public void setUp() { new SlaveServer(GraphName.of("/foo"), BindAddress.newPrivate(), AdvertiseAddress.newPrivate(), BindAddress.newPrivate(), AdvertiseAddress.newPrivate(), masterClient, topicParticipantManager, serviceManager, parameterManager, - executorService); + executorService, null); slaveServer.start(); slaveClient = new SlaveClient(GraphName.of("/bar"), slaveServer.getUri()); }