MasterXmlRpcEndpointImpl.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 
00021 import org.ros.exception.RosRuntimeException;
00022 import org.ros.internal.node.response.Response;
00023 import org.ros.internal.node.server.NodeIdentifier;
00024 import org.ros.internal.node.server.ParameterServer;
00025 import org.ros.internal.node.server.master.MasterServer;
00026 import org.ros.namespace.GraphName;
00027 
00028 import java.net.URI;
00029 import java.net.URISyntaxException;
00030 import java.util.Collection;
00031 import java.util.HashMap;
00032 import java.util.List;
00033 import java.util.Map;
00034 
00040 public class MasterXmlRpcEndpointImpl implements MasterXmlRpcEndpoint,
00041     ParameterServerXmlRpcEndpoint {
00042 
00043   private final MasterServer master;
00044   private final ParameterServer parameterServer;
00045 
00046   public MasterXmlRpcEndpointImpl(MasterServer master) {
00047     this.master = master;
00048     parameterServer = new ParameterServer();
00049   }
00050 
00051   @Override
00052   public List<Object> getPid(String callerId) {
00053     return Response.newSuccess("server pid", master.getPid()).toList();
00054   }
00055 
00056   @Override
00057   public List<Object> getPublishedTopics(String callerId, String subgraph) {
00058     return Response.newSuccess("current topics",
00059         master.getPublishedTopics(GraphName.of(callerId), GraphName.of(subgraph))).toList();
00060   }
00061 
00062   @Override
00063   public List<Object> getTopicTypes(String callerId) {
00064     return Response.newSuccess("topic types", master.getTopicTypes(GraphName.of(callerId)))
00065         .toList();
00066   }
00067 
00068   @Override
00069   public List<Object> getSystemState(String callerId) {
00070     return Response.newSuccess("current system state", master.getSystemState()).toList();
00071   }
00072 
00073   @Override
00074   public List<Object> getUri(String callerId) {
00075     return Response.newSuccess("Success", master.getUri().toString()).toList();
00076   }
00077 
00078   @Override
00079   public List<Object> lookupNode(String callerId, String nodeName) {
00080     URI nodeSlaveUri = master.lookupNode(GraphName.of(nodeName));
00081     if (nodeSlaveUri != null) {
00082       return Response.newSuccess("Success", nodeSlaveUri.toString()).toList();
00083     } else {
00084       return Response.newError("No such node", null).toList();
00085     }
00086   }
00087 
00088   @Override
00089   public List<Object> registerPublisher(String callerId, String topicName, String topicMessageType,
00090       String callerSlaveUri) {
00091     try {
00092       List<URI> subscribers =
00093           master.registerPublisher(GraphName.of(callerId), new URI(callerSlaveUri), GraphName.of(
00094               topicName), topicMessageType);
00095       List<String> urls = Lists.newArrayList();
00096       for (URI uri : subscribers) {
00097         urls.add(uri.toString());
00098       }
00099       return Response.newSuccess("Success", urls).toList();
00100     } catch (URISyntaxException e) {
00101       throw new RosRuntimeException(String.format("Improperly formatted URI %s for publisher",
00102           callerSlaveUri), e);
00103     }
00104   }
00105 
00106   @Override
00107   public List<Object> unregisterPublisher(String callerId, String topicName, String callerSlaveUri) {
00108     boolean result = master.unregisterPublisher(GraphName.of(callerId), GraphName.of(topicName));
00109     return Response.newSuccess("Success", result ? 1 : 0).toList();
00110   }
00111 
00112   @Override
00113   public List<Object> registerSubscriber(String callerId, String topicName,
00114       String topicMessageType, String callerSlaveUri) {
00115     try {
00116       List<URI> publishers =
00117           master.registerSubscriber(GraphName.of(callerId), new URI(callerSlaveUri),
00118               GraphName.of(topicName), topicMessageType);
00119       List<String> urls = Lists.newArrayList();
00120       for (URI uri : publishers) {
00121         urls.add(uri.toString());
00122       }
00123       return Response.newSuccess("Success", urls).toList();
00124     } catch (URISyntaxException e) {
00125       throw new RosRuntimeException(String.format("Improperly formatted URI %s for subscriber",
00126           callerSlaveUri), e);
00127     }
00128   }
00129 
00130   @Override
00131   public List<Object>
00132       unregisterSubscriber(String callerId, String topicName, String callerSlaveUri) {
00133     boolean result = master.unregisterSubscriber(GraphName.of(callerId), GraphName.of(topicName));
00134     return Response.newSuccess("Success", result ? 1 : 0).toList();
00135   }
00136 
00137   @Override
00138   public List<Object> lookupService(String callerId, String serviceName) {
00139     URI slaveUri = master.lookupService(GraphName.of(serviceName));
00140     if (slaveUri != null) {
00141       return Response.newSuccess("Success", slaveUri.toString()).toList();
00142     }
00143     return Response.newError("No such service.", null).toList();
00144   }
00145 
00146   @Override
00147   public List<Object> registerService(String callerId, String serviceName, String serviceUri,
00148       String callerSlaveUri) {
00149     try {
00150       master.registerService(GraphName.of(callerId), new URI(callerSlaveUri), GraphName.of(
00151           serviceName), new URI(serviceUri));
00152       return Response.newSuccess("Success", 0).toList();
00153     } catch (URISyntaxException e) {
00154       throw new RosRuntimeException(e);
00155     }
00156   }
00157 
00158   @Override
00159   public List<Object> unregisterService(String callerId, String serviceName, String serviceUri) {
00160     try {
00161       boolean result =
00162           master.unregisterService(GraphName.of(callerId), GraphName.of(serviceName), new URI(
00163               serviceUri));
00164       return Response.newSuccess("Success", result ? 1 : 0).toList();
00165     } catch (URISyntaxException e) {
00166       throw new RosRuntimeException(e);
00167     }
00168   }
00169 
00170   @Override
00171   public List<Object> setParam(String callerId, String key, Boolean value) {
00172     parameterServer.set(GraphName.of(key), value);
00173     return Response.newSuccess("Success", null).toList();
00174   }
00175 
00176   @Override
00177   public List<Object> setParam(String callerId, String key, Integer value) {
00178     parameterServer.set(GraphName.of(key), value);
00179     return Response.newSuccess("Success", null).toList();
00180   }
00181 
00182   @Override
00183   public List<Object> setParam(String callerId, String key, Double value) {
00184     parameterServer.set(GraphName.of(key), value);
00185     return Response.newSuccess("Success", null).toList();
00186   }
00187 
00188   @Override
00189   public List<Object> setParam(String callerId, String key, String value) {
00190     parameterServer.set(GraphName.of(key), value);
00191     return Response.newSuccess("Success", null).toList();
00192   }
00193 
00194   @Override
00195   public List<Object> setParam(String callerId, String key, List<?> value) {
00196     parameterServer.set(GraphName.of(key), value);
00197     return Response.newSuccess("Success", null).toList();
00198   }
00199 
00200   @Override
00201   public List<Object> setParam(String callerId, String key, Map<?, ?> value) {
00202     parameterServer.set(GraphName.of(key), value);
00203     return Response.newSuccess("Success", null).toList();
00204   }
00205 
00206   @Override
00207   public List<Object> getParam(String callerId, String key) {
00208     Object value = parameterServer.get(GraphName.of(key));
00209     if (value == null) {
00210       return Response.newError("Parameter \"" + key + "\" is not set.", null).toList();
00211     }
00212     return Response.newSuccess("Success", value).toList();
00213   }
00214 
00215   @Override
00216   public List<Object> searchParam(String callerId, String key) {
00217     throw new UnsupportedOperationException();
00218   }
00219 
00220   @Override
00221   public List<Object> subscribeParam(String callerId, String callerSlaveUri, String key) {
00222     parameterServer.subscribe(GraphName.of(key),
00223         NodeIdentifier.forNameAndUri(callerId, callerSlaveUri));
00224     Object value = parameterServer.get(GraphName.of(key));
00225     if (value == null) {
00226       // Must return an empty map as the value of an unset parameter.
00227       value = new HashMap<String, Object>();
00228     }
00229     return Response.newSuccess("Success", value).toList();
00230   }
00231 
00232   @Override
00233   public List<Object> unsubscribeParam(String callerId, String callerSlaveUri, String key) {
00234     throw new UnsupportedOperationException();
00235   }
00236 
00237   @Override
00238   public List<Object> deleteParam(String callerId, String key) {
00239     parameterServer.delete(GraphName.of(key));
00240     return Response.newSuccess("Success", null).toList();
00241   }
00242 
00243   @Override
00244   public List<Object> hasParam(String callerId, String key) {
00245     return Response.newSuccess("Success", parameterServer.has(GraphName.of(key))).toList();
00246   }
00247 
00248   @Override
00249   public List<Object> getParamNames(String callerId) {
00250     Collection<GraphName> names = parameterServer.getNames();
00251     List<String> stringNames = Lists.newArrayList();
00252     for (GraphName name : names) {
00253       stringNames.add(name.toString());
00254     }
00255     return Response.newSuccess("Success", stringNames).toList();
00256   }
00257 }


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