Commit 4d7c1c69 authored by Hugo Roenick's avatar Hugo Roenick

formatação: substituindo tabs por espaços

git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/openbus/sdk/lua/branches/openbus_v2_proto@126878 ae0415b3-e90b-0410-900d-d0be9363c56b
parent d70d2f10
......@@ -14,42 +14,42 @@ cothread.next(coroutine.create(orb.run), orb)
-- setup action on login termination
function conn:onInvalidLogin()
print("login terminated, shutting the server down")
conn:close() -- free connection resources
orb:shutdown() -- stop the ORB and free all its resources
print("login terminated, shutting the server down")
conn:close() -- free connection resources
orb:shutdown() -- stop the ORB and free all its resources
end
-- create service implementation
Broadcaster = {
__type = orb.types:lookup("tecgraf::openbus::demo::delegation::Broadcaster"),
subscribers = {},
__type = orb.types:lookup("tecgraf::openbus::demo::delegation::Broadcaster"),
subscribers = {},
}
function Broadcaster:post(message)
conn:joinChain() -- joins the current caller chain
for user in pairs(self.subscribers) do
Messenger:post(user, message)
end
conn:joinChain() -- joins the current caller chain
for user in pairs(self.subscribers) do
Messenger:post(user, message)
end
end
function Broadcaster:subscribe()
local callers = conn:getCallerChain().callers
local user = callers[1].entity
print("subscription by "..chain2str(callers))
self.subscribers[user] = true
local callers = conn:getCallerChain().callers
local user = callers[1].entity
print("subscription by "..chain2str(callers))
self.subscribers[user] = true
end
function Broadcaster:unsubscribe()
local callers = conn:getCallerChain().callers
local user = callers[1].entity
print("unsubscription by "..chain2str(callers))
self.subscribers[user] = nil
local callers = conn:getCallerChain().callers
local user = callers[1].entity
print("unsubscription by "..chain2str(callers))
self.subscribers[user] = nil
end
-- create service SCS component
component = ComponentContext(orb, {
name = "Broadcaster",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "",
name = "Broadcaster",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "",
})
component:addFacet("broadcaster", Broadcaster.__type.repID, Broadcaster)
......@@ -58,9 +58,9 @@ conn:loginByPassword("broadcaster", "broadcaster")
-- find messenger
offers = conn.offers:findServices({
{name="openbus.offer.entity",value="messenger"}, -- automatic property
{name="openbus.component.facet",value="messenger"}, -- automatic property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
{name="openbus.offer.entity",value="messenger"}, -- automatic property
{name="openbus.component.facet",value="messenger"}, -- automatic property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
assert(#offers > 0, "unable to find offered service")
Messenger = orb:narrow(offers[1].service_ref:getFacetByName("messenger"),
......@@ -68,5 +68,5 @@ Messenger = orb:narrow(offers[1].service_ref:getFacetByName("messenger"),
-- offer the service
conn.offers:registerService(component.IComponent, {
{name="offer.domain",value="OpenBus Demos"}, -- provided property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
return function(chain)
local entities = {}
for index, login in ipairs(chain) do
entities[index] = login.entity
end
return table.concat(entities, ":")
local entities = {}
for index, login in ipairs(chain) do
entities[index] = login.entity
end
return table.concat(entities, ":")
end
......@@ -13,21 +13,21 @@ conn:loginByPassword("demo", "demo")
-- find the messenger service
offers = conn.offers:findServices({
{name="offer.domain",value="OpenBus Demos"}, -- provided property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
assert(#offers == 3, "wrong offered service")
for _, offer in ipairs(offers) do
local name, iface
for _, prop in ipairs(offer.properties) do
if prop.name == "openbus.component.facet" then
assert(name == nil)
name = prop.value
elseif prop.name == "openbus.component.interface" then
assert(iface == nil)
iface = prop.value
end
end
_G[name] = orb:narrow(offer.service_ref:getFacet(iface), iface)
local name, iface
for _, prop in ipairs(offer.properties) do
if prop.name == "openbus.component.facet" then
assert(name == nil)
name = prop.value
elseif prop.name == "openbus.component.interface" then
assert(iface == nil)
iface = prop.value
end
end
_G[name] = orb:narrow(offer.service_ref:getFacet(iface), iface)
end
-- logout from the bus
......@@ -56,18 +56,18 @@ for i = 1, 10 do io.write(".") io.flush() cothread.delay(1) end
print(" done!")
function showPostsOf(user, posts)
print(user.." received "..#posts.." messages:")
for index, post in ipairs(posts) do
print(index..") "..post.from..": "..post.message)
end
print()
print(user.." received "..#posts.." messages:")
for index, post in ipairs(posts) do
print(index..") "..post.from..": "..post.message)
end
print()
end
for _, user in ipairs{"willian", "bill", "paul", "mary", "steve"} do
conn:loginByPassword(user, user)
showPostsOf(user, messenger:receivePosts())
broadcaster:unsubscribe()
conn:logout()
conn:loginByPassword(user, user)
showPostsOf(user, messenger:receivePosts())
broadcaster:unsubscribe()
conn:logout()
end
conn:loginByPassword("willian", "willian")
......
......@@ -16,66 +16,66 @@ cothread.next(coroutine.create(orb.run), orb)
-- create service implementation
Forwarder = {
__type = orb.types:lookup("tecgraf::openbus::demo::delegation::Forwarder"),
forwardsOf = {},
__type = orb.types:lookup("tecgraf::openbus::demo::delegation::Forwarder"),
forwardsOf = {},
}
function Forwarder:setForward(to)
local chain = conn:getCallerChain()
local callers = chain.callers
local user = callers[1].entity
print("setup forward to "..to.." by "..chain2str(callers))
self.forwardsOf[user] = {chain=chain, to=to}
local chain = conn:getCallerChain()
local callers = chain.callers
local user = callers[1].entity
print("setup forward to "..to.." by "..chain2str(callers))
self.forwardsOf[user] = {chain=chain, to=to}
end
function Forwarder:cancelForward()
local callers = conn:getCallerChain().callers
local user = callers[1].entity
local forward = self.forwardsOf[user]
if forward ~= nil then
print("cancel forward to "..forward.to.." by "..chain2str(callers))
self.forwardsOf[user] = nil
end
local callers = conn:getCallerChain().callers
local user = callers[1].entity
local forward = self.forwardsOf[user]
if forward ~= nil then
print("cancel forward to "..forward.to.." by "..chain2str(callers))
self.forwardsOf[user] = nil
end
end
function Forwarder:getForward()
local callers = conn:getCallerChain().callers
local user = callers[1].entity
local forward = self.forwardsOf[user]
if forward == nil then
error(orb:newexcept{"tecgraf::openbus::demo::delegation::NoForward"})
end
return forward.to
local callers = conn:getCallerChain().callers
local user = callers[1].entity
local forward = self.forwardsOf[user]
if forward == nil then
error(orb:newexcept{"tecgraf::openbus::demo::delegation::NoForward"})
end
return forward.to
end
-- create timer to forward messages from time to time
timer = Timer{ rate = 5 }
function timer:action()
for user, forward in pairs(Forwarder.forwardsOf) do
local to = forward.to
print("checking messages of "..to)
conn:joinChain(forward.chain)
local posts = Messenger:receivePosts()
conn:exitChain()
for _, post in ipairs(posts) do
Messenger:post(to, "forwared from "..post.from..": "..post.message)
end
end
for user, forward in pairs(Forwarder.forwardsOf) do
local to = forward.to
print("checking messages of "..to)
conn:joinChain(forward.chain)
local posts = Messenger:receivePosts()
conn:exitChain()
for _, post in ipairs(posts) do
Messenger:post(to, "forwared from "..post.from..": "..post.message)
end
end
end
timer:enable()
-- setup action on login termination
function conn:onInvalidLogin()
print("login terminated, shutting the server down")
timer:disable() -- stop the timer
conn:close() -- free connection resources
orb:shutdown() -- stop the ORB and free all its resources
print("login terminated, shutting the server down")
timer:disable() -- stop the timer
conn:close() -- free connection resources
orb:shutdown() -- stop the ORB and free all its resources
end
-- create service SCS component
component = ComponentContext(orb, {
name = "Forwarder",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "",
name = "Forwarder",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "",
})
component:addFacet("forwarder", Forwarder.__type.repID, Forwarder)
......@@ -84,9 +84,9 @@ conn:loginByPassword("forwarder", "forwarder")
-- find messenger
offers = conn.offers:findServices({
{name="openbus.offer.entity",value="messenger"}, -- automatic property
{name="openbus.component.facet",value="messenger"}, -- automatic property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
{name="openbus.offer.entity",value="messenger"}, -- automatic property
{name="openbus.component.facet",value="messenger"}, -- automatic property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
assert(#offers > 0, "unable to find offered service")
Messenger = orb:narrow(offers[1].service_ref:getFacetByName("messenger"),
......@@ -94,5 +94,5 @@ Messenger = orb:narrow(offers[1].service_ref:getFacetByName("messenger"),
-- offer the service
conn.offers:registerService(component.IComponent, {
{name="offer.domain",value="OpenBus Demos"}, -- provided property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
......@@ -5,36 +5,36 @@ module openbus {
module demo {
module delegation {
typedef string Identifier;
exception NoForward {};
struct PostDesc {
Identifier from;
string message;
};
typedef sequence<PostDesc> PostDescSeq;
interface Messenger {
void post(in Identifier to, in string message);
PostDescSeq receivePosts();
};
interface Broadcaster {
void post(in string message);
void subscribe();
void unsubscribe();
};
interface Forwarder {
void setForward(in Identifier to);
void cancelForward(in Identifier to);
Identifier getForward() raises (NoForward);
};
typedef string Identifier;
exception NoForward {};
struct PostDesc {
Identifier from;
string message;
};
typedef sequence<PostDesc> PostDescSeq;
interface Messenger {
void post(in Identifier to, in string message);
PostDescSeq receivePosts();
};
interface Broadcaster {
void post(in string message);
void subscribe();
void unsubscribe();
};
interface Forwarder {
void setForward(in Identifier to);
void cancelForward(in Identifier to);
Identifier getForward() raises (NoForward);
};
}; // delegation
......
......@@ -15,39 +15,39 @@ cothread.next(coroutine.create(orb.run), orb)
-- setup action on login termination
function conn:onInvalidLogin()
print("login terminated, shutting the server down")
conn:close() -- free connection resources
orb:shutdown() -- stop the ORB and free all its resources
print("login terminated, shutting the server down")
conn:close() -- free connection resources
orb:shutdown() -- stop the ORB and free all its resources
end
-- create service implementation
Messenger = {
__type = orb.types:lookup("tecgraf::openbus::demo::delegation::Messenger"),
inboxOf = memoize(function() return {} end),
__type = orb.types:lookup("tecgraf::openbus::demo::delegation::Messenger"),
inboxOf = memoize(function() return {} end),
}
function Messenger:post(to, message)
local callers = conn:getCallerChain().callers
local from = callers[1].entity
print("post to "..to.." by "..chain2str(callers))
local inbox = self.inboxOf[to]
inbox[#inbox+1] = { from=from, message=message }
local callers = conn:getCallerChain().callers
local from = callers[1].entity
print("post to "..to.." by "..chain2str(callers))
local inbox = self.inboxOf[to]
inbox[#inbox+1] = { from=from, message=message }
end
function Messenger:receivePosts()
local callers = conn:getCallerChain().callers
local owner = callers[1].entity
print("downdoad of messsages by "..chain2str(callers))
local inbox = self.inboxOf[owner]
self.inboxOf[owner] = nil
return inbox
local callers = conn:getCallerChain().callers
local owner = callers[1].entity
print("downdoad of messsages by "..chain2str(callers))
local inbox = self.inboxOf[owner]
self.inboxOf[owner] = nil
return inbox
end
-- create service SCS component
component = ComponentContext(orb, {
name = "Messenger",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "",
name = "Messenger",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "",
})
component:addFacet("messenger", Messenger.__type.repID, Messenger)
......@@ -56,5 +56,5 @@ conn:loginByPassword("messenger", "messenger")
-- offer the service
conn.offers:registerService(component.IComponent, {
{name="offer.domain",value="OpenBus Demos"}, -- provided property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
......@@ -11,17 +11,17 @@ local interfaces = conn.interfaces
local entities = conn.entities
local category = entities:getEntityCategory("OpenBusDemos")
if category == nil then
category = entities:createEntityCategory("OpenBusDemos", "OpenBus Demo Entities")
category = entities:createEntityCategory("OpenBusDemos", "OpenBus Demo Entities")
end
for _, name in ipairs{"Messenger","Broadcaster","Forwarder"} do
local ifaceId = "IDL:tecgraf/openbus/demo/delegation/"..name..":1.0"
interfaces:registerInterface(ifaceId)
local entityId = name:lower()
local entity = entities:getEntity(entityId)
if entity == nil then
entity = category:registerEntity(entityId, "entity used in OpenBus demos")
end
entity:grantInterface(ifaceId)
local ifaceId = "IDL:tecgraf/openbus/demo/delegation/"..name..":1.0"
interfaces:registerInterface(ifaceId)
local entityId = name:lower()
local entity = entities:getEntity(entityId)
if entity == nil then
entity = category:registerEntity(entityId, "entity used in OpenBus demos")
end
entity:grantInterface(ifaceId)
end
-- logout from the bus and free connection resources permanently
......
......@@ -9,32 +9,32 @@ conn:loginByPassword("admin", "admin")
-- remove the whole category
local category = conn.entities:getEntityCategory("OpenBusDemos")
if category ~= nil then
category:removeAll()
category:removeAll()
end
-- remove interfaces
local interfaces = conn.interfaces
for _, name in ipairs{"Messenger","Broadcaster","Forwarder"} do
local ifaceId = "IDL:tecgraf/openbus/demo/delegation/"..name..":1.0"
interfaces:removeInterface(ifaceId)
local ifaceId = "IDL:tecgraf/openbus/demo/delegation/"..name..":1.0"
interfaces:removeInterface(ifaceId)
end
-- terminate logins
local entities = {
messenger = true,
broadcaster = true,
forwarder = true,
willian = true,
bill = true,
paul = true,
mary = true,
steve = true,
messenger = true,
broadcaster = true,
forwarder = true,
willian = true,
bill = true,
paul = true,
mary = true,
steve = true,
}
local logins = conn.logins
for entity in pairs(entities) do
for _, login in ipairs(logins:getEntityLogins(entity)) do
logins:invalidateLogin(login.id)
end
for _, login in ipairs(logins:getEntityLogins(entity)) do
logins:invalidateLogin(login.id)
end
end
-- logout from the bus and free connection resources permanently
......
......@@ -8,13 +8,13 @@ conn:loginByPassword("demo", "demo")
-- find the offered service
local offers = conn.offers:findServices({
{name="openbus.offer.entity",value="demo"}, -- automatic property
{name="openbus.component.facet",value="hello"}, -- automatic property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
{name="openbus.offer.entity",value="demo"}, -- automatic property
{name="openbus.component.facet",value="hello"}, -- automatic property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
assert(#offers > 0, "unable to find offered service")
for _, offer in ipairs(offers) do
local hello = offer.service_ref:getFacetByName("hello"):__narrow()
hello:sayHello()
local hello = offer.service_ref:getFacetByName("hello"):__narrow()
hello:sayHello()
end
conn:close()
......@@ -13,25 +13,25 @@ cothread.next(coroutine.create(orb.run), orb)
-- setup action on login termination
function conn:onInvalidLogin()
print("login terminated, shutting the server down")
conn:close() -- free connection resources
orb:shutdown() -- stop the ORB and free all its resources
print("login terminated, shutting the server down")
conn:close() -- free connection resources
orb:shutdown() -- stop the ORB and free all its resources
end
-- create service implementation
local hello = {}
function hello:sayHello()
local callers = conn:getCallerChain().callers
print("Hello from "..callers[1].entity.."!")
local callers = conn:getCallerChain().callers
print("Hello from "..callers[1].entity.."!")
end
-- create service SCS component
local component = ComponentContext(orb, {
name = "Hello",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "",
name = "Hello",
major_version = 1,
minor_version = 0,
patch_version = 0,
platform_spec = "",
})
component:addFacet("hello", orb.types:lookup("Hello").repID, hello)
......@@ -40,5 +40,5 @@ conn:loginByPassword("demo", "demo")
-- offer the service
conn.offers:registerService(component.IComponent, {
{name="offer.domain",value="OpenBus Demos"}, -- provided property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
......@@ -13,11 +13,11 @@ conn.interfaces:registerInterface("IDL:Hello:1.0")
local entities = conn.entities
local entity = entities:getEntity("demo")
if entity == nil then
local category = entities:getEntityCategory("OpenBusDemos")
if category == nil then
category = entities:createEntityCategory("OpenBusDemos", "OpenBus Demo Entities")
end
entity = category:registerEntity("demo", "entity used in OpenBus demos")
local category = entities:getEntityCategory("OpenBusDemos")
if category == nil then
category = entities:createEntityCategory("OpenBusDemos", "OpenBus Demo Entities")
end
entity = category:registerEntity("demo", "entity used in OpenBus demos")
end
entity:grantInterface("IDL:Hello:1.0")
......
......@@ -10,18 +10,18 @@ conn:loginByPassword("admin", "admin")
local LoginRegistry = conn.logins
local logins = LoginRegistry:getEntityLogins("demo")
for _, login in ipairs(logins) do
LoginRegistry:invalidateLogin(login.id)
LoginRegistry:invalidateLogin(login.id)
end
-- remove authorizations
local entities = conn.entities
local entity = entities:getEntity("demo")
if entity ~= nil then
entity:remove()
local category = entities:getEntityCategory("OpenBusDemos")
if category ~= nil then
category:remove()
end
entity:remove()
local category = entities:getEntityCategory("OpenBusDemos")
if category ~= nil then
category:remove()
end
end
-- remove interface
......
local openbus = require "openbus"
for _, port in ipairs{2089, 2090} do
-- connect to the bus
local conn = openbus.connectByAddress("localhost", port)
-- login to the bus
conn:loginByPassword("demo@"..port, "demo@"..port)
-- find the offered services and invoke them
local offers = conn.offers:findServices({
{name="openbus.component.facet",value="hello"}, -- automatic property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
for _, offer in ipairs(offers) do
for _, prop in ipairs(offer.properties) do
if prop.name == "openbus.offer.entity" then
print("found offer from "..prop.value.." on bus at port "..port)
break
end
end
local hello = offer.service_ref:getFacetByName("hello"):__narrow()
hello:sayHello()
end
-- logout from the bus and free connection resources permanently
conn:close()
-- connect to the bus
local conn = openbus.connectByAddress("localhost", port)
-- login to the bus
conn:loginByPassword("demo@"..port, "demo@"..port)
-- find the offered services and invoke them
local offers = conn.offers:findServices({
{name="openbus.component.facet",value="hello"}, -- automatic property
{name="offer.domain",value="OpenBus Demos"}, -- provided property
})
for _, offer in ipairs(offers) do
for _, prop in ipairs(offer.properties) do
if prop.name == "openbus.offer.entity" then
print("found offer from "..prop.value.." on bus at port "..port)
break
end
end
local hello = offer.service_ref:getFacetByName("hello"):__narrow()
hello:sayHello()
end
-- logout from the bus and free connection resources permanently
conn:close()
end
\ No newline at end of file
function registerEntity(conn, name)
local entities = conn.entities
local entity = entities:getEntity(name)
if entity == nil then
local category = entities:getEntityCategory("OpenBusDemos")
if category == nil then
category = entities:createEntityCategory("OpenBusDemos", "OpenBus Demo Entities")
end
entity = category:registerEntity(name, "entity used in OpenBus demos")
end
entity:grantInterface("IDL:Hello:1.0")
local entities = conn.entities
local entity = entities:getEntity(name)
if entity == nil then
local category = entities:getEntityCategory("OpenBusDemos")
if category == nil then
category = entities:createEntityCategory("OpenBusDemos", "OpenBus Demo Entities")
end
entity = category:registerEntity(name, "entity used in OpenBus demos")
end
entity:grantInterface("IDL:Hello:1.0")
end
local openbus = require "openbus"
do
-- connect to the bus
local conn = openbus.connectByAddress("localhost", 2089)
-- login to the bus
conn:loginByPassword("admin", "admin")
-- add interface
conn.interfaces:registerInterface("IDL:Hello:1.0")