client.lua 1.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
local utils = require "utils"
local openbus = require "openbus"


-- 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,
}


18 19 20 21 22 23 24 25 26 27
-- setup the ORB and connect to the bus
local OpenBusContext = openbus.initORB().OpenBusContext
OpenBusContext:setDefaultConnection(
  OpenBusContext:createConnection(bushost, busport))

-- call in protected mode
local ok, result = pcall(function ()
  -- login to the bus 
  OpenBusContext:getCurrentConnection():loginByPassword(entity, password
                                                                or entity)
28
  -- find offers of the required service
29
  local OfferRegistry = OpenBusContext:getOfferRegistry()
30 31
  return OfferRegistry:findServices{{name="offer.domain",value="Hello Demo"}}
end)
32 33

-- show eventual errors or call services found
34
if not ok then
35 36
  utils.showerror(result, params, utils.errmsg.LoginByPassword,
                                  utils.errmsg.BusCore)
37
else
38 39 40 41 42 43 44 45 46 47 48 49 50
  -- for each service offer found
  for _, offer in ipairs(result) 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
51 52 53 54
  end
end

-- free any resoures allocated
55
OpenBusContext:getCurrentConnection():logout()