f698451c4c
- in Redis aggiunta lettura ultimo Messaggio.
235 lines
7.4 KiB
C++
235 lines
7.4 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2025-2025
|
|
//----------------------------------------------------------------------------
|
|
// File : LUA_Redis.cpp Data : 17.09.25 Versione : 2.7i3
|
|
// Contenuto : Funzioni per interfacciarsi con server Redis.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 17.09.25 RE Creazione modulo.
|
|
//
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
//--------------------------- Include ----------------------------------------
|
|
#include "stdafx.h"
|
|
#include "LUA.h"
|
|
#include "/EgtDev/Include/EXeExecutor.h"
|
|
#include "/EgtDev/Include/EGkLuaAux.h"
|
|
|
|
using namespace std ;
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisConnect( lua_State* L)
|
|
{
|
|
// 1 parametro : sConnection
|
|
string sConnection ;
|
|
LuaCheckParam( L, 1, sConnection)
|
|
LuaClearStack( L) ;
|
|
// Imposto la connessione
|
|
int nIdConnection = 0 ;
|
|
bool bOk = ExeRedisConnect( sConnection, nIdConnection) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
LuaSetParam( L, nIdConnection) ;
|
|
return 2 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisDisconnect( lua_State* L)
|
|
{
|
|
// 1 parametro : nIdConnection
|
|
int nIdConnection = 0 ;
|
|
LuaCheckParam( L, 1, nIdConnection) ;
|
|
LuaClearStack( L) ;
|
|
// Imposto la connessione
|
|
bool bOk = ExeRedisDisconnect( nIdConnection) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisSetValFromKey( lua_State* L)
|
|
{
|
|
// 3 parametri : nIdConnection, sKey, sVal
|
|
int nIdConnection = 0 ;
|
|
LuaCheckParam( L, 1, nIdConnection) ;
|
|
string sKey ;
|
|
LuaCheckParam( L, 2, sKey) ;
|
|
string sVal ;
|
|
LuaCheckParam( L, 3, sVal) ;
|
|
LuaClearStack( L) ;
|
|
// Imposto il valore della chiave
|
|
bool bOk = ExeRedisSetValFromKey( nIdConnection, sKey, sVal) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisGetValFromKey( lua_State* L)
|
|
{
|
|
// 2 parametri : nIdConnection, sKey
|
|
int nIdConnection = 0 ;
|
|
LuaCheckParam( L, 1, nIdConnection) ;
|
|
string sKey ;
|
|
LuaCheckParam( L, 2, sKey)
|
|
LuaClearStack( L) ;
|
|
// Recupero il valore della chiave
|
|
string sVal, sType ;
|
|
bool bOk = ExeRedisGetValFromKey( nIdConnection, sKey, sVal) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
LuaSetParam( L, sVal) ;
|
|
return 2 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisAsyncConnect( lua_State* L)
|
|
{
|
|
// 2 parametri : sConnection
|
|
string sConnection ;
|
|
LuaCheckParam( L, 1, sConnection)
|
|
LuaClearStack( L) ;
|
|
// Imposto la connessione
|
|
int nIdConnection = -1 ;
|
|
bool bOk = ExeRedisAsyncConnect( sConnection, nIdConnection) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
LuaSetParam( L, nIdConnection) ;
|
|
return 2 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisAsyncDiconnect( lua_State* L)
|
|
{
|
|
// 1 parametro : nIdConnection
|
|
int nIdConnection = 0 ;
|
|
LuaCheckParam( L, 1, nIdConnection) ;
|
|
LuaClearStack( L) ;
|
|
// Imposto la connessione
|
|
bool bOK = ExeRedisAsyncDisconnect( nIdConnection) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOK) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisAsyncPublish( lua_State* L)
|
|
{
|
|
// 3 parametri : nIdConnection, sChannel, sMessage
|
|
int nIdConnection = 0 ;
|
|
LuaCheckParam( L, 1, nIdConnection) ;
|
|
string sChannel ;
|
|
LuaCheckParam( L, 2, sChannel) ;
|
|
string sMessage ;
|
|
LuaCheckParam( L, 3, sMessage) ;
|
|
LuaClearStack( L) ;
|
|
// Pubblico il messaggio sul canale
|
|
bool bOk = ExeRedisAsyncPublish( nIdConnection, sChannel, sMessage) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisAsyncSubscribe( lua_State* L)
|
|
{
|
|
// 2 parametri : nIdConnection, sChannel
|
|
int nIdConnection = 0 ;
|
|
LuaCheckParam( L, 1, nIdConnection) ;
|
|
string sChannel ;
|
|
LuaCheckParam( L, 2, sChannel) ;
|
|
LuaClearStack( L) ;
|
|
// Iscrizione al canale
|
|
bool bOk = ExeRedisAsyncSubscribe( nIdConnection, sChannel) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisAsyncUnsubscribe( lua_State* L)
|
|
{
|
|
// 2 parametri : nIdConnection, sChannel
|
|
int nIdConnection = 0 ;
|
|
LuaCheckParam( L, 1, nIdConnection) ;
|
|
string sChannel ;
|
|
LuaCheckParam( L, 2, sChannel) ;
|
|
LuaClearStack( L) ;
|
|
// Disiscrizione
|
|
bool bOk = ExeRedisAsyncUnsubscribe( nIdConnection, sChannel) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisAsyncSubscribeOneMessage( lua_State* L)
|
|
{
|
|
// 3 parametri : nIdConnection, sChannel, dMaxTimeOut
|
|
int nIdConnection = 0 ;
|
|
LuaCheckParam( L, 1, nIdConnection) ;
|
|
string sChannel ;
|
|
LuaCheckParam( L, 2, sChannel) ;
|
|
double dMaxTimeOut = 0. ;
|
|
LuaCheckParam( L, 3, dMaxTimeOut) ;
|
|
LuaClearStack( L) ;
|
|
// Eseguo funzione speciale di iscrizione - attesa messaggio - disiscrizione
|
|
string sMessage ;
|
|
bool bOk = ExeRedisAsyncSubscribeOneMessage( nIdConnection, sChannel, dMaxTimeOut, sMessage) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
LuaSetParam( L, sMessage) ;
|
|
return 2 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaRedisAsyncGetMessage( lua_State* L)
|
|
{
|
|
int nIdConnection = 0 ;
|
|
LuaCheckParam( L, 1, nIdConnection) ;
|
|
string sChannel ;
|
|
LuaCheckParam( L, 2, sChannel) ;
|
|
// Eseguo la funzione per leggere il messaggio
|
|
int nCount = 0 ;
|
|
string sMessage ;
|
|
bool bOk = ExeRedisAsyncGetMessage( nIdConnection, sChannel, nCount, sMessage) ;
|
|
// Restituisco il risultato
|
|
LuaSetParam( L, bOk) ;
|
|
LuaSetParam( L, nCount) ;
|
|
LuaSetParam( L, sMessage) ;
|
|
return 3 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
bool
|
|
LuaInstallRedis( LuaMgr& luaMgr)
|
|
{
|
|
bool bOk = ( &luaMgr != nullptr) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisConnect", LuaRedisConnect) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisDisconnect", LuaRedisDisconnect) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisSetValFromKey", LuaRedisSetValFromKey) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisGetValFromKey", LuaRedisGetValFromKey) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisAsyncConnect", LuaRedisAsyncConnect) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisAsyncDisconnect", LuaRedisAsyncDiconnect) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisAsyncPublish", LuaRedisAsyncPublish) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisAsyncSubscribe", LuaRedisAsyncSubscribe) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisAsyncUnsubscribe", LuaRedisAsyncUnsubscribe) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisAsyncSubscribeOneMessage", LuaRedisAsyncSubscribeOneMessage) ;
|
|
bOk = bOk && luaMgr.RegisterFunction( "EgtRedisAsyncGetMessage", LuaRedisAsyncGetMessage) ;
|
|
return bOk ;
|
|
}
|