/
status-server.cc
466 lines (420 loc) · 17.1 KB
/
status-server.cc
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Folve - A fuse filesystem that convolves audio files on-the-fly.
//
// Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <arpa/inet.h>
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
// Microhttpd's headers (at least v0.4.4) are broken and don't include stdarg.h
#include <stdarg.h>
#include <microhttpd.h>
#include <algorithm>
#include "folve-filesystem.h"
#include "status-server.h"
#include "util.h"
using folve::Appendf;
// To be used in CSS constant.
#define PROGRESS_WIDTH "300"
static const int kProgressWidth = 300;
static const size_t kMaxRetired = 20;
static const char kActiveAccessProgress[] = "#7070ff";
static const char kActiveBufferProgress[] = "#bbffbb";
static const char kRetiredAccessProgress[] = "#d0d0e8";
static const char kRetiredBufferProgress[] = "#e0f0e0";
static const char kSettingsUrl[] = "/settings";
// Aaah, I need to find the right Browser-Tab :)
// Sneak in a favicon without another resource access.
// TODO: make a nice icon, recognizable as something that has to do with "
// files and music ...
static const char kStartHtmlHeader[] = "<html><head>"
"<title>Folve</title>\n"
"<link rel='icon' type='image/png' "
"href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2"
"AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wJDwUlEA/UBrsA"
"AABSSURBVCjPrZIxDgAgDAKh8f9froOTirU1ssKFYqS7Q4mktAxFRQDJcsPORMDYsDCXhn331"
"9GPwHJVuaFl3l4D1+h0UjIdbTh9SpP2KQ2AgSfVAdEQGx23tOopAAAAAElFTkSuQmCC'/>\n"
"<meta http-equiv='Content-Type' content='text/html'; charset='utf-8'>\n";
// TODO: someone with a bit more stylesheet-fu can attempt to make this
// more pretty and the HTML more compact.
// Also, maybe move to external file, makes editing much faster, then compile
// that into a C-string that we can include it in the binary.
static const char kCSS[] =
"<style type='text/css'>"
" body { font-family:Sans-Serif; }\n"
" a:link { text-decoration:none; }\n"
" a:visited { text-decoration:none; }\n"
" a:hover { text-decoration:underline; }\n"
" a:active { text-decoration:underline; }\n"
" .lbox { border:1px solid black; padding-right:2em; }\n" // legend box
" .rounded_box, .filter_sel {\n"
" float: left;\n"
" margin: 5px;\n"
" margin-right: 5px;\n"
" margin-bottom: 5px;\n"
" padding: 5px 15px;\n"
" border-radius: 5px;\n"
" -moz-border-radius: 5px; }\n"
" .filter_sel { font-weight:bold; }\n"
" .active { background-color:#a0a0ff; }\n"
" .inactive { background-color:#e0e0e0; }\n"
" .inactive:hover { background-color:#e0e0ff;\n"
" color: #000000;\n"
" text-decoration:none;}\n"
" .inactive:link { color: #000000;text-decoration:none;}\n"
" .inactive:visited { color: #000000;text-decoration:none;}\n"
// CSS classes used in the file-listin. Keep things compact.
" td { text-wrap:none; white-space:nowrap; }\n"
" .fn { font-size:small; text-wrap:none; white-space:nowrap; }\n" // filename
" .pf { width:" PROGRESS_WIDTH "px;\n" // progress-
" background: white; border:1px solid black; }\n" // frame
" .nf { text-align:right; }\n" // number formatting.
" .fb { background-color:#c0c0c0;"
" border-radius: 3px;\n"
" -moz-border-radius: 3px; }\n" // format box
"</style>";
class StatusServer::HtmlFileHandler : public FileHandler {
public:
HtmlFileHandler(StatusServer *server) : FileHandler("") {
server->CreatePage(false, &content_);
memset(&stat_, 0, sizeof(stat_));
stat_.st_size = content_.length();
stat_.st_mtime = time(NULL);
stat_.st_nlink = 1;
stat_.st_mode = 0100444; // regular file. readonly.
}
virtual int Read(char *buf, size_t size, off_t offset) {
int end = size + offset;
if (end > (int) content_.length()) end = content_.length();
if (offset >= end) return 0;
memcpy(buf, content_.data() + offset, end - offset);
return end - offset;
}
virtual int Stat(struct stat *st) {
*st = stat_;
return 0;
}
// Get handler status.
virtual void GetHandlerStatus(HandlerStats *s) {}
private:
std::string content_;
struct stat stat_;
};
// Callback function called by micro http daemon. Gets the StatusServer pointer
// in the user_argument.
int StatusServer::HandleHttp(void* user_argument,
struct MHD_Connection *connection,
const char *url, const char *method,
const char *version,
const char *upload_data, size_t *upload_size,
void**) {
StatusServer* server = (StatusServer*) user_argument;
struct MHD_Response *response;
int ret;
if (strcmp(url, kSettingsUrl) == 0) {
server->SetFilter(MHD_lookup_connection_value(connection,
MHD_GET_ARGUMENT_KIND, "f"));
// We redirect to slash after this, to remove parameters from the GET-URL
response = MHD_create_response_from_data(0, (void*)"", MHD_NO, MHD_NO);
MHD_add_response_header(response, "Location", "/");
ret = MHD_queue_response(connection, 302, response);
} else {
const std::string &page = server->CreateHttpPage();
response = MHD_create_response_from_data(page.length(), (void*) page.data(),
MHD_NO, MHD_NO);
MHD_add_response_header(response, "Content-Type",
"text/html; charset=utf-8");
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
}
// Tell aggressive cachers not to do so.
MHD_add_response_header(response, "Cache-Control", "no-cache");
MHD_add_response_header(response, "Expires", "24 Nov 1972 23:42:42 GMT");
MHD_destroy_response(response);
return ret;
}
StatusServer::StatusServer(FolveFilesystem *fs)
: expunged_retired_(0),
meta_refresh_time_(-1),
filesystem_(fs), daemon_(NULL), filter_switched_(false) {
fs->handler_cache()->SetObserver(this);
}
FileHandler *StatusServer::CreateStatusFileHandler() {
return new HtmlFileHandler(this);
}
void StatusServer::SetFilter(const char *filter) {
if (filter == NULL) return;
filter_switched_ = filesystem_->SwitchCurrentConfigDir(filter);
}
bool StatusServer::Start(int port) {
daemon_ = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL,
&HandleHttp, this,
MHD_OPTION_END);
return daemon_ != NULL;
}
StatusServer::~StatusServer() {
if (daemon_) MHD_stop_daemon(daemon_);
}
bool StatusServer::show_details() {
// Right now, we just make this dependent on debug output. Could be a separate
// option.
return folve::IsDebugLogEnabled();
}
// FileHandlerCache::Observer interface.
void StatusServer::RetireHandlerEvent(FileHandler *handler) {
HandlerStats stats;
handler->GetHandlerStatus(&stats); // Get last available stats.
stats.last_access = folve::CurrentTime();
stats.status = HandlerStats::RETIRED;
folve::MutexLock l(&retired_mutex_);
retired_.push_front(stats);
while (retired_.size() > kMaxRetired) {
++expunged_retired_;
retired_.pop_back();
}
}
// The directories are user-input, so we need to sanitize stuff.
static void AppendSanitizedUrlParam(const std::string &in, std::string *out) {
for (std::string::const_iterator i = in.begin(); i != in.end(); ++i) {
if (isupper(*i) || islower(*i) || isdigit(*i)) {
out->append(1, *i);
} else {
Appendf(out, "%%%02x", (unsigned char) *i);
}
}
}
static void AppendSanitizedHTML(const std::string &in, std::string *out) {
for (std::string::const_iterator i = in.begin(); i != in.end(); ++i) {
switch (*i) {
case '<': out->append("<"); break;
case '>': out->append(">"); break;
case '&': out->append("&"); break;
default: out->append(1, *i);
}
}
}
// As ugly #defines, so that gcc can warn about printf() format problems.
#define sMessageRowHtml \
"<td>%s</td><td colspan='3' style='font-size:small;'>%s</td>"
#define sProgressRowHtml \
"<td>%s</td>" \
"<td>%s</td>" \
"<td><div class='pf'>" \
"<div style='width:%dpx;background:%s;float:left;'> </div>" \
"<div style='width:%dpx;background:%s;float:left;'> </div>" \
"<p style='clear:both;'></p>" \
"</div>\n</td><td>%s</td>"
#define sTimeColumns \
"<td class='nf'>%2d:%02d</td><td>/</td><td class='nf'>%2d:%02d</td>"
#define sDecibelColumn \
"<td class='nf'%s>%.1f dB</td>"
void StatusServer::AppendFileInfo(const char *progress_access_color,
const char *progress_buffer_color,
const HandlerStats &stats,
std::string *out) {
out->append("<tr>");
const char *status = "";
switch (stats.status) {
case HandlerStats::OPEN: status = "open"; break;
case HandlerStats::IDLE: status = "idle"; break;
case HandlerStats::RETIRED: status = " ---- "; break;
// no default to let the compiler detect new values.
}
if (!stats.message.empty()) {
Appendf(out, sMessageRowHtml, status, stats.message.c_str());
} else if (stats.access_progress == 0 && stats.buffer_progress <= 0) {
// TODO(hzeller): we really need a way to display message and progress
// bar in parallel.
Appendf(out, sMessageRowHtml, status, "Only header accessed");
} else {
float accessed = stats.access_progress;
float buffered
= stats.buffer_progress > accessed ? stats.buffer_progress - accessed : 0;
Appendf(out, sProgressRowHtml, status,
stats.in_gapless ? "→" : "",
(int) (kProgressWidth * accessed), progress_access_color,
(int) (kProgressWidth * buffered), progress_buffer_color,
stats.out_gapless ? "→" : "");
}
const int secs = stats.duration_seconds;
const int fract_sec = stats.access_progress * secs;
if (secs >= 0 && fract_sec >= 0) {
Appendf(out, sTimeColumns,
fract_sec / 60, fract_sec % 60,
secs / 60, secs % 60);
} else {
out->append("<td colspan='3'>-</td>");
}
if (stats.max_output_value > 1e-6) {
Appendf(out, sDecibelColumn,
stats.max_output_value > 1.0 ? " style='background:#FF8080;'" : "",
20 * log10f(stats.max_output_value));
} else {
out->append("<td>-</td>");
}
const char *filter_dir = stats.filter_dir.empty()
? "Pass Through" : stats.filter_dir.c_str();
Appendf(out, "<td class='fb'> %s (", stats.format.c_str());
AppendSanitizedHTML(filter_dir, out);
out->append(") </td><td class='fn'>");
AppendSanitizedHTML(stats.filename, out);
out->append("</td></tr>\n");
}
static void CreateSelection(bool for_http,
const std::set<std::string> &options,
const std::string &selected,
std::string *result) {
if (options.size() == 1) {
result->append(selected);
return;
}
typedef std::set<std::string> Set;
for (Set::const_iterator it = options.begin(); it != options.end(); ++it) {
const bool active = (*it == selected);
const char *title = it->empty() ? "None : Pass Through" : it->c_str();
if (active) {
result->append("<span class='filter_sel active'>");
AppendSanitizedHTML(title, result);
result->append("</span>");
} else if (for_http) { // only provide links if we have this in http.
Appendf(result, "<a class='filter_sel inactive' href='%s?f=",
kSettingsUrl);
AppendSanitizedUrlParam(*it, result);
result->append("'>");
AppendSanitizedHTML(title, result);
result->append("</a>\n");
}
}
}
void StatusServer::AppendSettingsForm(bool for_http, std::string *out) {
out->append("<p><span class='filter_sel'>Active filter:</span>");
std::set<std::string> available_dirs = filesystem_->GetAvailableConfigDirs();
CreateSelection(for_http,
available_dirs, filesystem_->current_config_subdir(), out);
if (available_dirs.empty() == 1) {
out->append(" (This is a boring configuration, add filter directories)");
} else if (filter_switched_) {
out->append("<span class='rounded_box' "
"style='font-size:small;background:#FFFFa0;'>"
"Affects re- or newly opened files.</span>");
filter_switched_ = false; // only show once.
}
out->append("</p><hr style='clear:both;'/>");
}
struct CompareStats {
bool operator() (const HandlerStats *a, const HandlerStats *b) {
if (a->status < b->status) return true; // open before idle.
else if (a->status > b->status) return false;
return b->last_access < a->last_access; // reverse time.
}
};
const std::string &StatusServer::CreateHttpPage() {
CreatePage(true, &http_content_);
return http_content_;
}
void StatusServer::CreatePage(bool for_http, std::string *content) {
const double start = folve::CurrentTime();
// We re-use a string to avoid re-allocing memory every time we generate
// a page. Since we run with MHD_USE_SELECT_INTERNALLY, this is only accessed
// by one thread.
content->clear();
content->append(kStartHtmlHeader);
if (for_http && meta_refresh_time_ > 0) {
Appendf(content, "<meta http-equiv='refresh' content='%d'>\n",
meta_refresh_time_);
}
content->append(kCSS);
content->append("</head>\n");
content->append("<body>\n");
Appendf(content, "<center style='background-color:#A0FFA0;'>"
"Welcome to "
"<a href='https://github.com/hzeller/folve#readme'>Folve</a> "
FOLVE_VERSION "</center>\n");
if (show_details()) {
Appendf(content, "Convolving audio files from <code>%s</code>; "
"Filter base directory <code>%s</code>\n",
filesystem_->underlying_dir().c_str(),
filesystem_->base_config_dir().c_str());
}
AppendSettingsForm(for_http, content);
std::vector<HandlerStats> stat_list;
filesystem_->handler_cache()->GetStats(&stat_list);
if (show_details()) {
Appendf(content, "Total opening files <b>%d</b> "
".. and re-opened from recency cache <b>%d</b><br/>",
filesystem_->total_file_openings(),
filesystem_->total_file_reopen());
}
content->append("<h3>Accessed Recently</h3>\n");
if (filesystem_->pre_buffer_size() > 0) {
Appendf(content,
"Accessed: <span class='lbox' style='background:%s;'> </span> "
" Predictive Buffer: "
"<span class='lbox' style='background:%s;'> </span> ",
kActiveAccessProgress, kActiveBufferProgress);
}
if (filesystem_->gapless_processing()) {
content->append("Gapless transfers indicated with '→'\n");
}
content->append("<table>\n");
Appendf(content, "<tr><th>Stat</th><td><!--gapless in--></td>"
"<th width='%dpx'>Progress</th>" // progress bar.
"<td><!-- gapless out --></td>"
"<th>Pos</th><td></td><th>Len</th><th>Max out</th>"
"<th>Format (used filter)</th>"
"<th align='left'>File</th></tr>\n", kProgressWidth);
// ICE in arm 2.7.1 compiler if we sort values. So sort pointers
// to the values instead.
std::vector<HandlerStats *> stat_ptrs;
for (size_t i = 0; i < stat_list.size(); ++i) {
stat_ptrs.push_back(&stat_list[i]);
}
CompareStats comparator;
std::sort(stat_ptrs.begin(), stat_ptrs.end(), comparator);
for (size_t i = 0; i < stat_ptrs.size(); ++i) {
AppendFileInfo(kActiveAccessProgress, kActiveBufferProgress, *stat_ptrs[i],
content);
}
content->append("</table><hr/>\n");
if (retired_.size() > 0) {
content->append("<h3>Retired</h3>\n");
content->append("<table>\n");
folve::MutexLock l(&retired_mutex_);
for (RetiredList::const_iterator it = retired_.begin();
it != retired_.end(); ++it) {
AppendFileInfo(kRetiredAccessProgress, kRetiredBufferProgress, *it,
content);
}
content->append("</table>\n");
if (expunged_retired_ > 0) {
Appendf(content, "... (%d more)<p></p>", expunged_retired_);
}
content->append("<hr/>");
}
const double duration = folve::CurrentTime() - start;
Appendf(content,
"<span style='float:left;font-size:x-small;'>%.2fms</span>"
"<span style='float:right;font-size:x-small;'>"
"© 2012 Henner Zeller"
" | Folve is free software and comes with no warranty. "
" | Conveyed under the terms of the "
"<a href='http://www.gnu.org/licenses/gpl.html'>GPLv3</a>.</span>"
"</body></html>\n", duration * 1000.0);
}