server.cpp

00001 
00002 /***************************************************************************
00003  *  server.cpp - Web server encapsulation around libmicrohttpd
00004  *
00005  *  Created: Sun Aug 30 17:40:54 2009
00006  *  Copyright  2006-2009  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/server.h>
00024 #include <webview/request_dispatcher.h>
00025 #include <core/exception.h>
00026 #include <utils/logging/logger.h>
00027 
00028 #include <sys/socket.h>
00029 #include <microhttpd.h>
00030 
00031 namespace fawkes {
00032 #if 0 /* just to make Emacs auto-indent happy */
00033 }
00034 #endif
00035 
00036 /** @class WebServer <webview/server.h>
00037  * Encapsulation of the libmicrohttpd webserver.
00038  * This class opens a port serving websites and calls the supplied dispatcher
00039  * for requests.
00040  * @author Tim Niemueller
00041  */
00042 
00043 /** Constructor.
00044  * @param port TCP port to listen on
00045  * @param dispatcher dispatcher to call for requests
00046  * @param logger optional logger, used to output possible run-time problems
00047  */
00048 WebServer::WebServer(unsigned short int port, WebRequestDispatcher *dispatcher,
00049                      fawkes::Logger *logger)
00050 {
00051   __port       = port;
00052   __dispatcher = dispatcher;
00053   __logger     = logger;
00054 
00055   __daemon = MHD_start_daemon(MHD_NO_FLAG,
00056                               __port,
00057                               NULL,
00058                               NULL,
00059                               WebRequestDispatcher::process_request_cb,
00060                               (void *)__dispatcher,
00061                               MHD_OPTION_END);
00062 
00063   if ( __daemon == NULL ) {
00064     throw fawkes::Exception("Could not start microhttpd");
00065   }
00066 
00067 }
00068 
00069 
00070 /** Destructor. */
00071 WebServer::~WebServer()
00072 {
00073   MHD_stop_daemon(__daemon);
00074   __daemon = NULL;
00075   __dispatcher = NULL;
00076 }
00077 
00078 /** Process requests.
00079  * This method waits for new requests and processes them when received.
00080  */
00081 void
00082 WebServer::process()
00083 {
00084   fd_set read_fd, write_fd, except_fd;
00085   int max_fd = 0;
00086   FD_ZERO(&read_fd); FD_ZERO(&write_fd); FD_ZERO(&except_fd);
00087   if ( MHD_get_fdset(__daemon, &read_fd, &write_fd, &except_fd, &max_fd) != MHD_YES ) {
00088     if (__logger)
00089       __logger->log_warn("WebviewThread", "Could not get microhttpd fdsets");
00090     return;
00091   }
00092   select(max_fd + 1, &read_fd, &write_fd, &except_fd, NULL);
00093   MHD_run(__daemon);
00094 }
00095 
00096 } // end namespace fawkes

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1