Skip to content

Commit

Permalink
o main: report if parameters are not directories.
Browse files Browse the repository at this point in the history
o status-server: return page instead of awkardly setting a buffer in status server.
o status-server: report number of expunged retired files = not page anymore.
  • Loading branch information
hzeller committed Sep 16, 2012
1 parent 8268c37 commit 1e9ff21
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
20 changes: 18 additions & 2 deletions folve-main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "folve-filesystem.h"
#include "status-server.h"

#define FOLVE_VERSION "v. 0.79 — 2012-09-15"
#define FOLVE_VERSION "v. 0.8 — 2012-09-15"

// Compilation unit variables to communicate with the
static FolveFilesystem *folve_fs = NULL;
Expand Down Expand Up @@ -165,16 +165,32 @@ static int usage(const char *prg) {
return 1;
}

static bool IsDirectory(const char *path) {
struct stat st;
if (stat(path, &st) != 0)
return false;
return (st.st_mode & S_IFMT) == S_IFDIR;
}

int main(int argc, char *argv[]) {
const char *progname = argv[0];
if (argc < 4) {
return usage(argv[0]);
return usage(progname);
}

// First, let's extract our configuration.
const char *config_dir = argv[1];
underlying_dir = argv[2];
argc -=2;
argv += 2;
if (!IsDirectory(config_dir)) {
fprintf(stderr, "%s <config-dir>: not a directory.\n", config_dir);
return usage(progname);
}
if (!IsDirectory(underlying_dir)) {
fprintf(stderr, "%s <underlying-dir>: not a directory.\n", underlying_dir);
return usage(progname);
}
folve_fs = new FolveFilesystem(FOLVE_VERSION, underlying_dir, config_dir);

// TODO(hzeller): make this configurable
Expand Down
31 changes: 20 additions & 11 deletions status-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <microhttpd.h>

#include <boost/thread/locks.hpp>

#include "folve-filesystem.h"
#include "status-server.h"
#include "util.h"
Expand Down Expand Up @@ -62,18 +64,18 @@ int StatusServer::HandleHttp(void* user_argument,
StatusServer* server = (StatusServer*) user_argument;
struct MHD_Response *response;
int ret;
const char *buffer;
size_t size;
server->CreatePage(&buffer, &size);
response = MHD_create_response_from_data(size, (void*) buffer, MHD_NO, MHD_NO);
const std::string &page = server->CreatePage();
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);
MHD_destroy_response(response);
return ret;
}

StatusServer::StatusServer(FolveFilesystem *fs)
: total_seconds_filtered_(0), total_seconds_music_seen_(0),
: expunged_retired_(0), total_seconds_filtered_(0),
total_seconds_music_seen_(0),
filesystem_(fs), daemon_(NULL) {
fs->handler_cache()->SetObserver(this);
}
Expand All @@ -99,9 +101,12 @@ void StatusServer::RetireHandlerEvent(FileHandler *handler) {
}
stats.last_access = folve::CurrentTime();
stats.status = HandlerStats::RETIRED;
boost::lock_guard<boost::mutex> l(retired_mutex_);
retired_.push_front(stats);
while (retired_.size() > kMaxRetired)
while (retired_.size() > kMaxRetired) {
++expunged_retired_;
retired_.pop_back();
}
}

static const char sMessageRowHtml[] =
Expand Down Expand Up @@ -151,7 +156,7 @@ struct CompareStats {
}
};

void StatusServer::CreatePage(const char **buffer, size_t *size) {
const std::string &StatusServer::CreatePage() {
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
Expand Down Expand Up @@ -192,7 +197,7 @@ void StatusServer::CreatePage(const char **buffer, size_t *size) {
Appendf(&current_page_, "(%.1f%%)<br/>",
(t_seen == 0) ? 0.0 : (100.0 * t_filtered / t_seen));

Appendf(&current_page_, "<h3>Recent Files</h3>\n%ld in recency cache\n",
Appendf(&current_page_, "<h3>Accessed Recently</h3>\n%ld in recency cache\n",
stat_list.size());

current_page_.append("<table>\n");
Expand All @@ -209,18 +214,22 @@ void StatusServer::CreatePage(const char **buffer, size_t *size) {
if (retired_.size() > 0) {
current_page_.append("<h3>Retired</h3>\n");
current_page_.append("<table>\n");
boost::lock_guard<boost::mutex> l(retired_mutex_);
for (RetiredList::const_iterator it = retired_.begin();
it != retired_.end(); ++it) {
AppendFileInfo(&current_page_, kRetiredProgress, *it);
}
current_page_.append("</table><hr/>\n");
current_page_.append("</table>\n");
if (expunged_retired_ > 0) {
Appendf(&current_page_, "... (%d more)<p></p>", expunged_retired_);
}
current_page_.append("<hr/>");
}

const double duration = folve::CurrentTime() - start;
Appendf(&current_page_,
"<span style='float:left;font-size:small;'>page-gen %.2fms</span>"
"<span style='float:right;font-size:small;'>HZ</span>"
"</body>", duration * 1000.0);
*buffer = current_page_.data();
*size = current_page_.size();
return current_page_;
}
7 changes: 6 additions & 1 deletion status-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "file-handler-cache.h"
#include "file-handler.h"

#include <boost/thread/mutex.hpp>

class FolveFilesystem;
struct MHD_Daemon;
struct MHD_Connection;
Expand All @@ -44,14 +46,17 @@ class StatusServer : protected FileHandlerCache::Observer {
const char *, const char *, const char *,
const char *, size_t *, void **);

void CreatePage(const char **buffer, size_t *size);
const std::string &CreatePage();

// -- interface FileHandlerCache::Observer
virtual void InsertHandlerEvent(FileHandler *handler) {}
virtual void RetireHandlerEvent(FileHandler *handler);

typedef std::deque<HandlerStats> RetiredList;
RetiredList retired_;
int expunged_retired_;
boost::mutex retired_mutex_;

double total_seconds_filtered_;
double total_seconds_music_seen_;
FolveFilesystem *filesystem_;
Expand Down

0 comments on commit 1e9ff21

Please sign in to comment.