Skip to content

Commit

Permalink
Added first functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kürzeder committed Jul 27, 2019
1 parent e871017 commit cc7384e
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 21 deletions.
113 changes: 102 additions & 11 deletions module/CFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,129 @@
#include <sstream>

#include "Module.h"
#include "CFunctions.h"

#ifndef _WIN32
#include <sys/stat.h>
#endif

int CFunctions::redis_create_client(lua_State* lua_vm)
{
// redis_client redis_create_client()
try {
auto client = new cpp_redis::client();
client->connect();
const auto client = new redis_client();
g_Module->AddRedisClient(client);

lua_pushlightuserdata(lua_vm, static_cast<void*>(client));
return 1;
} catch(std::exception& e)
{
std::cout << e.what() << std::endl;
stringstream ss;
ss << "Failed to create to redis client. [" << e.what() << "]\n";
pModuleManager->ErrorPrintf(ss.str().c_str());
lua_pushboolean(lua_vm, false);
return 1;
}
}

int CFunctions::redis_connect(lua_State* lua_vm)
{
// bool redis_connect(redis_client client, string host, number port)
if (lua_type(lua_vm, 1) != LUA_TLIGHTUSERDATA || lua_type(lua_vm, 2) != LUA_TSTRING || lua_type(lua_vm, 3) != LUA_TNUMBER)
{
pModuleManager->ErrorPrintf("Bad argument @ redis_connect.\n");
lua_pushboolean(lua_vm, false);
return 1;
}

return 1;
try
{
// Read arguments
const auto client = static_cast<redis_client*>(lua_touserdata(lua_vm, 1));
const auto host = lua_tostring(lua_vm, 2);
const auto port = lua_tonumber(lua_vm, 3);

// verify client
if (!g_Module->HasRedisClient(client))
{
pModuleManager->ErrorPrintf("Bad argument @ redis_connect, invalid client has been passed.");
lua_pushboolean(lua_vm, false);
return 1;
}

client->connect(host, port);
lua_pushboolean(lua_vm, true);
return 1;
} catch(std::exception& e)
{
stringstream ss;
ss << "Failed to connect to redis server. [" << e.what() << "]\n";
pModuleManager->ErrorPrintf(ss.str().c_str());
lua_pushboolean(lua_vm, false);
return 1;
}
}

int CFunctions::redis_disconnect(lua_State* lua_vm)
{
// void redis_disconnect(redis_client client)
if (lua_type(lua_vm, 1) != LUA_TLIGHTUSERDATA)
{
pModuleManager->ErrorPrintf("Bad argument @ redis_disconnect.\n");
lua_pushboolean(lua_vm, false);
return 1;
}

try {
// Read arguments
const auto client = static_cast<redis_client*>(lua_touserdata(lua_vm, 1));

// verify client
if (!g_Module->HasRedisClient(client))
{
pModuleManager->ErrorPrintf("Bad argument @ redis_disconnect, invalid client has been passed.");
lua_pushboolean(lua_vm, false);
return 1;
}

client->disconnect(true);
lua_pushboolean(lua_vm, true);
return 1;
} catch(exception& e)
{
std::cout << e.what() << std::endl;
lua_pushboolean(lua_vm, false);
return 1;
}
}

int CFunctions::redis_test(lua_State* lua_vm)
{
// bool redis_test(redis_client client)
if (lua_type(lua_vm, 1) != LUA_TLIGHTUSERDATA)
{
pModuleManager->ErrorPrintf("Bad argument @ redis_test.\n");
lua_pushboolean(lua_vm, false);
return 1;
}

try
{
const auto ptr = static_cast<cpp_redis::client*>(lua_touserdata(lua_vm, 1));
std::cout << ptr->is_connected() << std::endl;
// Read arguments
const auto client = static_cast<redis_client*>(lua_touserdata(lua_vm, 1));

// verify client
if (!g_Module->HasRedisClient(client))
{
lua_pushboolean(lua_vm, false);
return 1;
}

std::cout << client->is_connected() << std::endl;
lua_pushboolean(lua_vm, true);
return 1;
} catch(std::exception& e)
{
std::cout << e.what() << std::endl;
lua_pushboolean(lua_vm, false);
return 1;
}

return 1;
}
5 changes: 4 additions & 1 deletion module/CFunctions.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once
#include "extra/CLuaArguments.h"
#include <iostream>
#include <cpp_redis/core/reply.hpp>
#include <cpp_redis/core/client.hpp>

#include "extra/CLuaArguments.h"

class ILuaModuleManager10;
struct lua_State;

Expand Down Expand Up @@ -42,5 +43,7 @@ class CFunctions
}

static int redis_create_client(lua_State* lua_vm);
static int redis_connect(lua_State* lua_vm);
static int redis_disconnect(lua_State* lua_vm);
static int redis_test(lua_State* lua_vm);
};
8 changes: 5 additions & 3 deletions module/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ MTAEXPORT void RegisterFunctions(lua_State* lua_vm)
pModuleManager->RegisterFunction(lua_vm, "redisTest", &CFunctions::Test);

// Register globals
pModuleManager->RegisterFunction(lua_vm, "redisTest", &CFunctions::Test);
pModuleManager->RegisterFunction(lua_vm, "redisConnect", &CFunctions::redis_create_client);
pModuleManager->RegisterFunction(lua_vm, "redisTest", &CFunctions::redis_test);
pModuleManager->RegisterFunction(lua_vm, "redisTest", &CFunctions::Test);
pModuleManager->RegisterFunction(lua_vm, "redis_create_client", &CFunctions::redis_create_client);
pModuleManager->RegisterFunction(lua_vm, "redis_connect", &CFunctions::redis_connect);
pModuleManager->RegisterFunction(lua_vm, "redis_disconnect", &CFunctions::redis_disconnect);
pModuleManager->RegisterFunction(lua_vm, "redis_test", &CFunctions::redis_test);
}

MTAEXPORT bool DoPulse()
Expand Down
7 changes: 1 addition & 6 deletions module/Module.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#include "Module.h"
#include <ostream>
#include <iostream>
#include <cpp_redis/core/reply.hpp>
#include <cpp_redis/core/client.hpp>
#include <tacopie/network/tcp_socket.hpp>

#include "Module.h"
Module* g_Module = nullptr;

constexpr std::size_t kNumWorkers = 2;
Expand Down
9 changes: 9 additions & 0 deletions module/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
#include <unordered_set>
#include <any>
#include <optional>
#include <cpp_redis/core/client.hpp>

#include "JobManager.h"
#include "ILuaModuleManager.h"

struct lua_State;
class ILuaModuleManager;

using redis_client = cpp_redis::client;

class Module
{
public:
Expand All @@ -21,12 +25,17 @@ 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 JobManager<const std::optional<std::any>>& GetJobManager() { return _jobManager; }

private:
ILuaModuleManager* _moduleManager;
JobManager<const std::optional<std::any>> _jobManager;
std::unordered_set<lua_State*> _luaStates;
std::unordered_set<redis_client*> _redisClients;
};

extern Module* g_Module;

0 comments on commit cc7384e

Please sign in to comment.