EnvironmentHandler.java
Go to the documentation of this file.
00001 /* \file EnvironmentHandler.java
00002  * \brief Environment handler
00003  *
00004  * The environment handler provides the callbacks for ROS services related to environments.
00005  * 
00006  * This file is part of the RoboEarth ROS re_comm_core package.
00007  * 
00008  * It was originally created for <a href="http://www.roboearth.org/">RoboEarth</a>.
00009  * The research leading to these results has received funding from the 
00010  * European Union Seventh Framework Programme FP7/2007-2013 
00011  * under grant agreement no248942 RoboEarth.
00012  *
00013  * Copyright (C) 2010 by 
00014  * <a href=" mailto:perzylo@cs.tum.edu">Alexander Perzylo</a>
00015  * Technische Universitaet Muenchen
00016  * 
00017  * Redistribution and use in source and binary forms, with or without
00018  * modification, are permitted provided that the following conditions are met:
00019  * 
00020  *    <UL>
00021  *     <LI> Redistributions of source code must retain the above copyright
00022  *       notice, this list of conditions and the following disclaimer.
00023  *     <LI> Redistributions in binary form must reproduce the above copyright
00024  *       notice, this list of conditions and the following disclaimer in the
00025  *       documentation and/or other materials provided with the distribution.
00026  *     <LI> Neither the name of Willow Garage, Inc. nor the names of its
00027  *       contributors may be used to endorse or promote products derived from
00028  *       this software without specific prior written permission.
00029  *    </UL>
00030  * 
00031  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00032  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00033  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00034  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00035  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00036  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00037  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00038  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00039  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00040  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00041  * POSSIBILITY OF SUCH DAMAGE.
00042  *
00043  * \author Alexander Perzylo
00044  * \version 1.0
00045  * \date 2010
00046  * \image html http://www.roboearth.org/sites/default/files/RoboEarth.org_logo.gif
00047  * \image latex http://www.roboearth.org/sites/default/files/RoboEarth.org_logo.gif
00048  */
00049 package roboearth.wp5.module;
00050 
00051 import java.util.ArrayList;
00052 
00053 import roboearth.wp5.conn.REInterface;
00054 import ros.NodeHandle;
00055 import ros.Ros;
00056 import ros.RosException;
00057 import ros.ServiceServer;
00058 import ros.pkg.re_msgs.msg.StringArray;
00059 import ros.pkg.re_srvs.srv.DelEnvironment;
00060 import ros.pkg.re_srvs.srv.GetEnvironment;
00061 import ros.pkg.re_srvs.srv.QueryEnvironments;
00062 import ros.pkg.re_srvs.srv.SearchEnvironments;
00063 import ros.pkg.re_srvs.srv.SetEnvironment;
00064 import ros.pkg.re_srvs.srv.UpdateEnvironment;
00065 
00066 public class EnvironmentHandler extends AbstractHandler {
00067 
00068         public EnvironmentHandler(Ros ros, NodeHandle n) throws RosException {
00069 
00070                 super(ros, n);
00071 
00072                 this.n.advertiseService("/re_comm/get_environment", new GetEnvironment(), new GetEnvironmentCallback());
00073                 this.n.advertiseService("/re_comm/set_environment", new SetEnvironment(), new SetEnvironmentCallback());
00074                 this.n.advertiseService("/re_comm/del_environment", new DelEnvironment(), new DelEnvironmentCallback());
00075                 this.n.advertiseService("/re_comm/update_environment", new UpdateEnvironment(), new UpdateEnvironmentCallback());
00076                 this.n.advertiseService("/re_comm/search_environments", new SearchEnvironments(), new SearchEnvironmentsCallback());
00077                 this.n.advertiseService("/re_comm/query_environments", new QueryEnvironments(), new QueryEnvironmentsCallback());
00078 
00079                 ros.logInfo("Module 'EnvironmentHandler' loaded.");
00080 
00081         }
00082 
00090         class GetEnvironmentCallback implements ServiceServer.Callback<GetEnvironment.Request, GetEnvironment.Response> {
00091 
00092                 @Override
00093                 public GetEnvironment.Response call(GetEnvironment.Request req) {
00094 
00095                         GetEnvironment.Response res = new GetEnvironment.Response();
00096                         
00097                         REInterface con = getREConnection(guestInterfaceKey);
00098                         ArrayList<String> outFilenames = new ArrayList<String>();
00099                         ArrayList<String> outFileURLs = new ArrayList<String>();
00100                         String ont = con.requestEnvironment(req.environmentUID, outFilenames, outFileURLs);
00101                         
00102                         if (ont != null) {
00103                                 res.environment = ont;
00104                                 res.filenames = outFilenames;
00105                                 res.fileURLs = outFileURLs;
00106                                 res.success = true;
00107                                 ros.logInfo("GetEnvironment (UID: " + req.environmentUID + "): Done");
00108                         } else {
00109                                 res.success = false;
00110                                 ros.logInfo("GetEnvironment (UID: " + req.environmentUID + "): Failed");
00111                         }
00112 
00113                         return res;
00114 
00115                 }
00116 
00117         }
00118 
00126         class SetEnvironmentCallback implements ServiceServer.Callback<SetEnvironment.Request, SetEnvironment.Response> {
00127 
00128                 @Override
00129                 public SetEnvironment.Response call(SetEnvironment.Request req) {
00130 
00131                         SetEnvironment.Response res = new SetEnvironment.Response();
00132                         res.success = false;
00133                         
00134                         if (req.apiKey != null && req.apiKey.length() > 0) {
00135 
00136                                 REInterface con = getREConnection(req.apiKey);
00137                                 
00138                                 ArrayList<String> filenames = new ArrayList<String>();
00139                                 ArrayList<byte[]> dataArray = new ArrayList<byte[]>();
00140                                 for (ros.pkg.re_msgs.msg.File file : req.files) {
00141                                         filenames.add(file.name);
00142                                         dataArray.add(file.data);
00143                                 }
00144                                 
00145                                 res.success = con.submitEnvironment(req.environment, req.cls, req.id, req.description, dataArray, filenames);
00146                                 if (res.success) {
00147                                         ros.logInfo("SetEnvironment (UID: " + req.cls+"."+req.id + "): Done");
00148                                 } else {
00149                                         ros.logInfo("SetEnvironment (UID: " + req.cls+"."+req.id + "): Failed");
00150                                 }
00151 
00152                         } else {
00153                                 ros.logError("SetEnvironment (UID: " + req.cls+"."+req.id + "): API key is missing!");
00154                         }
00155 
00156                         return res;
00157 
00158                 }
00159 
00160         }
00161 
00169         class DelEnvironmentCallback implements ServiceServer.Callback<DelEnvironment.Request, DelEnvironment.Response> {
00170 
00171                 @Override
00172                 public DelEnvironment.Response call(DelEnvironment.Request req) {
00173 
00174                         DelEnvironment.Response res = new DelEnvironment.Response();
00175                         res.success = false;
00176 
00177                         if (req.apiKey != null && req.apiKey.length() > 0) {
00178 
00179                                 REInterface con = getREConnection(req.apiKey);
00180                                 res.success = con.deleteEnvironment(req.environmentUID);
00181                                 if (res.success) {
00182                                         ros.logInfo("DelEnvironment (UID: " + req.environmentUID + "): Done");
00183                                 } else {
00184                                         ros.logInfo("DelEnvironment (UID: " + req.environmentUID + "): Failed");
00185                                 }
00186 
00187                         } else {
00188                                 ros.logError("DelEnvironment (UID: " + req.environmentUID + "): API key is missing!");
00189                         }
00190 
00191                         return res;
00192 
00193                 }
00194 
00195         }
00196 
00204         class UpdateEnvironmentCallback implements ServiceServer.Callback<UpdateEnvironment.Request, UpdateEnvironment.Response> {
00205 
00206                 @Override
00207                 public UpdateEnvironment.Response call(UpdateEnvironment.Request req) {
00208 
00209                         UpdateEnvironment.Response res = new UpdateEnvironment.Response();
00210                         res.success = false;
00211 
00212                         if (req.apiKey != null && req.apiKey.length() > 0) {
00213 
00214                                 REInterface con = getREConnection(req.apiKey);
00215                                 res.success = con.updateEnvironment(req.uid, req.environment, req.description);
00216                                 if (res.success) {
00217                                         ros.logInfo("UpdateEnvironment (UID: " + req.uid + "): Done");
00218                                 } else {
00219                                         ros.logInfo("UpdateEnvironment (UID: " + req.uid + "): Failed");
00220                                 }
00221 
00222                         } else {
00223                                 ros.logError("UpdateEnvironment (UID: " + req.uid + "): API key is missing!");
00224                         }
00225 
00226                         return res;
00227 
00228                 }
00229 
00230         }
00231 
00239         class SearchEnvironmentsCallback implements ServiceServer.Callback<SearchEnvironments.Request, SearchEnvironments.Response> {
00240 
00241                 @Override
00242                 public SearchEnvironments.Response call(SearchEnvironments.Request req) {
00243 
00244                         SearchEnvironments.Response res = new SearchEnvironments.Response();
00245                         res.success = false;
00246 
00247                         REInterface con = getREConnection(guestInterfaceKey);
00248                         ArrayList<String> uids = new ArrayList<String>();
00249                         ArrayList<ArrayList<String>> filenames = new ArrayList<ArrayList<String>>();
00250                         ArrayList<ArrayList<String>> urls = new ArrayList<ArrayList<String>>();
00251                         
00252                         String[] onts = con.searchEnvironments(req.searchID, uids, filenames, urls);
00253                         if (onts != null) {
00254                                 for (String ont : onts) {
00255                                         StringArray saNames = new StringArray();
00256                                         saNames.list = filenames.remove(0);
00257                                         res.filenames.add(saNames);
00258                                         
00259                                         StringArray saURLs = new StringArray();
00260                                         saURLs.list = urls.remove(0);
00261                                         res.fileURLs.add(saURLs);
00262                                         
00263                                         res.environments.add(ont);
00264                                 }
00265                                 res.uids = uids;
00266                                 res.success = true;
00267                                 ros.logInfo("SearchEnvironments (search: " + req.searchID + "): Done");                         
00268                         } else {
00269                                 res.success = false;
00270                                 ros.logInfo("SearchEnvironments (search: " + req.searchID + "): Failed");
00271                         }
00272 
00273                         return res;
00274 
00275                 }
00276 
00277         }
00278         
00286         class QueryEnvironmentsCallback implements ServiceServer.Callback<QueryEnvironments.Request, QueryEnvironments.Response> {
00287 
00288                 @Override
00289                 public QueryEnvironments.Response call(QueryEnvironments.Request req) {
00290 
00291                         QueryEnvironments.Response res = new QueryEnvironments.Response();
00292                         res.result = "";
00293 
00294                         REInterface con = getREConnection(guestInterfaceKey);
00295                         String result = con.queryEnvironmentDB(req.query);
00296                         if (result != null) {
00297                                 res.result = result;
00298                                 ros.logInfo("QueryEnvironment (query:\n" + req.query + "): Done");
00299                         } else {
00300                                 ros.logInfo("QueryEnvironment (query:\n" + req.query + "): Failed");
00301                         }
00302 
00303                         return res;
00304 
00305                 }
00306 
00307         }
00308         
00309 }


re_comm_core
Author(s): Alexander Perzylo
autogenerated on Sun Jan 5 2014 11:29:33