Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 237 lines (213 sloc) 9.39 kB
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
1 // Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 #include <arpa/inet.h>
17 #include <fcntl.h>
b5c1e96 @hzeller o more useful output on status server.
authored
18 #include <stdarg.h>
19 #include <stdio.h>
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
20 #include <stdlib.h>
21 #include <sys/select.h>
22 #include <sys/socket.h>
23 #include <sys/time.h>
24 #include <sys/types.h>
25
26 #include <microhttpd.h>
27
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
28 #include "folve-filesystem.h"
c1a03f2 @hzeller o Needed convenient sub-second resolution time. Added CurrentTime()
authored
29 #include "status-server.h"
30 #include "util.h"
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
31
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
32 // TODO: someone with a bit more stylesheet-fu can attempt to make this
33 // more pretty and the HTML more compact.
34
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
35 static const size_t kMaxRetired = 200;
b698853 @hzeller o README updates.
authored
36 static const int kProgressWidth = 300;
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
37 static const char kActiveProgress[] = "#7070ff";
38 static const char kRetiredProgress[] = "#d0d0d0";
39
78e66dd @hzeller o I need to find my browser-tab :) Add favicon.
authored
40 // Aaah, I need to find the right Browser-Tab :)
41 // Sneak in a favicon without another resource access.
42 // TODO: make a nice icon, recognizable as something that has to do with "
43 // files and music ...
b15aaf9 @hzeller o add title.
authored
44 static const char kHtmlHeader[] = "<header>"
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
45 "<title>Folve</title>\n"
b15aaf9 @hzeller o add title.
authored
46 "<link rel='icon' type='image/png'"
78e66dd @hzeller o I need to find my browser-tab :) Add favicon.
authored
47 "href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2"
48 "AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wJDwUlEA/UBrsA"
49 "AABSSURBVCjPrZIxDgAgDAKh8f9froOTirU1ssKFYqS7Q4mktAxFRQDJcsPORMDYsDCXhn331"
b15aaf9 @hzeller o add title.
authored
50 "9GPwHJVuaFl3l4D1+h0UjIdbTh9SpP2KQ2AgSfVAdEQGx23tOopAAAAAElFTkSuQmCC'/>\n"
78e66dd @hzeller o I need to find my browser-tab :) Add favicon.
authored
51 "</header>";
52
b698853 @hzeller o README updates.
authored
53 // Callback function called by micro http daemon. Gets the StatusServer pointer
54 // in the user_argument.
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
55 int StatusServer::HandleHttp(void* user_argument,
56 struct MHD_Connection *connection,
57 const char *url, const char *method,
58 const char *version,
59 const char *upload_data, size_t *upload_size,
60 void**) {
61 StatusServer* server = (StatusServer*) user_argument;
62 struct MHD_Response *response;
63 int ret;
64 const char *buffer;
65 size_t size;
66 server->CreatePage(&buffer, &size);
b698853 @hzeller o README updates.
authored
67 response = MHD_create_response_from_data(size, (void*) buffer, MHD_NO, MHD_NO);
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
68 MHD_add_response_header(response, "Content-Type", "text/html; charset=utf-8");
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
69 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
70 MHD_destroy_response(response);
71 return ret;
72 }
73
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
74 StatusServer::StatusServer(FolveFilesystem *fs)
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
75 : total_seconds_filtered_(0), total_seconds_music_seen_(0),
76 filesystem_(fs), daemon_(NULL) {
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
77 fs->handler_cache()->SetObserver(this);
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
78 }
79
80 bool StatusServer::Start(int port) {
81 daemon_ = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL,
82 &HandleHttp, this,
83 MHD_OPTION_END);
84 return daemon_ != NULL;
85 }
86
87 StatusServer::~StatusServer() {
b698853 @hzeller o README updates.
authored
88 if (daemon_) MHD_stop_daemon(daemon_);
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
89 }
90
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
91 // FileHandlerCache::Observer interface.
92 void StatusServer::RetireHandlerEvent(FileHandler *handler) {
93 HandlerStats stats;
94 handler->GetHandlerStatus(&stats); // Get last available stats.
95 if (stats.progress >= 0) {
96 total_seconds_music_seen_ += stats.total_duration_seconds;
97 total_seconds_filtered_ += stats.total_duration_seconds * stats.progress;
98 }
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
99 stats.last_access = folve::CurrentTime();
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
100 stats.status = HandlerStats::RETIRED;
101 retired_.push_front(stats);
102 while (retired_.size() > kMaxRetired)
103 retired_.pop_back();
104 }
105
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
106 static void Appendf(std::string *str, const char *format, ...) {
107 va_list ap;
108 const size_t orig_len = str->length();
109 const size_t space = 1024;
110 str->resize(orig_len + space);
111 va_start(ap, format);
112 int written = vsnprintf((char*)str->data() + orig_len, space, format, ap);
113 va_end(ap);
114 str->resize(orig_len + written);
115 }
116
117 static const char sMessageRowHtml[] = "<td>%s</td><td>%s</td>"
b5c1e96 @hzeller o more useful output on status server.
authored
118 "<td colspan='3'>-</td>";
119
120 static const char sProgressRowHtml[] =
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
121 "<td>%s</td><td>"
b5c1e96 @hzeller o more useful output on status server.
authored
122 "<div style='width:%dpx; border:1px solid black;'>\n"
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
123 " <div style='width:%d%%;background:%s;'>&nbsp;</div>\n</div></td>"
b5c1e96 @hzeller o more useful output on status server.
authored
124 "<td align='right'>%2d:%02d</td><td>/</td><td align='right'>%2d:%02d</td>";
125
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
126 static void AppendFileInfo(std::string *result, const char *progress_style,
127 const HandlerStats &stats) {
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
128 result->append("<tr style='text-wrap:none;white-space:nowrap;'>");
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
129 const char *status = "";
130 switch (stats.status) {
131 case HandlerStats::OPEN: status = "open"; break;
132 case HandlerStats::IDLE: status = "idle"; break;
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
133 case HandlerStats::RETIRED: status = "&nbsp;----&nbsp;"; break;
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
134 // no default to let the compiler detect new values.
135 }
136 if (stats.progress <= 0) {
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
137 Appendf(result, sMessageRowHtml, status,
138 (stats.progress < 0)
139 ? "Not a sound file or no filter found. Pass through."
140 : "Only Header accessed.");
b5c1e96 @hzeller o more useful output on status server.
authored
141 } else {
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
142 const int secs = stats.total_duration_seconds;
143 const int fract_sec = stats.progress * secs;
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
144 Appendf(result, sProgressRowHtml, status,
145 kProgressWidth, (int) (100 * stats.progress), progress_style,
146 fract_sec / 60, fract_sec % 60, secs / 60, secs % 60);
b5c1e96 @hzeller o more useful output on status server.
authored
147 }
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
148 Appendf(result, "<td bgcolor='#c0c0c0'>&nbsp;%s&nbsp;</td>",
149 stats.format.c_str());
150 Appendf(result,"<td "
151 "style='font-size:small;text-wrap:none;white-space:nowrap'>%s</td>",
152 stats.filename.c_str());
b5c1e96 @hzeller o more useful output on status server.
authored
153 result->append("</tr>\n");
154 }
155
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
156 struct CompareStats {
157 bool operator() (const HandlerStats &a, const HandlerStats &b) {
158 if (a.status < b.status) return true; // open before idle.
159 else if (a.status > b.status) return false;
160 return b.last_access < a.last_access; // reverse time.
161 }
162 };
163
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
164 void StatusServer::CreatePage(const char **buffer, size_t *size) {
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
165 const double start = folve::CurrentTime();
b698853 @hzeller o README updates.
authored
166 // We re-use a string to avoid re-allocing memory every time we generate
167 // a page. Since we run with MHD_USE_SELECT_INTERNALLY, this is only accessed
168 // by one thread.
b5c1e96 @hzeller o more useful output on status server.
authored
169 current_page_.clear();
78e66dd @hzeller o I need to find my browser-tab :) Add favicon.
authored
170 current_page_.append(kHtmlHeader);
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
171 current_page_.append("<body style='font-family:Sans-Serif;'>\n");
b698853 @hzeller o README updates.
authored
172 Appendf(&current_page_, "<center style='background-color:#A0FFA0;'>"
173 "Welcome to Folve %s</center>\n"
174 "Convolving audio files from <code>%s</code><br/>\n",
0aee3bb @hzeller o Show base dir that is mounted.
authored
175 filesystem_->version().c_str(), filesystem_->underlying_dir().c_str());
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
176
177 std::vector<HandlerStats> stat_list;
178 filesystem_->handler_cache()->GetStats(&stat_list);
179
562b493 @hzeller o Print out config file name while spotting a problem
authored
180 // Get statistics of active files to add to the existing ones.
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
181 double active_music_seen = 0.0;
182 double active_filtered = 0.0;
183 for (size_t i = 0; i < stat_list.size(); ++i) {
184 const HandlerStats &stats = stat_list[i];
185 if (stats.progress >= 0) {
186 active_music_seen += stats.total_duration_seconds;
187 active_filtered += stats.total_duration_seconds * stats.progress;
188 }
189 }
190 const int t_seen = total_seconds_music_seen_ + active_music_seen;
191 const int t_filtered = total_seconds_filtered_ + active_filtered;
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
192 Appendf(&current_page_, "Total opening files <b>%d</b> "
193 ".. and re-opened from recency cache <b>%d</b><br/>",
194 filesystem_->total_file_openings(),
195 filesystem_->total_file_reopen());
196 Appendf(&current_page_, "Total music seen <b>%dd %d:%02d:%02d</b> ",
197 t_seen / 86400, (t_seen % 86400) / 3600,
198 (t_seen % 3600) / 60, t_seen % 60);
199 Appendf(&current_page_, ".. and convolved <b>%dd %d:%02d:%02d</b> ",
200 t_filtered / 86400, (t_filtered % 86400) / 3600,
201 (t_filtered % 3600) / 60, t_filtered % 60);
202 Appendf(&current_page_, "(%.1f%%)<br/>",
203 (t_seen == 0) ? 0.0 : (100.0 * t_filtered / t_seen));
204
205 Appendf(&current_page_, "<h3>Recent Files</h3>\n%ld in recency cache\n",
206 stat_list.size());
207
b5c1e96 @hzeller o more useful output on status server.
authored
208 current_page_.append("<table>\n");
b698853 @hzeller o README updates.
authored
209 Appendf(&current_page_, "<tr><th>Stat</th><th width='%dpx'>Progress</th>"
210 "<th>Pos</th><td></td><th>Len</th><th>Format</th>"
211 "<th align='left'>File</th></tr>\n", kProgressWidth);
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
212 CompareStats comparator;
213 std::sort(stat_list.begin(), stat_list.end(), comparator);
214 for (size_t i = 0; i < stat_list.size(); ++i) {
215 AppendFileInfo(&current_page_, kActiveProgress, stat_list[i]);
b5c1e96 @hzeller o more useful output on status server.
authored
216 }
217 current_page_.append("</table><hr/>\n");
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
218
219 if (retired_.size() > 0) {
220 current_page_.append("<h3>Retired</h3>\n");
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
221 current_page_.append("<table>\n");
b698853 @hzeller o README updates.
authored
222 for (RetiredList::const_iterator it = retired_.begin();
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
223 it != retired_.end(); ++it) {
224 AppendFileInfo(&current_page_, kRetiredProgress, *it);
225 }
226 current_page_.append("</table><hr/>\n");
227 }
228
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
229 const double duration = folve::CurrentTime() - start;
230 Appendf(&current_page_,
231 "<span style='float:left;font-size:small;'>page-gen %.2fms</span>"
232 "<span style='float:right;font-size:small;'>HZ</span>"
233 "</body>", duration * 1000.0);
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
234 *buffer = current_page_.data();
235 *size = current_page_.size();
236 }
Something went wrong with that request. Please try again.