version 0.3.4
[fms.git] / src / http / httpthread.cpp
index dd4bd9b..89edc93 100644 (file)
@@ -1,7 +1,11 @@
 #include "../../include/http/httpthread.h"\r
+#include "../../include/http/fmshttprequesthandlerfactory.h"\r
 #include "../../include/option.h"\r
 #include "../../include/stringfunctions.h"\r
-#include "../../include/http/pages/homepage.h"\r
+\r
+#include <Poco/Net/ServerSocket.h>\r
+#include <Poco/Net/HTTPServer.h>\r
+#include <Poco/Net/HTTPServerParams.h>\r
 \r
 #include <iostream>\r
 \r
 \r
 HTTPThread::HTTPThread()\r
 {\r
-\r
-       int port;\r
+       m_listenport=8080;\r
        std::string portstr;\r
        Option::Instance()->Get("HTTPListenPort",portstr);\r
-       StringFunctions::Convert(portstr,port);\r
-\r
-       m_pagehandlers.push_back(new HomePage());\r
-\r
-       m_ctx=0;\r
-       m_ctx=shttpd_init(NULL,"listen_ports",portstr.c_str(),NULL);\r
-       shttpd_listen(m_ctx,port,false);\r
-\r
-       shttpd_register_uri(m_ctx,"*",HTTPThread::PageCallback,this);\r
-\r
-}\r
-\r
-HTTPThread::~HTTPThread()\r
-{\r
-       shttpd_fini(m_ctx);\r
-\r
-       for(std::vector<IPageHandler *>::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++)\r
-       {\r
-               delete (*i);\r
-       }\r
-\r
+       StringFunctions::Convert(portstr,m_listenport);\r
 }\r
 \r
-void HTTPThread::PageCallback(shttpd_arg *arg)\r
+void HTTPThread::run()\r
 {\r
+       m_log->debug("HTTPThread::run thread started.");\r
 \r
-       HTTPThread *thread=(HTTPThread *)arg->user_data;\r
-\r
-       for(std::vector<IPageHandler *>::iterator i=thread->m_pagehandlers.begin(); i!=thread->m_pagehandlers.end(); )\r
-       {\r
-               if((*i)->Handle(arg)==true)\r
-               {\r
-                       i=thread->m_pagehandlers.end();\r
-               }\r
-               else\r
-               {\r
-                       i++;\r
-               }\r
-       }\r
+       Poco::Net::ServerSocket sock(m_listenport);\r
+       Poco::Net::HTTPServerParams* pParams = new Poco::Net::HTTPServerParams;\r
+       pParams->setMaxQueued(30);\r
+       pParams->setMaxThreads(5);\r
+       Poco::Net::HTTPServer srv(new FMSHTTPRequestHandlerFactory,sock,pParams);\r
 \r
-}\r
-\r
-void HTTPThread::Run()\r
-{\r
-       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread started.");\r
+       srv.start();\r
+       m_log->trace("Started HTTPServer");\r
 \r
        do\r
        {\r
-               shttpd_poll(m_ctx,1000);\r
+               Poco::Thread::sleep(1000);\r
        }while(!IsCancelled());\r
 \r
-       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread exiting.");\r
+       m_log->trace("Trying to stop HTTPServer");\r
+       srv.stop();\r
+       m_log->trace("Stopped HTTPServer");\r
+       \r
+       m_log->trace("Waiting for current HTTP requests to finish");\r
+       while(srv.currentConnections()>0)\r
+       {\r
+               Poco::Thread::sleep(500);\r
+       }\r
+\r
+       m_log->debug("HTTPThread::run thread exiting.");\r
 \r
 }\r