webview_thread.cpp

00001 
00002 /***************************************************************************
00003  *  webview_thread.cpp - Thread that handles web interface requests
00004  *
00005  *  Created: Mon Oct 13 17:51:31 2008 (I5 Developer's Day)
00006  *  Copyright  2006-2008  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 "static_processor.h"
00025 #include "blackboard_processor.h"
00026 #include "startpage_processor.h"
00027 #include "plugins_processor.h"
00028 #include "service_browse_handler.h"
00029 #include "header_generator.h"
00030 #include "footer_generator.h"
00031 
00032 #include <core/version.h>
00033 #include <webview/request_dispatcher.h>
00034 #include <webview/page_reply.h>
00035 #include <webview/server.h>
00036 
00037 using namespace fawkes;
00038 
00039 
00040 /** Prefix for the WebStaticRequestProcessor. */
00041 const char *WebviewThread::STATIC_URL_PREFIX = "/static";
00042 /** Prefix for the WebBlackBoardRequestProcessor. */
00043 const char *WebviewThread::BLACKBOARD_URL_PREFIX = "/blackboard";
00044 /** Prefix for the WebPluginsRequestProcessor. */
00045 const char *WebviewThread::PLUGINS_URL_PREFIX = "/plugins";
00046 
00047 /** @class WebviewThread "webview_thread.h"
00048  * Webview Thread.
00049  * This thread runs the HTTP server and handles requests via the
00050  * WebRequestDispatcher.
00051  * @author Tim Niemueller
00052  */
00053 
00054 
00055 /** Constructor. */
00056 WebviewThread::WebviewThread()
00057   : Thread("WebviewThread", Thread::OPMODE_CONTINUOUS),
00058     LoggerAspect(&__cache_logger)
00059 {
00060   set_prepfin_conc_loop(true);
00061   
00062 }
00063 
00064 
00065 WebviewThread::~WebviewThread()
00066 {
00067 }
00068 
00069 void
00070 WebviewThread::init()
00071 {
00072   __cfg_port = config->get_uint("/webview/port");
00073 
00074   __cache_logger.clear();
00075 
00076   __webview_service = new NetworkService(nnresolver, "Fawkes Webview on %h",
00077                                          "_http._tcp", __cfg_port);
00078   __webview_service->add_txt("fawkesver=%u.%u.%u",
00079                              FAWKES_VERSION_MAJOR, FAWKES_VERSION_MINOR,
00080                              FAWKES_VERSION_MICRO);
00081   __service_browse_handler = new WebviewServiceBrowseHandler(logger, __webview_service);
00082 
00083   __header_gen = new WebviewHeaderGenerator();
00084   __footer_gen = new WebviewFooterGenerator(__service_browse_handler);
00085 
00086   __dispatcher = new WebRequestDispatcher(__header_gen, __footer_gen);
00087   __webserver  = new WebServer(__cfg_port, __dispatcher, logger);
00088 
00089   __startpage_processor  = new WebviewStartPageRequestProcessor(&__cache_logger);
00090   __static_processor     = new WebviewStaticRequestProcessor(STATIC_URL_PREFIX, RESDIR"/webview", logger);
00091   __blackboard_processor = new WebviewBlackBoardRequestProcessor(BLACKBOARD_URL_PREFIX, blackboard);
00092   __plugins_processor    = new WebviewPluginsRequestProcessor(PLUGINS_URL_PREFIX, plugin_manager);
00093   __dispatcher->add_processor("/", __startpage_processor);
00094   __dispatcher->add_processor(STATIC_URL_PREFIX, __static_processor);
00095   __dispatcher->add_processor(BLACKBOARD_URL_PREFIX, __blackboard_processor);
00096   __dispatcher->add_processor(PLUGINS_URL_PREFIX, __plugins_processor);
00097 
00098   __header_gen->add_nav_entry(BLACKBOARD_URL_PREFIX, "BlackBoard");
00099   __header_gen->add_nav_entry(PLUGINS_URL_PREFIX, "Plugins");
00100 
00101   logger->log_info("WebviewThread", "Listening for HTTP connections on port %u", __cfg_port);
00102 
00103   service_publisher->publish_service(__webview_service);
00104   service_browser->watch_service("_http._tcp", __service_browse_handler);
00105 
00106 }
00107 
00108 void
00109 WebviewThread::finalize()
00110 {
00111   service_publisher->unpublish_service(__webview_service);
00112   service_browser->unwatch_service("_http._tcp", __service_browse_handler);
00113 
00114   delete __webserver;
00115 
00116   delete __webview_service;
00117   delete __service_browse_handler;
00118 
00119   delete __dispatcher;
00120   delete __static_processor;
00121   delete __blackboard_processor;
00122   delete __startpage_processor;
00123   delete __plugins_processor;
00124   delete __footer_gen;
00125   delete __header_gen;
00126   __dispatcher = NULL;
00127 }
00128 
00129 
00130 void
00131 WebviewThread::loop()
00132 {
00133   __webserver->process();
00134 }

Generated on Tue Feb 22 13:32:31 2011 for Fawkes API by  doxygen 1.4.7