Implement RedisStore sync connect and schema reads/writes
This commit is contained in:
1 parent
b64b79f2a7
commit
6ecbf69bc0
1 file changed
+173
-11
@@ -7,23 +7,186 @@ RedisStore::~RedisStore() {
|
|||||||
if (m_sync) { redisFree(m_sync); m_sync = nullptr; }
|
if (m_sync) { redisFree(m_sync); m_sync = nullptr; }
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RedisStore::connect(void) {
|
bool RedisStore::connectSync(void) {
|
||||||
std::cerr << "RedisStore::connect not implemented yet" << std::endl;
|
struct timeval tv = { 5, 0 };
|
||||||
|
if (!m_cfg.unix_socket.empty()) {
|
||||||
|
m_sync = redisConnectUnixWithTimeout(m_cfg.unix_socket.c_str(), tv);
|
||||||
|
} else {
|
||||||
|
m_sync = redisConnectWithTimeout(m_cfg.host.c_str(), m_cfg.port, tv);
|
||||||
|
}
|
||||||
|
if (!m_sync || m_sync->err) {
|
||||||
|
std::cerr << "*** ERROR: Redis sync connect failed: "
|
||||||
|
<< (m_sync ? m_sync->errstr : "alloc failed") << std::endl;
|
||||||
|
if (m_sync) { redisFree(m_sync); m_sync = nullptr; }
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
if (!m_cfg.password.empty()) {
|
||||||
|
redisReply* r = (redisReply*)redisCommand(m_sync, "AUTH %s", m_cfg.password.c_str());
|
||||||
|
if (!r || r->type == REDIS_REPLY_ERROR) {
|
||||||
|
std::cerr << "*** ERROR: Redis AUTH failed" << std::endl;
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
redisFree(m_sync); m_sync = nullptr;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
freeReplyObject(r);
|
||||||
|
}
|
||||||
|
if (m_cfg.db != 0) {
|
||||||
|
redisReply* r = (redisReply*)redisCommand(m_sync, "SELECT %d", m_cfg.db);
|
||||||
|
if (!r || r->type == REDIS_REPLY_ERROR) {
|
||||||
|
std::cerr << "*** ERROR: Redis SELECT failed" << std::endl;
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
redisFree(m_sync); m_sync = nullptr;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
freeReplyObject(r);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RedisStore::connect(void) {
|
||||||
|
if (!connectSync()) return false;
|
||||||
|
std::cout << "RedisStore: connected (sync) to "
|
||||||
|
<< (m_cfg.unix_socket.empty()
|
||||||
|
? (m_cfg.host + ":" + std::to_string(m_cfg.port))
|
||||||
|
: m_cfg.unix_socket)
|
||||||
|
<< std::endl;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RedisStore::isReady(void) const { return m_sync != nullptr; }
|
bool RedisStore::isReady(void) const { return m_sync != nullptr; }
|
||||||
|
|
||||||
std::string RedisStore::lookupUserKey(const std::string&) { return ""; }
|
std::string RedisStore::lookupUserKey(const std::string& callsign) {
|
||||||
|
if (!m_sync) return "";
|
||||||
|
std::string user_key = keyFor("user:" + callsign);
|
||||||
|
redisReply* r = (redisReply*)redisCommand(m_sync,
|
||||||
|
"HMGET %s group enabled", user_key.c_str());
|
||||||
|
if (!r || r->type != REDIS_REPLY_ARRAY || r->elements != 2) {
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (r->element[0]->type != REDIS_REPLY_STRING) {
|
||||||
|
freeReplyObject(r);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
std::string group(r->element[0]->str, r->element[0]->len);
|
||||||
|
bool enabled = true;
|
||||||
|
if (r->element[1]->type == REDIS_REPLY_STRING) {
|
||||||
|
enabled = std::string(r->element[1]->str, r->element[1]->len) != "0";
|
||||||
|
}
|
||||||
|
freeReplyObject(r);
|
||||||
|
if (!enabled) {
|
||||||
|
std::cout << "RedisStore: user " << callsign << " disabled" << std::endl;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string group_key = keyFor("group:" + group);
|
||||||
|
r = (redisReply*)redisCommand(m_sync, "HGET %s password", group_key.c_str());
|
||||||
|
if (!r || r->type != REDIS_REPLY_STRING) {
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
std::cout << "*** WARNING: group " << group << " missing password" << std::endl;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
std::string pw(r->str, r->len);
|
||||||
|
freeReplyObject(r);
|
||||||
|
return pw;
|
||||||
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string> RedisStore::loadAllUsers(void) { return {}; }
|
std::map<std::string, std::string> RedisStore::loadAllUsers(void) { return {}; }
|
||||||
bool RedisStore::isUserEnabled(const std::string&) { return true; }
|
bool RedisStore::isUserEnabled(const std::string&) { return true; }
|
||||||
std::set<uint32_t> RedisStore::loadClusterTgs(void) { return {}; }
|
|
||||||
std::string RedisStore::loadTrunkFilter(const std::string&, const std::string&) { return ""; }
|
std::set<uint32_t> RedisStore::loadClusterTgs(void) {
|
||||||
std::map<uint32_t, uint32_t> RedisStore::loadTrunkTgMap(const std::string&) { return {}; }
|
std::set<uint32_t> out;
|
||||||
std::set<std::string> RedisStore::loadTrunkMutes(const std::string&) { return {}; }
|
if (!m_sync) return out;
|
||||||
void RedisStore::addTrunkMute(const std::string&, const std::string&) {}
|
std::string k = keyFor("cluster:tgs");
|
||||||
void RedisStore::removeTrunkMute(const std::string&, const std::string&) {}
|
redisReply* r = (redisReply*)redisCommand(m_sync, "SMEMBERS %s", k.c_str());
|
||||||
void RedisStore::publishConfigChanged(const std::string&) {}
|
if (r && r->type == REDIS_REPLY_ARRAY) {
|
||||||
|
for (size_t i = 0; i < r->elements; ++i) {
|
||||||
|
if (r->element[i]->type == REDIS_REPLY_STRING) {
|
||||||
|
try { out.insert(std::stoul(std::string(r->element[i]->str, r->element[i]->len))); }
|
||||||
|
catch (...) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string RedisStore::loadTrunkFilter(const std::string& section,
|
||||||
|
const std::string& field) {
|
||||||
|
if (!m_sync) return "";
|
||||||
|
std::string k = keyFor("trunk:" + section + ":" + field);
|
||||||
|
redisReply* r = (redisReply*)redisCommand(m_sync, "SMEMBERS %s", k.c_str());
|
||||||
|
std::string out;
|
||||||
|
if (r && r->type == REDIS_REPLY_ARRAY) {
|
||||||
|
for (size_t i = 0; i < r->elements; ++i) {
|
||||||
|
if (r->element[i]->type == REDIS_REPLY_STRING) {
|
||||||
|
if (!out.empty()) out += ",";
|
||||||
|
out.append(r->element[i]->str, r->element[i]->len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<uint32_t, uint32_t> RedisStore::loadTrunkTgMap(const std::string& section) {
|
||||||
|
std::map<uint32_t, uint32_t> out;
|
||||||
|
if (!m_sync) return out;
|
||||||
|
std::string k = keyFor("trunk:" + section + ":tgmap");
|
||||||
|
redisReply* r = (redisReply*)redisCommand(m_sync, "HGETALL %s", k.c_str());
|
||||||
|
if (r && r->type == REDIS_REPLY_ARRAY) {
|
||||||
|
for (size_t i = 0; i + 1 < r->elements; i += 2) {
|
||||||
|
try {
|
||||||
|
uint32_t peer = std::stoul(std::string(r->element[i]->str, r->element[i]->len));
|
||||||
|
uint32_t local = std::stoul(std::string(r->element[i+1]->str, r->element[i+1]->len));
|
||||||
|
out[peer] = local;
|
||||||
|
} catch (...) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::set<std::string> RedisStore::loadTrunkMutes(const std::string& section) {
|
||||||
|
std::set<std::string> out;
|
||||||
|
if (!m_sync) return out;
|
||||||
|
std::string k = keyFor("trunk:" + section + ":mutes");
|
||||||
|
redisReply* r = (redisReply*)redisCommand(m_sync, "SMEMBERS %s", k.c_str());
|
||||||
|
if (r && r->type == REDIS_REPLY_ARRAY) {
|
||||||
|
for (size_t i = 0; i < r->elements; ++i) {
|
||||||
|
if (r->element[i]->type == REDIS_REPLY_STRING) {
|
||||||
|
out.emplace(r->element[i]->str, r->element[i]->len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RedisStore::addTrunkMute(const std::string& section, const std::string& callsign) {
|
||||||
|
if (!m_sync) return;
|
||||||
|
std::string k = keyFor("trunk:" + section + ":mutes");
|
||||||
|
redisReply* r = (redisReply*)redisCommand(m_sync, "SADD %s %s",
|
||||||
|
k.c_str(), callsign.c_str());
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RedisStore::removeTrunkMute(const std::string& section, const std::string& callsign) {
|
||||||
|
if (!m_sync) return;
|
||||||
|
std::string k = keyFor("trunk:" + section + ":mutes");
|
||||||
|
redisReply* r = (redisReply*)redisCommand(m_sync, "SREM %s %s",
|
||||||
|
k.c_str(), callsign.c_str());
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RedisStore::publishConfigChanged(const std::string& scope) {
|
||||||
|
if (!m_sync) return;
|
||||||
|
std::string ch = channelName();
|
||||||
|
redisReply* r = (redisReply*)redisCommand(m_sync, "PUBLISH %s %s",
|
||||||
|
ch.c_str(), scope.c_str());
|
||||||
|
if (r) freeReplyObject(r);
|
||||||
|
}
|
||||||
|
|
||||||
void RedisStore::pushLiveTalker(uint32_t, const std::string&, const std::string&) {}
|
void RedisStore::pushLiveTalker(uint32_t, const std::string&, const std::string&) {}
|
||||||
void RedisStore::clearLiveTalker(uint32_t) {}
|
void RedisStore::clearLiveTalker(uint32_t) {}
|
||||||
void RedisStore::pushLiveClient(const std::string&, const std::string&,
|
void RedisStore::pushLiveClient(const std::string&, const std::string&,
|
||||||
@@ -34,7 +197,6 @@ 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::connectSync(void) { return false; }
|
|
||||||
bool RedisStore::connectAsync(void) { return false; }
|
bool RedisStore::connectAsync(void) { return false; }
|
||||||
void RedisStore::subscribe(void) {}
|
void RedisStore::subscribe(void) {}
|
||||||
void RedisStore::scheduleReconnect(void) {}
|
void RedisStore::scheduleReconnect(void) {}
|
||||||
|
|||||||
Reference in new issue
Block a user