From 7cfba39a23628cea4ffeb451f7bfed74ad5330ad Mon Sep 17 00:00:00 2001 From: Hannes Matuschek Date: Sun, 14 Jun 2015 16:57:11 +0200 Subject: [PATCH] Fixed FM demod and http server. --- cmd/CMakeLists.txt | 2 +- cmd/aprsapplication.cc | 87 ++++++++++++++++ cmd/aprsapplication.hh | 29 ++++++ cmd/main.cc | 23 +---- cmd/shared/index.html | 67 ++++++------ src/aprs.cc | 8 ++ src/demod.hh | 26 ++--- src/http.cc | 226 +++++++++++++++++++++++++---------------- src/http.hh | 150 +++++++++++++++++---------- 9 files changed, 404 insertions(+), 214 deletions(-) create mode 100644 cmd/aprsapplication.cc create mode 100644 cmd/aprsapplication.hh diff --git a/cmd/CMakeLists.txt b/cmd/CMakeLists.txt index bb11808..55ca216 100644 --- a/cmd/CMakeLists.txt +++ b/cmd/CMakeLists.txt @@ -3,6 +3,6 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) link_resources(sdr_cmd_resources shared/index.html) -add_executable(sdr_cmd main.cc) +add_executable(sdr_cmd main.cc aprsapplication.cc) add_dependencies(sdr_cmd sdr_cmd_resources) target_link_libraries(sdr_cmd ${LIBS} libsdr ) diff --git a/cmd/aprsapplication.cc b/cmd/aprsapplication.cc new file mode 100644 index 0000000..5b6f363 --- /dev/null +++ b/cmd/aprsapplication.cc @@ -0,0 +1,87 @@ +#include "aprsapplication.hh" + +// This file will be generated at build time +// and contains the static content served. +#include "sdr_cmd_resources.hh" + + +using namespace sdr; + +APRSApplication::APRSApplication(http::Server &server) + : APRS(), _server(server), _messages() +{ + // Register callbacks + server.addStatic("/", std::string(index_html, index_html_size), "text/html"); + server.addJSON("/spots", this, &APRSApplication::spots); + server.addHandler("/update", this, &APRSApplication::update); +} + +APRSApplication::~APRSApplication() { + // pass... +} + + +bool +APRSApplication::spots(const http::JSON &request, http::JSON &response) { + std::list msg_list; + for (std::list::iterator msg = _messages.begin(); msg != _messages.end(); msg++) { + std::map message; + message["call"] = http::JSON(msg->from().call()); + if (msg->hasLocation()) { + message["lat"] = http::JSON(msg->latitude()); + message["lon"] = http::JSON(msg->longitude()); + } + time_t time = msg->time(); + message["time"] = http::JSON(ctime(&time)); + msg_list.push_back(message); + } + response = http::JSON(msg_list); + return true; +} + +void +APRSApplication::handleAPRSMessage(const Message &message) { + _messages.push_back(message); + // Serialize JSON message + std::string json_text; + if (_clients.size()) { + std::map msg; + msg["call"] = http::JSON(message.from().call()); + if (message.hasLocation()) { + msg["lat"] = http::JSON(message.latitude()); + msg["lon"] = http::JSON(message.longitude()); + } + time_t time = message.time(); + msg["time"] = http::JSON(ctime(&time)); + http::JSON(msg).serialize(json_text); + } + // a list collecting the closed + // signal all clients connected + std::list::iterator client = _clients.begin(); + while (client != _clients.end()) { + if (client->isClosed()) { + // remove client from list + client = _clients.erase(client); + } else { + // send event + client->send("data: "); + client->send(json_text); + client->send("\n\n"); + } + } +} + +void +APRSApplication::update(const http::Request &request, http::Response &response) { + // This call back implements a server side event stream, means the response will + // be blocked until the connection is closed + response.setHeader("Content-Type", "text/event-stream"); + response.setHeader("Cache-Control", "no-cache"); + response.setStatus(http::Response::STATUS_OK); + response.sendHeaders(); + // Signal connection thread to exit without closing the connection + response.connection().setProtocolUpgrade(); + // Store connection + _clients.push_back(response.connection()); +} + diff --git a/cmd/aprsapplication.hh b/cmd/aprsapplication.hh new file mode 100644 index 0000000..f0c2c2b --- /dev/null +++ b/cmd/aprsapplication.hh @@ -0,0 +1,29 @@ +#ifndef __SDR_APRS_APRSAPPLICATION_HH__ +#define __SDR_APRS_APRSAPPLICATION_HH__ + +#include "http.hh" +#include "aprs.hh" + + +namespace sdr { + +class APRSApplication: public APRS +{ +public: + APRSApplication(http::Server &server); + ~APRSApplication(); + + bool spots(const http::JSON &request, http::JSON &response); + void update(const http::Request &request, http::Response &response); + + void handleAPRSMessage(const Message &message); + +protected: + http::Server &_server; + std::list _messages; + std::list _clients; +}; + +} + +#endif // APRSAPPLICATION_HH diff --git a/cmd/main.cc b/cmd/main.cc index 1b2ce87..e46c7d2 100644 --- a/cmd/main.cc +++ b/cmd/main.cc @@ -1,36 +1,26 @@ #include #include "queue.hh" -#include "http.hh" #include "logger.hh" #include #include -#include "sdr_cmd_resources.hh" + +#include "aprsapplication.hh" using namespace sdr; -class Application -{ -public: - Application() {} - - bool echo(const http::JSON &request, http::JSON &response) { - response = request; - return true; - } -}; - static http::Server *server = 0; static void __sigint_handler(int signo) { - server->stop(true); + if (server) { server->stop(true); } } + int main(int argc, char *argv[]) { - Application app; server = new http::Server(8080); + APRSApplication app(*server); // Install log handler sdr::Logger::get().addHandler( @@ -39,9 +29,6 @@ int main(int argc, char *argv[]) { // Register signal handler: signal(SIGINT, __sigint_handler); - // Register callbacks - server->addStatic("/", std::string(index_html, index_html_size), "text/html"); - server->addJSON("/echo", &app, &Application::echo); // start server server->start(true); diff --git a/cmd/shared/index.html b/cmd/shared/index.html index f94428c..c9e3e0b 100644 --- a/cmd/shared/index.html +++ b/cmd/shared/index.html @@ -13,43 +13,27 @@