Commit 95e04ac5 authored by louiz’'s avatar louiz’

Include a basic webserver inside the master server

parent 00b0b45e
......@@ -43,6 +43,7 @@ find_package(Boost 1.48.0 COMPONENTS filesystem system iostreams locale serializ
find_package(BOTAN REQUIRED)
find_package(Protobuf REQUIRED)
find_package(LITESQL REQUIRED)
find_package(WEBSOCKETPP REQUIRED)
find_package(OpenSSL REQUIRED)
if(BUILD_CLIENT)
......@@ -61,6 +62,7 @@ include_directories(${Boost_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${BOTAN_INCLUDE_DIRS})
include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${WEBSOCKETPP_INCLUDE_DIRS})
if(GTEST_FOUND)
include_directories(${GTEST_INCLUDE_DIRS})
endif()
......
......@@ -13,7 +13,7 @@ int main()
log_debug(Config::get("db_login", ""));
srand(getpid());
MasterServer s(7878, 7877);
MasterServer s(7878, 7877, 9002);
s.run();
......
#include <master_server/master_server.hpp>
MasterServer::MasterServer(const short client_port,
const short slave_port):
const short slave_port,
const short web_port):
to_client_server(this, client_port),
to_slave_server(this, slave_port)
to_slave_server(this, slave_port),
to_web_server(web_port)
{
}
......@@ -16,6 +18,5 @@ void MasterServer::run()
{
this->to_client_server.start();
this->to_slave_server.start();
while (true)
IoService::get().poll();
IoService::get().run();
}
......@@ -3,13 +3,15 @@
#include <master_server/master_to_client_server.hpp>
#include <master_server/master_to_slave_server.hpp>
#include <master_server/master_to_web_server.hpp>
#include <database/database.hpp>
class MasterServer
{
public:
MasterServer(const short client_port, const short slave_port);
MasterServer(const short client_port, const short slave_port,
const short web_port);
~MasterServer() = default;
Database* get_database();
void run();
......@@ -17,6 +19,7 @@ public:
private:
MasterToClientServer to_client_server;
MasterToSlaveServer to_slave_server;
MasterToWebServer to_web_server;
Database database;
......
#include <master_server/master_to_web_server.hpp>
#include <iostream>
MasterToWebServer::MasterToWebServer(const short port):
WebsocketServer(port)
{
}
void MasterToWebServer::on_message(wspp::connection_hdl hdl, WsServer::message_ptr msg)
{
std::cout << "message " << msg->get_payload() << std::endl;
}
void MasterToWebServer::on_open(wspp::connection_hdl hdl)
{
std::cout << "open" << std::endl;
}
void MasterToWebServer::on_close(wspp::connection_hdl hdl)
{
std::cout << "close" << std::endl;
}
#ifndef MASTER_TO_WEB_SERVER_HPP_INCLUDED
#define MASTER_TO_WEB_SERVER_HPP_INCLUDED
#include <network/websocket_server.hpp>
class MasterToWebServer: public WebsocketServer
{
public:
MasterToWebServer(const short port);
~MasterToWebServer() = default;
void on_message(wspp::connection_hdl hdl, WsServer::message_ptr msg) override final;
void on_open(wspp::connection_hdl hdl) override final;
void on_close(wspp::connection_hdl hdl) override final;
private:
MasterToWebServer(const MasterToWebServer&) = delete;
MasterToWebServer(MasterToWebServer&&) = delete;
MasterToWebServer& operator=(const MasterToWebServer&) = delete;
MasterToWebServer& operator=(MasterToWebServer&&) = delete;
};
#endif /* MASTER_TO_WEB_SERVER_HPP_INCLUDED */
#include <network/websocket_server.hpp>
#include <functional>
WebsocketServer::WebsocketServer(const short port)
{
this->server.set_message_handler([this](wspp::connection_hdl hdl, WsServer::message_ptr msg)
{
this->on_message(hdl, msg);
});
this->server.set_open_handler([this](wspp::connection_hdl hdl)
{
this->on_open(hdl);
});
this->server.set_close_handler([this](wspp::connection_hdl hdl)
{
this->on_close(hdl);
});
this->server.init_asio(&IoService::get());
this->server.listen(port);
this->server.start_accept();
}
#ifndef WEBSOCKET_SERVER_HPP_INCLUDED
#define WEBSOCKET_SERVER_HPP_INCLUDED
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <network/ioservice.hpp>
namespace wspp = websocketpp;
using WsServer = wspp::server<websocketpp::config::asio>;
class WebsocketServer
{
public:
WebsocketServer(const short port);
~WebsocketServer() = default;
protected:
virtual void on_message(wspp::connection_hdl hdl, WsServer::message_ptr msg) = 0;
virtual void on_open(wspp::connection_hdl hdl) = 0;
virtual void on_close(wspp::connection_hdl hdl) = 0;
private:
WsServer server;
WebsocketServer(const WebsocketServer&) = delete;
WebsocketServer(WebsocketServer&&) = delete;
WebsocketServer& operator=(const WebsocketServer&) = delete;
WebsocketServer& operator=(WebsocketServer&&) = delete;
};
#endif /* WEBSOCKET_SERVER_HPP_INCLUDED */
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