diff --git a/src/svxlink/reflector/CMakeLists.txt b/src/svxlink/reflector/CMakeLists.txt index 4313fdd..07c5ce4 100644 --- a/src/svxlink/reflector/CMakeLists.txt +++ b/src/svxlink/reflector/CMakeLists.txt @@ -35,7 +35,7 @@ set(LIBS asynccpp asyncaudio asynccore svxmisc ${LIBS}) # Build the executable add_executable(svxreflector svxreflector.cpp Reflector.cpp ReflectorClient.cpp TGHandler.cpp TrunkLink.cpp - SatelliteLink.cpp SatelliteClient.cpp MqttPublisher.cpp + SatelliteLink.cpp SatelliteClient.cpp MqttPublisher.cpp RedisStore.cpp ) target_link_libraries(svxreflector ${LIBS}) set_target_properties(svxreflector PROPERTIES diff --git a/src/svxlink/reflector/RedisStore.cpp b/src/svxlink/reflector/RedisStore.cpp new file mode 100644 index 0000000..2983cbf --- /dev/null +++ b/src/svxlink/reflector/RedisStore.cpp @@ -0,0 +1,53 @@ +#include "RedisStore.h" +#include +#include + +RedisStore::RedisStore(const Config& cfg) : m_cfg(cfg) {} +RedisStore::~RedisStore() { + if (m_sync) { redisFree(m_sync); m_sync = nullptr; } +} + +bool RedisStore::connect(void) { + std::cerr << "RedisStore::connect not implemented yet" << std::endl; + return false; +} + +bool RedisStore::isReady(void) const { return m_sync != nullptr; } + +std::string RedisStore::lookupUserKey(const std::string&) { return ""; } +std::map RedisStore::loadAllUsers(void) { return {}; } +bool RedisStore::isUserEnabled(const std::string&) { return true; } +std::set RedisStore::loadClusterTgs(void) { return {}; } +std::string RedisStore::loadTrunkFilter(const std::string&, const std::string&) { return ""; } +std::map RedisStore::loadTrunkTgMap(const std::string&) { return {}; } +std::set RedisStore::loadTrunkMutes(const std::string&) { return {}; } +void RedisStore::addTrunkMute(const std::string&, const std::string&) {} +void RedisStore::removeTrunkMute(const std::string&, const std::string&) {} +void RedisStore::publishConfigChanged(const std::string&) {} +void RedisStore::pushLiveTalker(uint32_t, const std::string&, const std::string&) {} +void RedisStore::clearLiveTalker(uint32_t) {} +void RedisStore::pushLiveClient(const std::string&, const std::string&, + const std::string&, uint32_t) {} +void RedisStore::clearLiveClient(const std::string&) {} +void RedisStore::pushLiveTrunk(const std::string&, const std::string&, + const std::string&) {} + +size_t RedisStore::liveQueueSize(void) const { return 0; } + +bool RedisStore::connectSync(void) { return false; } +bool RedisStore::connectAsync(void) { return false; } +void RedisStore::subscribe(void) {} +void RedisStore::scheduleReconnect(void) {} +void RedisStore::onAsyncDisconnect(int) {} +void RedisStore::onPubSubMessage(const std::string&, const std::string&) {} +void RedisStore::drainLiveQueue(Async::Timer*) {} +void RedisStore::refreshLiveExpire(Async::Timer*) {} + +std::string RedisStore::keyFor(const std::string& suffix) const { + if (m_cfg.key_prefix.empty()) return suffix; + return m_cfg.key_prefix + ":" + suffix; +} + +std::string RedisStore::channelName(void) const { + return keyFor("config.changed"); +} diff --git a/src/svxlink/reflector/RedisStore.h b/src/svxlink/reflector/RedisStore.h new file mode 100644 index 0000000..108f919 --- /dev/null +++ b/src/svxlink/reflector/RedisStore.h @@ -0,0 +1,109 @@ +#ifndef REDIS_STORE_INCLUDED +#define REDIS_STORE_INCLUDED + +#include +#include +#include +#include +#include +#include + +struct redisContext; +struct redisAsyncContext; +namespace Async { class Config; class Timer; } + +class RedisAsyncAdapter; +class RedisLiveQueue; + +class RedisStore : public sigc::trackable +{ + public: + struct Config + { + std::string host; + uint16_t port = 6379; + std::string password; + int db = 0; + std::string key_prefix; + std::string unix_socket; + bool tls_enabled = false; + std::string tls_ca_cert; + std::string tls_client_cert; + std::string tls_client_key; + }; + + explicit RedisStore(const Config& cfg); + ~RedisStore(); + + // Synchronous startup connect. Returns false on failure (caller exits). + bool connect(void); + + bool isReady(void) const; + + // Emitted when a config.changed message arrives from Redis. The + // string payload identifies the scope: "users", "cluster", + // "trunk:
", or "all". + sigc::signal configChanged; + + // Schema accessors (sync). Empty results on Redis errors are logged + // and treated as "no entries". + std::string lookupUserKey(const std::string& callsign); + std::map loadAllUsers(void); + bool isUserEnabled(const std::string& callsign); + + std::set loadClusterTgs(void); + + std::string loadTrunkFilter(const std::string& section, + const std::string& field); // "blacklist"|"allow" + std::map loadTrunkTgMap(const std::string& section); + std::set loadTrunkMutes(const std::string& section); + + // Mute set ops (sync, used by PTY persistence). + void addTrunkMute(const std::string& section, const std::string& callsign); + void removeTrunkMute(const std::string& section, const std::string& callsign); + + // Publish a config.changed scope token (sync). + void publishConfigChanged(const std::string& scope); + + // Live-state push (non-blocking; enqueues onto live queue). + void pushLiveTalker(uint32_t tg, const std::string& callsign, + const std::string& source); + void clearLiveTalker(uint32_t tg); + void pushLiveClient(const std::string& callsign, + const std::string& ip, + const std::string& codecs, + uint32_t current_tg); + void clearLiveClient(const std::string& callsign); + void pushLiveTrunk(const std::string& section, + const std::string& state, + const std::string& peer_id); + + uint64_t droppedLiveWrites(void) const { return m_dropped_live_writes; } + size_t liveQueueSize(void) const; + + private: + Config m_cfg; + redisContext* m_sync = nullptr; + redisAsyncContext* m_async = nullptr; + RedisAsyncAdapter* m_adapter = nullptr; + RedisLiveQueue* m_live_queue = nullptr; + Async::Timer* m_drain_timer = nullptr; + Async::Timer* m_reconnect_timer = nullptr; + Async::Timer* m_heartbeat_timer = nullptr; + int m_reconnect_backoff_s = 1; + uint64_t m_dropped_live_writes = 0; + + bool connectSync(void); + bool connectAsync(void); + void subscribe(void); + void scheduleReconnect(void); + void onAsyncDisconnect(int status); + void onPubSubMessage(const std::string& channel, const std::string& payload); + void drainLiveQueue(Async::Timer*); + void refreshLiveExpire(Async::Timer*); + + std::string keyFor(const std::string& suffix) const; + std::string channelName(void) const; +}; + +#endif /* REDIS_STORE_INCLUDED */