Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * webview_thread.cpp - Webview ROS integration thread 00004 * 00005 * Created: Fri May 06 11:12:05 2011 00006 * Copyright 2006-2011 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include "webview_thread.h" 00024 #include "webview_reqproc.h" 00025 00026 #include <webview/url_manager.h> 00027 #include <webview/nav_manager.h> 00028 00029 #include <ros/ros.h> 00030 00031 using namespace fawkes; 00032 00033 /** @class ROSWebviewThread "webview_thread.h" 00034 * Provide webview to ROS. 00035 * 00036 * @author Tim Niemueller 00037 */ 00038 00039 /** Constructor. */ 00040 ROSWebviewThread::ROSWebviewThread() 00041 : Thread("ROSWebviewThread", Thread::OPMODE_WAITFORWAKEUP) 00042 { 00043 } 00044 00045 00046 /** Destructor. */ 00047 ROSWebviewThread::~ROSWebviewThread() 00048 { 00049 } 00050 00051 00052 void 00053 ROSWebviewThread::init() 00054 { 00055 __srv_register = 00056 rosnode->advertiseService("/webview/register", 00057 &ROSWebviewThread::srv_register_cb, 00058 this); 00059 __srv_unregister = 00060 rosnode->advertiseService("/webview/unregister", 00061 &ROSWebviewThread::srv_unregister_cb, 00062 this); 00063 00064 __srv_add_nav = 00065 rosnode->advertiseService("/webview/add_nav_entry", 00066 &ROSWebviewThread::srv_add_nav_cb, 00067 this); 00068 __srv_remove_nav = 00069 rosnode->advertiseService("/webview/remove_nav_entry", 00070 &ROSWebviewThread::srv_remove_nav_cb, 00071 this); 00072 } 00073 00074 00075 void 00076 ROSWebviewThread::finalize() 00077 { 00078 __srv_register.shutdown(); 00079 __srv_unregister.shutdown(); 00080 00081 __srv_add_nav.shutdown(); 00082 __srv_remove_nav.shutdown(); 00083 00084 std::map<std::string, ROSWebviewRequestProcessor *>::iterator i; 00085 for (i = __procs.begin(); i != __procs.end(); ++i) { 00086 webview_url_manager->unregister_baseurl(i->first.c_str()); 00087 delete i->second; 00088 } 00089 __procs.clear(); 00090 00091 std::map<std::string, std::string>::iterator ne; 00092 for (ne = __nav_entries.begin(); ne != __nav_entries.end(); ++ne) { 00093 webview_nav_manager->remove_nav_entry(ne->first); 00094 } 00095 __nav_entries.clear(); 00096 } 00097 00098 00099 void 00100 ROSWebviewThread::loop() 00101 { 00102 } 00103 00104 00105 bool 00106 ROSWebviewThread::srv_register_cb(webview_msgs::UrlRegistration::Request &req, 00107 webview_msgs::UrlRegistration::Response &resp) 00108 { 00109 if (__procs.find(req.url_prefix) != __procs.end()) { 00110 resp.success = false; 00111 resp.error = "A processor has already been registered for this prefix"; 00112 } else { 00113 try { 00114 logger->log_info(name(), "Registering srv '%s' for URL '%s'", 00115 req.service_name.c_str(), req.url_prefix.c_str()); 00116 __procs[req.url_prefix] = new ROSWebviewRequestProcessor(rosnode, logger, 00117 req.url_prefix, 00118 req.service_name); 00119 try { 00120 webview_url_manager->register_baseurl(req.url_prefix.c_str(), 00121 __procs[req.url_prefix]); 00122 resp.success = true; 00123 } catch (fawkes::Exception &e) { 00124 resp.success = false; 00125 resp.error = std::string("Failed to register processor: ") + e.what(); 00126 } 00127 } catch (ros::Exception &e) { 00128 resp.success = false; 00129 resp.error = std::string("Registration failed: ") + e.what(); 00130 } 00131 } 00132 return true; 00133 } 00134 00135 bool 00136 ROSWebviewThread::srv_unregister_cb(webview_msgs::UrlRegistration::Request &req, 00137 webview_msgs::UrlRegistration::Response &resp) 00138 { 00139 logger->log_debug(name(), "%s unregisters for %s", req.service_name.c_str(), 00140 req.url_prefix.c_str()); 00141 if (__procs.find(req.url_prefix) == __procs.end()) { 00142 resp.success = false; 00143 resp.error = "A processor has not been registered for this prefix"; 00144 } else { 00145 if (__procs.find(req.url_prefix) != __procs.end()) { 00146 logger->log_info(name(), "De-registering URL '%s'", 00147 req.url_prefix.c_str()); 00148 webview_url_manager->unregister_baseurl(req.url_prefix.c_str()); 00149 delete __procs[req.url_prefix]; 00150 __procs.erase(req.url_prefix); 00151 } else { 00152 logger->log_warn(name(), "Unregister request for %s, but is not registered", 00153 req.url_prefix.c_str()); 00154 } 00155 resp.success = true; 00156 } 00157 return true; 00158 } 00159 00160 bool 00161 ROSWebviewThread::srv_add_nav_cb(webview_msgs::NavRegistration::Request &req, 00162 webview_msgs::NavRegistration::Response &resp) 00163 { 00164 try { 00165 webview_nav_manager->add_nav_entry(req.url, req.name); 00166 __nav_entries[req.url] = req.name; 00167 resp.success = true; 00168 } catch (Exception &e) { 00169 resp.success = false; 00170 resp.error = e.what(); 00171 } 00172 00173 return true; 00174 } 00175 00176 bool 00177 ROSWebviewThread::srv_remove_nav_cb(webview_msgs::NavRegistration::Request &req, 00178 webview_msgs::NavRegistration::Response &resp) 00179 { 00180 webview_nav_manager->remove_nav_entry(req.url); 00181 __nav_entries.erase(req.url); 00182 resp.success = true; 00183 return true; 00184 }