DefaultNodeTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.node;
00018 
00019 import static org.junit.Assert.assertEquals;
00020 import static org.junit.Assert.assertFalse;
00021 import static org.junit.Assert.assertTrue;
00022 import static org.ros.Assert.assertGraphNameEquals;
00023 
00024 import com.google.common.collect.Lists;
00025 import com.google.common.net.InetAddresses;
00026 
00027 import org.junit.Test;
00028 import org.ros.RosCore;
00029 import org.ros.RosTest;
00030 import org.ros.concurrent.Holder;
00031 import org.ros.internal.node.client.SlaveClient;
00032 import org.ros.internal.node.response.Response;
00033 import org.ros.internal.node.server.master.MasterServer;
00034 import org.ros.internal.transport.ProtocolDescription;
00035 import org.ros.internal.transport.ProtocolNames;
00036 import org.ros.namespace.GraphName;
00037 import org.ros.namespace.NameResolver;
00038 import org.ros.node.AbstractNodeMain;
00039 import org.ros.node.ConnectedNode;
00040 import org.ros.node.NodeConfiguration;
00041 import org.ros.node.NodeMain;
00042 import org.ros.node.topic.CountDownPublisherListener;
00043 import org.ros.node.topic.CountDownSubscriberListener;
00044 import org.ros.node.topic.Publisher;
00045 import org.ros.node.topic.Subscriber;
00046 
00047 import java.net.InetAddress;
00048 import java.net.InetSocketAddress;
00049 import java.net.URI;
00050 import java.util.List;
00051 import java.util.concurrent.TimeUnit;
00052 
00059 public class DefaultNodeTest extends RosTest {
00060 
00061   void checkHostName(String hostName) {
00062     assertTrue(!hostName.equals("0.0.0.0"));
00063     assertTrue(!hostName.equals("0:0:0:0:0:0:0:0"));
00064   }
00065 
00066   private void checkNodeAddress(final String host) throws InterruptedException {
00067     final Holder<InetSocketAddress> holder = Holder.newEmpty();
00068     NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(host, rosCore.getUri());
00069     nodeMainExecutor.execute(new AbstractNodeMain() {
00070       @Override
00071       public GraphName getDefaultNodeName() {
00072         return GraphName.of("node");
00073       }
00074 
00075       @Override
00076       public void onStart(ConnectedNode connectedNode) {
00077         holder.set(((DefaultNode) connectedNode).getAddress());
00078       }
00079     }, nodeConfiguration);
00080 
00081     assertTrue(holder.await(1, TimeUnit.SECONDS));
00082     assertTrue(holder.get().getPort() > 0);
00083     assertEquals(holder.get().getHostName(), host);
00084   }
00085 
00086   @Test
00087   public void testCreatePublic() throws Exception {
00088     String host = InetAddress.getLocalHost().getCanonicalHostName();
00089     assertFalse(InetAddresses.isInetAddress(host));
00090     checkNodeAddress(host);
00091   }
00092 
00093   @Test
00094   public void testCreatePublicWithIpv4() throws InterruptedException {
00095     String host = "1.2.3.4";
00096     checkNodeAddress(host);
00097   }
00098 
00099   @Test
00100   public void testCreatePublicWithIpv6() throws InterruptedException {
00101     String host = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
00102     checkNodeAddress(host);
00103   }
00104 
00105   @Test
00106   public void testCreatePrivate() throws InterruptedException {
00107     checkNodeAddress(nodeConfiguration.getTcpRosAdvertiseAddress().getHost());
00108   }
00109 
00110   @SuppressWarnings("unchecked")
00111   @Test
00112   public void testRegistration() throws InterruptedException {
00113     final CountDownPublisherListener<std_msgs.String> publisherListener =
00114         CountDownPublisherListener.newDefault();
00115     final CountDownSubscriberListener<std_msgs.String> subscriberListener =
00116         CountDownSubscriberListener.newDefault();
00117 
00118     NodeMain nodeMain = new AbstractNodeMain() {
00119       @Override
00120       public GraphName getDefaultNodeName() {
00121         return GraphName.of("node");
00122       }
00123 
00124       @Override
00125       public void onStart(ConnectedNode connectedNode) {
00126         Publisher<std_msgs.String> publisher =
00127             connectedNode.newPublisher("foo", std_msgs.String._TYPE);
00128         publisher.addListener(publisherListener);
00129         Subscriber<std_msgs.String> subscriber =
00130             connectedNode.newSubscriber("foo", std_msgs.String._TYPE);
00131         subscriber.addSubscriberListener(subscriberListener);
00132       }
00133     };
00134 
00135     nodeMainExecutor.execute(nodeMain, nodeConfiguration);
00136 
00137     assertTrue(publisherListener.awaitMasterRegistrationSuccess(1, TimeUnit.SECONDS));
00138     assertTrue(subscriberListener.awaitMasterRegistrationSuccess(1, TimeUnit.SECONDS));
00139 
00140     // There are now two registered publishers /rosout and /foo.
00141     List<Object> systemState = rosCore.getMasterServer().getSystemState();
00142     assertEquals(2, ((List<Object>) systemState.get(MasterServer.SYSTEM_STATE_PUBLISHERS)).size());
00143     assertEquals(1, ((List<Object>) systemState.get(MasterServer.SYSTEM_STATE_SUBSCRIBERS)).size());
00144 
00145     nodeMainExecutor.shutdownNodeMain(nodeMain);
00146 
00147     assertTrue(publisherListener.awaitShutdown(1, TimeUnit.SECONDS));
00148     assertTrue(subscriberListener.awaitShutdown(1, TimeUnit.SECONDS));
00149 
00150     systemState = rosCore.getMasterServer().getSystemState();
00151     assertEquals(0, ((List<Object>) systemState.get(MasterServer.SYSTEM_STATE_PUBLISHERS)).size());
00152     assertEquals(0, ((List<Object>) systemState.get(MasterServer.SYSTEM_STATE_SUBSCRIBERS)).size());
00153   }
00154 
00155   @Test
00156   public void testResolveName() throws InterruptedException {
00157     final Holder<ConnectedNode> holder = Holder.newEmpty();
00158     nodeConfiguration.setParentResolver(NameResolver.newFromNamespace("/ns1"));
00159     nodeMainExecutor.execute(new AbstractNodeMain() {
00160       @Override
00161       public GraphName getDefaultNodeName() {
00162         return GraphName.of("test_resolver");
00163       }
00164 
00165       @Override
00166       public void onStart(ConnectedNode connectedNode) {
00167         holder.set(connectedNode);
00168       }
00169     }, nodeConfiguration);
00170 
00171     assertTrue(holder.await(1, TimeUnit.SECONDS));
00172     ConnectedNode connectedNode = holder.get();
00173 
00174     assertGraphNameEquals("/foo", connectedNode.resolveName("/foo"));
00175     assertGraphNameEquals("/ns1/foo", connectedNode.resolveName("foo"));
00176     assertGraphNameEquals("/ns1/test_resolver/foo", connectedNode.resolveName("~foo"));
00177 
00178     Publisher<std_msgs.Int64> pub = connectedNode.newPublisher("pub", std_msgs.Int64._TYPE);
00179     assertGraphNameEquals("/ns1/pub", pub.getTopicName());
00180     pub = connectedNode.newPublisher("/pub", std_msgs.Int64._TYPE);
00181     assertGraphNameEquals("/pub", pub.getTopicName());
00182     pub = connectedNode.newPublisher("~pub", std_msgs.Int64._TYPE);
00183     assertGraphNameEquals("/ns1/test_resolver/pub", pub.getTopicName());
00184 
00185     Subscriber<std_msgs.Int64> sub = connectedNode.newSubscriber("sub", std_msgs.Int64._TYPE);
00186     assertGraphNameEquals("/ns1/sub", sub.getTopicName());
00187     sub = connectedNode.newSubscriber("/sub", std_msgs.Int64._TYPE);
00188     assertGraphNameEquals("/sub", sub.getTopicName());
00189     sub = connectedNode.newSubscriber("~sub", std_msgs.Int64._TYPE);
00190     assertGraphNameEquals("/ns1/test_resolver/sub", sub.getTopicName());
00191   }
00192 
00193   @Test
00194   public void testPublicAddresses() throws InterruptedException {
00195     RosCore rosCore = RosCore.newPublic();
00196     rosCore.start();
00197     assertTrue(rosCore.awaitStart(1, TimeUnit.SECONDS));
00198 
00199     URI masterUri = rosCore.getUri();
00200     checkHostName(masterUri.getHost());
00201 
00202     final Holder<ConnectedNode> holder = Holder.newEmpty();
00203     NodeConfiguration nodeConfiguration =
00204         NodeConfiguration.newPublic(masterUri.getHost(), masterUri);
00205     nodeMainExecutor.execute(new AbstractNodeMain() {
00206       @Override
00207       public GraphName getDefaultNodeName() {
00208         return GraphName.of("test_addresses");
00209       }
00210 
00211       @Override
00212       public void onStart(ConnectedNode connectedNode) {
00213         holder.set(connectedNode);
00214       };
00215     }, nodeConfiguration);
00216 
00217     assertTrue(holder.await(1, TimeUnit.SECONDS));
00218 
00219     ConnectedNode connectedNode = holder.get();
00220     URI nodeUri = connectedNode.getUri();
00221     assertTrue(nodeUri.getPort() > 0);
00222     checkHostName(nodeUri.getHost());
00223 
00224     CountDownPublisherListener<std_msgs.Int64> publisherListener =
00225         CountDownPublisherListener.newDefault();
00226     Publisher<std_msgs.Int64> publisher =
00227         connectedNode.newPublisher("test_addresses_pub", std_msgs.Int64._TYPE);
00228     publisher.addListener(publisherListener);
00229     assertTrue(publisherListener.awaitMasterRegistrationSuccess(1, TimeUnit.SECONDS));
00230 
00231     // Check the TCPROS server address via the XML-RPC API.
00232     SlaveClient slaveClient = new SlaveClient(GraphName.of("test_addresses"), nodeUri);
00233     Response<ProtocolDescription> response =
00234         slaveClient.requestTopic(GraphName.of("test_addresses_pub"),
00235             Lists.newArrayList(ProtocolNames.TCPROS));
00236     ProtocolDescription result = response.getResult();
00237     InetSocketAddress tcpRosAddress = result.getAdverstiseAddress().toInetSocketAddress();
00238     checkHostName(tcpRosAddress.getHostName());
00239   }
00240 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49