Commit 4f8a077e authored by Ricardo Calheiros de Miranda Cosme's avatar Ricardo Calheiros de Miranda Cosme
Browse files

[OPENBUS-89]

+ Primeira versão para o mecanismo de interceptação no lado do servidor para a lib CPPOIL.
 - Implementação de um ponto 'receiverequest'.
 - Disponibilização de um método getCredential() para se obter a credencial interceptada.

[OPENBUS-92]
+ Demo Hello do CPPOIL passa a utilizar o interceptador servidor para saber que esta fazendo chamada
  ao serviço hello.



git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/openbus/trunk@85287 ae0415b3-e90b-0410-900d-d0be9363c56b
parent 7611868b
/*
** common/ServerInterceptor.cpp
*/
#include "ServerInterceptor.h"
#include <lua.hpp>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#define BUFFER_SIZE 1024
using namespace std;
namespace openbus {
namespace common {
Lua_State* ServerInterceptor::LuaVM = 0;
ServerInterceptor::ServerInterceptor() {
#if VERBOSE
printf("\n\n[ServerInterceptor::ServerInterceptor() COMECO]\n");
printf("\t[Criando instancia de ServerInterceptor]\n");
#endif
openbus = Openbus::getInstance();
LuaVM = openbus->getLuaVM();
lua_newtable(LuaVM);
lua_pushlightuserdata(LuaVM, this);
lua_insert(LuaVM, -2);
lua_settable(LuaVM, LUA_REGISTRYINDEX);
#if VERBOSE
printf("\t[Tamanho da pilha de Lua: %d]\n", lua_gettop(LuaVM));
printf("[ServerInterceptor::ServerInterceptor() FIM]\n\n");
#endif
}
ServerInterceptor::~ServerInterceptor() {
/* empty */
}
services::Credential* ServerInterceptor::getCredential() {
services::Credential* returnValue;
#if VERBOSE
cout << endl << endl << "[ServerInterceptor::getCredential() COMECO]" << endl;
#endif
returnValue = new services::Credential;
lua_getglobal(LuaVM, "getCredential");
if (lua_pcall(LuaVM, 0, 1, 0) != 0) {
#if VERBOSE
printf("\t[ERRO ao realizar pcall do metodo]\n") ;
printf("\t[Tamanho da pilha de Lua: %d]\n" , lua_gettop(LuaVM)) ;
printf("\t[Tipo do elemento do TOPO: %s]\n" , \
lua_typename(LuaVM, lua_type(LuaVM, -1))) ;
#endif
const char * errmsg ;
lua_getglobal(LuaVM, "tostring") ;
lua_insert(LuaVM, -2) ;
lua_pcall(LuaVM, 1, 1, 0) ;
errmsg = lua_tostring(LuaVM, -1) ;
lua_pop(LuaVM, 1) ;
#if VERBOSE
printf("\t[lancando excecao: %s]\n", errmsg) ;
printf("\t[Tamanho da pilha de Lua: %d]\n" , lua_gettop(LuaVM)) ;
printf("[ServerInterceptor::getCredential() FIM]\n\n") ;
#endif
throw errmsg ;
}
lua_getfield(LuaVM, -1, "identifier");
const char* identifier = lua_tostring(LuaVM, -1);
returnValue->identifier = identifier;
lua_pop(LuaVM, 1);
lua_getfield(LuaVM, -1, "owner");
const char* owner = lua_tostring(LuaVM, -1);
returnValue->owner = owner;
lua_pop(LuaVM, 1);
lua_getfield(LuaVM, -1, "delegate");
const char* delegate = lua_tostring(LuaVM, -1);
returnValue->delegate = delegate;
lua_pop(LuaVM, 1);
#if VERBOSE
cout << endl << endl << "[ServerInterceptor::getCredential() FIM]" << endl;
#endif
return returnValue;
}
}
}
/*
* common/ServerInterceptor.h
*/
#ifndef SERVER_INTERCEPTOR_H_
#define SERVER_INTERCEPTOR_H_
namespace openbus {
namespace common {
class ServerInterceptor;
}
}
#include "../openbus.h"
namespace openbus {
namespace common {
class ServerInterceptor {
private:
Openbus* openbus;
static Lua_State* LuaVM;
Long contextID;
String credentialType;
CredentialManager* credentialManager;
public:
ServerInterceptor ();
~ServerInterceptor();
services::Credential* getCredential();
};
}
}
#endif
......@@ -22,6 +22,7 @@ LIBS= scsoil oilall scsall luasocket tolua5.1
SRC= ${PRECMP_DIR}/auxiliar.c \
common/ClientInterceptor.cpp \
common/ServerInterceptor.cpp \
common/CredentialManager.cpp \
openbus.cpp \
stubs/IAccessControlService.cpp \
......
......@@ -23,11 +23,16 @@ namespace openbus {
Openbus* Openbus::instance = 0;
common::CredentialManager* Openbus::credentialManager = 0;
common::ClientInterceptor* Openbus::clientInterceptor = 0;
common::ServerInterceptor* Openbus::serverInterceptor = 0;
Lua_State* Openbus::getLuaVM(void) {
return LuaVM;
}
common::ServerInterceptor* Openbus::getServerInterceptor() {
return serverInterceptor;
}
void Openbus::initLuaVM(void) {
#if VERBOSE
cout << "\t[Carregando bibliotecas Lua.]" << endl;
......@@ -150,6 +155,56 @@ namespace openbus {
#endif
}
void Openbus::setServerInterceptor(common::ServerInterceptor* serverInterceptor) {
#if VERBOSE
cout << "[Openbus::setServerInterceptor() COMECO]" << endl;
cout << "\t[Tamanho da pilha de Lua: " << lua_gettop(LuaVM) << "]" << endl;
cout << "\t[Chamando metodo orb:setServerInterceptor(" << serverInterceptor << ")]" << endl;
#endif
lua_getglobal(LuaVM, "orb");
lua_getfield(LuaVM, -1, "setserverinterceptor");
lua_getglobal(LuaVM, "orb");
lua_newtable(LuaVM);
lua_pushstring(LuaVM, "serverInterceptor");
lua_pushlightuserdata(LuaVM, serverInterceptor);
lua_settable(LuaVM, -3);
#if VERBOSE
cout << "\t[parametro {}.clientInterceptor (pointer) empilhado]" << endl;
cout << "\t[Tamanho da pilha de Lua: " << lua_gettop(LuaVM) << "]" << endl;
#endif
lua_pushstring(LuaVM, "receiverequest");
lua_getglobal(LuaVM, "receiverequest");
lua_settable(LuaVM, -3);
#if VERBOSE
cout << "\t[parametro {}.receiverequest (function) empilhado]" << endl;
cout << "\t[Tamanho da pilha de Lua: " << lua_gettop(LuaVM) << "]" << endl;
#endif
if (lua_pcall(LuaVM, 2, 0, 0) != 0) {
#if VERBOSE
cout << "\t[ERRO ao realizar pcall do metodo]" << endl;
cout << "\t[Tamanho da pilha de Lua: " << lua_gettop(LuaVM) << endl;
cout << "\t[Tipo do elemento do TOPO: " << lua_typename(LuaVM, lua_type(LuaVM, -1)) << "]" << endl;
#endif
const char * errmsg ;
lua_getglobal(LuaVM, "tostring");
lua_insert(LuaVM, -2);
lua_pcall(LuaVM, 1, 1, 0);
errmsg = lua_tostring(LuaVM, -1);
lua_pop(LuaVM, 1);
#if VERBOSE
cout << "\t[lancando excecao]" << endl;
cout << "\t[Tamanho da pilha de Lua: " << lua_gettop(LuaVM) << "]" << endl;
cout << "[Openbus::setServerInterceptor() FIM]" << endl << endl;
#endif
throw errmsg;
} /* if */
lua_pop(LuaVM, 1);
#if VERBOSE
cout << "\t[Tamanho da pilha de Lua: " << lua_gettop(LuaVM) << "]" << endl;
cout << "[Openbus::setServerInterceptor() FIM]" << endl << endl;
#endif
}
common::CredentialManager* Openbus::getCredentialManager() {
return credentialManager;
}
......@@ -164,6 +219,8 @@ namespace openbus {
services::Credential* aCredential, services::Lease* aLease) {
try {
services::IAccessControlService* acs = getACS(host, port);
serverInterceptor = new common::ServerInterceptor();
this->setServerInterceptor(serverInterceptor);
if (!acs->loginByPassword(user, password, aCredential, aLease)) {
throw "Par usuario/senha nao validado.";
} else {
......
......@@ -36,6 +36,7 @@ namespace openbus {
#include "common/CredentialManager.h"
#include "common/ClientInterceptor.h"
#include "common/ServerInterceptor.h"
#include "stubs/IAccessControlService.h"
#include "stubs/IRegistryService.h"
#include "stubs/ISessionService.h"
......@@ -49,6 +50,7 @@ namespace openbus {
static Lua_State* LuaVM;
static common::CredentialManager* credentialManager;
static common::ClientInterceptor* clientInterceptor;
static common::ServerInterceptor* serverInterceptor;
void initLuaVM();
Openbus();
Openbus(const Openbus&);
......@@ -57,7 +59,9 @@ namespace openbus {
static Openbus* getInstance();
void run();
Lua_State* getLuaVM();
common::ServerInterceptor* getServerInterceptor();
void setClientInterceptor(common::ClientInterceptor* clientInterceptor);
void setServerInterceptor(common::ServerInterceptor* serverInterceptor);
common::CredentialManager* getCredentialManager();
services::IAccessControlService* getACS(String host, unsigned short port);
services::IAccessControlService* connect(String host, unsigned short port, String user, String password, \
......
......@@ -16,12 +16,14 @@ orb:loadidlfile(os.getenv("OPENBUS_HOME" ).."/idlpath/session_service.idl")
local lir = orb:getLIR()
IComponent = require 'scs.core.IComponent'
IComponent = require "scs.core.IComponent"
oil.verbose:level(0)
oil.tasks.verbose:level(0)
oil.tasks.verbose:flag("threads", false)
picurrentTable = {}
-- Metodo utilizado pelo interceptador do OiL
function sendrequest(credential, credentialType, contextID, request)
local encoder = orb:newencoder()
......@@ -31,6 +33,22 @@ function sendrequest(credential, credentialType, contextID, request)
}
end
function receiverequest(self, request)
local credential
for _, context in ipairs(request.service_context) do
if context.context_id == 1234 then
local decoder = orb:newdecoder(context.context_data)
credential = decoder:get(lir:lookup_id("IDL:openbusidl/acs/Credential:1.0").type)
break
end
end
picurrentTable[oil.tasks.current] = credential
end
function getCredential()
return picurrentTable[oil.tasks.current]
end
if not oil.isrunning then
oil.isrunning = true
oil.tasks:register(coroutine.create(function() return orb:run() end))
......
......@@ -194,7 +194,7 @@ namespace openbus {
returnValue = lua_tostring( LuaVM, -1 ) ;
lua_pop( LuaVM, 1 ) ;
#if VERBOSE
printf( "\t[lancando excecao]\n" ) ;
printf( "\t[lancando excecao: %s]\n", returnValue ) ;
printf( "\t[Tamanho da pilha de Lua: %d]\n" , lua_gettop( LuaVM ) ) ;
printf( "[IRegistryService::Register(ServiceOffer,RegistryIdentifier) FIM]\n\n" ) ;
#endif
......
#include <iostream>
#include <openbus.h>
using namespace openbus;
class IHello {
public:
IHello() {}
~IHello() {}
void sayHello() {
std::cout << "Hello!" << std::endl;
services::Credential* credential = Openbus::getInstance()->getServerInterceptor()->getCredential();
std::cout << "Hello!" << std::endl << "Owner: " << credential->owner << std::endl;
}
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment