From 87f4134201f0a0840415aab531e32ca714bc7a36 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Thu, 18 Apr 2013 17:34:27 +0200 Subject: leveldb geht so --- datastore-leveldb/src/db.cpp | 3 +- datastore-leveldb/src/main.cpp | 78 ---------------------------------------- datastore-leveldb/src/server.cpp | 78 ++++++++++++++++++++++++++++++++++++++++ datastore-leveldb/src/web.cpp | 27 +++++++------- 4 files changed, 95 insertions(+), 91 deletions(-) delete mode 100644 datastore-leveldb/src/main.cpp create mode 100644 datastore-leveldb/src/server.cpp (limited to 'datastore-leveldb/src') diff --git a/datastore-leveldb/src/db.cpp b/datastore-leveldb/src/db.cpp index a952512..84e30f3 100644 --- a/datastore-leveldb/src/db.cpp +++ b/datastore-leveldb/src/db.cpp @@ -12,7 +12,8 @@ static bool sensor_name_is_sane(std::string& name) { for (auto it = name.begin(); it != name.end(); ++it) { if (not ((*it >= '0' and *it <= '9') or (*it >= 'A' and *it <= 'Z') or - (*it >= 'a' and *it <= 'z'))) { + (*it >= 'a' and *it <= 'z') or + (*it == '.'))) { return false; } } diff --git a/datastore-leveldb/src/main.cpp b/datastore-leveldb/src/main.cpp deleted file mode 100644 index 3000a25..0000000 --- a/datastore-leveldb/src/main.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include -#include -#include - -#include -#include "mongoose.h" - -#include "db.h" -#include "web.h" - -typedef void(web_handler_t)(const boost::cmatch&, struct mg_connection *conn); - -std::forward_list>> web_handler; - -struct mg_context *ctx; - -int begin_request_handler(struct mg_connection *conn) { - boost::cmatch match; - - const struct mg_request_info *request_info = mg_get_request_info(conn); - - for (auto item = web_handler.begin(); item != web_handler.end(); ++item) { - if (boost::regex_match(request_info->uri, match, (*item).first)) { - std::cout << request_info->request_method << " " - << request_info->uri << std::endl; - (*item).second(match, conn); - return 1; - } - } - - return 0; -} - -struct sigaction old_action; - -// Stop the server. -void sigint_handler(int s) { - mg_stop(ctx); - closeDB(); - exit(0); -} - -int main(int argc, char **argv) { - struct mg_callbacks callbacks; - const char *options[] = { - "listening_ports", "8080", - "document_root", "wwwroot", - NULL}; - - memset(&callbacks, 0, sizeof(callbacks)); - callbacks.begin_request = begin_request_handler; - - // Routing - web_handler.push_front(std::make_pair( - web_handle_api_value_R, - web_handle_api_value)); - web_handler.push_front(std::make_pair( - web_handle_api_range_R, - web_handle_api_range)); - web_handler.push_front(std::make_pair( - web_handle_api_range_size_R, - web_handle_api_range_size)); - - - // Signals: handle C-c - 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) - std::this_thread::sleep_for(std::chrono::seconds(1)); - return 1; -} diff --git a/datastore-leveldb/src/server.cpp b/datastore-leveldb/src/server.cpp new file mode 100644 index 0000000..3000a25 --- /dev/null +++ b/datastore-leveldb/src/server.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include + +#include +#include "mongoose.h" + +#include "db.h" +#include "web.h" + +typedef void(web_handler_t)(const boost::cmatch&, struct mg_connection *conn); + +std::forward_list>> web_handler; + +struct mg_context *ctx; + +int begin_request_handler(struct mg_connection *conn) { + boost::cmatch match; + + const struct mg_request_info *request_info = mg_get_request_info(conn); + + for (auto item = web_handler.begin(); item != web_handler.end(); ++item) { + if (boost::regex_match(request_info->uri, match, (*item).first)) { + std::cout << request_info->request_method << " " + << request_info->uri << std::endl; + (*item).second(match, conn); + return 1; + } + } + + return 0; +} + +struct sigaction old_action; + +// Stop the server. +void sigint_handler(int s) { + mg_stop(ctx); + closeDB(); + exit(0); +} + +int main(int argc, char **argv) { + struct mg_callbacks callbacks; + const char *options[] = { + "listening_ports", "8080", + "document_root", "wwwroot", + NULL}; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.begin_request = begin_request_handler; + + // Routing + web_handler.push_front(std::make_pair( + web_handle_api_value_R, + web_handle_api_value)); + web_handler.push_front(std::make_pair( + web_handle_api_range_R, + web_handle_api_range)); + web_handler.push_front(std::make_pair( + web_handle_api_range_size_R, + web_handle_api_range_size)); + + + // Signals: handle C-c + 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) + std::this_thread::sleep_for(std::chrono::seconds(1)); + return 1; +} diff --git a/datastore-leveldb/src/web.cpp b/datastore-leveldb/src/web.cpp index 2adf8eb..efff6b9 100644 --- a/datastore-leveldb/src/web.cpp +++ b/datastore-leveldb/src/web.cpp @@ -23,7 +23,7 @@ static inline bool parse_key(leveldb::Slice &&key, uint64_t *value) { const boost::regex web_handle_api_value_R( - "/api/value/([a-zA-Z0-9]+)/([0-9]+)"); + "/api/value/([a-zA-Z0-9\\.]+)/([0-9]+)"); void web_handle_api_value(const boost::cmatch &match, struct mg_connection *conn) { std::string sensor(match[1].str()); uint64_t timestamp = std::stoul(match[2].str()); @@ -69,7 +69,7 @@ static inline void print_json_tuple(struct mg_connection *conn, const boost::regex web_handle_api_range_R( - "/api/range/([a-zA-Z0-9]+)/([0-9]+)/([0-9]+)"); + "/api/range/([a-zA-Z0-9\\.]+)/([0-9]+)/([0-9]+)"); void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn) { std::string sensor(match[1].str()); uint64_t start = std::stoul(match[2].str()); @@ -86,11 +86,13 @@ void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn mg_printf(conn, "HTTP/1.1 200 OK\r\n" - "Content-Type: application/json; encoding=UTF-8\r\n" + "Content-Type: application/json\r\n" "\r\n"); std::ostringstream out; - out << "{'sensor':'" << sensor << "', 'error':null, 'data':["; + out << "{\"sensor\":\"" << sensor << "\", " + << "\"error\":null, " + << "\"data\":["; mg_write(conn, out.str().c_str(), out.str().size()); leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions()); @@ -109,7 +111,7 @@ void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn } const boost::regex web_handle_api_range_size_R( - "/api/range/([a-zA-Z0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)"); + "/api/range/([a-zA-Z0-9\\.]+)/([0-9]+)/([0-9]+)/([0-9]+)"); void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection *conn) { std::string sensor(match[1].str()); uint64_t start = std::stoul(match[2].str()); @@ -125,17 +127,17 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection mg_printf(conn, "HTTP/1.1 200 OK\r\n" - "Content-Type: application/json; encoding=UTF-8\r\n" + "Content-Type: application/json\r\n" "\r\n"); uint64_t step = std::max((uint64_t)1, (end-start) / size); std::ostringstream out; - out << "{'sensor':'" << sensor << "', " - << "'error':null, " - << "'size':" << size << ", " - << "'step':" << step << ", " - << "'data':["; + out << "{\"sensor\":\"" << sensor << "\", " + << "\"error\":null, " + << "\"size\":" << size << ", " + << "\"step\":" << step << ", " + << "\"data\":["; mg_write(conn, out.str().c_str(), out.str().size()); bool first = true; @@ -144,6 +146,7 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions()); for (uint64_t key = start; key <= end; key += step) { it->Seek(make_key(key)); + if (!it->Valid()) continue; if (first) first = false; else outbuf << ","; @@ -152,6 +155,6 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection outbuf.seekp(0); actual_size++; } - mg_printf(conn, "], 'actual_size':%ld}\r\n", actual_size); + mg_printf(conn, "], \"actual_size\":%ld}\r\n", actual_size); delete it; } -- cgit v1.2.1