Skip to content
Snippets Groups Projects
Commit 0155d7d2 authored by Damon Kohler's avatar Damon Kohler
Browse files

Change NodeMain to be a pure interface (more flexible).

Fix surrounding code to work with the new NodeMain.
parent 3974787b
No related branches found
No related tags found
No related merge requests found
Showing
with 77 additions and 38 deletions
......@@ -141,7 +141,7 @@ public class ActionClient<T_ACTION_FEEDBACK extends Message, T_ACTION_GOAL exten
throws RosException {
this.nodeName = name;
this.spec = spec;
this.publisherListener = new CountDownPublisherListener(2, 2, 2);
this.publisherListener = new CountDownPublisherListener(2, 2, 2, 2);
}
/**
......@@ -319,7 +319,7 @@ public class ActionClient<T_ACTION_FEEDBACK extends Message, T_ACTION_GOAL exten
* established)
*/
public void waitForActionServerToStart() throws InterruptedException {
publisherListener.awaitRemoteConnection();
publisherListener.awaitNewSubscriber();
}
/**
......@@ -341,7 +341,7 @@ public class ActionClient<T_ACTION_FEEDBACK extends Message, T_ACTION_GOAL exten
// TODO(keith): This isn't quite right since it will only have connection to
// the goal and cancel publishers. This should be extended to include the
// subscribers.
return publisherListener.awaitRemoteConnection(timeout, units);
return publisherListener.awaitNewSubscriber(timeout, units);
}
/**
......
......@@ -41,10 +41,14 @@ public class RunFibonacciSimpleActionClient {
runner.run(new NodeMain() {
@Override
public void onNodeCreate(Node node) {
public void onStart(Node node) {
sac.addClientPubSub(node);
}
@Override
public void onShutdown(Node node) {
}
}, configuration);
System.out.println("[Test] Waiting for action server to start");
......@@ -112,10 +116,14 @@ public class RunFibonacciSimpleActionClient {
runner.run(new NodeMain() {
@Override
public void onNodeCreate(Node node) {
public void onStart(Node node) {
sac.addClientPubSub(node);
}
@Override
public void onShutdown(Node node) {
}
}, configuration);
System.out.println("[Test] Waiting for action server to start");
......
package org.ros.actionlib.example;
import org.ros.node.Node;
import org.ros.node.DefaultNodeRunner;
import org.ros.node.Node;
import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMain;
import org.ros.node.NodeRunner;
......@@ -32,10 +31,14 @@ public class RunFibonacciSimpleActionServer {
runner.run(new NodeMain() {
@Override
public void onNodeCreate(Node node) {
public void onStart(Node node) {
sas.addClientPubSub(node);
}
@Override
public void onShutdown(Node node) {
}
}, configuration);
} catch (Exception e) {
// TODO Auto-generated catch block
......
......@@ -17,8 +17,11 @@
package org.ros.node;
import com.google.common.base.Preconditions;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
......@@ -45,6 +48,7 @@ public class DefaultNodeRunner implements NodeRunner {
private final NodeFactory nodeFactory;
private final ExecutorService executorService;
private final Multimap<GraphName, Node> nodes;
private final BiMap<Node, NodeMain> nodeMains;
/**
* @return an instance of {@link DefaultNodeRunner} that uses a default
......@@ -72,6 +76,7 @@ public class DefaultNodeRunner implements NodeRunner {
this.nodeFactory = nodeFactory;
this.executorService = executorService;
nodes = Multimaps.synchronizedMultimap(HashMultimap.<GraphName, Node>create());
nodeMains = Maps.synchronizedBiMap(HashBiMap.<Node, NodeMain>create());
}
@Override
......@@ -94,7 +99,8 @@ public class DefaultNodeRunner implements NodeRunner {
nodeListenersCopy.addAll(nodeListeners);
}
// The new Node will call onStart().
nodeFactory.newNode(nodeConfigurationCopy, nodeListenersCopy);
Node node = nodeFactory.newNode(nodeConfigurationCopy, nodeListenersCopy);
nodeMains.put(node, nodeMain);
}
});
}
......@@ -104,6 +110,14 @@ public class DefaultNodeRunner implements NodeRunner {
run(nodeMain, nodeConfiguration, null);
}
@Override
public void shutdownNodeMain(NodeMain nodeMain) {
Node node = nodeMains.inverse().get(nodeMain);
if (node != null) {
safelyShutdownNode(node);
}
}
@Override
public void shutdown() {
synchronized (nodes) {
......@@ -161,6 +175,7 @@ public class DefaultNodeRunner implements NodeRunner {
*/
private void unregisterNode(Node node) {
nodes.get(node.getName()).remove(node);
nodeMains.remove(node);
}
private class RegistrationListener implements NodeListener {
......
......@@ -16,22 +16,18 @@
package org.ros.node;
import org.ros.node.topic.Publisher;
import org.ros.node.topic.Subscriber;
/**
* The one required {@link NodeListener} for node creation.
*
* <p>
* {@link NodeListener#onStart(Node)} should be used to initialize a node.
* {@link NodeListener#onStart(Node)} should be used to set up your program's
* {@link Publisher}s, {@link Subscriber}s, and services.
*
* @author ethan.rublee@gmail.com (Ethan Rublee)
* @author damonkohler@google.com (Damon Kohler)
*/
public abstract class NodeMain implements NodeListener {
/**
* Program shutdown routine.
*/
@Override
public void onShutdown(Node node) {
// Default is to do nothing.
}
public interface NodeMain extends NodeListener {
}
......@@ -17,6 +17,7 @@
package org.ros.node;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
/**
* Executes {@link NodeMain}s.
......@@ -54,7 +55,22 @@ public interface NodeRunner {
void run(NodeMain nodeMain, NodeConfiguration nodeConfiguration);
/**
* Shutdown all started {@link Node}s.
* Shuts down the supplied {@link NodeMain} (i.e.
* {@link NodeMain#onShutdown(Node)} will be called). This does not
* necessarily shut down the {@link Node} that is associated with the
* {@link NodeMain}.
*
* <p>
* This has no effect if the {@link NodeMain} has not started.
*
* @param nodeMain
* the {@link NodeMain} to shutdown
*/
void shutdownNodeMain(NodeMain nodeMain);
/**
* Shutdown all started {@link Node}s. This does not shut down the supplied
* {@link ExecutorService}.
*/
void shutdown();
}
......@@ -38,7 +38,7 @@ import java.util.Map;
*
* @author kwc@willowgarage.com (Ken Conley)
*/
public class ParameterServerTestNode extends NodeMain {
public class ParameterServerTestNode implements NodeMain {
@SuppressWarnings("rawtypes")
@Override
......@@ -121,4 +121,8 @@ public class ParameterServerTestNode extends NodeMain {
}
}
}
@Override
public void onShutdown(Node node) {
}
}
......@@ -27,7 +27,7 @@ import org.ros.node.topic.Publisher;
*
* @author kwc@willowgarage.com (Ken Conley)
*/
public class PassthroughTestNode extends NodeMain {
public class PassthroughTestNode implements NodeMain {
@Override
public void onStart(final Node node) {
......@@ -85,4 +85,8 @@ public class PassthroughTestNode extends NodeMain {
};
node.newSubscriber("composite_in", "test_ros/Composite", composite_cb);
}
@Override
public void onShutdown(Node node) {
}
}
......@@ -27,7 +27,7 @@ import org.ros.node.topic.Publisher;
*
* @author kwc@willowgarage.com (Ken Conley)
*/
public class SlaveApiTestNode extends NodeMain {
public class SlaveApiTestNode implements NodeMain {
@Override
public void onStart(Node node) {
......@@ -71,4 +71,8 @@ public class SlaveApiTestNode extends NodeMain {
}
}
}
@Override
public void onShutdown(Node node) {
}
}
......@@ -22,8 +22,6 @@ import org.ros.node.Node;
import org.ros.node.NodeMain;
import org.ros.node.topic.Subscriber;
import com.google.common.base.Preconditions;
/**
* This is a simple rosjava {@link Subscriber} {@link Node}. It assumes an
* external roscore is already running.
......@@ -32,12 +30,8 @@ import com.google.common.base.Preconditions;
*/
public class Listener implements NodeMain {
private Node node;
@Override
public void main(Node node) {
Preconditions.checkState(this.node == null);
this.node = node;
public void onStart(Node node) {
try {
final Log log = node.getLog();
node.newSubscriber("chatter", "std_msgs/String",
......@@ -57,9 +51,6 @@ public class Listener implements NodeMain {
}
@Override
public void shutdown() {
node.shutdown();
node = null;
public void onShutdown(Node node) {
}
}
......@@ -34,7 +34,7 @@ public class Talker implements NodeMain {
private Node node;
@Override
public void main(Node node) {
public void onStart(Node node) {
Preconditions.checkState(this.node == null);
this.node = node;
try {
......@@ -59,8 +59,6 @@ public class Talker implements NodeMain {
}
@Override
public void shutdown() {
node.shutdown();
node = null;
public void onShutdown(Node node) {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment