version 0.1.6
[fms.git] / src / http / httpthread.cpp
diff --git a/src/http/httpthread.cpp b/src/http/httpthread.cpp
new file mode 100644 (file)
index 0000000..dd4bd9b
--- /dev/null
@@ -0,0 +1,71 @@
+#include "../../include/http/httpthread.h"\r
+#include "../../include/option.h"\r
+#include "../../include/stringfunctions.h"\r
+#include "../../include/http/pages/homepage.h"\r
+\r
+#include <iostream>\r
+\r
+#ifdef XMEM\r
+       #include <xmem.h>\r
+#endif\r
+\r
+HTTPThread::HTTPThread()\r
+{\r
+\r
+       int port;\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
+}\r
+\r
+void HTTPThread::PageCallback(shttpd_arg *arg)\r
+{\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
+\r
+}\r
+\r
+void HTTPThread::Run()\r
+{\r
+       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread started.");\r
+\r
+       do\r
+       {\r
+               shttpd_poll(m_ctx,1000);\r
+       }while(!IsCancelled());\r
+\r
+       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread exiting.");\r
+\r
+}\r