SlaveXmlRpcEndpointImpl.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.xmlrpc;
00018 
00019 import com.google.common.collect.Lists;
00020 import com.google.common.collect.Sets;
00021 
00022 import org.apache.commons.logging.Log;
00023 import org.apache.commons.logging.LogFactory;
00024 import org.ros.internal.node.response.Response;
00025 import org.ros.internal.node.response.StatusCode;
00026 import org.ros.internal.node.server.ServerException;
00027 import org.ros.internal.node.server.SlaveServer;
00028 import org.ros.internal.node.topic.DefaultPublisher;
00029 import org.ros.internal.node.topic.DefaultSubscriber;
00030 import org.ros.internal.transport.ProtocolDescription;
00031 import org.ros.namespace.GraphName;
00032 
00033 import java.net.URI;
00034 import java.net.URISyntaxException;
00035 import java.util.ArrayList;
00036 import java.util.Collection;
00037 import java.util.List;
00038 import java.util.Map;
00039 import java.util.Set;
00040 import java.util.Vector;
00041 
00045 public class SlaveXmlRpcEndpointImpl implements SlaveXmlRpcEndpoint {
00046 
00047   private static final boolean DEBUG = false;
00048   private static final Log log = LogFactory.getLog(SlaveXmlRpcEndpointImpl.class);
00049 
00050   private final SlaveServer slave;
00051 
00052   public SlaveXmlRpcEndpointImpl(SlaveServer slave) {
00053     this.slave = slave;
00054   }
00055 
00056   @Override
00057   public List<Object> getBusStats(String callerId) {
00058     return slave.getBusStats(callerId);
00059   }
00060 
00061   @Override
00062   public List<Object> getBusInfo(String callerId) {
00063     List<Object> busInfo = slave.getBusInfo(callerId);
00064     return Response.newSuccess("bus info", busInfo).toList();
00065   }
00066 
00067   @Override
00068   public List<Object> getMasterUri(String callerId) {
00069     URI uri = slave.getMasterUri();
00070     return new Response<String>(StatusCode.SUCCESS, "", uri.toString()).toList();
00071   }
00072 
00073   @Override
00074   public List<Object> shutdown(String callerId, String message) {
00075     log.info("Shutdown requested by " + callerId + " with message \"" + message + "\"");
00076     slave.shutdown();
00077     return Response.newSuccess("Shutdown successful.", null).toList();
00078   }
00079 
00080   @Override
00081   public List<Object> getPid(String callerId) {
00082     try {
00083       int pid = slave.getPid();
00084       return Response.newSuccess("PID: " + pid, pid).toList();
00085     } catch (UnsupportedOperationException e) {
00086       return Response.newFailure("Cannot retrieve PID on this platform.", -1).toList();
00087     }
00088   }
00089 
00090   @Override
00091   public List<Object> getSubscriptions(String callerId) {
00092     Collection<DefaultSubscriber<?>> subscribers = slave.getSubscriptions();
00093     List<List<String>> subscriptions = Lists.newArrayList();
00094     for (DefaultSubscriber<?> subscriber : subscribers) {
00095       subscriptions.add(subscriber.getTopicDeclarationAsList());
00096     }
00097     return Response.newSuccess("Success", subscriptions).toList();
00098   }
00099 
00100   @Override
00101   public List<Object> getPublications(String callerId) {
00102     Collection<DefaultPublisher<?>> publishers = slave.getPublications();
00103     List<List<String>> publications = Lists.newArrayList();
00104     for (DefaultPublisher<?> publisher : publishers) {
00105       publications.add(publisher.getTopicDeclarationAsList());
00106     }
00107     return Response.newSuccess("Success", publications).toList();
00108   }
00109 
00110   private List<Object> parameterUpdate(String parameterName, Object parameterValue) {
00111     if (slave.paramUpdate(GraphName.of(parameterName), parameterValue) > 0) {
00112       return Response.newSuccess("Success", null).toList();
00113     }
00114     return Response
00115         .newError("No subscribers for parameter key \"" + parameterName + "\".", null).toList();
00116   }
00117 
00118   @Override
00119   public List<Object> paramUpdate(String callerId, String key, boolean value) {
00120     return parameterUpdate(key, value);
00121   }
00122 
00123   @Override
00124   public List<Object> paramUpdate(String callerId, String key, char value) {
00125     return parameterUpdate(key, value);
00126   }
00127 
00128   @Override
00129   public List<Object> paramUpdate(String callerId, String key, byte value) {
00130     return parameterUpdate(key, value);
00131   }
00132 
00133   @Override
00134   public List<Object> paramUpdate(String callerId, String key, short value) {
00135     return parameterUpdate(key, value);
00136   }
00137 
00138   @Override
00139   public List<Object> paramUpdate(String callerId, String key, int value) {
00140     return parameterUpdate(key, value);
00141   }
00142 
00143   @Override
00144   public List<Object> paramUpdate(String callerId, String key, double value) {
00145     return parameterUpdate(key, value);
00146   }
00147 
00148   @Override
00149   public List<Object> paramUpdate(String callerId, String key, String value) {
00150     return parameterUpdate(key, value);
00151   }
00152 
00153   @Override
00154   public List<Object> paramUpdate(String callerId, String key, List<?> value) {
00155     return parameterUpdate(key, value);
00156   }
00157 
00158   @Override
00159   public List<Object> paramUpdate(String callerId, String key, Vector<?> value) {
00160     return parameterUpdate(key, value);
00161   }
00162 
00163   @Override
00164   public List<Object> paramUpdate(String callerId, String key, Map<?, ?> value) {
00165     return parameterUpdate(key, value);
00166   }
00167 
00168   @Override
00169   public List<Object> publisherUpdate(String callerId, String topicName, Object[] publishers) {
00170     try {
00171       ArrayList<URI> publisherUris = new ArrayList<URI>(publishers.length);
00172       for (Object publisher : publishers) {
00173         URI uri = new URI(publisher.toString());
00174         if (!uri.getScheme().equals("http") && !uri.getScheme().equals("https")) {
00175           return Response.newError("Unknown URI scheme sent in update.", 0).toList();
00176         }
00177         publisherUris.add(uri);
00178       }
00179       slave.publisherUpdate(callerId, topicName, publisherUris);
00180       return Response.newSuccess("Publisher update received.", 0).toList();
00181     } catch (URISyntaxException e) {
00182       return Response.newError("Invalid URI sent in update.", 0).toList();
00183     }
00184   }
00185 
00186   @Override
00187   public List<Object> requestTopic(String callerId, String topic, Object[] protocols) {
00188     Set<String> requestedProtocols = Sets.newHashSet();
00189     for (int i = 0; i < protocols.length; i++) {
00190       requestedProtocols.add((String) ((Object[]) protocols[i])[0]);
00191     }
00192     ProtocolDescription protocol;
00193     try {
00194       protocol = slave.requestTopic(topic, requestedProtocols);
00195     } catch (ServerException e) {
00196       return Response.newError(e.getMessage(), null).toList();
00197     }
00198     List<Object> response = Response.newSuccess(protocol.toString(), protocol.toList()).toList();
00199     if (DEBUG) {
00200       log.info("requestTopic(" + topic + ", " + requestedProtocols + ") response: "
00201           + response.toString());
00202     }
00203     return response;
00204   }
00205 }


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