Commit fc82d78b authored by Micha Mueller's avatar Micha Mueller
Browse files

Add first mockup of an HttpServer (not compiling)

parent da1dca74
......@@ -11,9 +11,10 @@ DISTFILES_HASHES = bacnet-stack-0.8.5.tgz|66b69111d91432fa67a7c6c1a653434d;freei
include $(DCDBCOREPATH)/common.mk
CXXFLAGS = -std=c++11 -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG -O2 -g -Wall -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-variable -DBOOST_LOG_DYN_LINK -I$(DCDBBASEPATH)/dcdb/include -I$(DCDBDEPLOYPATH)/include
CXXFLAGS = -std=c++11 -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG -O2 -g -Wall -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-variable -DBOOST_LOG_DYN_LINK -I$(DCDBBASEPATH)/dcdb/include -I$(DCDBDEPLOYPATH)/include -I$(DCDBDEPSPATH)/cpp-netlib-0.12.0-final/deps/asio/asio/include -I$(DCDBDEPSPATH)/cpp-netlib-0.12.0-final -I$(DCDBDEPSPATH)/cpp-netlib-0.12.0-final/deps/cxxopts/src
LIBS = -L../deps/mosquitto_build/lib -L$(DCDBDEPLOYPATH)/lib/ -ldl -lmosquitto -lboost_system -lboost_thread -lboost_log_setup -lboost_log -lpthread -rdynamic
OBJS = src/dcdbpusher.o src/Configuration.o src/Sensor.o src/MQTTPusher.o
OBJS = src/dcdbpusher.o src/Configuration.o src/Sensor.o src/MQTTPusher.o src/HttpsServer.o
PLUGINS_BASE = libdcdbplugin_pdu libdcdbplugin_sysfs libdcdbplugin_ipmi libdcdbplugin_bacnet
ifeq ($(OS),Darwin)
......
global {
restAddr localhost:8000
mqttBroker localhost:1883
mqttprefix /00112233445566778899AABB0000
threads 24
......
......@@ -25,6 +25,8 @@ Configuration::Configuration(const std::string& cfgFilePath) :
//set default values for global variables
_global.daemonize = 0;
_global.restHost = "";
_global.restPort = "8000";
_global.brokerHost = "";
_global.brokerPort = 1883;
_global.mqttPrefix = "";
......@@ -62,7 +64,14 @@ bool Configuration::readGlobal() {
//read global variables
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
if (boost::iequals(global.first, "mqttBroker")) {
if (boost::iequals(global.first, "restHost")) {
_global.restHost = global.second.data();
size_t pos = _global.restHost.find(":");
if (pos != string::npos) {
_global.restPort = _global.restHost.substr(pos+1);
_global.restHost.erase(pos);
}
} else if (boost::iequals(global.first, "mqttBroker")) {
_global.brokerHost = global.second.data();
size_t pos = _global.brokerHost.find(":");
if (pos != string::npos) {
......
......@@ -16,6 +16,8 @@
typedef struct {
int daemonize;
int brokerPort;
std::string restHost;
std::string restPort;
std::string brokerHost;
std::string mqttPrefix;
std::string tempdir;
......
/*
* HttpsServer.cpp
*
* Created on: 25.05.2018
* Author: Micha Mueller
*/
#include "HttpsServer.h"
#include <iostream>
struct HttpsServer::requestHandler {
void operator()(server::request const &request, server::connection_ptr connection) {
server::string_type ip = source(request);
unsigned int port = request.source_port;
std::ostringstream data;
server::response_header headers[] = { {"Connection", "close"}, {"Content-Type", "text/plain"} };
data << "Hello, " << ip << ':' << port << '!';
connection->set_status(server::connection::ok);
connection->set_headers(boost::make_iterator_range(headers, headers + 2));
connection->write(data.str());
//response = server::response::stock_reply(server::response::ok, data.str());
}
void log(const server::string_type& message) {
LOG(error) << message;
}
};
HttpsServer::HttpsServer(const std::string& host, const std::string& port,
pluginVector_t& plugins) :
_host(host), _port(port), _plugins(plugins) {
server::options options(_handler);
_server = new server(options.address(_host).port(_port));
}
HttpsServer::~HttpsServer() {
delete _server;
}
/*
* HttpsServer.h
*
* Created on: 25.05.2018
* Author: Micha Mueller
*/
#ifndef HTTPSSERVER_H_
#define HTTPSSERVER_H_
#include "Configuration.h"
#include "Logging.h"
#include <boost/network/protocol/http/server.hpp>
/*
* Provides REST API services over configurable host and port.
*/
class HttpsServer {
struct requestHandler;
typedef boost::network::http::server<requestHandler> server;
public:
HttpsServer(const std::string& host, const std::string& port,
pluginVector_t& plugins);
virtual ~HttpsServer();
void run() {
_server->run();
}
void stop() {
_server->stop();
}
private:
std::string _host;
std::string _port;
pluginVector_t& _plugins;
server* _server;
requestHandler _handler;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
};
#endif /* HTTPSSERVER_H_ */
......@@ -31,6 +31,7 @@
#include "Sensor.h"
#include "Configuration.h"
#include "MQTTPusher.h"
#include "HttpsServer.h"
#include <boost/foreach.hpp>
#include <boost/asio.hpp>
......@@ -47,6 +48,7 @@ using namespace std;
volatile int keepRunning;
pluginVector_t plugins;
HttpsServer httpsServer;
void sigintHandler(int sig) {
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
......@@ -62,6 +64,8 @@ void sigintHandler(int sig) {
LOG(info) << "Flushing MQTT queues...";
//Stop MQTTPusher
keepRunning = 0;
//Stop https server
httpsServer.stop();
}
void sigtermHandler(int sig) {
......@@ -266,6 +270,7 @@ int main(int argc, char** argv) {
//give some feedback
LOG(info) << "Global Settings:";
LOG(info) << " REST Server: " << globalSettings.restHost << ":" << globalSettings.restPort;
LOG(info) << " Broker: " << globalSettings.brokerHost << ":" << globalSettings.brokerPort;
LOG(info) << " MQTT-prefix: " << globalSettings.mqttPrefix;
LOG(info) << " Threads: " << globalSettings.threads;
......@@ -319,9 +324,12 @@ int main(int argc, char** argv) {
threads.create_thread(bind(static_cast< size_t (boost::asio::io_service::*) () >(&boost::asio::io_service::run), &io));
}
//MQTTPusher gets his own thread
//MQTTPusher and Https server get their own threads
MQTTPusher mqttPusher(globalSettings.brokerPort, globalSettings.brokerHost, globalSettings.mqttPrefix, plugins);
httpsServer(globalSettings.restHost, globalSettings.restPort, plugins);
boost::thread mqttThread(bind(&MQTTPusher::push, &mqttPusher));
boost::thread restThread(bind(&HttpsServer::run, &httpsServer));
LOG(info) << "Threads created!";
LOG(info) << "Setup complete!";
......@@ -330,6 +338,7 @@ int main(int argc, char** argv) {
//Run until Strg+C
mqttThread.join();
restThread.join();
threads.join_all();
return 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment