Cleanup rpc interfaces

This commit is contained in:
2025-10-22 23:07:41 -04:00
parent c1512a1fa6
commit 6a1bf027b4
28 changed files with 224 additions and 97 deletions

View File

@@ -0,0 +1,27 @@
//
// Created by Johnathon Slightham on 2025-10-22.
//
// Although we currently only support wireless, this is architected to be easy to add other connection methods
#ifndef COMMUNICATIONFACTORY_H
#define COMMUNICATIONFACTORY_H
#include <memory>
#include <vector>
#include "IConnectionManager.h"
#include "IDiscoveryService.h"
#include "IRPCServer.h"
#include "PtrQueue.h"
#include "enums.h"
class CommunicationFactory {
public:
static std::unique_ptr<IConnectionManager> create_connection_manager(CommunicationMethod type);
static std::unique_ptr<IDiscoveryService> create_discovery_service(CommunicationMethod type);
static std::unique_ptr<IRPCServer> create_lossy_server(CommunicationMethod type, const std::shared_ptr<PtrQueue<std::vector<uint8_t>>> &rx_queue);
static std::unique_ptr<IRPCServer> create_lossless_server(CommunicationMethod type, const std::shared_ptr<PtrQueue<std::vector<uint8_t>>>& rx_queue);
};
#endif //COMMUNICATIONFACTORY_H

View File

