parent
b2a12cc2dc
commit
3508870c7f
@ -1,261 +1,184 @@
|
|||||||
local component = require("component")
|
local serialization = require("serialization")
|
||||||
local event = require("event")
|
|
||||||
local serializer = require("serialization")
|
|
||||||
local uuid = require("uuid")
|
local uuid = require("uuid")
|
||||||
|
|
||||||
local BC_VERSION = {0, 1}
|
local Version = {0, 1}
|
||||||
local BC_PORT = 0xBC00 | (BC_VERSION[1] << 4) | BC_VERSION[2]
|
|
||||||
|
|
||||||
local Message = {
|
local Message = {
|
||||||
Hello = 0x48454c4f,
|
Hello = 0x48454c4f,
|
||||||
Register = 0x00524547,
|
Register = 0x52454749,
|
||||||
Disband = 0x44524547,
|
Deregister = 0x44524547,
|
||||||
NounRequest = 0x4e524551,
|
NounRequest = 0x4e524551,
|
||||||
NounResponse = 0x4e524553,
|
NounResponse = 0x4e524553,
|
||||||
VerbRequest = 0x56524551,
|
VerbRequest = 0x56524551,
|
||||||
|
VerbResponse = 0x56524553,
|
||||||
ListenRequest = 0x4c524551,
|
ListenRequest = 0x4c524551,
|
||||||
ListenNotify = 0x4c4e4f54,
|
ListenNotify = 0x4c4e4f54,
|
||||||
ListenCancel = 0x4c535450,
|
ListenCancel = 0x4c535450,
|
||||||
}
|
}
|
||||||
|
|
||||||
local Event = {
|
local function query_param(self, v)
|
||||||
Change = 1,
|
return {ty=self.ty, v=v}
|
||||||
Rising = 2,
|
end
|
||||||
Falling = 3,
|
local Query = {
|
||||||
Equals = 4,
|
Change = {ty=0x434847},
|
||||||
Above = 5,
|
Rising = {ty=0x524953},
|
||||||
Below = 6,
|
Falling = {ty=0x46414c},
|
||||||
|
Equals = setmetatable({ty=0x455155}, {__call=query_param}),
|
||||||
|
Above = setmetatable({ty=0x414256}, {__call=query_param}),
|
||||||
|
Below = setmetatable({ty=0x424c4f}, {__call=query_param}),
|
||||||
}
|
}
|
||||||
|
|
||||||
local bc = {
|
-- Network ---------------------------------------------------------------- {{{
|
||||||
_version = BC_VERSION,
|
local Network = {
|
||||||
_default_port = BC_PORT,
|
default_port = 0xBC00 | (Version[1] << 4) | Version[2],
|
||||||
Event = Event,
|
|
||||||
Message = Message,
|
|
||||||
}
|
}
|
||||||
bc.__index = bc
|
|
||||||
|
|
||||||
-- Helpers
|
function Network:new(modem, port)
|
||||||
local function send_msg(self, remote, msg_table)
|
if type(modem) == "number" then
|
||||||
self.modem.send(remote, self.port, serializer.serialize(msg_table))
|
port = modem
|
||||||
|
modem = nil
|
||||||
|
end
|
||||||
|
local self = {
|
||||||
|
modem = modem or require("component").modem,
|
||||||
|
port = port or Network.default_port,
|
||||||
|
}
|
||||||
|
setmetatable(self, {__index=Network})
|
||||||
|
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
setmetatable(Network, {__call=Network.new})
|
||||||
|
|
||||||
local function broadcast_msg(self, msg_table)
|
function Network:start(callback)
|
||||||
self.modem.broadcast(self.port, serializer.serialize(msg_table))
|
self.modem.open(self.port)
|
||||||
|
self.listener = function(_, addr_lo, addr_remote, port, _, msg)
|
||||||
|
-- Filter everything we don't care about
|
||||||
|
if addr_lo ~= self.modem.address then return end
|
||||||
|
if port ~= self.port then return end
|
||||||
|
callback(addr_remote, serialization.unserialize(msg))
|
||||||
|
end
|
||||||
|
require("event").listen("modem_message", self.listener)
|
||||||
end
|
end
|
||||||
|
|
||||||
function bc:has_noun(noun)
|
function Network:send(addr, msg)
|
||||||
return self.local_nouns[noun] ~= nil or self.remote_nouns[noun] ~= nil
|
self.modem.send(addr, self.port, serialization.serialize(msg))
|
||||||
end
|
end
|
||||||
|
|
||||||
function bc:set_noun(noun, value)
|
function Network:broadcast(msg)
|
||||||
local last_value = self.local_nouns[noun]
|
self.modem.broadcast(self.port, serialization.serialize(msg))
|
||||||
if last_value ~= nil then
|
|
||||||
self.local_nouns[noun] = value
|
|
||||||
for id, par in pairs(self.remote_listeners[noun]) do
|
|
||||||
if (par.event == Event.Change and value ~= last_value)
|
|
||||||
or (par.event == Event.Rising and value > last_value)
|
|
||||||
or (par.event == Event.Falling and value < last_value)
|
|
||||||
or (par.event == Event.Equals and value == par.evparam)
|
|
||||||
or (par.event == Event.Above and value > par.evparam)
|
|
||||||
or (par.event == Event.Below and value < par.evparam)
|
|
||||||
then
|
|
||||||
send_msg(self, par.addr, {ty=BC_MESSAGE.LISTEN_NOTIFY, id=id, value=value})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
error("Noun \""..noun.."\" does not exist or is non-local!")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function bc:get_noun(noun)
|
function Network:stop()
|
||||||
if self.local_nouns[noun] ~= nil then
|
require("event").ignore("modem_message", self.listener)
|
||||||
return self.local_nouns[noun]
|
self.modem.close(self.port)
|
||||||
elseif self.remote_nouns[noun] ~= nil then
|
end
|
||||||
send_msg(self, self.remote_nouns[noun], {
|
-- }}}
|
||||||
ty = BC_MESSAGE.NOUN_REQUEST,
|
|
||||||
noun = noun,
|
|
||||||
})
|
|
||||||
local value
|
|
||||||
event.pullFiltered(self.timeout, function(ev, ...)
|
|
||||||
if ev ~= "modem_message" then return false end
|
|
||||||
if select(2, ...) ~= self.remote_nouns[noun] then return false end
|
|
||||||
if select(3, ...) ~= self.port then return false end
|
|
||||||
local msg = serializer.unserialize(select(5, ...))
|
|
||||||
if msg.ty ~= BC_MESSAGE.NOUN_RESPONSE then return false end
|
|
||||||
if msg.noun ~= noun then return false end
|
|
||||||
|
|
||||||
value = msg.value
|
-- BaseControl ------------------------------------------------------------ {{{
|
||||||
return true
|
local BaseControl = {
|
||||||
end)
|
version = Version,
|
||||||
return value
|
Message = Message,
|
||||||
else -- Not found at all
|
Query = Query,
|
||||||
return nil
|
Network = Network,
|
||||||
end
|
}
|
||||||
|
|
||||||
|
function BaseControl:new(self, network)
|
||||||
|
local self = {
|
||||||
|
locals = {
|
||||||
|
nouns = {},
|
||||||
|
verbs = {},
|
||||||
|
},
|
||||||
|
remotes = {
|
||||||
|
nouns = {},
|
||||||
|
verbs = {},
|
||||||
|
},
|
||||||
|
live = false,
|
||||||
|
network = network or Network:new(),
|
||||||
|
}
|
||||||
|
setmetatable(self, {__index=BaseControl, __call=BaseControl.new})
|
||||||
|
|
||||||
|
self.network:start(function() error("net unimplemented") end)
|
||||||
|
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function bc:listen(noun, event, evparam, callback)
|
function BaseControl:register(name, value)
|
||||||
-- You can leave out evparam
|
if self.live then
|
||||||
if type(evparam) == "function" then
|
error("can't register \""..name.."\" after finalizing")
|
||||||
callback = evparam
|
|
||||||
evparam = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local remote_addr = self.remote_nouns[noun]
|
if type(value) == "function" then
|
||||||
if remote_addr == nil then
|
if self.locals.verbs[name] ~= nil then
|
||||||
error("Noun \""..noun.."\" is not listenable!")
|
error("\""..name.."\" already registered")
|
||||||
|
else
|
||||||
|
self.locals.verbs[name] = value
|
||||||
|
end
|
||||||
else
|
else
|
||||||
local id = uuid.next()
|
if self.locals.nouns[name] ~= nil then
|
||||||
self.local_listeners[id] = {addr=remote_addr, callback=callback}
|
error("\""..name.."\" already registered")
|
||||||
send_msg(self, remote_addr, {
|
else
|
||||||
ty=BC_MESSAGE.LISTEN_REQUEST,
|
self.locals.nouns[name] = value
|
||||||
noun=noun,
|
end
|
||||||
event=event,
|
|
||||||
evparam=evparam,
|
|
||||||
id=id,
|
|
||||||
})
|
|
||||||
return id
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function bc:cancel(id)
|
function BaseControl:finalize(waits, timeout)
|
||||||
local l = self.local_listeners[id]
|
local self = self
|
||||||
send_msg(self, l.addr, {ty=BC_MESSAGE.LISTEN_CANCEL, noun=l.noun, id=id})
|
if self == BaseControl then
|
||||||
self.local_listeners[id] = nil
|
-- Called as a constructor
|
||||||
|
self = BaseControl:new()
|
||||||
|
end
|
||||||
|
|
||||||
|
self.live = true
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function bc:has_verb(verb)
|
-- Nouns {{{
|
||||||
return self.local_verbs[verb] ~= nil or self.remote_verbs[verb] ~= nil
|
function BaseControl:has_noun(noun)
|
||||||
|
return self.locals.nouns[noun] ~= nil or self.remotes.nouns[noun] ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function bc:call_verb(verb, ...)
|
function BaseControl:nouns(local_only)
|
||||||
if self.local_verbs[verb] ~= nil then
|
local nouns = {}
|
||||||
self.local_verbs[verb](self, nil, ...)
|
for noun in pairs(self.locals.nouns) do
|
||||||
elseif self.remote_verbs[verb] ~= nil then
|
table.insert(nouns, noun)
|
||||||
send_msg(self, self.remote_verbs[verb], {
|
|
||||||
ty=BC_MESSAGE.VERB_REQUEST, verb=verb, param={...},
|
|
||||||
})
|
|
||||||
else
|
|
||||||
error("Verb \""..verb.."\" does not exist!")
|
|
||||||
end
|
end
|
||||||
|
-- TODO: Remote
|
||||||
|
return nouns
|
||||||
end
|
end
|
||||||
|
|
||||||
function bc:cleanup()
|
function BaseControl:get(noun, timeout)
|
||||||
event.ignore("modem_message", self._modem_listener)
|
if self.locals.nouns[noun] ~= nil then
|
||||||
local nouns, verbs = self:locals()
|
return self.locals.nouns[noun]
|
||||||
broadcast_msg(self, {
|
elseif self.locals.verbs[noun] ~= nil then
|
||||||
ty=BC_MESSAGE.DISBAND,
|
error("\""..noun.."\" is not a noun")
|
||||||
nouns=nouns,
|
else
|
||||||
verbs=verbs,
|
return nil
|
||||||
})
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function bc:locals()
|
function BaseControl:set(name, value)
|
||||||
local nouns, verbs = {}, {}
|
error("set unimplemented")
|
||||||
for noun in pairs(self.local_nouns) do table.insert(nouns, noun) end
|
|
||||||
for verb in pairs(self.local_verbs) do table.insert(verbs, verb) end
|
|
||||||
return nouns, verbs
|
|
||||||
end
|
end
|
||||||
|
-- }}}
|
||||||
|
|
||||||
local function new(_, local_nouns, local_verbs, overrides)
|
-- Verbs {{{
|
||||||
local self = {
|
function BaseControl:has_verb(verb)
|
||||||
local_nouns=local_nouns or {},
|
return self.locals.verbs[verb] ~= nil or self.remotes.verbs[verb] ~= nil
|
||||||
local_verbs=local_verbs or {},
|
end
|
||||||
remote_nouns={},
|
|
||||||
remote_verbs={},
|
|
||||||
|
|
||||||
local_listeners={},
|
|
||||||
remote_listeners={},
|
|
||||||
|
|
||||||
port=BC_PORT,
|
|
||||||
modem=component.modem,
|
|
||||||
timeout=5,
|
|
||||||
}
|
|
||||||
if overrides ~= nil then
|
|
||||||
for name, value in pairs(overrides) do
|
|
||||||
self[name] = value
|
|
||||||
end
|
|
||||||
end
|
|
||||||
setmetatable(self, bc)
|
|
||||||
|
|
||||||
for noun in pairs(self.local_nouns) do
|
|
||||||
self.remote_listeners[noun] = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
self._modem_listener = function(_, _, remote_addr, port, _, msg)
|
|
||||||
if port ~= self.port then -- Ignore other ports
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local msg = serializer.unserialize(msg)
|
function BaseControl:verbs(local_only)
|
||||||
if msg.ty == BC_MESSAGE.HELLO then
|
local verbs = {}
|
||||||
local nouns, verbs = self:locals()
|
for verb in pairs(self.locals.verbs) do
|
||||||
send_msg(self, remote_addr, {
|
table.insert(verbs, verb)
|
||||||
ty=BC_MESSAGE.REGISTER,
|
|
||||||
nouns=nouns,
|
|
||||||
verbs=verbs,
|
|
||||||
})
|
|
||||||
elseif msg.ty == BC_MESSAGE.REGISTER then
|
|
||||||
for _, noun in ipairs(msg.nouns or {}) do
|
|
||||||
self.remote_nouns[noun] = remote_addr
|
|
||||||
end
|
|
||||||
for _, verb in ipairs(msg.verbs or {}) do
|
|
||||||
self.remote_verbs[verb] = remote_addr
|
|
||||||
end
|
|
||||||
elseif msg.ty == BC_MESSAGE.NOUN_REQUEST then
|
|
||||||
send_msg(self, remote_addr, {
|
|
||||||
ty=BC_MESSAGE.NOUN_RESPONSE,
|
|
||||||
noun=msg.noun,
|
|
||||||
value=self.local_nouns[msg.noun],
|
|
||||||
})
|
|
||||||
elseif msg.ty == BC_MESSAGE.VERB_REQUEST then
|
|
||||||
local callback = self.local_verbs[msg.verb]
|
|
||||||
if callback ~= nil then
|
|
||||||
callback(self, remote_addr, table.unpack(msg.param))
|
|
||||||
end
|
|
||||||
elseif msg.ty == BC_MESSAGE.LISTEN_REQUEST then
|
|
||||||
if self.local_nouns[msg.noun] ~= nil then
|
|
||||||
self.remote_listeners[msg.noun][msg.id] = {
|
|
||||||
event=msg.event,
|
|
||||||
evparam=msg.evparam,
|
|
||||||
addr=remote_addr,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
elseif msg.ty == BC_MESSAGE.LISTEN_NOTIFY then
|
|
||||||
local listener = self.local_listeners[msg.id]
|
|
||||||
if listener ~= nil and listener.addr == remote_addr then
|
|
||||||
listener.callback(msg.value)
|
|
||||||
end
|
|
||||||
elseif msg.ty == BC_MESSAGE.LISTEN_CANCEL then
|
|
||||||
if self.remote_listeners[msg.noun] ~= nil then
|
|
||||||
self.remote_listeners[msg.noun][msg.id] = nil
|
|
||||||
end
|
|
||||||
elseif msg.ty == BC_MESSAGE.DISBAND then
|
|
||||||
for _, noun in ipairs(msg.nouns or {}) do
|
|
||||||
if self.remote_nouns[noun] == remote_addr then
|
|
||||||
self.remote_nouns[noun] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
for _, verb in ipairs(msg.verbs or {}) do
|
|
||||||
if self.remote_verbs[verb] == remote_addr then
|
|
||||||
self.remote_verbs[verb] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
-- TODO: Remote
|
||||||
|
return verbs
|
||||||
|
end
|
||||||
|
|
||||||
-- Setup connection and say hello
|
function BaseControl:call(verb, ...)
|
||||||
self.modem.open(self.port)
|
error("call unimplemented")
|
||||||
event.listen("modem_message", self._modem_listener)
|
|
||||||
broadcast_msg(self, {ty=BC_MESSAGE.HELLO})
|
|
||||||
local nouns, verbs = self:locals()
|
|
||||||
broadcast_msg(self, {
|
|
||||||
ty=BC_MESSAGE.REGISTER,
|
|
||||||
nouns=nouns,
|
|
||||||
verbs=verbs,
|
|
||||||
})
|
|
||||||
|
|
||||||
return self
|
|
||||||
end
|
end
|
||||||
|
-- }}}
|
||||||
|
-- }}}
|
||||||
|
|
||||||
return setmetatable(bc, {__call=new, __index={new=new}})
|
return setmetatable(BaseControl, {__call=BaseControl.new})
|
||||||
|
|||||||
Loading…
Reference in new issue