Basic network
Signed-off-by: Rahix <rahix@rahix.de>
This commit is contained in:
parent
e328ae0ab9
commit
7063e7445d
2 changed files with 84 additions and 10 deletions
43
bc-tests.lua
43
bc-tests.lua
|
|
@ -168,6 +168,7 @@ function test_iter_empty()
|
|||
BaseControl.Network(31211) -- Network isolation
|
||||
):finalize()
|
||||
|
||||
assert_equal(31211, bc.network.port, "wrong port")
|
||||
assert_equal(0, #bc:nouns(), "nouns found in empty network")
|
||||
assert_equal(0, #bc:verbs(), "verbs found in empty network")
|
||||
end
|
||||
|
|
@ -175,14 +176,44 @@ end
|
|||
|
||||
-- Network {{{
|
||||
function test_multinode()
|
||||
local bc1 = BaseControl()
|
||||
bc1:register("multinode1", 123)
|
||||
bc1:register("multinode2v", function() end)
|
||||
local bc1 = BaseControl:finalize()
|
||||
local bc2 = BaseControl:new()
|
||||
bc2:register("multinode1", 123)
|
||||
bc2:register("multinode2v", function() end)
|
||||
bc2:finalize()
|
||||
local bc3 = BaseControl:finalize()
|
||||
|
||||
assert_true(bc1:has_noun("multinode1"), "d1: remote noun missing")
|
||||
assert_true(bc1:has_verb("multinode2v"), "d1: remote verb missing")
|
||||
assert_true(bc3:has_noun("multinode1"), "d2: remote noun missing")
|
||||
assert_true(bc3:has_verb("multinode2v"), "d2: remote verb missing")
|
||||
end
|
||||
|
||||
function test_multinode_iters()
|
||||
local bc1 = BaseControl:new(
|
||||
BaseControl.Network(121233)
|
||||
)
|
||||
bc1:register("miter1", 123)
|
||||
bc1:register("miter2v", function() end)
|
||||
bc1:finalize()
|
||||
|
||||
local bc2 = BaseControl:finalize()
|
||||
local bc2 = BaseControl:new(
|
||||
BaseControl.Network(121233)
|
||||
)
|
||||
bc2:register("miter3", 321)
|
||||
bc2:register("miter4v", function() end)
|
||||
bc2:finalize()
|
||||
|
||||
assert_true(bc2:has_noun("multinode1"), "remote noun missing")
|
||||
assert_true(bc2:has_verb("multinode2v"), "remote verb missing")
|
||||
local nouns = bc2:nouns()
|
||||
table.sort(nouns)
|
||||
assert_equal(2, #nouns, "noun-list incorrect")
|
||||
assert_equal("miter1", nouns[1], "noun-list incorrect")
|
||||
assert_equal("miter3", nouns[2], "noun-list incorrect")
|
||||
|
||||
local verbs = bc2:verbs()
|
||||
table.sort(verbs)
|
||||
assert_equal(2, #verbs, "verb-list incorrect")
|
||||
assert_equal("miter2v", verbs[1], "verb-list incorrect")
|
||||
assert_equal("miter4v", verbs[2], "verb-list incorrect")
|
||||
end
|
||||
-- }}}
|
||||
|
|
|
|||
51
bc.lua
51
bc.lua
|
|
@ -81,7 +81,7 @@ local BaseControl = {
|
|||
Network = Network,
|
||||
}
|
||||
|
||||
function BaseControl:new(self, network)
|
||||
function BaseControl:new(network)
|
||||
local self = {
|
||||
local_nouns = {},
|
||||
local_verbs = {},
|
||||
|
|
@ -93,7 +93,11 @@ function BaseControl:new(self, network)
|
|||
}
|
||||
setmetatable(self, {__index=BaseControl, __call=BaseControl.new})
|
||||
|
||||
self.network:start(function() error("net unimplemented") end)
|
||||
self.network:start(function(remote, msg)
|
||||
self:_network_handler(remote, msg)
|
||||
end)
|
||||
-- Announce own presence so registers start coming in
|
||||
self.network:broadcast{ty=Message.Hello}
|
||||
|
||||
return self
|
||||
end
|
||||
|
|
@ -125,6 +129,13 @@ function BaseControl:finalize(waits, timeout)
|
|||
self = BaseControl:new()
|
||||
end
|
||||
|
||||
-- Announce own nouns and verbs
|
||||
self.network:broadcast{
|
||||
ty=Message.Register,
|
||||
nouns=self:nouns(true),
|
||||
verbs=self:verbs(true),
|
||||
}
|
||||
|
||||
self.live = true
|
||||
return self
|
||||
end
|
||||
|
|
@ -139,7 +150,11 @@ function BaseControl:nouns(local_only)
|
|||
for noun in pairs(self.local_nouns) do
|
||||
table.insert(nouns, noun)
|
||||
end
|
||||
-- TODO: Remote
|
||||
if not local_only then
|
||||
for noun in pairs(self.remote_nouns) do
|
||||
table.insert(nouns, noun)
|
||||
end
|
||||
end
|
||||
return nouns
|
||||
end
|
||||
|
||||
|
|
@ -182,7 +197,11 @@ function BaseControl:verbs(local_only)
|
|||
for verb in pairs(self.local_verbs) do
|
||||
table.insert(verbs, verb)
|
||||
end
|
||||
-- TODO: Remote
|
||||
if not local_only then
|
||||
for verb in pairs(self.remote_verbs) do
|
||||
table.insert(verbs, verb)
|
||||
end
|
||||
end
|
||||
return verbs
|
||||
end
|
||||
|
||||
|
|
@ -194,6 +213,30 @@ function BaseControl:call(verb, ...)
|
|||
end
|
||||
end
|
||||
-- }}}
|
||||
|
||||
-- Network Handler {{{
|
||||
function BaseControl:_network_handler(remote, msg)
|
||||
if msg.ty == Message.Hello then
|
||||
if self.live then
|
||||
-- If we are live already, answer with own nouns and verbs
|
||||
self.network:send(remote, {
|
||||
ty=Message.Register,
|
||||
nouns=self:nouns(true),
|
||||
verbs=self:verbs(true),
|
||||
})
|
||||
end
|
||||
elseif msg.ty == Message.Register then
|
||||
for _, noun in ipairs(msg.nouns or {}) do
|
||||
self.remote_nouns[noun] = remote
|
||||
end
|
||||
for _, verb in ipairs(msg.verbs or {}) do
|
||||
self.remote_verbs[verb] = remote
|
||||
end
|
||||
else
|
||||
error("TODO: Delet this")
|
||||
end
|
||||
end
|
||||
-- }}}
|
||||
-- }}}
|
||||
|
||||
return setmetatable(BaseControl, {__call=BaseControl.new})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue