diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2013-04-18 02:08:47 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2013-04-18 02:08:47 +0200 |
commit | 1ba325a1679acc7692cb9f674cfbe452cf763e12 (patch) | |
tree | c9ed99c010df16c485af172057325b06182dace9 /datastore-leveldb/src/main.cpp | |
parent | 89ea9e946a7da8ba2ac425182e1867b7fe064d2c (diff) | |
download | ebus-alt-1ba325a1679acc7692cb9f674cfbe452cf763e12.tar.gz ebus-alt-1ba325a1679acc7692cb9f674cfbe452cf763e12.zip |
remove libmagic, fix sighandler
Diffstat (limited to 'datastore-leveldb/src/main.cpp')
-rw-r--r-- | datastore-leveldb/src/main.cpp | 103 |
1 files changed, 37 insertions, 66 deletions
diff --git a/datastore-leveldb/src/main.cpp b/datastore-leveldb/src/main.cpp index dab88cd..f7f2f5d 100644 --- a/datastore-leveldb/src/main.cpp +++ b/datastore-leveldb/src/main.cpp @@ -2,7 +2,6 @@ extern "C" { #include <unistd.h> #include <signal.h> #include <string.h> -#include <magic.h> #include "mongoose.h" } @@ -25,8 +24,6 @@ std::forward_list<std::pair<boost::regex,std::function<void(const boost::cmatch& static std::map<std::string,leveldb::DB*> dbs; -static magic_t magic_cookie; - struct mg_context *ctx; bool sensor_name_is_sane(std::string& name) { @@ -67,26 +64,6 @@ leveldb::DB *getDB(std::string& name) { } } -// see http_parser.h -// char DELETE = 0; -// char GET = 1; -// char HEAD = 2; -// char POST = 3; -// char PUT = 4; - - -static inline void http_ok(int fd, const char *content_type, const char *extra_headers) { -#define write_const(fd, text) \ - write(fd, text, strlen(text)); - - write_const(fd, "HTTP/1.1 200 OK\r\nContent-Type: "); - write_const(fd, content_type); - write_const(fd, "\r\n"); - if (extra_headers) write_const(fd, extra_headers); - write_const(fd, "\r\n"); -#undef write_const -} - std::string make_key(uint64_t timestamp) { std::stringstream key; key << "ts-"; @@ -129,11 +106,11 @@ void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n\r\n"); return; } - + mg_printf(conn, - "HTTP/1.1 200 Value received\r\n" - "Content-Type: application/json; encoding=UTF-8\r\n" - "\r\n"); + "HTTP/1.1 200 Value received\r\n" + "Content-Type: application/json; encoding=UTF-8\r\n" + "\r\n"); std::cout << "sensor=" << sensor << " start=" << start << " end=" << end << std::endl; @@ -145,33 +122,33 @@ void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn bool first = true; std::ostringstream outbuf; for (it->Seek(key_start); - it->Valid() && cmp->Compare(it->key(), key_end) < 0; - it->Next()) { - const char *key = it->key().data(); - size_t key_size = it->key().size(); - if (key_size != 20+3) { - std::cerr << "invalid key" << std::endl; - return; - } - - int offset = 3; //ts- - // skip zeros in timestamp - while (offset < key_size-1 and *(key+offset) == '0') - offset++; - - if (first) - first = false; - else - outbuf << ','; - - outbuf << '['; - outbuf.write(key+offset, key_size-offset); + it->Valid() && cmp->Compare(it->key(), key_end) < 0; + it->Next()) { + const char *key = it->key().data(); + size_t key_size = it->key().size(); + if (key_size != 20+3) { + std::cerr << "invalid key" << std::endl; + return; + } + + int offset = 3; //ts- + // skip zeros in timestamp + while (offset < key_size-1 and *(key+offset) == '0') + offset++; + + if (first) + first = false; + else outbuf << ','; - outbuf.write(it->value().data(), it->value().size()); - outbuf << ']'; - mg_write(conn, outbuf.str().c_str(), outbuf.tellp()); - outbuf.seekp(0); + outbuf << '['; + outbuf.write(key+offset, key_size-offset); + outbuf << ','; + outbuf.write(it->value().data(), it->value().size()); + outbuf << ']'; + + mg_write(conn, outbuf.str().c_str(), outbuf.tellp()); + outbuf.seekp(0); } mg_printf(conn, "]}\r\n"); delete it; @@ -188,11 +165,13 @@ int begin_request_handler(struct mg_connection *conn) { return 1; } } - + return 0; } +struct sigaction old_action; + void sigint_handler(int s) { // Stop the server. mg_stop(ctx); @@ -203,7 +182,6 @@ void sigint_handler(int s) { delete (*it).second; dbs.erase(it); } - exit(0); } @@ -227,22 +205,15 @@ int main(int argc, char **argv) { boost::regex("/api/range/([a-zA-Z0-9]+)/([0-9]+)/([0-9]+)"), web_handle_api_range)); - magic_cookie = magic_open(MAGIC_MIME_TYPE); - magic_load(magic_cookie, NULL); - // Signals: handle C-c - struct sigaction on_sigint; - on_sigint.sa_handler = sigint_handler; - sigemptyset(&on_sigint.sa_mask); - on_sigint.sa_flags = 0; - - sigaction(SIGINT, &on_sigint, NULL); - + struct sigaction action; + memset(&action, 0, sizeof(action)); + action.sa_handler = &sigint_handler; + sigaction(SIGINT, &action, &old_action); // Start the web server. ctx = mg_start(&callbacks, NULL, options); - - while (1) sleep(1); + pause(); return 1; } |