version 0.1.6
[fms.git] / src / http / httpthread.cpp
1 #include "../../include/http/httpthread.h"\r
2 #include "../../include/option.h"\r
3 #include "../../include/stringfunctions.h"\r
4 #include "../../include/http/pages/homepage.h"\r
5 \r
6 #include <iostream>\r
7 \r
8 #ifdef XMEM\r
9         #include <xmem.h>\r
10 #endif\r
11 \r
12 HTTPThread::HTTPThread()\r
13 {\r
14 \r
15         int port;\r
16         std::string portstr;\r
17         Option::Instance()->Get("HTTPListenPort",portstr);\r
18         StringFunctions::Convert(portstr,port);\r
19 \r
20         m_pagehandlers.push_back(new HomePage());\r
21 \r
22         m_ctx=0;\r
23         m_ctx=shttpd_init(NULL,"listen_ports",portstr.c_str(),NULL);\r
24         shttpd_listen(m_ctx,port,false);\r
25 \r
26         shttpd_register_uri(m_ctx,"*",HTTPThread::PageCallback,this);\r
27 \r
28 }\r
29 \r
30 HTTPThread::~HTTPThread()\r
31 {\r
32         shttpd_fini(m_ctx);\r
33 \r
34         for(std::vector<IPageHandler *>::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++)\r
35         {\r
36                 delete (*i);\r
37         }\r
38 \r
39 }\r
40 \r
41 void HTTPThread::PageCallback(shttpd_arg *arg)\r
42 {\r
43 \r
44         HTTPThread *thread=(HTTPThread *)arg->user_data;\r
45 \r
46         for(std::vector<IPageHandler *>::iterator i=thread->m_pagehandlers.begin(); i!=thread->m_pagehandlers.end(); )\r
47         {\r
48                 if((*i)->Handle(arg)==true)\r
49                 {\r
50                         i=thread->m_pagehandlers.end();\r
51                 }\r
52                 else\r
53                 {\r
54                         i++;\r
55                 }\r
56         }\r
57 \r
58 }\r
59 \r
60 void HTTPThread::Run()\r
61 {\r
62         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread started.");\r
63 \r
64         do\r
65         {\r
66                 shttpd_poll(m_ctx,1000);\r
67         }while(!IsCancelled());\r
68 \r
69         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread exiting.");\r
70 \r
71 }\r