mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
Lint
This commit is contained in:
+24
-17
@@ -3,32 +3,36 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "spdlog/async.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
#include "message_parser.hpp"
|
||||
#include "simulators.hpp"
|
||||
#include "websocket.hpp"
|
||||
#include "message_parser.hpp"
|
||||
|
||||
class Spdlog_buffer : public std::stringbuf
|
||||
{
|
||||
public:
|
||||
Spdlog_buffer(std::function <void(std::string)> log_function) : log_function(log_function) {}
|
||||
int sync() {
|
||||
public:
|
||||
Spdlog_buffer(std::function<void(std::string)> log_function) : log_function(log_function)
|
||||
{
|
||||
}
|
||||
int sync()
|
||||
{
|
||||
std::string str = this->str();
|
||||
str.pop_back();
|
||||
log_function(str);
|
||||
this->str("");
|
||||
return 0;
|
||||
}
|
||||
private:
|
||||
std::function <void(std::string)> log_function;
|
||||
|
||||
private:
|
||||
std::function<void(std::string)> log_function;
|
||||
};
|
||||
|
||||
Simulators* g_simulators;
|
||||
Websocket* g_web_socket;
|
||||
Simulators *g_simulators;
|
||||
Websocket *g_web_socket;
|
||||
Spdlog_buffer g_info_buffer([](std::string message) { spdlog::info(message); });
|
||||
Spdlog_buffer g_error_buffer([](std::string message) { spdlog::error(message); });
|
||||
std::ostream g_out(&g_info_buffer);
|
||||
@@ -52,10 +56,11 @@ void int_handler(int s)
|
||||
void init_logger()
|
||||
{
|
||||
spdlog::init_thread_pool(8192, 1);
|
||||
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
|
||||
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("log-sim_us.txt", 1024*1024*10, 3);
|
||||
std::vector<spdlog::sink_ptr> sinks {stdout_sink, rotating_sink};
|
||||
auto logger = std::make_shared<spdlog::async_logger>("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
|
||||
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("log-sim_us.txt", 1024 * 1024 * 10, 3);
|
||||
std::vector<spdlog::sink_ptr> sinks{stdout_sink, rotating_sink};
|
||||
auto logger = std::make_shared<spdlog::async_logger>("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(),
|
||||
spdlog::async_overflow_policy::block);
|
||||
spdlog::set_default_logger(logger);
|
||||
spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");
|
||||
spdlog::flush_every(std::chrono::seconds(3));
|
||||
@@ -80,14 +85,16 @@ int main(void)
|
||||
spdlog::info("Starting server");
|
||||
Simulators simulators;
|
||||
g_simulators = &simulators;
|
||||
|
||||
Websocket web_socket(9002, &g_out, &g_err, [](std::string message) { return message_parser(message); }, []() { return g_simulators->changed_UI_items().dump(); });
|
||||
|
||||
Websocket web_socket(
|
||||
9002, &g_out, &g_err, [](std::string message) { return message_parser(message); },
|
||||
[]() { return g_simulators->changed_UI_items().dump(); });
|
||||
g_web_socket = &web_socket;
|
||||
|
||||
// Start the message processor and broadcast processor in separate threads
|
||||
std::thread message_processor_thread(std::bind(&Websocket::process_messages, &web_socket));
|
||||
std::thread broadcast_processor_thred(std::bind(&Websocket::process_broadcast, &web_socket));
|
||||
|
||||
|
||||
// Run the asio loop with the main thread
|
||||
web_socket.run();
|
||||
|
||||
|
||||
+62
-60
@@ -1,8 +1,8 @@
|
||||
#include "message_parser.hpp"
|
||||
#include <magic_enum.hpp>
|
||||
#include <iostream>
|
||||
#include <magic_enum.hpp>
|
||||
|
||||
extern Simulators* g_simulators;
|
||||
extern Simulators *g_simulators;
|
||||
|
||||
std::string message_parser(std::string message)
|
||||
{
|
||||
@@ -31,36 +31,37 @@ json command_parser(json command)
|
||||
Command cmd = magic_enum::enum_cast<Command>(type).value_or(Command::end);
|
||||
switch (cmd)
|
||||
{
|
||||
case Command::get_UI_element:
|
||||
spdlog::info("Command {}: {}", type, std::string(command.at("id")));
|
||||
j["response"]["type"] = type;
|
||||
j["response"]["UI_item"] = g_simulators->invoke_active_simulator()->get_UI_item(command.at("id"))->to_json();
|
||||
break;
|
||||
case Command::get_UI_elements:
|
||||
spdlog::info("Command {}", type);
|
||||
j["response"]["type"] = type;
|
||||
j["response"]["UI_items"] = g_simulators->invoke_active_simulator()->get_UI_items()["UI_items"];
|
||||
break;
|
||||
case Command::switch_simulator:
|
||||
spdlog::info("Command {} to {}", type, std::string(command.at("name")));
|
||||
g_simulators->switch_simulator(command.at("name"));
|
||||
j["response"]["type"] = type;
|
||||
break;
|
||||
case Command::get_active_simulator_name:
|
||||
spdlog::info("Command {}", type);
|
||||
j["response"]["type"] = type;
|
||||
j["response"]["name"] = g_simulators->active_simulator_name();
|
||||
break;
|
||||
case Command::get_simulators:
|
||||
spdlog::info("Command {}", type);
|
||||
j["response"]["type"] = type;
|
||||
j["response"]["simulators"] = g_simulators->list_simulators();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case Command::get_UI_element:
|
||||
spdlog::info("Command {}: {}", type, std::string(command.at("id")));
|
||||
j["response"]["type"] = type;
|
||||
j["response"]["UI_item"] =
|
||||
g_simulators->invoke_active_simulator()->get_UI_item(command.at("id"))->to_json();
|
||||
break;
|
||||
case Command::get_UI_elements:
|
||||
spdlog::info("Command {}", type);
|
||||
j["response"]["type"] = type;
|
||||
j["response"]["UI_items"] = g_simulators->invoke_active_simulator()->get_UI_items()["UI_items"];
|
||||
break;
|
||||
case Command::switch_simulator:
|
||||
spdlog::info("Command {} to {}", type, std::string(command.at("name")));
|
||||
g_simulators->switch_simulator(command.at("name"));
|
||||
j["response"]["type"] = type;
|
||||
break;
|
||||
case Command::get_active_simulator_name:
|
||||
spdlog::info("Command {}", type);
|
||||
j["response"]["type"] = type;
|
||||
j["response"]["name"] = g_simulators->active_simulator_name();
|
||||
break;
|
||||
case Command::get_simulators:
|
||||
spdlog::info("Command {}", type);
|
||||
j["response"]["type"] = type;
|
||||
j["response"]["simulators"] = g_simulators->list_simulators();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch(const json::exception& e)
|
||||
catch (const json::exception &e)
|
||||
{
|
||||
spdlog::error(e.what());
|
||||
}
|
||||
@@ -80,39 +81,40 @@ void event_handler(json event)
|
||||
return;
|
||||
}
|
||||
|
||||
switch(e)
|
||||
switch (e)
|
||||
{
|
||||
case Event::clicked:
|
||||
{
|
||||
spdlog::debug("Event {}: {}", type, std::string(event.at("id")));
|
||||
g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click();
|
||||
break;
|
||||
}
|
||||
case Event::value_changed:
|
||||
{
|
||||
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"]));
|
||||
g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_value((double)event.at("value"));
|
||||
break;
|
||||
}
|
||||
case Event::text_changed:
|
||||
{
|
||||
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text")));
|
||||
g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_text(std::string(event.at("text")));
|
||||
break;
|
||||
}
|
||||
case Event::selected:
|
||||
{
|
||||
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("selected")));
|
||||
g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_selected(std::string(event.at("selected")));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
case Event::clicked: {
|
||||
spdlog::debug("Event {}: {}", type, std::string(event.at("id")));
|
||||
g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click();
|
||||
break;
|
||||
}
|
||||
case Event::value_changed: {
|
||||
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"]));
|
||||
g_simulators->invoke_active_simulator()
|
||||
->get_UI_item(std::string(event.at("id")))
|
||||
->set_value((double)event.at("value"));
|
||||
break;
|
||||
}
|
||||
case Event::text_changed: {
|
||||
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text")));
|
||||
g_simulators->invoke_active_simulator()
|
||||
->get_UI_item(std::string(event.at("id")))
|
||||
->set_text(std::string(event.at("text")));
|
||||
break;
|
||||
}
|
||||
case Event::selected: {
|
||||
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("selected")));
|
||||
g_simulators->invoke_active_simulator()
|
||||
->get_UI_item(std::string(event.at("id")))
|
||||
->set_selected(std::string(event.at("selected")));
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(const json::exception& e)
|
||||
catch (const json::exception &e)
|
||||
{
|
||||
spdlog::error(e.what());
|
||||
}
|
||||
|
||||
+27
-15
@@ -1,20 +1,23 @@
|
||||
#include "simulator_base.hpp"
|
||||
|
||||
Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds interval)
|
||||
: m_name(name), m_timer([this]{ timer(); }), m_interval(interval)
|
||||
: m_name(name), m_timer([this] { timer(); }), m_interval(interval)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string Simulator_base::name() const {
|
||||
std::string Simulator_base::name() const
|
||||
{
|
||||
// Get the name of the simulator
|
||||
return m_name;
|
||||
}
|
||||
|
||||
bool Simulator_base::is_ui_id_unique(std::string id) const {
|
||||
bool Simulator_base::is_ui_id_unique(std::string id) const
|
||||
{
|
||||
// Check if the UI name is unique
|
||||
for (UI_item* item : m_UI_items) {
|
||||
if (item->id() == id) {
|
||||
for (UI_item *item : m_UI_items)
|
||||
{
|
||||
if (item->id() == id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -22,48 +25,57 @@ bool Simulator_base::is_ui_id_unique(std::string id) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Simulator_base::add_UI_item(UI_item* item) {
|
||||
void Simulator_base::add_UI_item(UI_item *item)
|
||||
{
|
||||
// Add a UI item to the simulator
|
||||
if (is_ui_id_unique(item->id()) == false) {
|
||||
if (is_ui_id_unique(item->id()) == false)
|
||||
{
|
||||
throw std::runtime_error("UI item id " + item->id() + " is not unique");
|
||||
}
|
||||
m_UI_items_map[item->id()] = item;
|
||||
m_UI_items.push_back(item);
|
||||
}
|
||||
|
||||
json Simulator_base::get_UI_items() const {
|
||||
json Simulator_base::get_UI_items() const
|
||||
{
|
||||
// Get the UI items
|
||||
json items;
|
||||
items["name"] = m_name;
|
||||
for (UI_item* item : m_UI_items) {
|
||||
for (UI_item *item : m_UI_items)
|
||||
{
|
||||
items["UI_items"].push_back(item->to_json());
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
UI_item* Simulator_base::get_UI_item(std::string id) const {
|
||||
UI_item *Simulator_base::get_UI_item(std::string id) const
|
||||
{
|
||||
// Get a UI item by id
|
||||
return m_UI_items_map.at(id);
|
||||
}
|
||||
|
||||
void Simulator_base::run() {
|
||||
void Simulator_base::run()
|
||||
{
|
||||
// Start the timer
|
||||
m_timer.setInterval(m_interval);
|
||||
run_at_startup();
|
||||
}
|
||||
|
||||
void Simulator_base::stop() {
|
||||
void Simulator_base::stop()
|
||||
{
|
||||
// Stop the simulator
|
||||
m_timer.stop();
|
||||
}
|
||||
|
||||
void Simulator_base::shutdown() {
|
||||
void Simulator_base::shutdown()
|
||||
{
|
||||
// Shutdown the simulator
|
||||
system("shutdown -h now");
|
||||
}
|
||||
|
||||
void Simulator_base::reboot() {
|
||||
void Simulator_base::reboot()
|
||||
{
|
||||
// Reboot the simulator
|
||||
system("reboot");
|
||||
}
|
||||
+76
-74
@@ -1,90 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include "UI.hpp"
|
||||
#include "timer.hpp"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "timer.hpp"
|
||||
|
||||
class Simulator_base {
|
||||
class Simulator_base
|
||||
{
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new Simulator_base object
|
||||
*
|
||||
* @param name The name of the simulator
|
||||
* @param interval The interval of the timer (default is 100ms)
|
||||
*/
|
||||
Simulator_base(std::string name, std::chrono::milliseconds interval = std::chrono::milliseconds(100));
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new Simulator_base object
|
||||
*
|
||||
* @param name The name of the simulator
|
||||
* @param interval The interval of the timer (default is 100ms)
|
||||
*/
|
||||
Simulator_base(std::string name, std::chrono::milliseconds interval = std::chrono::milliseconds(100));
|
||||
|
||||
/**
|
||||
* @brief Returns the name of the simulator
|
||||
*
|
||||
* @return std::string The name of the simulator
|
||||
*/
|
||||
std::string name() const;
|
||||
/**
|
||||
* @brief Returns the name of the simulator
|
||||
*
|
||||
* @return std::string The name of the simulator
|
||||
*/
|
||||
std::string name() const;
|
||||
|
||||
/**
|
||||
* @brief This function checks if the UI id is unique
|
||||
* (hence not already pushed in with the add_UI_item function)
|
||||
*
|
||||
* @param id The id to check
|
||||
* @return true The id is unique
|
||||
* @return false The id is already in use
|
||||
*/
|
||||
bool is_ui_id_unique(std::string id) const;
|
||||
/**
|
||||
* @brief This function checks if the UI id is unique
|
||||
* (hence not already pushed in with the add_UI_item function)
|
||||
*
|
||||
* @param id The id to check
|
||||
* @return true The id is unique
|
||||
* @return false The id is already in use
|
||||
*/
|
||||
bool is_ui_id_unique(std::string id) const;
|
||||
|
||||
/**
|
||||
* @brief This function adds a UI item to the simulator
|
||||
* only if the id is unique
|
||||
* Use this function only in the constructor of the simulator
|
||||
*
|
||||
* @param item The UI item to add
|
||||
*/
|
||||
void add_UI_item(UI_item* item);
|
||||
/**
|
||||
* @brief Get the UI items object
|
||||
*
|
||||
* @return json The UI items in json format
|
||||
*/
|
||||
json get_UI_items() const;
|
||||
/**
|
||||
* @brief This function adds a UI item to the simulator
|
||||
* only if the id is unique
|
||||
* Use this function only in the constructor of the simulator
|
||||
*
|
||||
* @param item The UI item to add
|
||||
*/
|
||||
void add_UI_item(UI_item *item);
|
||||
/**
|
||||
* @brief Get the UI items object
|
||||
*
|
||||
* @return json The UI items in json format
|
||||
*/
|
||||
json get_UI_items() const;
|
||||
|
||||
/**
|
||||
* @brief Get the UI item object by id
|
||||
*
|
||||
* @param id The id of the UI item
|
||||
* @return UI_item* A pointer to the UI item object
|
||||
*/
|
||||
UI_item* get_UI_item(std::string id) const;
|
||||
/**
|
||||
* @brief Get the UI item object by id
|
||||
*
|
||||
* @param id The id of the UI item
|
||||
* @return UI_item* A pointer to the UI item object
|
||||
*/
|
||||
UI_item *get_UI_item(std::string id) const;
|
||||
|
||||
/**
|
||||
* @brief Run the simulator
|
||||
*/
|
||||
void run();
|
||||
/**
|
||||
* @brief Run the simulator
|
||||
*/
|
||||
void run();
|
||||
|
||||
/**
|
||||
* @brief Stop the simulator
|
||||
*/
|
||||
void stop();
|
||||
/**
|
||||
* @brief Stop the simulator
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/**
|
||||
* @brief Timer function
|
||||
* This function is called every time the timer ticks
|
||||
* Override this function in the derived class
|
||||
*/
|
||||
virtual void timer() {};
|
||||
/**
|
||||
* @brief Timer function
|
||||
* This function is called every time the timer ticks
|
||||
* Override this function in the derived class
|
||||
*/
|
||||
virtual void timer() {};
|
||||
|
||||
/**
|
||||
* @brief Run at startup
|
||||
* This function is called at startup
|
||||
* Override this function in the derived class
|
||||
*/
|
||||
virtual void run_at_startup() {};
|
||||
/**
|
||||
* @brief Run at startup
|
||||
* This function is called at startup
|
||||
* Override this function in the derived class
|
||||
*/
|
||||
virtual void run_at_startup() {};
|
||||
|
||||
void shutdown();
|
||||
void reboot();
|
||||
protected:
|
||||
std::string m_name;
|
||||
std::vector<UI_item*> m_UI_items;
|
||||
std::map<std::string, UI_item*> m_UI_items_map;
|
||||
Timer m_timer;
|
||||
std::chrono::milliseconds m_interval;
|
||||
void shutdown();
|
||||
void reboot();
|
||||
|
||||
protected:
|
||||
std::string m_name;
|
||||
std::vector<UI_item *> m_UI_items;
|
||||
std::map<std::string, UI_item *> m_UI_items_map;
|
||||
Timer m_timer;
|
||||
std::chrono::milliseconds m_interval;
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "simulators.hpp"
|
||||
#include <unordered_set>
|
||||
#include "test_sim.hpp"
|
||||
#include "can_debugger.hpp"
|
||||
#include "test_sim.hpp"
|
||||
#include <unordered_set>
|
||||
|
||||
Simulators::Simulators()
|
||||
{
|
||||
@@ -66,14 +66,14 @@ void Simulators::switch_simulator(std::string name)
|
||||
std::vector<std::string> Simulators::list_simulators() const
|
||||
{
|
||||
std::vector<std::string> names;
|
||||
for (const auto& simulator : this->m_simulators)
|
||||
for (const auto &simulator : this->m_simulators)
|
||||
{
|
||||
names.push_back(simulator.first);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
Simulator_base* Simulators::invoke_active_simulator()
|
||||
Simulator_base *Simulators::invoke_active_simulator()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
if (this->m_simulators.empty())
|
||||
@@ -84,7 +84,7 @@ Simulator_base* Simulators::invoke_active_simulator()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
m_before = this->m_simulators.at(m_current_simulator)->get_UI_items();
|
||||
|
||||
return this->m_simulators.at(m_current_simulator).get();
|
||||
@@ -105,7 +105,7 @@ json Simulators::changed_UI_items()
|
||||
json after = this->m_simulators.at(m_current_simulator)->get_UI_items();
|
||||
json changed;
|
||||
changed["event"]["type"] = "UI_changed";
|
||||
|
||||
|
||||
json diff = json::diff(m_before, after);
|
||||
|
||||
if (diff.empty())
|
||||
@@ -121,18 +121,18 @@ json Simulators::changed_UI_items()
|
||||
|
||||
try
|
||||
{
|
||||
for (auto& item : diff)
|
||||
for (auto &item : diff)
|
||||
{
|
||||
std::string path = item["path"];
|
||||
size_t index = std::stoul(path.substr(10, path.find("/", 10) - 10));
|
||||
if (addedIndices.find(index) != addedIndices.end())
|
||||
continue;
|
||||
|
||||
|
||||
changed["event"]["UI_items"].push_back(after["UI_items"][index]);
|
||||
addedIndices.insert(index);
|
||||
}
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
spdlog::error(e.what());
|
||||
}
|
||||
|
||||
+18
-16
@@ -1,25 +1,27 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include "simulator_base.hpp"
|
||||
|
||||
#define INSERT_SIMULATOR(type) m_simulators.insert(std::make_pair(type().name(), std::make_unique<type>()));
|
||||
|
||||
class Simulators {
|
||||
public:
|
||||
Simulators();
|
||||
std::string active_simulator_name() const;
|
||||
void run();
|
||||
void stop();
|
||||
void switch_simulator(std::string name);
|
||||
std::vector<std::string> list_simulators() const;
|
||||
Simulator_base* invoke_active_simulator();
|
||||
json changed_UI_items();
|
||||
private:
|
||||
std::map<std::string, std::unique_ptr<Simulator_base>> m_simulators;
|
||||
std::string m_current_simulator = "";
|
||||
std::mutex m_mutex;
|
||||
json m_before;
|
||||
class Simulators
|
||||
{
|
||||
public:
|
||||
Simulators();
|
||||
std::string active_simulator_name() const;
|
||||
void run();
|
||||
void stop();
|
||||
void switch_simulator(std::string name);
|
||||
std::vector<std::string> list_simulators() const;
|
||||
Simulator_base *invoke_active_simulator();
|
||||
json changed_UI_items();
|
||||
|
||||
private:
|
||||
std::map<std::string, std::unique_ptr<Simulator_base>> m_simulators;
|
||||
std::string m_current_simulator = "";
|
||||
std::mutex m_mutex;
|
||||
json m_before;
|
||||
};
|
||||
+30
-31
@@ -1,9 +1,8 @@
|
||||
#include "websocket.hpp"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
Websocket::Websocket(uint16_t port, std::ostream* out, std::ostream* err,
|
||||
std::function<std::string(std::string)> on_message,
|
||||
std::function<std::string()> on_update)
|
||||
Websocket::Websocket(uint16_t port, std::ostream *out, std::ostream *err,
|
||||
std::function<std::string(std::string)> on_message, std::function<std::string()> on_update)
|
||||
: m_on_message(on_message), m_on_update(on_update), m_port(port)
|
||||
{
|
||||
// Set logging settings
|
||||
@@ -11,10 +10,8 @@ Websocket::Websocket(uint16_t port, std::ostream* out, std::ostream* err,
|
||||
m_server.get_elog().set_custom(err != &std::cerr);
|
||||
m_server.get_alog().set_ostream(out);
|
||||
m_server.get_elog().set_ostream(err);
|
||||
m_server.set_access_channels(websocketpp::log::alevel::connect +
|
||||
websocketpp::log::alevel::disconnect);
|
||||
m_server.clear_access_channels(websocketpp::log::alevel::frame_payload +
|
||||
websocketpp::log::alevel::frame_header);
|
||||
m_server.set_access_channels(websocketpp::log::alevel::connect + websocketpp::log::alevel::disconnect);
|
||||
m_server.clear_access_channels(websocketpp::log::alevel::frame_payload + websocketpp::log::alevel::frame_header);
|
||||
|
||||
// Initialize Asio
|
||||
m_server.init_asio();
|
||||
@@ -35,10 +32,14 @@ void Websocket::run()
|
||||
m_server.start_accept();
|
||||
// Start the ASIO io_service run loop
|
||||
m_server.run();
|
||||
} catch (websocketpp::exception const& e) {
|
||||
}
|
||||
catch (websocketpp::exception const &e)
|
||||
{
|
||||
spdlog::critical("{}", e.what());
|
||||
exit(1);
|
||||
} catch (...) {
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
spdlog::critical("other exception");
|
||||
exit(1);
|
||||
}
|
||||
@@ -94,31 +95,29 @@ void Websocket::process_messages()
|
||||
|
||||
lock.unlock();
|
||||
|
||||
switch (a.type) {
|
||||
case SUBSCRIBE:
|
||||
switch (a.type)
|
||||
{
|
||||
case SUBSCRIBE: {
|
||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||
m_connections.insert(a.hdl);
|
||||
}
|
||||
break;
|
||||
case UNSUBSCRIBE: {
|
||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||
m_connections.erase(a.hdl);
|
||||
}
|
||||
break;
|
||||
case MESSAGE: {
|
||||
std::string response = m_on_message(a.msg->get_payload());
|
||||
if (response.empty() || response == "{}")
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||
m_connections.insert(a.hdl);
|
||||
continue;
|
||||
}
|
||||
m_server.send(a.hdl, response, websocketpp::frame::opcode::TEXT);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case UNSUBSCRIBE:
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||
m_connections.erase(a.hdl);
|
||||
}
|
||||
break;
|
||||
case MESSAGE:
|
||||
{
|
||||
std::string response = m_on_message(a.msg->get_payload());
|
||||
if (response.empty() || response == "{}")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
m_server.send(a.hdl, response, websocketpp::frame::opcode::TEXT);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
-11
@@ -16,17 +16,22 @@ using websocketpp::lib::placeholders::_2;
|
||||
* on_message queue send to all channels
|
||||
*/
|
||||
|
||||
enum action_type {
|
||||
enum action_type
|
||||
{
|
||||
SUBSCRIBE,
|
||||
UNSUBSCRIBE,
|
||||
MESSAGE,
|
||||
BROADCAST
|
||||
};
|
||||
|
||||
struct action {
|
||||
action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h) {}
|
||||
action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m)
|
||||
: type(t), hdl(h), msg(m) {}
|
||||
struct action
|
||||
{
|
||||
action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h)
|
||||
{
|
||||
}
|
||||
action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m) : type(t), hdl(h), msg(m)
|
||||
{
|
||||
}
|
||||
|
||||
action_type type;
|
||||
websocketpp::connection_hdl hdl;
|
||||
@@ -35,22 +40,25 @@ struct action {
|
||||
|
||||
class Websocket
|
||||
{
|
||||
public:
|
||||
Websocket(uint16_t port, std::ostream* out, std::ostream* err, std::function<std::string(std::string)> on_message, std::function<std::string()> on_update);
|
||||
public:
|
||||
Websocket(uint16_t port, std::ostream *out, std::ostream *err, std::function<std::string(std::string)> on_message,
|
||||
std::function<std::string()> on_update);
|
||||
void run();
|
||||
void stop();
|
||||
void process_messages();
|
||||
void process_broadcast();
|
||||
private:
|
||||
|
||||
private:
|
||||
void on_open(websocketpp::connection_hdl hdl);
|
||||
void on_close(websocketpp::connection_hdl hdl);
|
||||
void on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg);
|
||||
private:
|
||||
|
||||
private:
|
||||
std::function<std::string(std::string)> m_on_message;
|
||||
std::function<std::string()> m_on_update;
|
||||
Server m_server;
|
||||
Server m_server;
|
||||
uint16_t m_port;
|
||||
typedef std::set<websocketpp::connection_hdl,std::owner_less<websocketpp::connection_hdl>> con_list;
|
||||
typedef std::set<websocketpp::connection_hdl, std::owner_less<websocketpp::connection_hdl>> con_list;
|
||||
con_list m_connections;
|
||||
|
||||
std::queue<action> m_actions;
|
||||
|
||||
Reference in New Issue
Block a user