Commit 61878ffb authored by Renato Figueiro Maia's avatar Renato Figueiro Maia

[OPENBUS-1545] Incluir as funcionalidades do Helper no SDK Lua

- Algumas correções nas demos usando a API.


git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/openbus/sdk/lua/branches/openbus_v2_proto@132230 ae0415b3-e90b-0410-900d-d0be9363c56b
parent 7fe092a5
local utils = require "utils"
local assistant = require "openbus.assistant"
-- process command-line arguments
local bushost, busport, entity, password = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
busport = assert(tonumber(busport), "o 2o. argumento é um número de porta")
entity = assert(entity, "o 3o. argumento é a entidade a ser autenticada")
local params = {
bushost = bushost,
busport = busport,
entity = entity,
}
-- obtain an assistant
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
password = password or entity,
observer = utils.failureObserver(params),
}
local repID = "IDL:Messenger:1.0"
-- find offers of the required service
local offers = OpenBusAssistant:findServices{
{name="openbus.component.interface",value=repID},
{name="offer.domain",value="Demo Chain Validation"},
}
ok = nil
for _, offer in ipairs(offers) do
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"),
" não autorizou a chamada\n")
elseif result._repid == "IDL:Unavailable:1.0" then
io.stderr:write("serviço com papel ",utils.getprop(offer, "offer.role"),
" está indisponível\n")
else
utils.showerror(result, params, utils.errmsg.Service)
end
end
end
if ok == nil then
io.stderr:write("não foi possível encontrar o serviço esperado\n")
end
-- free any resoures allocated
OpenBusAssistant:shutdown()
local utils = require "utils"
local openbus = require "openbus"
local assistant = require "openbus.assistant"
local ComponentContext = require "scs.core.ComponentContext"
-- process command-line arguments
local bushost, busport, entity, privatekeypath = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
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(openbus.readKeyFile(privatekeypath))
local params = {
bushost = bushost,
busport = busport,
entity = entity,
privatekeypath = privatekeypath,
}
-- create an assistant, the ORB, and the context manager
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
privatekey = privatekey,
observer = utils.failureObserver(params),
}
-- setup and start the ORB
local OpenBusORB = OpenBusAssistant.orb
openbus.newThread(OpenBusORB.run, OpenBusORB)
-- get bus context manager
local OpenBusContext = OpenBusORB.OpenBusContext
-- load interface definitions
OpenBusORB:loadidlfile("callchain/messenger.idl")
local iface = OpenBusORB.types:lookup("Messenger")
params.interface = iface.name
-- create service implementation
local function chain2str(chain)
local entities = {}
for _, login in ipairs(chain.originators) do
entities[#entities+1] = login.entity
end
entities[#entities+1] = chain.caller.entity
return table.concat(entities, "->")
end
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 = OpenBusContext:getCallerChain()
print("repassando mensagem de "..chain2str(chain))
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
local component = ComponentContext(OpenBusORB, {
name = iface.name,
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "Lua",
})
component:addFacet(iface.name, iface.repID, messenger)
-- find offers of the required service
offers = OpenBusAssistant:findServices{
{name="openbus.component.interface",value=iface.repID},
{name="offer.domain",value="Demo Chain Validation"},
}
-- check if some offer was found
if #offers == 0 then
io.stderr:write("nenhum serviço encontrado para repassar mensagens\n")
return
end
-- register service offer
OpenBusAssistant:registerService(component.IComponent, {
{name="offer.role",value="proxy messenger"},
{name="offer.domain",value="Demo Chain Validation"},
})
local utils = require "utils"
local openbus = require "openbus"
local assistant = require "openbus.assistant"
local ComponentContext = require "scs.core.ComponentContext"
-- process command-line arguments
local bushost, busport, entity, privatekeypath = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
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(openbus.readKeyFile(privatekeypath))
local params = {
bushost = bushost,
busport = busport,
entity = entity,
privatekeypath = privatekeypath,
}
-- create an assistant, the ORB, and the context manager
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
privatekey = privatekey,
observer = utils.failureObserver(params),
}
-- setup and start the ORB
local OpenBusORB = OpenBusAssistant.orb
openbus.newThread(OpenBusORB.run, OpenBusORB)
-- get bus context manager
local OpenBusContext = OpenBusORB.OpenBusContext
-- create service implementation
local function chain2str(chain)
local entities = {}
for _, login in ipairs(chain.originators) do
entities[#entities+1] = login.entity
end
entities[#entities+1] = chain.caller.entity
return table.concat(entities, "->")
end
local messenger = {}
function messenger:showMessage(message)
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
OpenBusORB:loadidlfile("callchain/messenger.idl")
local iface = OpenBusORB.types:lookup("Messenger")
params.interface = iface.name
-- create service SCS component
local component = ComponentContext(OpenBusORB, {
name = iface.name,
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "Lua",
})
component:addFacet(iface.name, iface.repID, messenger)
-- register service offer
OpenBusAssistant:registerService(component.IComponent, {
{name="offer.role",value="actual messenger"},
{name="offer.domain",value="Demo Chain Validation"},
})
local utils = require "utils"
local assistant = require "openbus.assistant"
-- process command-line arguments
local bushost, busport, entity, password, interval, timeout = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
busport = assert(tonumber(busport), "o 2o. argumento é um número de porta")
entity = assert(entity, "o 3o. argumento é a entidade a ser autenticada")
interval = assert(tonumber(interval or 1), "o 5o. argumento é um tempo entre "..
"tentativas de acesso ao barramento em virtude de falhas")
retries = assert(tonumber(timeout or 10), "o 6o. argumento é o número máximo "..
"de tentativas de acesso ao barramento em virtude de falhas")
local params = {
bushost = bushost,
busport = busport,
entity = entity,
}
-- obtain an assistant
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
password = password or entity,
observer = utils.failureObserver(params),
}
-- find offers of the required service
local props = {{name="offer.domain",value="Demo Dedicated Clock"}}
local offers = OpenBusAssistant:findServices(props, retries)
-- for each service offer found
local timestamp
for _, offer in ipairs(offers) do
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.errmsg.Service)
else
break
end
end
if timestamp == nil then
io.stderr:write("não foi possível utilizar os serviços encontrados\n")
end
-- show the obtained timestamp, if any
print(os.date(nil, timestamp))
-- free any resoures allocated
OpenBusAssistant:shutdown()
local utils = require "utils"
local openbus = require "openbus"
local assistant = require "openbus.assistant"
local ComponentContext = require "scs.core.ComponentContext"
-- process command-line arguments
local bushost, busport, entity, privatekeypath, interval = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
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(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 = {
bushost = bushost,
busport = busport,
entity = entity,
privatekeypath = privatekeypath,
}
-- create service implementation
local clock = {}
function clock:getTime()
return os.time() - 24*60*60 -- send the time of the same moment yesterday
end
-- create an assistant, the ORB, and the context manager
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
privatekey = privatekey,
observer = utils.failureObserver(params),
}
-- setup and start the ORB
local OpenBusORB = OpenBusAssistant.orb
openbus.newThread(OpenBusORB.run, OpenBusORB)
-- load interface definition
local iface = OpenBusORB:loadidl("interface Clock { double getTime(); };")
params.interface = iface.name
-- create service SCS component
local component = ComponentContext(OpenBusORB, {
name = iface.name,
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "Lua",
})
component:addFacet(iface.name, iface.repID, clock)
-- register service offer
OpenBusAssistant:registerService(component.IComponent, {
{name="offer.domain",value="Demo Dedicated Clock"},
})
local utils = require "utils"
local assistant = require "openbus.assistant"
-- process command-line arguments
local bushost, busport, entity, password, language = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
busport = assert(tonumber(busport), "o 2o. argumento é um número de porta")
entity = assert(entity, "o 3o. argumento é a entidade a ser autenticada")
language = assert(language or "Portuguese",
"o 4o. argumento deve ser uma das opções: English, Spanish, Portuguese")
local params = {
bushost = bushost,
busport = busport,
entity = entity,
}
-- obtain an assistant
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
password = password or entity,
observer = utils.failureObserver(params),
}
-- find offers of the required service
local offers = OpenBusAssistant:findServices{
{name="offer.domain",value="Demo Greetings"},
{name="greetings.language",value=language},
}
-- for each service offer found
for _, offer in ipairs(offers) 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
end)
if not ok then
utils.showerror(result, params, utils.errmsg.Service)
end
end
-- free any resoures allocated
OpenBusAssistant:shutdown()
local utils = require "utils"
local oo = require "openbus.util.oo"
local openbus = require "openbus"
local assistant = require "openbus.assistant"
local ComponentContext = require "scs.core.ComponentContext"
-- process command-line arguments
local bushost, busport, entity, privatekeypath = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
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(openbus.readKeyFile(privatekeypath))
local params = {
bushost = bushost,
busport = busport,
entity = entity,
privatekeypath = privatekeypath,
}
-- create an assistant, the ORB, and the context manager
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
privatekey = privatekey,
observer = utils.failureObserver(params),
}
-- setup and start the ORB
local OpenBusORB = OpenBusAssistant.orb
openbus.newThread(OpenBusORB.run, OpenBusORB)
-- get bus context manager
local OpenBusContext = OpenBusORB.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 = OpenBusORB:loadidl("interface Greetings {void sayGreetings();};")
params.interface = iface.name
-- create service SCS components and register them
local messages = {
English = {
GoodMorning = "Good morning %s",
GoodAfternoon = "Good afternoon %s",
GoodNight = "Good night %s",
},
Spanish = {
GoodMorning = "Buenos días %s",
GoodAfternoon = "Buenas tardes %s",
GoodNight = "Buenas noches %s",
},
Portuguese = {
GoodMorning = "Bom dia %s",
GoodAfternoon = "Boa tarde %s",
GoodNight = "Boa noite %s",
},
}
for language, greetings in pairs(messages) do
-- create service SCS component
local component = ComponentContext(OpenBusORB, {
name = language.." "..iface.name,
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "Lua",
})
for name, message in pairs(greetings) do
component:addFacet(name, iface.repID, Greetings{message=message})
end
-- register service offer
OpenBusAssistant:registerService(component.IComponent, {
{name="offer.domain",value="Demo Greetings"},
{name="greetings.language",value=language},
})
end
local utils = require "utils"
local assistant = require "openbus.assistant"
-- process command-line arguments
local bushost, busport, entity, password = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
busport = assert(tonumber(busport), "o 2o. argumento é um número de porta")
entity = assert(entity, "o 3o. argumento é a entidade a ser autenticada")
local params = {
bushost = bushost,
busport = busport,
entity = entity,
}
-- obtain an assistant
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
password = password or entity,
observer = utils.failureObserver(params),
}
-- find offers of the required service
local offers = OpenBusAssistant:findServices{
{name="offer.domain",value="Hello Demo"},
}
-- for each service offer found
for _, offer in ipairs(offers) do
-- call in protected mode
local ok, result = pcall(function ()
-- get the facet providing the service
local facet = assert(offer.service_ref:getFacetByName("Hello"),
"o serviço encontrado não provê a faceta ofertada")
-- invoke the service
facet:__narrow():sayHello()
end)
if not ok then
utils.showerror(result, params, utils.errmsg.Service)
end
end
-- free any resoures allocated
OpenBusAssistant:shutdown()
local utils = require "utils"
local openbus = require "openbus"
local assistant = require "openbus.assistant"
local ComponentContext = require "scs.core.ComponentContext"
-- process command-line arguments
local bushost, busport, entity, privatekeypath = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
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(openbus.readKeyFile(privatekeypath))
local params = {
bushost = bushost,
busport = busport,
entity = entity,
privatekeypath = privatekeypath,
}
-- create an assistant, the ORB, and the context manager
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
privatekey = privatekey,
observer = utils.failureObserver(params),
}
-- setup and start the ORB
local OpenBusORB = OpenBusAssistant.orb
openbus.newThread(OpenBusORB.run, OpenBusORB)
-- get bus context manager
local OpenBusContext = OpenBusORB.OpenBusContext
-- create service implementation
local hello = {}
function hello:sayHello()
print("Hello "..OpenBusContext:getCallerChain().caller.entity.."!")
end
-- load IDL definitions
local iface = OpenBusORB:loadidl("interface Hello { void sayHello(); };")
params.interface = iface.name
-- create service SCS component
local component = ComponentContext(OpenBusORB, {
name = iface.name,
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "Lua",
})
component:addFacet("Hello", iface.repID, hello)
-- register service offer
OpenBusAssistant:registerService(component.IComponent, {
{name="offer.domain",value="Hello Demo"},
})
local utils = require "utils"
local openbus = require "openbus"
local assistant = require "openbus.assistant"
-- process command-line arguments
local bushost, busport, entity, password, interval = ...
bushost = assert(bushost, "o 1o. argumento é o host do barramento")
busport = assert(busport, "o 2o. argumento é a porta do barramento")
busport = assert(tonumber(busport), "o 2o. argumento é um número de porta")
entity = assert(entity, "o 3o. argumento é a entidade a ser autenticada")
interval = assert(tonumber(interval or 1), "o 5o. argumento é um tempo entre "..
"tentativas de acesso ao barramento em virtude de falhas")
local params = {
bushost = bushost,
busport = busport,
entity = entity,
}
-- obtain an assistant
local OpenBusAssistant = assistant.create{
bushost = bushost,
busport = busport,
entity = entity,
password = password or entity,
observer = utils.failureObserver(params),
}
-- create object to search for offer
local clock
local searching
local function findclock()
if not searching then
openbus.newThread(function ()
searching = true
clock = nil -- discard current service reference
repeat
-- find offers of the required service
local props = {{name="offer.domain",value="Demo Independent Clock"}}
local offers = OpenBusAssistant:findServices(props, 0)
for _, offer in ipairs(offers) do
local ok, result = pcall(function ()
local facet = assert(offer.service_ref:getFacetByName("Clock"),
"o serviço encontrado não provê a faceta ofertada")
clock = facet:__narrow()
end)
if not ok then
utils.showerror(result, params, utils.errmsg.Service)
else
break
end
end
openbus.sleep(interval)
until clock
searching = nil
end)
end
end
findclock()
-- independent clock
for i = 1, 10 do
local time
if clock ~= nil then
local ok, result = pcall(clock.getTime, clock)
if ok then
time = result
else
utils.showerror(result, params, utils.errmsg.Service)
findclock()
end
end
print(os.date(nil, time))
openbus.sleep(1)
end