Commit 22905394 authored by Rodrigo Carneiro Henrique's avatar Rodrigo Carneiro Henrique
Browse files

[OPENBUS-689]: Migrar a API Lua de core/utilities/lua para sdk/lua


git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/openbus/sdk/lua/trunk@104598 ae0415b3-e90b-0410-900d-d0be9363c56b
parent 56c13c1f
login = tester
password = tester
host.name = localhost
host.port = 2089
entity.name = DelegateService
private.key = DelegateService.key
acs.certificate = AccessControlService.crt
--
-- OpenBus Demo
-- client.lua
--
local oil = require "oil"
--oil.verbose:level(3)
local openbus = require "openbus.Openbus"
local scsutils = require ("scs.core.utils").Utils()
local log = require "openbus.util.Log"
-- Inicializacao do barramento
local props = {}
scsutils:readProperties(props, "Delegate.properties")
local host = props["host.name"].value
local port = props["host.port"].value
openbus:init(host, tonumber(port))
--openbus:enableFaultTolerance()
local orb = openbus:getORB()
local finished = {A = false, B = false}
-- Funes para diferentes co-rotinas
function delegate(helloObj, name)
local cred = openbus:getCredential()
local newCred = {identifier = cred.identifier, owner = cred.owner,
delegate = name}
openbus:setThreadCredential(newCred)
local status, err
for i=0,10 do
status, err = oil.pcall(helloObj.sayHello, helloObj, name)
if not status then
print("HelloClient: Erro na chamada sayHello pela 'thread' "..name..".")
break
end
oil.sleep(1)
end
finished[name] = true
end
-- Execuo
function main ()
-- Carga da IDL Hello
orb:loadidlfile("../idl/delegate.idl")
-- Conexo com o barramento e obteno do componente HelloComponent
local login = props.login.value
local password = props.password.value
local registryService = openbus:connectByLoginPassword(login, password)
if not registryService then
io.stderr:write("HelloClient: Erro ao conectar ao barramento.\n")
os.exit(1)
end
local properties = {{name="component_id", value={"DelegateService:1.0.0"}}}
local offers = registryService:findByCriteria({"IHello"}, properties)
-- Assume que HelloComponent o nico servio cadastrado.
local helloComponent = orb:narrow(offers[1].member,
"IDL:scs/core/IComponent:1.0")
local helloFacet = helloComponent:getFacet("IDL:demoidl/demoDelegate/IHello:1.0")
helloFacet = orb:narrow(helloFacet, "IDL:demoidl/demoDelegate/IHello:1.0")
oil.newthread(delegate, helloFacet, "A")
oil.newthread(delegate, helloFacet, "B")
while true do
if finished["A"] and finished["B"] then
break
else
oil.sleep(1)
end
end
openbus:disconnect()
openbus:destroy()
end
oil.main(function()
print(oil.pcall(main))
end)
#!/bin/sh
${OPENBUS_HOME}/core/bin/servicelauncher server.lua&
pid=$!
#echo "PID: $pid"
trap "kill $pid > /dev/null 2> /dev/null" 0
sleep 6
${OPENBUS_HOME}/core/bin/servicelauncher client.lua
--
-- OpenBus Delegate Demo
-- server.lua
--
local oil = require "oil"
local oop = require "loop.base"
local openbus = require "openbus.Openbus"
local scsutils = require ("scs.core.utils").Utils()
-- Inicializao do barramento
local props = {}
scsutils:readProperties(props, "Delegate.properties")
local host = props["host.name"].value
local port = props["host.port"].value
openbus:init(host, tonumber(port))
--openbus:enableFaultTolerance()
local orb = openbus:getORB()
local scs = require "scs.core.base"
-- Implementao da Faceta IHello
local Hello = oop.class {}
function Hello:sayHello()
local intCred = openbus:getInterceptedCredential()
print "HELLO!\n\n"
print("[Thread " .. intCred.delegate .. "]: Hello " .. intCred.owner .. " !")
end
-- Descries do componente HelloComponent
local facetDescriptions = {}
facetDescriptions.IHello = {
name = "IHello",
interface_name = "IDL:demoidl/demoDelegate/IHello:1.0",
class = Hello
}
local componentId = {
name = "DelegateService",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = ""
}
-- Execuo
function main ()
-- Carga da IDL Hello
orb:loadidlfile("../idl/delegate.idl")
-- Permite que o ORB comece a aguardar requisies
openbus:run()
-- Instanciao do componente HelloComponent
local component = scs.newComponent(facetDescriptions, {}, componentId)
-- Conexo com o barramento e registro do componente
local entityName = props["entity.name"].value
local privateKeyFile = props["private.key"].value
local acsCertificateFile = props["acs.certificate"].value
local registryService = openbus:connectByCertificate(entityName, privateKeyFile, acsCertificateFile)
if not registryService then
io.stderr:write("HelloServer: Erro ao conectar ao barramento.\n")
os.exit(1)
end
local suc, id = registryService.__try:register({ properties = {}, member = component.IComponent})
if not suc then
io.stderr:write("HelloServer: Erro ao registrar ofertas.\n")
if id[1] == "IDL:tecgraf/openbus/core/v1_05/registry_service/UnathorizedFacets:1.0" then
for _, facet in ipairs(id.facets) do
io.stderr:write(string.format("Registro da faceta '%s' no autorizado\n", facet))
end
else
io.stderr:write(string.format("Erro: %s.\n", id[1]))
end
os.exit(1)
end
print("*********************************************\n")
print("PUBLISHER\nServio Hello registrado no barramento do OpenBus.\n")
print("*********************************************")
end
oil.main(function()
print(oil.pcall(main))
end)
login = tester
password = tester
host.name = localhost
host.port = 2089
entity.name = HelloService
private.key = HelloService.key
acs.certificate = AccessControlService.crt
--
-- OpenBus Demo
-- client.lua
--
local oil = require "oil"
--oil.verbose:level(3)
local openbus = require "openbus.Openbus"
local scsutils = require ("scs.core.utils").Utils()
local log = require "openbus.util.Log"
-- Inicializacao do barramento
local props = {}
scsutils:readProperties(props, "Hello.properties")
local host = props["host.name"].value
local port = props["host.port"].value
openbus:init(host, tonumber(port))
if arg[1] == "ft" then
openbus:enableFaultTolerance()
end
--
local orb = openbus:getORB()
-- Execuo
function main ()
-- Carga da IDL Hello
orb:loadidlfile("../idl/hello.idl")
-- Conexo com o barramento e obteno do componente HelloComponent
local login = props.login.value
local password = props.password.value
local registryService = openbus:connectByLoginPassword(login, password)
if not registryService then
io.stderr:write("HelloClient: Erro ao conectar ao barramento.\n")
os.exit(1)
end
local flag = true
while flag do
--se o mecanismo de tolerancia a falhas estiver habilitado,
--executa eternamente
local offers = openbus:getRegistryService():find({"IHello"})
-- Assume que HelloComponent o nico servio cadastrado.
local helloComponent = orb:narrow(offers[1].member,
"IDL:scs/core/IComponent:1.0")
local helloFacet = helloComponent:getFacet("IDL:demoidl/hello/IHello:1.0")
helloFacet = orb:narrow(helloFacet, "IDL:demoidl/hello/IHello:1.0")
helloFacet:sayHello()
flag = openbus.isFaultToleranceEnable
if flag then
oil.sleep(5)
end
end
openbus:disconnect()
openbus:destroy()
end
oil.main(function()
print(oil.pcall(main))
end)
#!/bin/sh
${OPENBUS_HOME}/core/bin/servicelauncher server.lua&
pid=$!
#echo "PID: $pid"
trap "kill $pid > /dev/null 2> /dev/null" 0
sleep 6
${OPENBUS_HOME}/core/bin/servicelauncher client.lua $1
--
-- OpenBus Demo
-- server.lua
--
local oil = require "oil"
local oop = require "loop.base"
local openbus = require "openbus.Openbus"
local scsutils = require ("scs.core.utils").Utils()
-- Inicializao do barramento
local props = {}
scsutils:readProperties(props, "Hello.properties")
local host = props["host.name"].value
local port = props["host.port"].value
openbus:init(host, tonumber(port))
--openbus:enableFaultTolerance()
local orb = openbus:getORB()
local scs = require "scs.core.base"
-- Implementao da Faceta IHello
local Hello = oop.class {}
function Hello:sayHello()
local user = openbus:getInterceptedCredential().owner
print "HELLO!\n\n"
print("O usurio OpenBus " .. user .. " requisitou a operao sayHello.")
end
-- Descries do componente HelloComponent
local facetDescriptions = {}
facetDescriptions.IComponent = {
name = "IComponent",
interface_name = "IDL:scs/core/IComponent:1.0",
class = scs.Component
}
facetDescriptions.IMetaInterface = {
name = "IMetaInterface",
interface_name = "IDL:scs/core/IMetaInterface:1.0",
class = scs.MetaInterface
}
facetDescriptions.IHello = {
name = "IHello",
interface_name = "IDL:demoidl/hello/IHello:1.0",
class = Hello
}
local componentId = {
name = "HelloComponent",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = ""
}
-- Execuo
function main ()
-- Carga da IDL Hello
orb:loadidlfile("../idl/hello.idl")
-- Permite que o ORB comece a aguardar requisies
openbus:run()
-- Instanciao do componente HelloComponent
local component = scs.newComponent(facetDescriptions, {}, componentId)
-- Conexo com o barramento e registro do componente
local entityName = props["entity.name"].value
local privateKeyFile = props["private.key"].value
local acsCertificateFile = props["acs.certificate"].value
local registryService = openbus:connectByCertificate(entityName, privateKeyFile, acsCertificateFile)
if not registryService then
io.stderr:write("HelloServer: Erro ao conectar ao barramento.\n")
os.exit(1)
end
local suc, id = registryService.__try:register({
member = component.IComponent,
properties = {
{ name = "facets", value = {"IDL:demoidl/hello/IHello:1.0",} }
},
})
if not suc then
io.stderr:write("HelloServer: Erro ao registrar ofertas.\n")
if id[1] == "IDL:tecgraf/openbus/core/v1_05/registry_service/UnathorizedFacets:1.0" then
for _, facet in ipairs(id.facets) do
io.stderr:write(string.format("Registro da faceta '%s' no autorizado\n", facet))
end
else
io.stderr:write(string.format("Erro: %s.\n", id[1]))
end
os.exit(1)
end
print("*********************************************\n")
print("PUBLISHER\nServio Hello registrado no barramento do OpenBus.\n")
print("*********************************************")
end
oil.main(function()
print(oil.pcall(main))
end)
login = tester
password = tester
host.name = localhost
host.port = 2089
entity.name = HelloSession
private.key = HelloSession.key
acs.certificate = AccessControlService.crt
-------------------------------------------------------------------------------
-- Demo de eventos do Servio de Sesso
-- client.lua
--
local oo = require "loop.base"
local oil = require "oil"
local utils = require ("scs.core.utils").Utils()
local openbus = require "openbus.Openbus"
--oil.verbose:level(3)
-- Carrega as propriedades
local props = {}
utils:readProperties(props, "HelloSession.properties")
-- Aliases
local host = props["host.name"].value
local port = tonumber(props["host.port"].value)
local login = props["login"].value
local password = props["password"].value
local privateKeyFile = props["private.key"].value
local acsCertificateFile = props["acs.certificate"].value
-- Inicializao
openbus:init(host, port)
-- ORB deve estar inicializado antes de carregar o SCS
local scs = require "scs.core.base"
-- Auxiliares
local orb = openbus:getORB()
local compFacet = "IDL:scs/core/IComponent:1.0"
local sessionFacet = "IDL:tecgraf/openbus/session_service/v1_05/ISession:1.0"
local sinkFacet = "IDL:tecgraf/openbus/session_service/v1_05/SessionEventSink:1.0"
local context, sessionId
-------------------------------------------------------------------------------
-- Componente para os eventos
--
local Sink = oo.class{}
function Sink:disconnect()
-- do nothing
end
function Sink:push(event)
print("Recebido: " .. event.value._anyval)
end
-------------------------------------------------------------------------------
-- Descries do componente
local facetDescriptions = {}
facetDescriptions.IComponent = {
name = "IComponent",
interface_name = "IDL:scs/core/IComponent:1.0",
class = scs.Component
}
facetDescriptions.IMetaInterface = {
name = "IMetaInterface",
interface_name = "IDL:scs/core/IMetaInterface:1.0",
class = scs.MetaInterface
}
facetDescriptions.SessionEventSink = {
name = "SessionEventSink",
interface_name = sinkFacet,
class = Sink,
}
local componentId = {
name = "HelloSink",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = ""
}
-------------------------------------------------------------------------------
function main ()
-- Permite que o ORB comece a aguardar requisies
openbus:run()
-- Instanciao do componente
context = scs.newComponent(facetDescriptions, {}, componentId)
-- Conexo com o barramento e obteno do componente de sesso
local registryService = openbus:connectByLoginPassword(login, password)
if not registryService then
io.stderr:write("[ERRO] No foi possvel se conectar ao barramento.\n")
os.exit(1)
end
-- Assume que h s uma sesso cadastrada no registro
-- local offers = registryService:find({"ISession"})
local offers = registryService:findByCriteria({"ISession"}, { {name = "sessionName", value = {"HelloSession"}} })
for _, offer in ipairs(offers) do
local succ = offer.member.__try:_component()
if succ then
local component = orb:narrow(offer.member, compFacet)
local session = orb:narrow(component:getFacet(sessionFacet), sessionFacet)
sessionId = session:addMember(context.IComponent)
return
end
end
io.stderr:write("[ERRO] No foi possvel localizar sesso.\n")
openbus:disconnect()
openbus:destroy()
os.exit(1)
end
oil.main(function()
local succ, msg = oil.pcall(main)
if not succ then
print(msg)
end
end)
#!/bin/sh
${OPENBUS_HOME}/core/bin/servicelauncher client.lua $1
-------------------------------------------------------------------------------
-- Demo de eventos do Servio de Sesso
-- server.lua
--
local oo = require "loop.base"
local utils = require ("scs.core.utils").Utils()
local oil = require "oil"
local openbus = require "openbus.Openbus"
--oil.verbose:level(5)
-- Carrega as propriedades
local props = {}
utils:readProperties(props, "HelloSession.properties")
-- Aliases
local host = props["host.name"].value
local port = tonumber(props["host.port"].value)
local entityName = props["entity.name"].value
local privateKeyFile = props["private.key"].value
local acsCertificateFile = props["acs.certificate"].value
-- Inicializao
openbus:init(host, port)
-- ORB deve estar inicializado antes de carregar o SCS
local scs = require "scs.core.base"
-- Auxiliares
local orb = openbus:getORB()
local compFacet = "IDL:scs/core/IComponent:1.0"
local sinkFacet = "IDL:tecgraf/openbus/session_service/v1_05/SessionEventSink:1.0"
local context
-------------------------------------------------------------------------------
-- Descries do componente
local facetDescriptions = {}
facetDescriptions.IComponent = {
name = "IComponent",
interface_name = "IDL:scs/core/IComponent:1.0",
class = scs.Component
}
facetDescriptions.IMetaInterface = {
name = "IMetaInterface",
interface_name = "IDL:scs/core/IMetaInterface:1.0",
class = scs.MetaInterface
}
local componentId = {
name = "HelloSource",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = ""
}
-------------------------------------------------------------------------------
-- Publica os eventos no canal
--
local function publish(session, sink)
while true do
oil.sleep(3)
local num = math.random(1, 10)
print("Publicando: " .. num)
local event = {}
event.type = "LongEvent"
event.value = setmetatable({ _anyval = num }, oil.corba.idl.long)
sink:push(event)
end
end
-- Funo principal
local function main ()
-- Permite que o ORB comece a aguardar requisies
openbus:run()
-- Instanciao do componente
context = scs.newComponent(facetDescriptions, {}, componentId)
-- Conexo com o barramento
local registryService = openbus:connectByCertificate(entityName,
privateKeyFile, acsCertificateFile)
if not registryService then
io.stderr:write("[ERRO] No foi possvel conectar ao barramento.\n")
os.exit(1)
end
-- Cria a sesso
local session, sessionId
local sessionService = openbus:getSessionService()
succ, session, sessionId = sessionService:createSession(context.IComponent)
if not succ then
io.stderr:write("[ERRO] No foi possvel criar sesso.\n")
openbus.disconnect()
openbus.destroy()
os.exit(1)
end
-- Recupera servio de eventos
local comp = orb:narrow(session:_component(), compFacet)
local sink = orb:narrow(comp:getFacet(sinkFacet), sinkFacet)
-- Registro do componente
local succ, registryId = registryService.__try:register {
properties = {
{name = "sessionName", value = {"HelloSession"}},
{name = "facets", value ={"IDL:tecgraf/openbus/session_service/v1_05/ISession:1.0"}},
},
member = comp,
}
if not succ then
if registryId[1] == "IDL:tecgraf/openbus/core/v1_05/registry_service/UnathorizedFacets:1.0" then
io.stderr:write("[ERRO] No foi possvel registrar a oferta.\n")
for _, facet in ipairs(registryId.facets) do