Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
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 }