1 /*******************************************************
2 Copyright (C) 2014 James Higley
3 Copyright (C) 2014 Guan Lisheng (guanlisheng@gmail.com)
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ********************************************************/
19
20 #include "webserver.h"
21 #include "defs.h"
22 #include "platfdep.h"
23 #include "paths.h"
24 #include "mongoose/mongoose.h"
25 #include "singleton.h"
26 #include "model/Model_Setting.h"
27
event_to_name(enum mg_event ev)28 std::string event_to_name(enum mg_event ev)
29 {
30 switch (ev)
31 {
32 case MG_POLL: return "MG_POLL";
33 case MG_CONNECT: return "MG_CONNECT";
34 case MG_AUTH: return "MG_AUTH";
35 case MG_REQUEST: return "MG_REQUEST";
36 case MG_REPLY: return "MG_REPLY";
37 case MG_CLOSE: return "MG_CLOSE";
38 case MG_LUA: return "MG_LUA";
39 case MG_HTTP_ERROR: return "MG_HTTP_ERROR";
40 default: return "UNKNOWN";
41 }
42 }
43
ev_handler(struct mg_connection * conn,enum mg_event ev)44 static int ev_handler(struct mg_connection *conn, enum mg_event ev)
45 {
46 wxLogDebug("%s, RUI: %s, TYPE: %s", conn->request_method, conn->uri, event_to_name(ev));
47 if (ev == MG_AUTH) return MG_TRUE;
48
49 return MG_FALSE;
50 }
51
WebServerThread()52 WebServerThread::WebServerThread(): wxThread()
53 {
54 }
55
~WebServerThread()56 WebServerThread::~WebServerThread()
57 {
58 }
59
Entry()60 wxThread::ExitCode WebServerThread::Entry()
61 {
62 // Get user setting
63 int webserverPort = Model_Setting::instance().GetIntSetting("WEBSERVERPORT", 8080);
64 const wxString& strPort = wxString::Format("%d", webserverPort);
65
66 // Create and configure the server
67 struct mg_server *server = mg_create_server(nullptr, ev_handler);
68 mg_set_option(server, "listening_port", strPort.mb_str());
69 mg_set_option(server, "document_root", wxFileName(mmex::getReportIndex()).GetPath().mb_str());
70 wxSetWorkingDirectory(mg_get_option(server, "document_root"));
71
72 // Serve requests
73 while (IsAlive())
74 {
75 mg_poll_server(server, 1000);
76 }
77
78 // Cleanup, and free server instance
79 mg_destroy_server(&server);
80
81 return (wxThread::ExitCode)0;
82 }
83
Mongoose_Service()84 Mongoose_Service::Mongoose_Service(): m_thread(0)
85 {
86 }
87
~Mongoose_Service()88 Mongoose_Service::~Mongoose_Service()
89 {}
90
91 Mongoose_Service&
instance()92 Mongoose_Service::instance()
93 {
94 return Singleton<Mongoose_Service>::instance();
95 }
96
open()97 int Mongoose_Service::open()
98 {
99 this->svc();
100 return 0;
101 }
102
svc()103 int Mongoose_Service::svc()
104 {
105 if (Model_Setting::instance().GetBoolSetting("ENABLEWEBSERVER", true))
106 {
107 m_thread = new WebServerThread();
108 wxLogDebug("Mongoose Service started");
109 m_thread->Run();
110 }
111 return 0;
112 }
113
stop()114 int Mongoose_Service::stop()
115 {
116 if (m_thread != 0)
117 {
118 if (m_thread->Delete() == wxTHREAD_NO_ERROR)
119 {
120 wxLogDebug("Mongoose Service ended.");
121 }
122 else
123 {
124 wxLogError("Can't delete the thread!");
125 }
126 }
127 return 0;
128 }
129