-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimal.cpp
137 lines (113 loc) · 3.79 KB
/
minimal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include <thread>
#include <string>
#include "mongoose.h"
static void ev_handler(struct mg_connection* nc, int ev, void* p) {
if (ev == MG_EV_HTTP_REQUEST) {
auto *s_http_server_opts = (mg_serve_http_opts*)nc->user_data;
mg_serve_http(nc, (struct http_message*)p, *s_http_server_opts);
}
}
constexpr int LOOPWAITINGTIME = 20;
bool stopIt = false;
auto StartWebServer(int portNo) -> int {
mg_serve_http_opts s_http_server_opts;
memset(&s_http_server_opts, 0, sizeof(mg_serve_http_opts));
struct mg_mgr mgr;
struct mg_connection* nc;
mg_mgr_init(&mgr, nullptr);
std::string p = std::to_string(portNo);
nc = mg_bind(&mgr, p.c_str(), ev_handler);
if (nc == nullptr) {
return -1;
}
// Set up HTTP server parameters
std::string startDir = "./web-";
startDir.append(p);
mg_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = startDir.c_str(); // Serve current directory
s_http_server_opts.enable_directory_listing = "yes";
nc->user_data = &s_http_server_opts;
for (; !stopIt;) {
mg_mgr_poll(&mgr, LOOPWAITINGTIME);
}
mg_mgr_free(&mgr);
return 0;
}
//--------------------- ui
#define STARTINGPORTNO 8000
int startPortNo = STARTINGPORTNO;
class MyApp : public wxApp {
std::unordered_map<int, std::thread> webServerList;
public:
auto OnInit() -> bool override;
auto GetNumberOfServers() -> int { return webServerList.size(); }
auto StartWebServer() ->int {
webServerList[startPortNo] = std::thread(::StartWebServer, startPortNo);
return startPortNo++;
}
auto OnExit() ->int override {
stopIt = true;
for (auto& t : webServerList) {
if (t.second.joinable()) {
t.second.join();
}
}
return wxApp::OnExit();
}
};
class MyFrame : public wxFrame {
public:
MyFrame(const wxString& title);
};
enum {
Minimal_Quit = wxID_EXIT,
Minimal_About = wxID_ABOUT,
Minimal_StartWebServer
};
wxIMPLEMENT_APP(MyApp);
auto MyApp::OnInit() -> bool {
if (!wxApp::OnInit()) {
return false;
}
MyFrame *frame = new MyFrame("Minimal wxWidgets App");
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title) {
auto *fileMenu = new wxMenu;
auto *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");
fileMenu->Append(Minimal_StartWebServer, "Start &Web\tAlt-W", "Start Web Server");
fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
auto *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, "&File");
menuBar->Append(helpMenu, "&Help");
SetMenuBar(menuBar);
CreateStatusBar(3);
SetStatusText("Welcome to wxWidgets!");
Bind(wxEVT_COMMAND_MENU_SELECTED, [this](wxCommandEvent &e) {
wxMessageBox(wxString::Format(
"Welcome to %s!\n"
"\n"
"This is the minimal wxWidgets sample\n"
"running under %s.",
wxVERSION_STRING,
wxGetOsDescription()
),
"About wxWidgets minimal sample",
wxOK | wxICON_INFORMATION,
this);
}, Minimal_About);
Bind(wxEVT_COMMAND_MENU_SELECTED, [this](wxCommandEvent &e) {
Close(true);
}, Minimal_Quit);
Bind(wxEVT_COMMAND_MENU_SELECTED, [this](wxCommandEvent &e) {
int pNo = wxGetApp().StartWebServer();
SetStatusText(wxString::Format("Web server started on port %d", pNo), 1);
SetStatusText(wxString::Format("No of server(s) running : %d", wxGetApp().GetNumberOfServers()), 2);
}, Minimal_StartWebServer);
}