Fawkes API  Fawkes Development Version
webview_thread.cpp
1 
2 /***************************************************************************
3  * webview_thread.cpp - Webview ROS integration thread
4  *
5  * Created: Fri May 06 11:12:05 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "webview_thread.h"
24 #include "webview_reqproc.h"
25 
26 #include <webview/url_manager.h>
27 #include <webview/nav_manager.h>
28 
29 #include <ros/ros.h>
30 
31 using namespace fawkes;
32 
33 /** @class ROSWebviewThread "webview_thread.h"
34  * Provide webview to ROS.
35  *
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor. */
41  : Thread("ROSWebviewThread", Thread::OPMODE_WAITFORWAKEUP)
42 {
43 }
44 
45 
46 /** Destructor. */
48 {
49 }
50 
51 
52 void
54 {
55  __srv_register =
56  rosnode->advertiseService("/webview/register",
57  &ROSWebviewThread::srv_register_cb, this);
58  __srv_unregister =
59  rosnode->advertiseService("/webview/unregister",
60  &ROSWebviewThread::srv_unregister_cb, this);
61 
62  __srv_add_nav =
63  rosnode->advertiseService("/webview/add_nav_entry",
64  &ROSWebviewThread::srv_add_nav_cb, this);
65  __srv_remove_nav =
66  rosnode->advertiseService("/webview/remove_nav_entry",
67  &ROSWebviewThread::srv_remove_nav_cb, this);
68 }
69 
70 
71 void
73 {
74  __srv_register.shutdown();
75  __srv_unregister.shutdown();
76 
77  __srv_add_nav.shutdown();
78  __srv_remove_nav.shutdown();
79 
80  std::map<std::string, ROSWebviewRequestProcessor *>::iterator i;
81  for (i = __procs.begin(); i != __procs.end(); ++i) {
82  webview_url_manager->unregister_baseurl(i->first.c_str());
83  delete i->second;
84  }
85  __procs.clear();
86 
87  std::map<std::string, std::string>::iterator ne;
88  for (ne = __nav_entries.begin(); ne != __nav_entries.end(); ++ne) {
90  }
91  __nav_entries.clear();
92 }
93 
94 
95 void
97 {
98 }
99 
100 
101 bool
102 ROSWebviewThread::srv_register_cb(fawkes_msgs::WebviewUrlRegistration::Request &req,
103  fawkes_msgs::WebviewUrlRegistration::Response &resp)
104 {
105  if (__procs.find(req.url_prefix) != __procs.end()) {
106  resp.success = false;
107  resp.error = "A processor has already been registered for this prefix";
108  } else {
109  try {
110  logger->log_info(name(), "Registering srv '%s' for URL '%s'",
111  req.service_name.c_str(), req.url_prefix.c_str());
112  __procs[req.url_prefix] = new ROSWebviewRequestProcessor(rosnode, logger,
113  req.url_prefix,
114  req.service_name);
115  try {
116  webview_url_manager->register_baseurl(req.url_prefix.c_str(),
117  __procs[req.url_prefix]);
118  resp.success = true;
119  } catch (fawkes::Exception &e) {
120  resp.success = false;
121  resp.error = std::string("Failed to register processor: ") + e.what();
122  }
123  } catch (ros::Exception &e) {
124  resp.success = false;
125  resp.error = std::string("Registration failed: ") + e.what();
126  }
127  }
128  return true;
129 }
130 
131 bool
132 ROSWebviewThread::srv_unregister_cb(fawkes_msgs::WebviewUrlRegistration::Request &req,
133  fawkes_msgs::WebviewUrlRegistration::Response &resp)
134 {
135  logger->log_debug(name(), "%s unregisters for %s", req.service_name.c_str(),
136  req.url_prefix.c_str());
137  if (__procs.find(req.url_prefix) == __procs.end()) {
138  resp.success = false;
139  resp.error = "A processor has not been registered for this prefix";
140  } else {
141  if (__procs.find(req.url_prefix) != __procs.end()) {
142  logger->log_info(name(), "De-registering URL '%s'",
143  req.url_prefix.c_str());
144  webview_url_manager->unregister_baseurl(req.url_prefix.c_str());
145  delete __procs[req.url_prefix];
146  __procs.erase(req.url_prefix);
147  } else {
148  logger->log_warn(name(), "Unregister request for %s, but is not registered",
149  req.url_prefix.c_str());
150  }
151  resp.success = true;
152  }
153  return true;
154 }
155 
156 bool
157 ROSWebviewThread::srv_add_nav_cb(fawkes_msgs::WebviewNavRegistration::Request &req,
158  fawkes_msgs::WebviewNavRegistration::Response &resp)
159 {
160  try {
161  webview_nav_manager->add_nav_entry(req.url, req.name);
162  __nav_entries[req.url] = req.name;
163  resp.success = true;
164  } catch (Exception &e) {
165  resp.success = false;
166  resp.error = e.what();
167  }
168 
169  return true;
170 }
171 
172 bool
173 ROSWebviewThread::srv_remove_nav_cb(fawkes_msgs::WebviewNavRegistration::Request &req,
174  fawkes_msgs::WebviewNavRegistration::Response &resp)
175 {
177  __nav_entries.erase(req.url);
178  resp.success = true;
179  return true;
180 }
WebNavManager * webview_nav_manager
Webview navigation manager.
Definition: webview.h:52
ROSWebviewThread()
Constructor.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
virtual void finalize()
Finalize the thread.
Fawkes library namespace.
virtual const char * what() const
Get primary string.
Definition: exception.cpp:661
Thread class encapsulation of pthreads.
Definition: thread.h:42
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
virtual void init()
Initialize the thread.
virtual ~ROSWebviewThread()
Destructor.
Base class for exceptions in Fawkes.
Definition: exception.h:36
WebUrlManager * webview_url_manager
Webview request processor manager.
Definition: webview.h:50
void unregister_baseurl(const char *url_prefix)
Remove a request processor.
Definition: url_manager.cpp:87
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
void add_nav_entry(std::string baseurl, std::string name)
Add a navigation entry.
Definition: nav_manager.cpp:61
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
Convert webview requests to ROS service calls.
LockPtr< ros::NodeHandle > rosnode
Central ROS node handle.
Definition: ros.h:48
void remove_nav_entry(std::string baseurl)
Remove a navigation entry.
Definition: nav_manager.cpp:76
void register_baseurl(const char *url_prefix, WebRequestProcessor *processor)
Add a request processor.
Definition: url_manager.cpp:64
virtual void loop()
Code to execute in the thread.