SlaveServer.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.server;
00018 
00019 import com.google.common.collect.Lists;
00020 
00021 import org.ros.address.AdvertiseAddress;
00022 import org.ros.address.BindAddress;
00023 import org.ros.internal.node.client.MasterClient;
00024 import org.ros.internal.node.parameter.ParameterManager;
00025 import org.ros.internal.node.service.ServiceManager;
00026 import org.ros.internal.node.topic.DefaultPublisher;
00027 import org.ros.internal.node.topic.DefaultSubscriber;
00028 import org.ros.internal.node.topic.PublisherIdentifier;
00029 import org.ros.internal.node.topic.SubscriberIdentifier;
00030 import org.ros.internal.node.topic.TopicDeclaration;
00031 import org.ros.internal.node.topic.TopicParticipantManager;
00032 import org.ros.internal.node.xmlrpc.SlaveXmlRpcEndpointImpl;
00033 import org.ros.internal.system.Process;
00034 import org.ros.internal.transport.ProtocolDescription;
00035 import org.ros.internal.transport.ProtocolNames;
00036 import org.ros.internal.transport.tcp.TcpRosProtocolDescription;
00037 import org.ros.internal.transport.tcp.TcpRosServer;
00038 import org.ros.namespace.GraphName;
00039 
00040 import java.net.URI;
00041 import java.util.Collection;
00042 import java.util.List;
00043 import java.util.concurrent.ScheduledExecutorService;
00044 
00048 public class SlaveServer extends XmlRpcServer {
00049 
00050   private final GraphName nodeName;
00051   private final MasterClient masterClient;
00052   private final TopicParticipantManager topicParticipantManager;
00053   private final ParameterManager parameterManager;
00054   private final TcpRosServer tcpRosServer;
00055 
00056   public SlaveServer(GraphName nodeName, BindAddress tcpRosBindAddress,
00057       AdvertiseAddress tcpRosAdvertiseAddress, BindAddress xmlRpcBindAddress,
00058       AdvertiseAddress xmlRpcAdvertiseAddress, MasterClient master,
00059       TopicParticipantManager topicParticipantManager, ServiceManager serviceManager,
00060       ParameterManager parameterManager, ScheduledExecutorService executorService) {
00061     super(xmlRpcBindAddress, xmlRpcAdvertiseAddress);
00062     this.nodeName = nodeName;
00063     this.masterClient = master;
00064     this.topicParticipantManager = topicParticipantManager;
00065     this.parameterManager = parameterManager;
00066     this.tcpRosServer =
00067         new TcpRosServer(tcpRosBindAddress, tcpRosAdvertiseAddress, topicParticipantManager,
00068             serviceManager, executorService);
00069   }
00070 
00071   public AdvertiseAddress getTcpRosAdvertiseAddress() {
00072     return tcpRosServer.getAdvertiseAddress();
00073   }
00074 
00080   public void start() {
00081     super.start(org.ros.internal.node.xmlrpc.SlaveXmlRpcEndpointImpl.class,
00082         new SlaveXmlRpcEndpointImpl(this));
00083     tcpRosServer.start();
00084   }
00085 
00086   // TODO(damonkohler): This should also shut down the Node.
00087   @Override
00088   public void shutdown() {
00089     super.shutdown();
00090     tcpRosServer.shutdown();
00091   }
00092 
00093   public List<Object> getBusStats(String callerId) {
00094     throw new UnsupportedOperationException();
00095   }
00096 
00097   public List<Object> getBusInfo(String callerId) {
00098     List<Object> busInfo = Lists.newArrayList();
00099     // The connection ID field is opaque to the user. A monotonically increasing
00100     // integer for now is sufficient.
00101     int id = 0;
00102     for (DefaultPublisher<?> publisher : getPublications()) {
00103       for (SubscriberIdentifier subscriberIdentifier : topicParticipantManager
00104           .getPublisherConnections(publisher)) {
00105         List<String> publisherBusInfo = Lists.newArrayList();
00106         publisherBusInfo.add(Integer.toString(id));
00107         publisherBusInfo.add(subscriberIdentifier.getNodeIdentifier().getName().toString());
00108         // TODO(damonkohler): Pull out BusInfo constants.
00109         publisherBusInfo.add("o");
00110         // TODO(damonkohler): Add getter for protocol to topic participants.
00111         publisherBusInfo.add(ProtocolNames.TCPROS);
00112         publisherBusInfo.add(publisher.getTopicName().toString());
00113         busInfo.add(publisherBusInfo);
00114         id++;
00115       }
00116     }
00117     for (DefaultSubscriber<?> subscriber : getSubscriptions()) {
00118       for (PublisherIdentifier publisherIdentifer : topicParticipantManager
00119           .getSubscriberConnections(subscriber)) {
00120         List<String> subscriberBusInfo = Lists.newArrayList();
00121         subscriberBusInfo.add(Integer.toString(id));
00122         // Subscriber connection PublisherIdentifiers are populated with node
00123         // URIs instead of names. As a result, the only identifier information
00124         // available is the URI.
00125         subscriberBusInfo.add(publisherIdentifer.getNodeIdentifier().getUri().toString());
00126         // TODO(damonkohler): Pull out BusInfo constants.
00127         subscriberBusInfo.add("i");
00128         // TODO(damonkohler): Add getter for protocol to topic participants.
00129         subscriberBusInfo.add(ProtocolNames.TCPROS);
00130         subscriberBusInfo.add(publisherIdentifer.getTopicName().toString());
00131         busInfo.add(subscriberBusInfo);
00132         id++;
00133       }
00134     }
00135     return busInfo;
00136   }
00137 
00138   public URI getMasterUri() {
00139     return masterClient.getRemoteUri();
00140   }
00141 
00146   @Override
00147   public int getPid() {
00148     return Process.getPid();
00149   }
00150 
00151   public Collection<DefaultSubscriber<?>> getSubscriptions() {
00152     return topicParticipantManager.getSubscribers();
00153   }
00154 
00155   public Collection<DefaultPublisher<?>> getPublications() {
00156     return topicParticipantManager.getPublishers();
00157   }
00158 
00164   public int paramUpdate(GraphName parameterName, Object parameterValue) {
00165     return parameterManager.updateParameter(parameterName, parameterValue);
00166   }
00167 
00168   public void publisherUpdate(String callerId, String topicName, Collection<URI> publisherUris) {
00169     GraphName graphName = GraphName.of(topicName);
00170     if (topicParticipantManager.hasSubscriber(graphName)) {
00171       DefaultSubscriber<?> subscriber = topicParticipantManager.getSubscriber(graphName);
00172       TopicDeclaration topicDeclaration = subscriber.getTopicDeclaration();
00173       Collection<PublisherIdentifier> identifiers =
00174           PublisherIdentifier.newCollectionFromUris(publisherUris, topicDeclaration);
00175       subscriber.updatePublishers(identifiers);
00176     }
00177   }
00178 
00179   public ProtocolDescription requestTopic(String topicName, Collection<String> protocols)
00180       throws ServerException {
00181     // TODO(damonkohler): Use NameResolver.
00182     // Canonicalize topic name.
00183     GraphName graphName = GraphName.of(topicName).toGlobal();
00184     if (!topicParticipantManager.hasPublisher(graphName)) {
00185       throw new ServerException("No publishers for topic: " + graphName);
00186     }
00187     for (String protocol : protocols) {
00188       if (protocol.equals(ProtocolNames.TCPROS)) {
00189         try {
00190           return new TcpRosProtocolDescription(tcpRosServer.getAdvertiseAddress());
00191         } catch (Exception e) {
00192           throw new ServerException(e);
00193         }
00194       }
00195     }
00196     throw new ServerException("No supported protocols specified.");
00197   }
00198 
00202   public NodeIdentifier toNodeIdentifier() {
00203     return new NodeIdentifier(nodeName, getUri());
00204   }
00205 }


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