Commit 9959f867 authored by Renato Figueiro Maia's avatar Renato Figueiro Maia

[OPENBUS-1961] Fazer experimentações para tratar problemas atuais com a API

- Implementação de uma nova proposta de API.
- Adaptação dos testes de interoperabilidade.
- Adaptação dos demos.


git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/openbus/sdk/lua/branches/openbus_v2_proto@132121 ae0415b3-e90b-0410-900d-d0be9363c56b
parent bc20e241
......@@ -15,47 +15,38 @@ local params = {
}
local repID = "IDL:Messenger:1.0"
-- setup the ORB and connect to the bus
local OpenBusContext = openbus.initORB().OpenBusContext
OpenBusContext:setDefaultConnection(
OpenBusContext:createConnection(bushost, busport))
-- variable to hold resources to be freed before termination
local conn
local repID = "IDL:Messenger:1.0"
-- function that uses the bus to find the required service
local function findService()
-- connect to the bus
local connections = openbus.initORB().OpenBusConnectionManager
conn = connections:createConnection(bushost, busport)
connections:setDefaultConnection(conn)
-- call in protected mode
local ok, result = pcall(function ()
-- login to the bus
conn:loginByPassword(entity, password or entity)
OpenBusContext:getCurrentConnection():loginByPassword(entity, password
or entity)
-- find the offered service
return conn.offers:findServices{
local OfferRegistry = OpenBusContext:getCoreService("OfferRegistry")
return OfferRegistry:findServices{
{name="openbus.component.interface",value=repID},
{name="offer.domain",value="Demo Chain Validation"},
}
end
-- function that uses the required service through the bus
local function callService(offer)
service = offer.service_ref
if not service:_non_existent() then
local facet = service:getFacet(repID)
if facet ~= nil then
facet:__narrow():showMessage("Hello!")
return true
end
end
end
-- perform operations in protected mode and handle eventual errors
local ok, result = pcall(findService)
end)
if not ok then
utils.showerror(result, params, utils.msg.Connect, utils.msg.LoginByPassword)
utils.showerror(result, params, utils.errmsg.LoginByPassword,
utils.errmsg.BusCore)
else
ok = nil
for _, offer in ipairs(result) do
ok, result = pcall(callService, offer)
ok, result = pcall(function ()
-- get the facet providing the service
local facet = assert(offer.service_ref:getFacet(repID),
"o serviço encontrado não provê a faceta ofertada")
-- invoke the service
facet:__narrow():showMessage("Hello!")
end)
if not ok then
if result._repid == "IDL:Unauthorized:1.0" then
io.stderr:write("serviço com papel ",utils.getprop(offer, "offer.role"),
......@@ -64,7 +55,7 @@ else
io.stderr:write("serviço com papel ",utils.getprop(offer, "offer.role"),
" está indisponível\n")
else
utils.showerror(result, params, utils.msg.Service)
utils.showerror(result, params, utils.errmsg.Service)
end
end
end
......@@ -74,4 +65,4 @@ else
end
-- free any resoures allocated
if conn ~= nil then conn:logout() end
OpenBusContext:getCurrentConnection():logout()
local utils = require "utils"
local server = require "openbus.util.server"
local openbus = require "openbus"
local ComponentContext = require "scs.core.ComponentContext"
......@@ -12,7 +11,7 @@ busport = assert(tonumber(busport), "o 2o. argumento é um número de porta")
entity = assert(entity, "o 3o. argumento é a entidade a ser autenticada")
privatekeypath = assert(privatekeypath,
"o 4o. argumento é o caminho da chave privada de autenticação da entidade")
local privatekey = assert(server.readfrom(privatekeypath))
local privatekey = assert(openbus.readkeyfile(privatekeypath))
local params = {
bushost = bushost,
busport = busport,
......@@ -25,27 +24,16 @@ local params = {
local orb = openbus.initORB()
openbus.newthread(orb.run, orb)
-- get bus context manager
local OpenBusContext = orb.OpenBusContext
-- load interface definitions
orb:loadidlfile("callchain/messenger.idl")
local iface = orb.types:lookup("Messenger")
params.interface = iface.name
-- create service implementation
local offers
local function callService(message)
if offers ~= nil then
for _, offer in ipairs(offers) do
service = offer.service_ref
if not service:_non_existent() then
local facet = service:getFacet(iface.repID)
if facet ~= nil then
facet:__narrow():showMessage(message)
return true
end
end
end
end
end
local function chain2str(chain)
local entities = {}
for _, login in ipairs(chain.originators) do
......@@ -54,19 +42,26 @@ local function chain2str(chain)
entities[#entities+1] = chain.caller.entity
return table.concat(entities, "->")
end
local conn
local function callService(offer, message)
local facet = assert(offer.service_ref:getFacet(iface.repID),
"o serviço encontrado não provê a faceta ofertada")
facet:__narrow():showMessage(message)
end
local messenger = {}
function messenger:showMessage(message)
local chain = conn:getCallerChain()
local chain = OpenBusContext:getCallerChain()
print("repassando mensagem de "..chain2str(chain))
conn:joinChain(chain)
local ok, result = pcall(callService, message)
if not ok then
utils.showerror(result, params, utils.msg.Service)
elseif not result then
print(" nenhum serviço encontrado para repassar mensagem")
error{_repid="IDL:Unavailable:1.0"}
OpenBusContext:joinChain(chain)
for _, offer in ipairs(offers) do
local ok, result = pcall(callService, offer, message)
if not ok then
utils.showerror(result, params, utils.errmsg.Service)
else
return
end
end
io.stderr:write("serviços encontrados não estão disponíveis\n")
error{_repid="IDL:Unavailable:1.0"}
end
-- create service SCS component
......@@ -79,39 +74,40 @@ local component = ComponentContext(orb, {
})
component:addFacet(iface.name, iface.repID, messenger)
-- connect to the bus
OpenBusContext:setDefaultConnection(
OpenBusContext:createConnection(bushost, busport))
-- call in protected mode
local ok, result = pcall(function ()
-- connect to the bus
local connections = orb.OpenBusConnectionManager
conn = connections:createConnection(bushost, busport)
connections:setDefaultConnection(conn)
-- login to the bus
conn:loginByCertificate(entity, privatekey)
-- register service at the bus
conn.offers:registerService(component.IComponent, {
{name="offer.role",value="proxy messenger"},
{name="offer.domain",value="Demo Chain Validation"},
})
OpenBusContext:getCurrentConnection():loginByCertificate(entity, privatekey)
-- get offer registry
local OfferRegistry = OpenBusContext:getCoreService("OfferRegistry")
-- find the offered service
offers = conn.offers:findServices{
offers = OfferRegistry:findServices{
{name="openbus.component.interface",value=iface.repID},
{name="offer.role",value="actual messenger"},
{name="offer.domain",value="Demo Chain Validation"},
}
assert(#offers, "nenhum serviço encontrado para repassar mensagens")
-- register service at the bus
OfferRegistry:registerService(component.IComponent, {
{name="offer.role",value="proxy messenger"},
{name="offer.domain",value="Demo Chain Validation"},
})
end)
-- show eventual errors
if not ok then
utils.showerror(result, params,
utils.msg.Connect,
utils.msg.LoginByCertificate,
utils.msg.Register)
utils.showerror(result, params, utils.errmsg.LoginByCertificate,
utils.errmsg.Register,
utils.errmsg.BusCore)
-- free any resoures allocated
if conn ~= nil then
conn:logout()
end
OpenBusContext:getCurrentConnection():logout()
orb:shutdown()
end
local utils = require "utils"
local server = require "openbus.util.server"
local openbus = require "openbus"
local ComponentContext = require "scs.core.ComponentContext"
......@@ -12,7 +11,7 @@ busport = assert(tonumber(busport), "o 2o. argumento é um número de porta")
entity = assert(entity, "o 3o. argumento é a entidade a ser autenticada")
privatekeypath = assert(privatekeypath,
"o 4o. argumento é o caminho da chave privada de autenticação da entidade")
local privatekey = assert(server.readfrom(privatekeypath))
local privatekey = assert(openbus.readkeyfile(privatekeypath))
local params = {
bushost = bushost,
busport = busport,
......@@ -25,10 +24,9 @@ local params = {
local orb = openbus.initORB()
openbus.newthread(orb.run, orb)
-- load interface definitions
orb:loadidlfile("callchain/messenger.idl")
local iface = orb.types:lookup("Messenger")
params.interface = iface.name
-- get bus context manager
local OpenBusContext = orb.OpenBusContext
-- create service implementation
local function chain2str(chain)
......@@ -39,17 +37,21 @@ local function chain2str(chain)
entities[#entities+1] = chain.caller.entity
return table.concat(entities, "->")
end
local conn
local messenger = {}
function messenger:showMessage(message)
local chain = conn:getCallerChain()
local chain = OpenBusContext:getCallerChain()
if chain.caller.entity ~= entity then
print("recusando mensagem de "..chain2str(chain))
error{_repid="IDL:Unauthorized:1.0"}
end
print("aceitando mensagem de "..chain2str(chain)..":", message)
end
-- load interface definitions
orb:loadidlfile("callchain/messenger.idl")
local iface = orb.types:lookup("Messenger")
params.interface = iface.name
-- create service SCS component
local component = ComponentContext(orb, {
name = iface.name,
......@@ -60,18 +62,18 @@ local component = ComponentContext(orb, {
})
component:addFacet(iface.name, iface.repID, messenger)
-- connect to the bus
OpenBusContext:setDefaultConnection(
OpenBusContext:createConnection(bushost, busport))
-- call in protected mode
local ok, result = pcall(function ()
-- connect to the bus
local connections = orb.OpenBusConnectionManager
conn = connections:createConnection(bushost, busport)
connections:setDefaultConnection(conn)
-- login to the bus
conn:loginByCertificate(entity, privatekey)
OpenBusContext:getCurrentConnection():loginByCertificate(entity, privatekey)
-- register service at the bus
conn.offers:registerService(component.IComponent, {
local OfferRegistry = OpenBusContext:getCoreService("OfferRegistry")
OfferRegistry:registerService(component.IComponent, {
{name="offer.role",value="actual messenger"},
{name="offer.domain",value="Demo Chain Validation"},
})
......@@ -79,13 +81,10 @@ end)
-- show eventual errors
if not ok then
utils.showerror(result, params,
utils.msg.Connect,
utils.msg.LoginByCertificate,
utils.msg.Register)
utils.showerror(result, params, utils.errmsg.LoginByCertificate,
utils.errmsg.Register,
utils.errmsg.BusCore)
-- free any resoures allocated
if conn ~= nil then
conn:logout()
end
OpenBusContext:getCurrentConnection():logout()
orb:shutdown()
end
......@@ -20,8 +20,8 @@ local params = {
}
-- independent clock thread
function retry()
-- function to count the number os remaining retries
local function retry()
if retries > 0 then
retries = retries-1
openbus.sleep(interval)
......@@ -32,33 +32,24 @@ end
-- setup the ORB
local orb = openbus.initORB()
-- get bus context manager
local OpenBusContext = orb.OpenBusContext
-- connect to the bus
local conn
local connections = orb.OpenBusConnectionManager
repeat
local ok, result = pcall(connections.createConnection, connections, bushost,
busport)
if ok then
conn = result
else
utils.showerror(result, params, utils.msg.Connect)
if not retry() then
return
end
end
until ok
connections:setDefaultConnection(conn)
local conn = OpenBusContext:createConnection(bushost, busport)
OpenBusContext:setDefaultConnection(conn)
-- define callback for auto-relogin
function conn:onInvalidLogin()
repeat
local ok, result = pcall(conn.loginByPassword, conn, entity, password
local ok, result = pcall(self.loginByPassword, self, entity, password
or entity)
if not ok then
if result._repid == except.repid.AlreadyLoggedIn then
ok = true -- ignore this exception
else
utils.showerror(result, params, utils.msg.LoginByPassword)
utils.showerror(result, params, utils.errmsg.LoginByPassword,
utils.errmsg.BusCore)
end
end
until ok or not retry()
......@@ -68,35 +59,37 @@ end
conn:onInvalidLogin()
-- search for offer
local timestamp
local props = {{name="offer.domain",value="Demo Independent Clock"}}
local function getfacet(offer)
return orb:narrow(offer.service_ref:getFacetByName("Clock"))
end
local offers = conn.offers
repeat
local ok, result = pcall(offers.findServices, offers, props)
if ok then
local OfferRegistry = OpenBusContext:getCoreService("OfferRegistry")
local ok, result = pcall(OfferRegistry.findServices, OfferRegistry, props)
if not ok then
utils.showerror(result, params, utils.errmsg.BusCore)
else
for _, offer in ipairs(result) do
ok, result = pcall(getfacet, offer)
local ok, result = pcall(function ()
-- get the facet providing the service
local facet = assert(offer.service_ref:getFacetByName("Clock"),
"o serviço encontrado não provê a faceta ofertada")
-- invoke the service
timestamp = facet:__narrow():getTime()
end)
if not ok then
utils.showerror(result, params, utils.msg.Service)
elseif result == nil then
io.stderr:write("serviço encontrado não oferece a faceta esperada.\n")
utils.showerror(result, params, utils.errmsg.Service)
else
ok, result = pcall(result.getTime, result)
if not ok then
utils.showerror(result, params, utils.msg.Service)
end
conn:logout()
return print(os.date(nil, result))
break
end
end
io.stderr:write("o serviço esperado não foi encontrado\n")
else
utils.showerror(result, params, utils.msg.Bus)
end
if not retry() then
conn:logout()
return
if timestamp == nil then
io.stderr:write("não foi possível utilizar os serviços encontrados\n")
end
end
until false
until timestamp or not retry()
-- free any resoures allocated
conn:logout()
print(os.date(nil, timestamp))
local utils = require "utils"
local server = require "openbus.util.server"
local openbus = require "openbus"
local ComponentContext = require "scs.core.ComponentContext"
......@@ -12,7 +11,7 @@ busport = assert(tonumber(busport), "o 2o. argumento é um número de porta")
entity = assert(entity, "o 3o. argumento é a entidade a ser autenticada")
privatekeypath = assert(privatekeypath,
"o 4o. argumento é o caminho da chave privada de autenticação da entidade")
local privatekey = assert(server.readfrom(privatekeypath))
local privatekey = assert(openbus.readkeyfile(privatekeypath))
interval = assert(tonumber(interval or 1), "o 5o. argumento é um tempo entre "..
"tentativas de acesso ao barramento em virtude de falhas")
local params = {
......@@ -29,13 +28,16 @@ function clock:getTime()
return os.time() - 24*60*60 -- send the time of the same moment yesterday
end
-- setup and start the ORB
local orb = openbus.initORB()
openbus.newthread(orb.run, orb)
-- create service SCS component
-- load interface definition
local iface = orb:loadidl("interface Clock { double getTime(); };")
params.interface = iface.name
-- create service SCS component
local component = ComponentContext(orb, {
name = iface.name,
major_version = 1,
......@@ -47,61 +49,55 @@ component:addFacet(iface.name, iface.repID, clock)
local props = {{name="offer.domain",value="Demo Independent Clock"}}
-- connect to the bus
local conn
local connections = orb.OpenBusConnectionManager
repeat
local ok, result = pcall(connections.createConnection, connections, bushost,
busport)
if ok then
conn = result
else
utils.showerror(result, params, utils.msg.Connect)
openbus.sleep(interval)
end
until ok
connections:setDefaultConnection(conn)
-- get bus context manager
local OpenBusContext = orb.OpenBusContext
-- create thread to register offer after (re)login
local offer
local function doregister(self)
self.active = coroutine.running()
local offers = conn.offers
repeat
local ok, result = pcall(offers.registerService, offers,
component.IComponent, props)
if ok then
offer = result
else
utils.showerror(result, params, utils.msg.Register)
openbus.sleep(interval)
end
until ok
self.active = nil
end
local registerer = {}
function registerer:enable()
if offer == nil and self.active == nil then
openbus.newthread(doregister, self)
self.disabled = nil
end
function registerer:activate()
if not self.disabled and self.active == nil then
openbus.newthread(function ()
self.active = true
repeat
local ok, result = pcall(function ()
local OfferRegistry = OpenBusContext:getCoreService("OfferRegistry")
OfferRegistry:registerService(component.IComponent, props)
self.disabled = true
end)
if not ok then
utils.showerror(result, params, utils.errmsg.Register,
utils.errmsg.BusCore)
openbus.sleep(interval)
end
until ok
self.active = nil
end)
end
end
-- connect to the bus
local conn = OpenBusContext:createConnection(bushost, busport)
OpenBusContext:setDefaultConnection(conn)
-- define callback for auto-relogin
function conn:onInvalidLogin()
offer = nil
registerer:enable()
repeat
local ok, result = pcall(conn.loginByCertificate, conn, entity, privatekey)
local ok, result = pcall(self.loginByCertificate, self, entity, privatekey)
if not ok then
if result._repid == repid.AlreadyLoggedIn then
ok = true -- ignore this exception
else
utils.showerror(result, params, utils.msg.LoginByCertificate)
utils.showerror(result, params, utils.errmsg.LoginByCertificate,
utils.errmsg.BusCore)
openbus.sleep(interval)
end
end
until ok
registerer:enable()
registerer:activate()
end
-- login to the bus
......
......@@ -17,53 +17,42 @@ local params = {
}
-- variable to hold resources to be freed before termination
local conn
-- setup the ORB and connect to the bus
local OpenBusContext = openbus.initORB().OpenBusContext
OpenBusContext:setDefaultConnection(
OpenBusContext:createConnection(bushost, busport))
-- function that uses the bus to find the required service
local function findService()
-- connect to the bus
local connections = openbus.initORB().OpenBusConnectionManager
conn = connections:createConnection(bushost, busport)
connections:setDefaultConnection(conn)
-- login to the bus
conn:loginByPassword(entity, password or entity)
-- find the offered service
return conn.offers:findServices{
-- perform operations in protected mode and handle eventual errors
local ok, result = pcall(function ()
-- login to the bus
OpenBusContext:getCurrentConnection():loginByPassword(entity, password
or entity)
-- find the offered service
local OfferRegistry = OpenBusContext:getCoreService("OfferRegistry")
return OfferRegistry:findServices{
{name="offer.domain",value="Demo Greetings"},
{name="greetings.language",value=language},
}
end
-- function that uses the required service through the bus
local facets = {"GoodMorning", "GoodAfternoon", "GoodNight"}
local function callService(offers)
for _, offer in ipairs(offers) do
service = offer.service_ref
if not service:_non_existent() then
for _, name in ipairs(facets) do
local facet = service:getFacetByName(name)
if facet == nil then
error("o serviço encontrado não oferece mais a faceta '"..name.."'")
end
end)
if not ok then
utils.showerror(result, params, utils.errmsg.LoginByPassword,
utils.errmsg.BusCore)
else
for _, offer in ipairs(result) do
ok, result = pcall(function ()
for _, name in ipairs{"GoodMorning", "GoodAfternoon", "GoodNight"} do
-- get the facet providing the service
local facet = assert(offer.service_ref:getFacetByName(name),
"o serviço encontrado não provê a faceta ofertada")
-- invoke the service
facet:__narrow():sayGreetings()
end
return
end
end)
end
error("não foi possível encontrar o serviço esperado")
end
-- perform operations in protected mode and handle eventual errors
local ok, result = pcall(findService)
if not ok then
utils.showerror(result, params, utils.msg.Connect, utils.msg.LoginByPassword)
else
ok, result = pcall(callService, result)
if not ok then
utils.showerror(result, params, utils.msg.Service)
utils.showerror(result, params, utils.errmsg.Service)
end
end
-- free any resoures allocated
if conn ~= nil then conn:logout() end
OpenBusContext:getCurrentConnection():logout()
local utils = require "utils"
local oo = require "openbus.util.oo"
local server = require "openbus.util.server"
local openbus = require "openbus"
local ComponentContext = require "scs.core.ComponentContext"
......@@ -13,7 +12,7 @@ busport = assert(tonumber(busport), "o 2o. argumento é um número de porta")
entity = assert(entity, "o 3o. argumento é a entidade a ser autenticada")
privatekeypath = assert(privatekeypath,
"o 4o. argumento é o caminho da chave privada de autenticação da entidade")
local privatekey = assert(server.readfrom(privatekeypath))
local privatekey = assert(openbus.readkeyfile(privatekeypath))
local params = {
bushost = bushost,
busport = busport,
......@@ -22,6 +21,25 @@ local params = {
}
-- setup and start the ORB
local orb = openbus.initORB()
openbus.newthread(orb.run, orb)
-- get bus context manager
local OpenBusContext = orb.OpenBusContext
-- create service implementation
local Greetings = oo.class{}
function Greetings:sayGreetings()
print(self.message:format(OpenBusContext:getCallerChain().caller.entity))
end
-- load IDL definitions
local iface = orb:loadidl("interface Greetings { void sayGreetings(); };")
params.interface = iface.name
-- create service SCS components
local messages = {
English = {
GoodMorning = "Good morning %s",
......@@ -39,21 +57,6 @@ local messages = {
GoodNight = "Boa noite %s",
},
}
-- create service implementation
local conn
local Greetings = oo.class{}
function Greetings:sayGreetings()
print(self.message:format(conn:getCallerChain().caller.entity))
end
-- setup and start the ORB
local orb = openbus.initORB()
openbus.newthread(orb.run, orb)
-- create service SCS component
local iface = orb:loadidl("interface Greetings { void sayGreetings(); };")
params.interface = iface.name
local components = {}
for language, greetings in pairs(messages) do
local component = ComponentContext(orb, {
......@@ -75,33 +78,28 @@ for language, greetings in pairs(messages) do
}
end
-- connect to the bus
OpenBusContext:setDefaultConnection(
OpenBusContext:createConnection(bushost, busport))
-- call in protected mode
local ok, result = pcall(function ()
-- connect to the bus
local connections = orb.OpenBusConnectionManager
conn = connections:createConnection(bushost, busport)
connections:setDefaultConnection(conn)
-- login to the bus
conn:loginByCertificate(entity, privatekey)