inital RPC-reladed commit

This commit is contained in:
Izaya 2019-12-08 14:01:53 +11:00
parent c9bb890a59
commit 52ae542344
3 changed files with 91 additions and 0 deletions

1
RPC/OpenOS/README.md Symbolic link
View File

@ -0,0 +1 @@
usr/man/rpc

View File

@ -0,0 +1,71 @@
local serial = require "serialization"
local computer = require "computer"
local minitel = require "minitel"
local event = require "event"
local rpcf = {}
local rpcrunning = false
local rpc = {}
rpc.port = 111
function rpc.call(hostname,fn,...)
if hostname == "localhost" then
return rpcf[fn](...)
end
local rv = minitel.genPacketID()
minitel.rsend(hostname,rpc.port,serial.serialize({fn,rv,...}),true)
local st = computer.uptime()
local rt = {}
repeat
local _, from, port, data = event.pull(30, "net_msg", hostname, rpc.port)
rt = serial.unserialize(data) or {}
until rt[1] == rv or computer.uptime() > st + 30
if table.remove(rt,1) == rv then
return table.unpack(rt)
end
return false
end
function rpc.proxy(hostname,filter)
filter=(filter or "").."(.+)"
local fnames = rpc.call(hostname,"list")
if not fnames then return false end
local rt = {}
for k,v in pairs(fnames) do
fv = v:match(filter)
if fv then
rt[fv] = function(...)
return rpc.call(hostname,v,...)
end
end
end
return rt
end
function rpc.register(name,fn)
if not rpcrunning then
event.listen("net_msg",function(_, from, port, data)
if port == rpc.port then
local rpcrq = serial.unserialize(data)
local rpcn, rpcid = table.remove(rpcrq,1), table.remove(rpcrq,1)
if rpcf[rpcn] then
local rt = {pcall(rpcf[rpcn],table.unpack(rpcrq))}
if rt[1] == true then
table.remove(rt,1)
end
minitel.send(from,port,serial.serialize({rpcid,table.unpack(rt)}))
else
end
end
end)
function rpcf.list()
local rt = {}
for k,v in pairs(rpcf) do
rt[#rt+1] = k
end
return rt
end
rpcrunning = true
end
rpcf[name] = fn
end
return rpc

19
RPC/OpenOS/usr/man/rpc Normal file
View File

@ -0,0 +1,19 @@
# RPC
Minitel Remote Procedure Call Library
## API
In all instances, if *hostname* is replaced with *localhost*, an attempt will be made to call the registered procedure on the local machine.
### rpc.call(*hostname*, *name*, ...)
Call function *name* on remote host *hostname* with arguments ...
### rpc.proxy(*hostname*, *filter*)
Return a table containing the functions on *hostname* matching *filter*, which is a Lua pattern.
### rpc.register(*name*, *function*)
Registers *function* as the RPC call for *name* on the current host.
## Variables
### rpc.port = 111
Port to use for RPC calls and registration.