RosJava Tutorial Integration / Multiple Listeners
I have a problem with topic publisher/subscriber integration in RosJava when using the rosjava version of RosCore. I can execute the following test successfully where multiple subscribers attach to a publisher.
public void testOnePublisherToManySubscriber() throws InterruptedException {
nodeConfiguration.setNodeName("publisher");
Node publisherNode = nodeFactory.newNode(nodeConfiguration);
CountDownPublisherListener<org.ros.message.std_msgs.String> publisherListener =
CountDownPublisherListener.newDefault();
Publisher<org.ros.message.std_msgs.String> publisher =
publisherNode.newPublisher("foo", "std_msgs/String");
publisher.addListener(publisherListener);
final org.ros.message.std_msgs.String helloMessage = new org.ros.message.std_msgs.String();
helloMessage.data = "Hello, ROS!";
final CountDownLatch messageReceived = new CountDownLatch(1);
nodeConfiguration.setNodeName("subscriber");
Node subscriberNode = nodeFactory.newNode(nodeConfiguration);
CountDownSubscriberListener<org.ros.message.std_msgs.String> subscriberListener =
CountDownSubscriberListener.newDefault();
CountDownSubscriberListener<org.ros.message.std_msgs.String> subscriberListener2 =
CountDownSubscriberListener.newDefault();
Subscriber<org.ros.message.std_msgs.String> subscriber1 =
subscriberNode.newSubscriber("foo", "std_msgs/String");
subscriber1.addMessageListener(new MessageListener<org.ros.message.std_msgs.String>() {
@Override
public void onNewMessage(org.ros.message.std_msgs.String message) {
assertEquals(helloMessage, message);
messageReceived.countDown();
}
});
subscriber1.addSubscriberListener(subscriberListener);
Subscriber<org.ros.message.std_msgs.String> subscriber2 =
subscriberNode.newSubscriber("foo", "std_msgs/String");
subscriber2.addMessageListener(new MessageListener<org.ros.message.std_msgs.String>() {
@Override
public void onNewMessage(org.ros.message.std_msgs.String message) {
assertEquals(helloMessage, message);
messageReceived.countDown();
}
});
subscriber2.addSubscriberListener(subscriberListener2);
assertTrue(publisherListener.awaitMasterRegistrationSuccess(1, TimeUnit.SECONDS));
assertTrue(subscriberListener.awaitMasterRegistrationSuccess(1, TimeUnit.SECONDS));
RepeatingPublisher<org.ros.message.std_msgs.String> repeatingPublisher =
new RepeatingPublisher<org.ros.message.std_msgs.String>(publisher, helloMessage, 1000,
executorService);
repeatingPublisher.start();
assertTrue(messageReceived.await(1, TimeUnit.SECONDS));
repeatingPublisher.cancel();
publisher.shutdown();
subscriber1.shutdown();
subscriber2.shutdown();
}
However, if I fire up a RosCore on my own with something like:
public static void main(String[] argv){
RosCore core;
if(argv.length == 1)
core = newPublic(Integer.valueOf(argv[0]));
else
core = newPublic(11311);
core.start();
}
And then I use RosRun to execute the Talker node, it registers successfully. But then if I attempt to start multiple listeners an after the first fail. Subsequently... other pub/sub features fail to work properly. For example, starting a listener prior to a publisher. What am I doing wrong?
I'm executing the code in Eclipse on Windows, where I execute RosRun and then pass the package as an argument. After executing the second listener I get the following from RosCore:
Jan 17, 2012 5:02:11 PM org.ros.internal.node.server.MasterServer start
INFO: Starting master server.
Jan 17, 2012 5:02:20 PM org.ros.internal.node.server.MasterServer registerPublisher
INFO: Registering publisher: PublisherIdentifier<SlaveIdentifier</talker, http://127.0.0.1:55060>, TopicIdentifier</chatter>>
Jan 17, 2012 5:02:20 PM org.ros.internal.node.server.MasterServer publisherUpdate
INFO: Publisher update: /chatter
Jan 17, 2012 5:02:20 PM org.ros.internal.node.server.MasterServer registerPublisher
INFO: Registering publisher: PublisherIdentifier<SlaveIdentifier</talker, http://127.0.0.1:55060>, TopicIdentifier</rosout>>
Jan 17, 2012 5:02:20 PM org.ros.internal.node.server.MasterServer publisherUpdate
INFO: Publisher update: /rosout
Jan 17, 2012 5:02:26 PM org.ros.internal.node.server.MasterServer registerPublisher
INFO: Registering publisher: PublisherIdentifier<SlaveIdentifier</Listener1, http://127.0.0.1:55070>, TopicIdentifier</rosout>>
Jan ...