Add hiredis async adapter and pub/sub subscription
This commit is contained in:
1 parent
98b7d0a89e
commit
0de78c459b
5 files changed
+139
-8
No files matched your search
@@ -36,6 +36,7 @@ set(LIBS asynccpp asyncaudio asynccore svxmisc ${LIBS})
|
|||||||
add_executable(svxreflector
|
add_executable(svxreflector
|
||||||
svxreflector.cpp Reflector.cpp ReflectorClient.cpp TGHandler.cpp TrunkLink.cpp
|
svxreflector.cpp Reflector.cpp ReflectorClient.cpp TGHandler.cpp TrunkLink.cpp
|
||||||
SatelliteLink.cpp SatelliteClient.cpp MqttPublisher.cpp RedisStore.cpp
|
SatelliteLink.cpp SatelliteClient.cpp MqttPublisher.cpp RedisStore.cpp
|
||||||
|
RedisAsyncAdapter.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(svxreflector ${LIBS})
|
target_link_libraries(svxreflector ${LIBS})
|
||||||
set_target_properties(svxreflector PROPERTIES
|
set_target_properties(svxreflector PROPERTIES
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#include "RedisAsyncAdapter.h"
|
||||||
|
#include <hiredis/hiredis.h>
|
||||||
|
#include <hiredis/async.h>
|
||||||
|
#include <AsyncFdWatch.h>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
struct AdapterData
|
||||||
|
{
|
||||||
|
redisAsyncContext* ac;
|
||||||
|
Async::FdWatch* rd = nullptr;
|
||||||
|
Async::FdWatch* wr = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
void adapterAddRead(void* priv) {
|
||||||
|
auto* d = static_cast<AdapterData*>(priv);
|
||||||
|
if (d->rd) d->rd->setEnabled(true);
|
||||||
|
}
|
||||||
|
void adapterDelRead(void* priv) {
|
||||||
|
auto* d = static_cast<AdapterData*>(priv);
|
||||||
|
if (d->rd) d->rd->setEnabled(false);
|
||||||
|
}
|
||||||
|
void adapterAddWrite(void* priv) {
|
||||||
|
auto* d = static_cast<AdapterData*>(priv);
|
||||||
|
if (d->wr) d->wr->setEnabled(true);
|
||||||
|
}
|
||||||
|
void adapterDelWrite(void* priv) {
|
||||||
|
auto* d = static_cast<AdapterData*>(priv);
|
||||||
|
if (d->wr) d->wr->setEnabled(false);
|
||||||
|
}
|
||||||
|
void adapterCleanup(void* priv) {
|
||||||
|
auto* d = static_cast<AdapterData*>(priv);
|
||||||
|
delete d->rd;
|
||||||
|
delete d->wr;
|
||||||
|
delete d;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
bool RedisAsyncAdapter::attach(redisAsyncContext* ac) {
|
||||||
|
if (!ac || ac->ev.data != nullptr) return false;
|
||||||
|
auto* d = new AdapterData;
|
||||||
|
d->ac = ac;
|
||||||
|
int fd = ac->c.fd;
|
||||||
|
d->rd = new Async::FdWatch(fd, Async::FdWatch::FD_WATCH_RD);
|
||||||
|
d->wr = new Async::FdWatch(fd, Async::FdWatch::FD_WATCH_WR);
|
||||||
|
d->rd->setEnabled(false);
|
||||||
|
d->wr->setEnabled(false);
|
||||||
|
d->rd->activity.connect(
|
||||||
|
[ac](Async::FdWatch*){ redisAsyncHandleRead(ac); });
|
||||||
|
d->wr->activity.connect(
|
||||||
|
[ac](Async::FdWatch*){ redisAsyncHandleWrite(ac); });
|
||||||
|
ac->ev.data = d;
|
||||||
|
ac->ev.addRead = adapterAddRead;
|
||||||
|
ac->ev.delRead = adapterDelRead;
|
||||||
|
ac->ev.addWrite = adapterAddWrite;
|
||||||
|
ac->ev.delWrite = adapterDelWrite;
|
||||||
|
ac->ev.cleanup = adapterCleanup;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef REDIS_ASYNC_ADAPTER_INCLUDED
|
||||||
|
#define REDIS_ASYNC_ADAPTER_INCLUDED
|
||||||
|
|
||||||
|
struct redisAsyncContext;
|
||||||
|
|
||||||
|
class RedisAsyncAdapter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// Attach hiredis async callbacks to the Async event loop via FdWatch.
|
||||||
|
// Returns false if the context is already attached or invalid.
|
||||||
|
// The adapter cleans itself up when hiredis invokes ev.cleanup
|
||||||
|
// (on disconnect/free).
|
||||||
|
static bool attach(redisAsyncContext* ac);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* REDIS_ASYNC_ADAPTER_INCLUDED */
|
||||||
@@ -1,7 +1,29 @@
|
|||||||
#include "RedisStore.h"
|
#include "RedisStore.h"
|
||||||
#include <hiredis/hiredis.h>
|
#include <hiredis/hiredis.h>
|
||||||
|
#include <hiredis/async.h>
|
||||||
|
#include "RedisAsyncAdapter.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void onPubSubMessageThunk(redisAsyncContext* /*ac*/, void* reply, void* priv) {
|
||||||
|
auto* self = static_cast<RedisStore*>(priv);
|
||||||
|
auto* r = static_cast<redisReply*>(reply);
|
||||||
|
if (!r || r->type != REDIS_REPLY_ARRAY || r->elements < 3) return;
|
||||||
|
if (r->element[0]->type == REDIS_REPLY_STRING &&
|
||||||
|
std::string(r->element[0]->str, r->element[0]->len) == "message") {
|
||||||
|
std::string payload(r->element[2]->str, r->element[2]->len);
|
||||||
|
self->configChanged.emit(payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void onAsyncDisconnectThunk(const redisAsyncContext* ac, int status) {
|
||||||
|
auto* self = static_cast<RedisStore*>(ac->data);
|
||||||
|
if (self) self->onAsyncDisconnect(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
RedisStore::RedisStore(const Config& cfg) : m_cfg(cfg) {}
|
RedisStore::RedisStore(const Config& cfg) : m_cfg(cfg) {}
|
||||||
RedisStore::~RedisStore() {
|
RedisStore::~RedisStore() {
|
||||||
if (m_sync) { redisFree(m_sync); m_sync = nullptr; }
|
if (m_sync) { redisFree(m_sync); m_sync = nullptr; }
|
||||||
@@ -45,11 +67,10 @@ bool RedisStore::connectSync(void) {
|
|||||||
|
|
||||||
bool RedisStore::connect(void) {
|
bool RedisStore::connect(void) {
|
||||||
if (!connectSync()) return false;
|
if (!connectSync()) return false;
|
||||||
std::cout << "RedisStore: connected (sync) to "
|
if (!connectAsync()) return false;
|
||||||
<< (m_cfg.unix_socket.empty()
|
subscribe();
|
||||||
? (m_cfg.host + ":" + std::to_string(m_cfg.port))
|
std::cout << "RedisStore: connected sync+async, subscribed to "
|
||||||
: m_cfg.unix_socket)
|
<< channelName() << std::endl;
|
||||||
<< std::endl;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,8 +218,40 @@ void RedisStore::pushLiveTrunk(const std::string&, const std::string&,
|
|||||||
|
|
||||||
size_t RedisStore::liveQueueSize(void) const { return 0; }
|
size_t RedisStore::liveQueueSize(void) const { return 0; }
|
||||||
|
|
||||||
bool RedisStore::connectAsync(void) { return false; }
|
bool RedisStore::connectAsync(void) {
|
||||||
void RedisStore::subscribe(void) {}
|
if (!m_cfg.unix_socket.empty()) {
|
||||||
|
m_async = redisAsyncConnectUnix(m_cfg.unix_socket.c_str());
|
||||||
|
} else {
|
||||||
|
m_async = redisAsyncConnect(m_cfg.host.c_str(), m_cfg.port);
|
||||||
|
}
|
||||||
|
if (!m_async || m_async->err) {
|
||||||
|
std::cerr << "*** ERROR: Redis async connect failed: "
|
||||||
|
<< (m_async ? m_async->errstr : "alloc failed") << std::endl;
|
||||||
|
if (m_async) { redisAsyncFree(m_async); m_async = nullptr; }
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
m_async->data = this;
|
||||||
|
if (!RedisAsyncAdapter::attach(m_async)) {
|
||||||
|
redisAsyncFree(m_async); m_async = nullptr;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
redisAsyncSetDisconnectCallback(m_async, onAsyncDisconnectThunk);
|
||||||
|
|
||||||
|
if (!m_cfg.password.empty()) {
|
||||||
|
redisAsyncCommand(m_async, nullptr, nullptr, "AUTH %s", m_cfg.password.c_str());
|
||||||
|
}
|
||||||
|
if (m_cfg.db != 0) {
|
||||||
|
redisAsyncCommand(m_async, nullptr, nullptr, "SELECT %d", m_cfg.db);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RedisStore::subscribe(void) {
|
||||||
|
if (!m_async) return;
|
||||||
|
std::string ch = channelName();
|
||||||
|
redisAsyncCommand(m_async, onPubSubMessageThunk, this,
|
||||||
|
"SUBSCRIBE %s", ch.c_str());
|
||||||
|
}
|
||||||
void RedisStore::scheduleReconnect(void) {}
|
void RedisStore::scheduleReconnect(void) {}
|
||||||
void RedisStore::onAsyncDisconnect(int) {}
|
void RedisStore::onAsyncDisconnect(int) {}
|
||||||
void RedisStore::onPubSubMessage(const std::string&, const std::string&) {}
|
void RedisStore::onPubSubMessage(const std::string&, const std::string&) {}
|
||||||
|
|||||||
@@ -81,6 +81,8 @@ class RedisStore : public sigc::trackable
|
|||||||
uint64_t droppedLiveWrites(void) const { return m_dropped_live_writes; }
|
uint64_t droppedLiveWrites(void) const { return m_dropped_live_writes; }
|
||||||
size_t liveQueueSize(void) const;
|
size_t liveQueueSize(void) const;
|
||||||
|
|
||||||
|
void onAsyncDisconnect(int status);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Config m_cfg;
|
Config m_cfg;
|
||||||
redisContext* m_sync = nullptr;
|
redisContext* m_sync = nullptr;
|
||||||
@@ -97,7 +99,6 @@ class RedisStore : public sigc::trackable
|
|||||||
bool connectAsync(void);
|
bool connectAsync(void);
|
||||||
void subscribe(void);
|
void subscribe(void);
|
||||||
void scheduleReconnect(void);
|
void scheduleReconnect(void);
|
||||||
void onAsyncDisconnect(int status);
|
|
||||||
void onPubSubMessage(const std::string& channel, const std::string& payload);
|
void onPubSubMessage(const std::string& channel, const std::string& payload);
|
||||||
void drainLiveQueue(Async::Timer*);
|
void drainLiveQueue(Async::Timer*);
|
||||||
void refreshLiveExpire(Async::Timer*);
|
void refreshLiveExpire(Async::Timer*);
|
||||||
|
|||||||
Reference in new issue
Block a user