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,
58  this);
59  __srv_unregister =
60  rosnode->advertiseService("/webview/unregister",
61  &ROSWebviewThread::srv_unregister_cb,
62  this);
63 
64  __srv_add_nav =
65  rosnode->advertiseService("/webview/add_nav_entry",
66  &ROSWebviewThread::srv_add_nav_cb,
67  this);
68  __srv_remove_nav =
69  rosnode->advertiseService("/webview/remove_nav_entry",
70  &ROSWebviewThread::srv_remove_nav_cb,
71  this);
72 }
73 
74 
75 void
77 {
78  __srv_register.shutdown();
79  __srv_unregister.shutdown();
80 
81  __srv_add_nav.shutdown();
82  __srv_remove_nav.shutdown();
83 
84  std::map<std::string, ROSWebviewRequestProcessor *>::iterator i;
85  for (i = __procs.begin(); i != __procs.end(); ++i) {
86  webview_url_manager->unregister_baseurl(i->first.c_str());
87  delete i->second;
88  }
89  __procs.clear();
90 
91  std::map<std::string, std::string>::iterator ne;
92  for (ne = __nav_entries.begin(); ne != __nav_entries.end(); ++ne) {
94  }
95  __nav_entries.clear();
96 }
97 
98 
99 void
101 {
102 }
103 
104 
105 bool
106 ROSWebviewThread::srv_register_cb(webview_msgs::UrlRegistration::Request &req,
107  webview_msgs::UrlRegistration::Response &resp)
108 {
109  if (__procs.find(req.url_prefix) != __procs.end()) {
110  resp.success = false;
111  resp.error = "A processor has already been registered for this prefix";
112  } else {
113  try {
114  logger->log_info(name(), "Registering srv '%s' for URL '%s'",
115  req.service_name.c_str(), req.url_prefix.c_str());
116  __procs[req.url_prefix] = new ROSWebviewRequestProcessor(rosnode, logger,
117  req.url_prefix,
118  req.service_name);
119  try {
120  webview_url_manager->register_baseurl(req.url_prefix.c_str(),
121  __procs[req.url_prefix]);
122  resp.success = true;
123  } catch (fawkes::Exception &e) {
124  resp.success = false;
125  resp.error = std::string("Failed to register processor: ") + e.what();
126  }
127  } catch (ros::Exception &e) {
128  resp.success = false;
129  resp.error = std::string("Registration failed: ") + e.what();
130  }
131  }
132  return true;
133 }
134 
135 bool
136 ROSWebviewThread::srv_unregister_cb(webview_msgs::UrlRegistration::Request &req,
137  webview_msgs::UrlRegistration::Response &resp)
138 {
139  logger->log_debug(name(), "%s unregisters for %s", req.service_name.c_str(),
140  req.url_prefix.c_str());
141  if (__procs.find(req.url_prefix) == __procs.end()) {
142  resp.success = false;
143  resp.error = "A processor has not been registered for this prefix";
144  } else {
145  if (__procs.find(req.url_prefix) != __procs.end()) {
146  logger->log_info(name(), "De-registering URL '%s'",
147  req.url_prefix.c_str());
148  webview_url_manager->unregister_baseurl(req.url_prefix.c_str());
149  delete __procs[req.url_prefix];
150  __procs.erase(req.url_prefix);
151  } else {
152  logger->log_warn(name(), "Unregister request for %s, but is not registered",
153  req.url_prefix.c_str());
154  }
155  resp.success = true;
156  }
157  return true;
158 }
159 
160 bool
161 ROSWebviewThread::srv_add_nav_cb(webview_msgs::NavRegistration::Request &req,
162  webview_msgs::NavRegistration::Response &resp)
163 {
164  try {
165  webview_nav_manager->add_nav_entry(req.url, req.name);
166  __nav_entries[req.url] = req.name;
167  resp.success = true;
168  } catch (Exception &e) {
169  resp.success = false;
170  resp.error = e.what();
171  }
172 
173  return true;
174 }
175 
176 bool
177 ROSWebviewThread::srv_remove_nav_cb(webview_msgs::NavRegistration::Request &req,
178  webview_msgs::NavRegistration::Response &resp)
179 {
181  __nav_entries.erase(req.url);
182  resp.success = true;
183  return true;
184 }
WebNavManager * webview_nav_manager
Webview navigation manager.
Definition: webview.h:50
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.
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.
virtual const char * what() const
Get primary string.
Definition: exception.cpp:661
Base class for exceptions in Fawkes.
Definition: exception.h:36
WebUrlManager * webview_url_manager
Webview request processor manager.
Definition: webview.h:48
void unregister_baseurl(const char *url_prefix)
Remove a request processor.
Definition: url_manager.cpp:87
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
const char * name() const
Get name of thread.
Definition: thread.h:95
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.