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 #include "xmlrpc_processor.h"
25 #include "methods/plugin.h"
26 #include "methods/log.h"
27 
28 #include <core/version.h>
29 #include <webview/server.h>
30 #include <webview/url_manager.h>
31 #include <webview/request_dispatcher.h>
32 
33 using namespace fawkes;
34 
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 
43 /** Constructor. */
45  : Thread("XmlRpcThread", Thread::OPMODE_CONTINUOUS),
46  LoggerAspect(&__cache_logger)
47 {
49 
50 }
51 
52 
53 XmlRpcThread::~XmlRpcThread()
54 {
55 }
56 
57 void
59 {
60  try {
61  __custom_server = config->get_bool("/xmlrpc/custom_server");
62  } catch (Exception &e) {
63  __custom_server = false;
64  }
65  if (__custom_server) {
66  __cfg_port = config->get_uint("/xmlrpc/port");
67  }
68 
69  __cache_logger.clear();
70 
71  __processor = new XmlRpcRequestProcessor(logger);
72 
73  xmlrpc_c::registry *registry = __processor->registry();
74  __plugin_methods = new XmlRpcPluginMethods(registry, plugin_manager, logger);
75  __log_methods = new XmlRpcLogMethods(registry, &__cache_logger, logger);
76 
77  if (__custom_server) {
78  __url_manager = new WebUrlManager();
79  __dispatcher = new WebRequestDispatcher(__url_manager);
80  __webserver = new WebServer(__cfg_port, __dispatcher);
81 
82  logger->log_info("XmlRpcThread", "Listening for HTTP connections on port %u",
83  __cfg_port);
84 
85  __url_manager->register_baseurl("/", __processor);
86 
87  __xmlrpc_service = new NetworkService(nnresolver, "Fawkes XML-RPC on %h",
88  "_http._tcp", __cfg_port);
89  __xmlrpc_service->add_txt("fawkesver=%u.%u.%u", FAWKES_VERSION_MAJOR,
90  FAWKES_VERSION_MINOR, FAWKES_VERSION_MICRO);
91  service_publisher->publish_service(__xmlrpc_service);
92  } else {
93  set_opmode(Thread::OPMODE_WAITFORWAKEUP);
94  logger->log_info("XmlRpcThread", "Registering as /xmlrpc in webview");
95  webview_url_manager->register_baseurl("/xmlrpc", __processor);
96  }
97 
98 }
99 
100 void
102 {
103  if (__custom_server) {
104  service_publisher->unpublish_service(__xmlrpc_service);
105  delete __xmlrpc_service;
106 
107  delete __webserver;
108  delete __plugin_methods;
109  delete __dispatcher;
110  delete __url_manager;
111  } else {
113  }
114  delete __processor;
115 }
116 
117 
118 void
120 {
121  if (__custom_server) {
122  __webserver->process();
123  } else {
124 
125  }
126 }
virtual void init()
Initialize the thread.
Thread aspect that allows to provide a logger to Fawkes.
Definition: logger.h:36
Web request dispatcher.
PluginManager * plugin_manager
This is the member used to access the PluginManager.
Encapsulation of the libmicrohttpd webserver.
Definition: server.h:43
virtual void finalize()
Finalize the thread.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
void clear()
Clear messages.
Definition: cache.cpp:75
ServicePublisher * service_publisher
Service publisher to publish services on the network.
Definition: network.h:49
virtual void unpublish_service(NetworkService *service)=0
Revoke service publication.
virtual void publish_service(NetworkService *service)=0
Publish service.
Fawkes library namespace.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
void process()
Process requests.
Definition: server.cpp:271
Wrapper class for plugin related XML-RPC methods.
Definition: plugin.h:33
Wrapper class for logging related XML-RPC methods.
Definition: log.h:34
virtual void loop()
Code to execute in the thread.
Thread class encapsulation of pthreads.
Definition: thread.h:42
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Definition: thread.cpp:727
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
XmlRpcThread()
Constructor.
Manage URL mappings.
Definition: url_manager.h:37
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
void set_opmode(OpMode op_mode)
Set operation mode.
Definition: thread.cpp:690
void add_txt(const char *format,...)
Add a TXT record.
Definition: service.cpp:273
NetworkNameResolver * nnresolver
Network name resolver to lookup IP addresses of hostnames and vice versa.
Definition: network.h:48
XML-RPC web request processor.
Representation of a service announced or found via service discovery (i.e.
Definition: service.h:37
xmlrpc_c::registry * registry()
Get XML-RPC registry.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:44
void register_baseurl(const char *url_prefix, WebRequestProcessor *processor)
Add a request processor.
Definition: url_manager.cpp:64