-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppFrame.cpp
More file actions
277 lines (222 loc) · 5.59 KB
/
Copy pathAppFrame.cpp
File metadata and controls
277 lines (222 loc) · 5.59 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "AppFrame.h"
#include <wx/sstream.h>
#include <wx/xml/xml.h>
#include <wx/richmsgdlg.h>
#include <curl/curl.h>
size_t WriteData(void* ptr, size_t size, size_t nmemb, FILE* stream)
{
size_t Written = fwrite(ptr, size, nmemb, stream);
return Written;
}
unsigned long long DigitFromString(const wxString& String)
{
if (String.IsEmpty())
return 0;
unsigned long long Result = 0;
try
{
Result = std::stoull(String.ToStdString());
}
catch (std::exception& except)
{
wxRichMessageDialog Dlg(nullptr, wxString::Format(_("Caught an exception:\n\n %s"), wxString(except.what())),
_("Something Went Wrong:"), wxOK | wxCENTRE);
Dlg.ShowModal();
}
return Result;
}
wxString SizeToString(unsigned long long Size)
{
char buffer[256] = { 0 };
if (Size < _1MB)
{
sprintf(buffer, "%.02f KB", (double)Size / 1024);
return wxString(buffer);
}
else if (Size >= _1MB && Size < _1GB)
{
sprintf(buffer, "%.02f MB", (double)Size / _1MB);
return wxString(buffer);
}
sprintf(buffer, "%.02f GB", (double)Size / _1GB);
return wxString(buffer);
}
size_t SendEventCurlWriteCallback(void* contents, size_t size, size_t nmemb, std::string* response)
{
size_t NewLength = size * nmemb;
try
{
response->append((char*)contents, NewLength);
}
catch (std::bad_alloc& except)
{
wxRichMessageDialog Dlg(nullptr, wxString::Format(_("Caught an exception:\n\n %s"), wxString(except.what())),
_("Something Went Wrong:"), wxOK | wxCENTRE);
Dlg.ShowModal();
return 0;
}
return NewLength;
}
wxString SecondsToTime(long Seconds)
{
char buffer[50] = { 0 };
sprintf(buffer, "%02ld:%02ld:%02ld", Seconds / 3600, (Seconds % 3600) / 60, Seconds % 60);
return wxString(buffer);
}
AppFrame::AppFrame(wxWindow* parent)
: AppFrameBase(parent)
{
Load();
}
AppFrame::~AppFrame()
{
}
void AppFrame::OnClose(wxCloseEvent& event)
{
Destroy();
}
void AppFrame::OnExit(wxCommandEvent& event)
{
Destroy();
}
void AppFrame::OnMinimize(wxCommandEvent& event)
{
}
void AppFrame::OnBackgroundTimer(wxTimerEvent& event)
{
CheckForUpdates();
}
void AppFrame::Load()
{
if (wxFileExists(CookieFile))
{
wxRemoveFile(CookieFile);
}
CURLcode Response;
CURL* Curl = curl_easy_init();
if (!Curl)
{
return;
}
const wxString HomeURL = "http://192.168.8.1/html/home.html";
std::string OutputString;
curl_easy_setopt(Curl, CURLOPT_URL, HomeURL.mb_str(wxConvUTF8).data());
curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, SendEventCurlWriteCallback);
curl_easy_setopt(Curl, CURLOPT_WRITEDATA, &OutputString);
curl_easy_setopt(Curl, CURLOPT_VERBOSE, true);
curl_easy_setopt(Curl, CURLOPT_COOKIEJAR, CookieFile.mb_str(wxConvUTF8).data());
curl_easy_setopt(Curl, CURLOPT_TIMEOUT, 5L);
Response = curl_easy_perform(Curl);
curl_easy_cleanup(Curl);
m_pBackgroundTimer->Start(1000);
if (Response != CURLE_OK)
{
const wxString empty = "";
CurrentConnectTime->SetValue(empty);
CurrentUpload->SetValue(empty);
CurrentDownload->SetValue(empty);
TotalUpload->SetValue(empty);
TotalDownload->SetValue(empty);
TotalUploadDownload->SetValue(empty);
}
}
void AppFrame::CheckForUpdates()
{
CURLcode Response;
CURL* Curl = curl_easy_init();
if (!Curl)
{
return;
}
const wxString StatsURL = "http://192.168.8.1/api/monitoring/traffic-statistics";
std::string OutputString;
curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, SendEventCurlWriteCallback);
curl_easy_setopt(Curl, CURLOPT_WRITEDATA, &OutputString);
curl_easy_setopt(Curl, CURLOPT_URL, StatsURL.mb_str(wxConvUTF8).data());
curl_easy_setopt(Curl, CURLOPT_VERBOSE, true);
curl_easy_setopt(Curl, CURLOPT_COOKIEFILE, CookieFile.mb_str(wxConvUTF8).data());
Response = curl_easy_perform(Curl);
if (Response != CURLE_OK)
{
const wxString empty = "";
CurrentConnectTime->SetValue(empty);
CurrentUpload->SetValue(empty);
CurrentDownload->SetValue(empty);
TotalUpload->SetValue(empty);
TotalDownload->SetValue(empty);
TotalUploadDownload->SetValue(empty);
return;
}
wxStringInputStream InputStream(OutputString);
wxXmlDocument doc;
{
wxLogNull LogNoMore;
if (!doc.Load(InputStream))
{
return;
}
}
wxXmlNode* child = doc.GetRoot()->GetChildren();
unsigned long long TotalUploadBytes = 0, TotalDownloadBytes = 0;
while (child)
{
wxString ChildName = child->GetName();
if (ChildName == "code")
{
wxString ChildContent = child->GetNodeContent();
unsigned long long Code = DigitFromString(ChildContent);
// Error
if (Code == 125002)
{
curl_easy_cleanup(Curl);
m_pBackgroundTimer->Stop();
Sleep(2000);
Load();
return;
}
}
else if (ChildName == "error")
{
curl_easy_cleanup(Curl);
return;
}
wxXmlNode* pSubChild = child->GetChildren();
if (!pSubChild)
{
curl_easy_cleanup(Curl);
return;
}
wxString ChildText = pSubChild->GetContent();
unsigned long long Digit = DigitFromString(ChildText);
if (ChildName == "CurrentConnectTime")
{
wxString t = SecondsToTime(Digit);
CurrentConnectTime->SetValue(t);
}
else if (ChildName == "CurrentUpload")
{
wxString d = SizeToString(Digit);
CurrentUpload->SetValue(d);
}
else if (ChildName == "CurrentDownload")
{
wxString d = SizeToString(Digit);
CurrentDownload->SetValue(d);
}
else if (ChildName == "TotalUpload")
{
wxString d = SizeToString(Digit);
TotalUpload->SetValue(d);
TotalUploadBytes += Digit;
}
else if (ChildName == "TotalDownload")
{
wxString d = SizeToString(Digit);
TotalDownload->SetValue(d);
TotalDownloadBytes += Digit;
TotalUploadDownload->SetValue(SizeToString(TotalUploadBytes + TotalDownloadBytes));
}
child = child->GetNext();
}
curl_easy_cleanup(Curl);
}