Fawkes API  Fawkes Development Version
xmlrpc_thread.cpp
1 
2 /***************************************************************************
3  * xmlrpc_thread.cpp - Thread that handles xml-rpc requests
4  *
5  * Created: Sun Aug 30 12:49:26 2009
6  * Copyright 2006-2009 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 "xmlrpc_thread.h"
24 
25 #include "methods/log.h"
26 #include "methods/plugin.h"
27 #include "xmlrpc_processor.h"
28 
29 #include <core/version.h>
30 #include <webview/request_dispatcher.h>
31 #include <webview/server.h>
32 #include <webview/url_manager.h>
33 
34 using namespace fawkes;
35 
36 /** @class XmlRpcThread "xmlrpc_thread.h"
37  * XML-RPC Thread.
38  * This thread runs the HTTP server and handles XML-RPC calls.
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor. */
44 : Thread("XmlRpcThread", Thread::OPMODE_CONTINUOUS), LoggerAspect(&cache_logger_)
45 {
47 }
48 
49 XmlRpcThread::~XmlRpcThread()
50 {
51 }
52 
53 void
55 {
56  try {
57  custom_server_ = config->get_bool("/xmlrpc/custom_server");
58  } catch (Exception &e) {
59  custom_server_ = false;
60  }
61  if (custom_server_) {
62  cfg_port_ = config->get_uint("/xmlrpc/port");
63  }
64 
65  cache_logger_.clear();
66 
67  processor_ = new XmlRpcRequestProcessor(logger);
68 
69  std::shared_ptr<xmlrpc_c::registry> registry = processor_->registry();
70  plugin_methods_ = new XmlRpcPluginMethods(registry, plugin_manager, logger);
71  log_methods_ = new XmlRpcLogMethods(registry, &cache_logger_, logger);
72 
73  if (custom_server_) {
74  url_manager_ = new WebUrlManager();
75  dispatcher_ = new WebRequestDispatcher(url_manager_);
76  webserver_ = new WebServer(cfg_port_, dispatcher_);
77 
78  logger->log_info("XmlRpcThread", "Listening for HTTP connections on port %u", cfg_port_);
79 
80  url_manager_->add_handler(WebRequest::METHOD_POST,
81  "/",
83  processor_,
84  std::placeholders::_1));
85 
86  xmlrpc_service_ =
87  new NetworkService(nnresolver, "Fawkes XML-RPC on %h", "_http._tcp", cfg_port_);
88  xmlrpc_service_->add_txt("fawkesver=%u.%u.%u",
89  FAWKES_VERSION_MAJOR,
90  FAWKES_VERSION_MINOR,
91  FAWKES_VERSION_MICRO);
92  service_publisher->publish_service(xmlrpc_service_);
93  } else {
94  set_opmode(Thread::OPMODE_WAITFORWAKEUP);
95  logger->log_info("XmlRpcThread", "Registering as /xmlrpc in webview");
96  webview_url_manager->add_handler(WebRequest::METHOD_POST,
97  "/xmlrpc",
99  processor_,
100  std::placeholders::_1));
101  }
102 }
103 
104 void
106 {
107  if (custom_server_) {
108  service_publisher->unpublish_service(xmlrpc_service_);
109  delete xmlrpc_service_;
110 
111  delete webserver_;
112  delete plugin_methods_;
113  delete log_methods_;
114  delete dispatcher_;
115  delete url_manager_;
116  } else {
117  webview_url_manager->remove_handler(WebRequest::METHOD_POST, "/xmlrpc");
118  }
119  delete processor_;
120 }
121 
122 void
124 {
125  if (custom_server_) {
126  webserver_->process();
127  }
128 }
Wrapper class for logging related XML-RPC methods.
Definition: log.h:35
Wrapper class for plugin related XML-RPC methods.
Definition: plugin.h:34
XML-RPC web request processor.
fawkes::WebReply * process_request(const fawkes::WebRequest *request)
Process request.
std::shared_ptr< xmlrpc_c::registry > registry()
Get XML-RPC registry.
virtual void init()
Initialize the thread.
XmlRpcThread()
Constructor.
virtual void loop()
Code to execute in the thread.
virtual void finalize()
Finalize the thread.
void clear()
Clear messages.
Definition: cache.cpp:71
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
Base class for exceptions in Fawkes.
Definition: exception.h:36
Thread aspect that allows to provide a logger to Fawkes.
Definition: logger.h:34
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
NetworkNameResolver * nnresolver
Network name resolver to lookup IP addresses of hostnames and vice versa.
Definition: network.h:45
ServicePublisher * service_publisher
Service publisher to publish services on the network.
Definition: network.h:46
Representation of a service announced or found via service discovery (i.e.
Definition: service.h:38
void add_txt(const char *format,...)
Add a TXT record.
Definition: service.cpp:313
PluginManager * plugin_manager
This is the member used to access the PluginManager.
virtual void unpublish_service(NetworkService *service)=0
Revoke service publication.
virtual void publish_service(NetworkService *service)=0
Publish service.
Thread class encapsulation of pthreads.
Definition: thread.h:46
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Definition: thread.cpp:716
void set_opmode(OpMode op_mode)
Set operation mode.
Definition: thread.cpp:682
Web request dispatcher.
Encapsulation of the libmicrohttpd webserver.
Definition: server.h:44
void process()
Process requests.
Definition: server.cpp:322
Manage URL mappings.
Definition: url_manager.h:40
void remove_handler(WebRequest::Method method, const std::string &path)
Remove a request processor.
Definition: url_manager.cpp:84
void add_handler(WebRequest::Method method, const std::string &path, Handler handler)
Add a request processor.
Definition: url_manager.cpp:54
WebUrlManager * webview_url_manager
Webview request processor manager.
Definition: webview.h:49
Fawkes library namespace.