server.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00033 }
00034 #endif
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
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
00071 WebServer::~WebServer()
00072 {
00073 MHD_stop_daemon(__daemon);
00074 __daemon = NULL;
00075 __dispatcher = NULL;
00076 }
00077
00078
00079
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 }