From 69aa4708de73df46fc1b0d70e9d6b6e9a4aa1821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=BCrzeder?= Date: Sat, 27 Jul 2019 15:49:13 +0200 Subject: [PATCH] Added RedisClient wrapper --- module/CFunctions.cpp | 12 ++++++------ module/Module.h | 10 +++++----- module/RedisClient.cpp | 35 +++++++++++++++++++++++++++++++++++ module/RedisClient.h | 21 +++++++++++++++++++++ 4 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 module/RedisClient.cpp create mode 100644 module/RedisClient.h diff --git a/module/CFunctions.cpp b/module/CFunctions.cpp index 2866e6e..790acef 100644 --- a/module/CFunctions.cpp +++ b/module/CFunctions.cpp @@ -9,7 +9,7 @@ int CFunctions::redis_create_client(lua_State* lua_vm) { // redis_client redis_create_client() try { - const auto client = new redis_client(); + const auto client = new RedisClient(); g_Module->AddRedisClient(client); lua_pushlightuserdata(lua_vm, static_cast(client)); @@ -37,7 +37,7 @@ int CFunctions::redis_connect(lua_State* lua_vm) try { // Read arguments - const auto client = static_cast(lua_touserdata(lua_vm, 1)); + const auto client = static_cast(lua_touserdata(lua_vm, 1)); const auto host = lua_tostring(lua_vm, 2); const auto port = lua_tonumber(lua_vm, 3); @@ -74,7 +74,7 @@ int CFunctions::redis_disconnect(lua_State* lua_vm) try { // Read arguments - const auto client = static_cast(lua_touserdata(lua_vm, 1)); + const auto client = static_cast(lua_touserdata(lua_vm, 1)); // verify client if (!g_Module->HasRedisClient(client)) @@ -84,7 +84,7 @@ int CFunctions::redis_disconnect(lua_State* lua_vm) return 1; } - client->disconnect(true); + client->disconnect(); lua_pushboolean(lua_vm, true); return 1; } catch(exception& e) @@ -108,7 +108,7 @@ int CFunctions::redis_test(lua_State* lua_vm) try { // Read arguments - const auto client = static_cast(lua_touserdata(lua_vm, 1)); + const auto client = static_cast(lua_touserdata(lua_vm, 1)); // verify client if (!g_Module->HasRedisClient(client)) @@ -117,7 +117,7 @@ int CFunctions::redis_test(lua_State* lua_vm) return 1; } - std::cout << client->is_connected() << std::endl; + std::cout << (client->is_connected() ? "true" : "false") << std::endl; lua_pushboolean(lua_vm, true); return 1; } catch(std::exception& e) diff --git a/module/Module.h b/module/Module.h index b13e81d..d8badcb 100644 --- a/module/Module.h +++ b/module/Module.h @@ -2,9 +2,9 @@ #include #include #include -#include #include "JobManager.h" +#include "RedisClient.h" #include "ILuaModuleManager.h" struct lua_State; @@ -25,9 +25,9 @@ class Module inline void RemoveLuaVM(lua_State* luaVM) { _luaStates.erase(luaVM); } inline bool HasLuaVM(lua_State* luaVM) { return _luaStates.find(luaVM) != _luaStates.end(); } - inline void AddRedisClient(redis_client* client) { _redisClients.insert(client); } - inline void RemoveRedisClient(redis_client* client) { _redisClients.erase(client); } - inline bool HasRedisClient(redis_client* client) { return _redisClients.find(client) != _redisClients.end(); } + inline void AddRedisClient(RedisClient* client) { _redisClients.insert(client); } + inline void RemoveRedisClient(RedisClient* client) { _redisClients.erase(client); } + inline bool HasRedisClient(RedisClient* client) { return _redisClients.find(client) != _redisClients.end(); } inline JobManager>& GetJobManager() { return _jobManager; } @@ -35,7 +35,7 @@ class Module ILuaModuleManager* _moduleManager; JobManager> _jobManager; std::unordered_set _luaStates; - std::unordered_set _redisClients; + std::unordered_set _redisClients; }; extern Module* g_Module; diff --git a/module/RedisClient.cpp b/module/RedisClient.cpp new file mode 100644 index 0000000..bd0d9fc --- /dev/null +++ b/module/RedisClient.cpp @@ -0,0 +1,35 @@ +#include "RedisClient.h" +#include "Module.h" + +RedisClient::RedisClient() +{ + _client = new redis_client(); + _subscriber = new redis_subscriber(); + + g_Module->AddRedisClient(this); +} + +RedisClient::~RedisClient() +{ + g_Module->RemoveRedisClient(this); + + delete _client; + delete _subscriber; +} + +void RedisClient::connect(const std::string& host, const int& port) const +{ + _client->connect(host, port); + _subscriber->connect(host, port); +} + +void RedisClient::disconnect() const +{ + _client->disconnect(true); + _subscriber->disconnect(true); +} + +bool RedisClient::is_connected() const +{ + return _client->is_connected() && _subscriber->is_connected(); +} diff --git a/module/RedisClient.h b/module/RedisClient.h new file mode 100644 index 0000000..4b6ff41 --- /dev/null +++ b/module/RedisClient.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +using redis_client = cpp_redis::client; +using redis_subscriber = cpp_redis::subscriber; + +class RedisClient +{ +public: + RedisClient(); + ~RedisClient(); + + void connect(const std::string& host, const int& port) const; + void disconnect() const; + bool is_connected() const; +private: + redis_client* _client; + redis_subscriber* _subscriber; +}; +