@@ -2,6 +2,8 @@
// Created by Johnathon Slightham on 2025-05-25.
//
// todo: this class is getting a bit large
#ifndef COMMUNICATIONROUTER_H
#define COMMUNICATIONROUTER_H
@@ -9,13 +11,14 @@
#include <memory>
#include <chrono>
#include "CommunicationFactory.h"
#include "freertos/FreeRTOS.h"
#include "IDiscoveryService.h"
#include "ConfigManager.h"
#include "OrientationDetection.h"
#include "WifiManager.h"
#include "freertos/FreeRTOS.h"
#include "TCPServer.h"
#include "wireless/WifiManager.h"
#include "wireless/TCPServer.h"
#include "DataLinkManager.h"
#include "constants/tcp.h"
#include "constants/module.h"
#include "PtrQueue.h"
@@ -29,15 +32,16 @@ class CommunicationRouter {
};
public:
CommunicationRouter(const std::function<void(char*, int)> &rx_callback, std::unique_ptr<WifiManager>&& pc_connection)
explicit CommunicationRouter(const std::function<void(char*, int)> &rx_callback)
: m_tcp_rx_queue(std::make_shared<PtrQueue<std::vector<uint8_t>>>(10)),
m_rx_callback(rx_callback),
m_config_manager(ConfigManager::get_instance()),
m_tcp_server(std::make_unique<TCPServer>(TCP_PORT, m_tcp_rx_queue)),
m_pc_connection(CommunicationFactory::create_connection_manager(m_config_manager.get_communication_method())),
m_lossless_server(CommunicationFactory::create_lossless_server(m_config_manager.get_communication_method(), m_tcp_rx_queue)),
m_data_link_manager(std::make_unique<DataLinkManager>(m_config_manager.get_module_id(), MODULE_TO_NUM_CHANNELS_MAP[m_config_manager.get_module_type()])),
m_pc_connection(std::move(pc_connection)),
m_module_id(m_config_manager.get_module_id()),
m_last_leader_updated(std::chrono::system_clock::now()){
m_last_leader_updated(std::chrono::system_clock::now()),
m_discovery_service(CommunicationFactory::create_discovery_service(m_config_manager.get_communication_method())){
OrientationDetection::init();
update_leader();
@@ -67,13 +71,14 @@ public:
private:
TaskHandle_t m_router_thread = nullptr;
ConfigManager &m_config_manager;
std::unique_ptr<IConnectionManager> m_pc_connection;
std::vector<TaskHandle_t> m_link_layer_threads;
std::unique_ptr<TCPServer> m_tcp_server; // todo: dependency injection
std::unique_ptr<IRPCServer> m_lossless_server;
std::unique_ptr<DataLinkManager> m_data_link_manager;
std::unique_ptr<WifiManager> m_pc_connection; // todo: change to dependency inject
uint8_t m_leader = 0;
uint8_t m_module_id;
std::chrono::time_point<std::chrono::system_clock> m_last_leader_updated;
std::unique_ptr<IDiscoveryService> m_discovery_service;
};
#endif //COMMUNICATIONROUTER_H

View File

@@ -2,12 +2,12 @@
// Created by Johnathon Slightham on 2025-05-26.
//
#ifndef IWIFIMANAGER_H
#define IWIFIMANAGER_H
#ifndef ICONNECTIONMANAGER_H
#define ICONNECTIONMANAGER_H
class IWifiManager {
class IConnectionManager{
public:
virtual ~IWifiManager() = default;
virtual ~IConnectionManager() = default;
virtual int connect() = 0;
virtual int disconnect() = 0;
};

View File

@@ -8,8 +8,8 @@
class IDiscoveryService {
public:
virtual ~IDiscoveryService() = default;
virtual static void setup() = 0;
virtual static void set_connected_boards(const std::vector<int>& boards) = 0;
}
virtual void set_connected_boards(const std::vector<int>& boards) = 0;
};
#endif //IDISCOVERYSERVICE_H

View File

@@ -14,10 +14,10 @@
class MessagingInterface {
public:
explicit MessagingInterface(std::unique_ptr<WifiManager>&& pc_connection)
explicit MessagingInterface()
: m_config_manager(ConfigManager::get_instance()),
m_mpi_rx_queue(xQueueCreate(MAX_RX_BUFFER_SIZE, RX_QUEUE_SIZE)),
m_router(std::make_unique<CommunicationRouter>([this](const char* buffer, const int size) { handleRecv(buffer, size); }, std::move(pc_connection))),
m_router(std::make_unique<CommunicationRouter>([this](const char* buffer, const int size) { handleRecv(buffer, size); })),
m_map_semaphore(xSemaphoreCreateMutex()) {};
~MessagingInterface();

View File

@@ -1,14 +0,0 @@
//
// Created by Johnathon Slightham on 2025-05-25.
//
#ifndef RPCFACTORY_H
#define RPCFACTORY_H
class RPCFactory {
public:
static std::shared_ptr<IRPCServer> createRPCServer() {
return std::make_shared<TCPServer>();
}
#endif //RPCFACTORY_H

View File

@@ -1,19 +0,0 @@
//
// Created by Johnathon Slightham on 2025-05-25.
//
#ifndef DISCOVERYSERVICE_H
#define DISCOVERYSERVICE_H
#include <vector>
class mDNSDiscoveryService final {
public:
mDNSDiscoveryService() = delete;
~mDNSDiscoveryService() = delete;
static void setup();
static void set_connected_boards(const std::vector<int>& boards);
};
#endif //DISCOVERYSERVICE_H

View File

@@ -13,7 +13,7 @@
#include "IRPCServer.h"
#include "PtrQueue.h"
class TCPServer final : IRPCServer {
class TCPServer final : public IRPCServer {
public:
TCPServer(int port, const std::shared_ptr<PtrQueue<std::vector<uint8_t>>>& rx_queue);
~TCPServer() override;

View File

@@ -8,9 +8,9 @@
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "ConfigManager.h"
#include "IWifiManager.h"
#include "IConnectionManager.h"
class WifiManager final : IWifiManager {
class WifiManager final : public IConnectionManager {
public:
WifiManager() : m_config_manager(ConfigManager::get_instance()),
m_mutex(xSemaphoreCreateMutex()),

View File

@@ -0,0 +1,20 @@
//
// Created by Johnathon Slightham on 2025-05-25.
//
#ifndef DISCOVERYSERVICE_H
#define DISCOVERYSERVICE_H
#include <vector>
#include "IDiscoveryService.h"
class mDNSDiscoveryService final : public IDiscoveryService {
public:
mDNSDiscoveryService();
~mDNSDiscoveryService() override;
void set_connected_boards(const std::vector<int>& boards) override;
};
#endif //DISCOVERYSERVICE_H