Client.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014 Mickael Gaillard. All rights reserved.
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 package org.ros.dynamic_reconfigure.client;
00017 
00018 import org.ros.dynamic_reconfigure.DynamicReconfigureParameterException;
00019 import org.ros.exception.RemoteException;
00020 import org.ros.exception.ServiceNotFoundException;
00021 import org.ros.internal.message.Message;
00022 import org.ros.message.MessageListener;
00023 import org.ros.node.ConnectedNode;
00024 import org.ros.node.service.ServiceClient;
00025 import org.ros.node.service.ServiceResponseListener;
00026 import org.ros.node.topic.Subscriber;
00027 
00028 import dynamic_reconfigure.Config;
00029 import dynamic_reconfigure.ConfigDescription;
00030 import dynamic_reconfigure.Group;
00031 import dynamic_reconfigure.Reconfigure;
00032 import dynamic_reconfigure.ReconfigureRequest;
00033 import dynamic_reconfigure.ReconfigureResponse;
00034 
00041 public class Client <TConfig> {
00042 
00043     // Current state & node engine
00045     private ConnectedNode node;
00046     private String name;
00047     private ServiceClient<ReconfigureRequest, ReconfigureResponse> service;
00048 
00049     // Topics and Services
00050     private Subscriber<ConfigDescription> descriptionSubscriber;
00051     private Subscriber<Config> configSubscriber;
00052 
00053     private MessageListener<ConfigDescription> internalDescriptionCallback;
00054     private MessageListener<Config> internalConfigCallback;
00055 
00056     private ConfigDescription description = null;
00057     private Config config = null;
00058     private Group groupDescription = null;
00059 
00060     public Client(ConnectedNode node, String name) {
00061         this(node, name, null, null);
00062     }
00063 
00071     public Client(ConnectedNode node, String name,
00072             MessageListener<ConfigDescription> descriptionCallback,
00073             MessageListener<Config> configCallback) {
00074 
00075         this.node = node;
00076         this.name = name;
00077 
00078         this.internalDescriptionCallback = descriptionCallback;
00079         this.internalConfigCallback = configCallback;
00080 
00081         this.service                = this.getService("set_parameters", Reconfigure._TYPE);
00082         this.descriptionSubscriber  = this.getSubscriber("parameter_descriptions", ConfigDescription._TYPE);
00083         this.configSubscriber       = this.getSubscriber("parameter_updates", Config._TYPE);
00084 
00085 //        this.internalDescriptionCallback = new MessageListener<ConfigDescription>() {
00086 //
00087 //            @Override
00088 //            public void onNewMessage(ConfigDescription msg) {
00089 //                Client.this.description = msg;
00090 //            }
00091 //        };
00092 //
00093 //        this.internalConfigCallback = new MessageListener<Config>() {
00094 //
00095 //            @Override
00096 //            public void onNewMessage(Config msg) {
00097 //                Client.this.config = msg;
00098 //            }
00099 //        };
00100 
00101 
00102         this.descriptionSubscriber.addMessageListener(this.internalDescriptionCallback);
00103         this.configSubscriber.addMessageListener(this.internalConfigCallback);
00104     }
00105 
00110     public Config getConfiguration() {
00111         return this.config;
00112     }
00113 
00119     public ConfigDescription getConfigDescription() {
00120         return this.description;
00121     }
00122 
00123     public Group getGroupDescription() {
00124         return this.groupDescription;
00125     }
00126 
00131     public void updateConfiguration(TConfig config) {
00132         ReconfigureRequest request = this.service.newMessage();
00133         //request.setConfig(config);
00134 
00135         this.service.call(request, new ServiceResponseListener<ReconfigureResponse>() {
00136 
00137             @Override
00138             public void onFailure(RemoteException arg0) {
00139                 // TODO Auto-generated method stub
00140 
00141             }
00142 
00143             @Override
00144             public void onSuccess(ReconfigureResponse arg0) {
00145                 // TODO Auto-generated method stub
00146 
00147             }
00148         });
00149     }
00150 
00154     public void updateGroups() {
00155 
00156     }
00157 
00161     public void close() {
00162         this.service.shutdown();
00163         this.descriptionSubscriber.shutdown();
00164         this.configSubscriber.shutdown();
00165     }
00166 
00171     public MessageListener<Config> getConfigCallback() {
00172         return this.internalConfigCallback;
00173     }
00174 
00179     public void setConfigCallback(MessageListener<Config> listener) {
00180         this.internalConfigCallback = listener;
00181         if (this.internalConfigCallback != null) {
00182             this.internalConfigCallback.onNewMessage(this.config);
00183         }
00184     }
00185 
00190     public MessageListener<ConfigDescription> getDescriptionCallback() {
00191         return this.internalDescriptionCallback;
00192     }
00193 
00197     public void setDescriptionCallback(MessageListener<ConfigDescription> listener) {
00198         this.internalDescriptionCallback = listener;
00199         if (this.internalDescriptionCallback != null) {
00200             this.internalDescriptionCallback.onNewMessage(this.description);
00201         }
00202     }
00203 
00204     protected <T extends Message, U extends Message> ServiceClient<T, U> getService(String suffix, String type) {
00205         ServiceClient<T, U> result = null;
00206 
00207         try {
00208             result = this.node.newServiceClient(name +"/" + suffix, type);
00209         } catch (ServiceNotFoundException e) {
00210             e.printStackTrace();
00211         }
00212 
00213         return result;
00214     }
00215 
00216     protected <T extends Message> Subscriber<T> getSubscriber(String suffix, String type) {
00217         Subscriber<T> result = null;
00218 
00219         result = this.node.newSubscriber(name + "/" + suffix, type);
00220 
00221         return result;
00222     }
00223 
00224     protected void updatesMsg(Config config) {
00225         this.internalConfigCallback.onNewMessage(config);
00226     }
00227 
00228     protected void descriptionsMsg() {
00229 
00230     }
00231 
00237     protected Class<?> paramTypeFromString(String type) {
00238         Class<?> result = null;
00239         if (type.equals("int")) {
00240             result = int.class;
00241         } else if (type.equals("double")) {
00242             result = double.class;
00243         } else if (type.equals("str")) {
00244             result = String.class;
00245         } else if (type.equals("bool")) {
00246             result = boolean.class;
00247         } else {
00248             throw new DynamicReconfigureParameterException(String.format(
00249                     "parameter has unknown type: %s. This is a bug in dynamic_reconfigure.",
00250                     type));
00251         }
00252         return result;
00253     }
00254 }


rosjava_dynamic_reconfigure
Author(s): Mickaƫl Gaillard , Erwan Le Huitouze
autogenerated on Thu Jun 6 2019 22:01:52