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.node; 00018 00019 import org.ros.internal.message.definition.MessageDefinitionReflectionProvider; 00020 00021 import org.ros.address.AdvertiseAddress; 00022 import org.ros.address.AdvertiseAddressFactory; 00023 import org.ros.address.BindAddress; 00024 import org.ros.address.PrivateAdvertiseAddressFactory; 00025 import org.ros.address.PublicAdvertiseAddressFactory; 00026 import org.ros.exception.RosRuntimeException; 00027 import org.ros.internal.message.DefaultMessageFactory; 00028 import org.ros.internal.message.DefaultMessageSerializationFactory; 00029 import org.ros.internal.message.service.ServiceDescriptionFactory; 00030 import org.ros.internal.message.service.ServiceRequestMessageFactory; 00031 import org.ros.internal.message.service.ServiceResponseMessageFactory; 00032 import org.ros.internal.message.topic.TopicDescriptionFactory; 00033 import org.ros.message.MessageDefinitionProvider; 00034 import org.ros.message.MessageFactory; 00035 import org.ros.message.MessageSerializationFactory; 00036 import org.ros.namespace.GraphName; 00037 import org.ros.namespace.NameResolver; 00038 import org.ros.time.TimeProvider; 00039 import org.ros.time.WallTimeProvider; 00040 00041 import java.io.File; 00042 import java.net.URI; 00043 import java.net.URISyntaxException; 00044 import java.util.List; 00045 import java.util.concurrent.ScheduledExecutorService; 00046 00057 public class NodeConfiguration { 00058 00062 public static final URI DEFAULT_MASTER_URI; 00063 00064 static { 00065 try { 00066 DEFAULT_MASTER_URI = new URI("http://localhost:11311/"); 00067 } catch (URISyntaxException e) { 00068 throw new RosRuntimeException(e); 00069 } 00070 } 00071 00072 private NameResolver parentResolver; 00073 private URI masterUri; 00074 private File rosRoot; 00075 private List<File> rosPackagePath; 00076 private GraphName nodeName; 00077 private TopicDescriptionFactory topicDescriptionFactory; 00078 private MessageFactory topicMessageFactory; 00079 private ServiceDescriptionFactory serviceDescriptionFactory; 00080 private MessageFactory serviceRequestMessageFactory; 00081 private MessageFactory serviceResponseMessageFactory; 00082 private MessageSerializationFactory messageSerializationFactory; 00083 private BindAddress tcpRosBindAddress; 00084 private AdvertiseAddressFactory tcpRosAdvertiseAddressFactory; 00085 private BindAddress xmlRpcBindAddress; 00086 private AdvertiseAddressFactory xmlRpcAdvertiseAddressFactory; 00087 private ScheduledExecutorService scheduledExecutorService; 00088 private TimeProvider timeProvider; 00089 00095 public static NodeConfiguration copyOf(NodeConfiguration nodeConfiguration) { 00096 NodeConfiguration copy = new NodeConfiguration(); 00097 copy.parentResolver = nodeConfiguration.parentResolver; 00098 copy.masterUri = nodeConfiguration.masterUri; 00099 copy.rosRoot = nodeConfiguration.rosRoot; 00100 copy.rosPackagePath = nodeConfiguration.rosPackagePath; 00101 copy.nodeName = nodeConfiguration.nodeName; 00102 copy.topicDescriptionFactory = nodeConfiguration.topicDescriptionFactory; 00103 copy.topicMessageFactory = nodeConfiguration.topicMessageFactory; 00104 copy.serviceDescriptionFactory = nodeConfiguration.serviceDescriptionFactory; 00105 copy.serviceRequestMessageFactory = nodeConfiguration.serviceRequestMessageFactory; 00106 copy.serviceResponseMessageFactory = nodeConfiguration.serviceResponseMessageFactory; 00107 copy.messageSerializationFactory = nodeConfiguration.messageSerializationFactory; 00108 copy.tcpRosBindAddress = nodeConfiguration.tcpRosBindAddress; 00109 copy.tcpRosAdvertiseAddressFactory = nodeConfiguration.tcpRosAdvertiseAddressFactory; 00110 copy.xmlRpcBindAddress = nodeConfiguration.xmlRpcBindAddress; 00111 copy.xmlRpcAdvertiseAddressFactory = nodeConfiguration.xmlRpcAdvertiseAddressFactory; 00112 copy.scheduledExecutorService = nodeConfiguration.scheduledExecutorService; 00113 copy.timeProvider = nodeConfiguration.timeProvider; 00114 return copy; 00115 } 00116 00129 public static NodeConfiguration newPublic(String host, URI masterUri) { 00130 NodeConfiguration configuration = new NodeConfiguration(); 00131 configuration.setXmlRpcBindAddress(BindAddress.newPublic()); 00132 configuration.setXmlRpcAdvertiseAddressFactory(new PublicAdvertiseAddressFactory(host)); 00133 configuration.setTcpRosBindAddress(BindAddress.newPublic()); 00134 configuration.setTcpRosAdvertiseAddressFactory(new PublicAdvertiseAddressFactory(host)); 00135 configuration.setMasterUri(masterUri); 00136 return configuration; 00137 } 00138 00148 public static NodeConfiguration newPublic(String host) { 00149 return newPublic(host, DEFAULT_MASTER_URI); 00150 } 00151 00161 public static NodeConfiguration newPrivate(URI masterUri) { 00162 NodeConfiguration configuration = new NodeConfiguration(); 00163 configuration.setXmlRpcBindAddress(BindAddress.newPrivate()); 00164 configuration.setXmlRpcAdvertiseAddressFactory(new PrivateAdvertiseAddressFactory()); 00165 configuration.setTcpRosBindAddress(BindAddress.newPrivate()); 00166 configuration.setTcpRosAdvertiseAddressFactory(new PrivateAdvertiseAddressFactory()); 00167 configuration.setMasterUri(masterUri); 00168 return configuration; 00169 } 00170 00177 public static NodeConfiguration newPrivate() { 00178 return newPrivate(DEFAULT_MASTER_URI); 00179 } 00180 00181 private NodeConfiguration() { 00182 MessageDefinitionProvider messageDefinitionProvider = new MessageDefinitionReflectionProvider(); 00183 setTopicDescriptionFactory(new TopicDescriptionFactory(messageDefinitionProvider)); 00184 setTopicMessageFactory(new DefaultMessageFactory(messageDefinitionProvider)); 00185 setServiceDescriptionFactory(new ServiceDescriptionFactory(messageDefinitionProvider)); 00186 setServiceRequestMessageFactory(new ServiceRequestMessageFactory(messageDefinitionProvider)); 00187 setServiceResponseMessageFactory(new ServiceResponseMessageFactory(messageDefinitionProvider)); 00188 setMessageSerializationFactory(new DefaultMessageSerializationFactory(messageDefinitionProvider)); 00189 setParentResolver(NameResolver.newRoot()); 00190 setTimeProvider(new WallTimeProvider()); 00191 } 00192 00196 public NameResolver getParentResolver() { 00197 return parentResolver; 00198 } 00199 00205 public NodeConfiguration setParentResolver(NameResolver resolver) { 00206 this.parentResolver = resolver; 00207 return this; 00208 } 00209 00217 public URI getMasterUri() { 00218 return masterUri; 00219 } 00220 00230 public NodeConfiguration setMasterUri(URI masterUri) { 00231 this.masterUri = masterUri; 00232 return this; 00233 } 00234 00241 public File getRosRoot() { 00242 return rosRoot; 00243 } 00244 00253 public NodeConfiguration setRosRoot(File rosRoot) { 00254 this.rosRoot = rosRoot; 00255 return this; 00256 } 00257 00269 public List<File> getRosPackagePath() { 00270 return rosPackagePath; 00271 } 00272 00286 public NodeConfiguration setRosPackagePath(List<File> rosPackagePath) { 00287 this.rosPackagePath = rosPackagePath; 00288 return this; 00289 } 00290 00294 public GraphName getNodeName() { 00295 return nodeName; 00296 } 00297 00303 public NodeConfiguration setNodeName(GraphName nodeName) { 00304 this.nodeName = nodeName; 00305 return this; 00306 } 00307 00313 public NodeConfiguration setNodeName(String nodeName) { 00314 return setNodeName(GraphName.of(nodeName)); 00315 } 00316 00324 public NodeConfiguration setDefaultNodeName(GraphName nodeName) { 00325 if (this.nodeName == null) { 00326 setNodeName(nodeName); 00327 } 00328 return this; 00329 } 00330 00338 public NodeConfiguration setDefaultNodeName(String nodeName) { 00339 return setDefaultNodeName(GraphName.of(nodeName)); 00340 } 00341 00345 public MessageSerializationFactory getMessageSerializationFactory() { 00346 return messageSerializationFactory; 00347 } 00348 00354 public NodeConfiguration setMessageSerializationFactory( 00355 MessageSerializationFactory messageSerializationFactory) { 00356 this.messageSerializationFactory = messageSerializationFactory; 00357 return this; 00358 } 00359 00365 public NodeConfiguration setTopicMessageFactory(MessageFactory topicMessageFactory) { 00366 this.topicMessageFactory = topicMessageFactory; 00367 return this; 00368 } 00369 00370 public MessageFactory getTopicMessageFactory() { 00371 return topicMessageFactory; 00372 } 00373 00379 public NodeConfiguration setServiceRequestMessageFactory( 00380 ServiceRequestMessageFactory serviceRequestMessageFactory) { 00381 this.serviceRequestMessageFactory = serviceRequestMessageFactory; 00382 return this; 00383 } 00384 00385 public MessageFactory getServiceRequestMessageFactory() { 00386 return serviceRequestMessageFactory; 00387 } 00388 00394 public NodeConfiguration setServiceResponseMessageFactory( 00395 ServiceResponseMessageFactory serviceResponseMessageFactory) { 00396 this.serviceResponseMessageFactory = serviceResponseMessageFactory; 00397 return this; 00398 } 00399 00400 public MessageFactory getServiceResponseMessageFactory() { 00401 return serviceResponseMessageFactory; 00402 } 00403 00409 public NodeConfiguration setTopicDescriptionFactory( 00410 TopicDescriptionFactory topicDescriptionFactory) { 00411 this.topicDescriptionFactory = topicDescriptionFactory; 00412 return this; 00413 } 00414 00415 public TopicDescriptionFactory getTopicDescriptionFactory() { 00416 return topicDescriptionFactory; 00417 } 00418 00424 public NodeConfiguration setServiceDescriptionFactory( 00425 ServiceDescriptionFactory serviceDescriptionFactory) { 00426 this.serviceDescriptionFactory = serviceDescriptionFactory; 00427 return this; 00428 } 00429 00430 public ServiceDescriptionFactory getServiceDescriptionFactory() { 00431 return serviceDescriptionFactory; 00432 } 00433 00439 public BindAddress getTcpRosBindAddress() { 00440 return tcpRosBindAddress; 00441 } 00442 00449 public NodeConfiguration setTcpRosBindAddress(BindAddress tcpRosBindAddress) { 00450 this.tcpRosBindAddress = tcpRosBindAddress; 00451 return this; 00452 } 00453 00460 public AdvertiseAddressFactory getTcpRosAdvertiseAddressFactory() { 00461 return tcpRosAdvertiseAddressFactory; 00462 } 00463 00472 public NodeConfiguration setTcpRosAdvertiseAddressFactory( 00473 AdvertiseAddressFactory tcpRosAdvertiseAddressFactory) { 00474 this.tcpRosAdvertiseAddressFactory = tcpRosAdvertiseAddressFactory; 00475 return this; 00476 } 00477 00483 public AdvertiseAddress getTcpRosAdvertiseAddress() { 00484 return tcpRosAdvertiseAddressFactory.newDefault(); 00485 } 00486 00493 public BindAddress getXmlRpcBindAddress() { 00494 return xmlRpcBindAddress; 00495 } 00496 00504 public NodeConfiguration setXmlRpcBindAddress(BindAddress xmlRpcBindAddress) { 00505 this.xmlRpcBindAddress = xmlRpcBindAddress; 00506 return this; 00507 } 00508 00515 public AdvertiseAddress getXmlRpcAdvertiseAddress() { 00516 return xmlRpcAdvertiseAddressFactory.newDefault(); 00517 } 00518 00526 public AdvertiseAddressFactory getXmlRpcAdvertiseAddressFactory() { 00527 return xmlRpcAdvertiseAddressFactory; 00528 } 00529 00538 public NodeConfiguration setXmlRpcAdvertiseAddressFactory( 00539 AdvertiseAddressFactory xmlRpcAdvertiseAddressFactory) { 00540 this.xmlRpcAdvertiseAddressFactory = xmlRpcAdvertiseAddressFactory; 00541 return this; 00542 } 00543 00547 public TimeProvider getTimeProvider() { 00548 return timeProvider; 00549 } 00550 00558 public NodeConfiguration setTimeProvider(TimeProvider timeProvider) { 00559 this.timeProvider = timeProvider; 00560 return this; 00561 } 00562 }