phase out peripheral.lookup

This commit is contained in:
kepler155c@gmail.com
2019-03-08 16:25:56 -05:00
parent 89f95ca45b
commit a9d03b06e9
11 changed files with 69 additions and 237 deletions

View File

@@ -1,5 +1,17 @@
local Event = require('event')
local Socket = require('socket')
local Util = require('util')
local function getProxy(path)
local x = Util.split(path, '(.-)/')
local proxy = _G
for _, v in pairs(x) do
proxy = proxy[v]
if not proxy then
break
end
end
end
Event.addRoutine(function()
while true do
@@ -9,30 +21,35 @@ Event.addRoutine(function()
print('proxy: connection from ' .. socket.dhost)
Event.addRoutine(function()
local api = socket:read(2)
if api then
local proxy = _G[api]
local path = socket:read(2)
if path then
local api = getProxy(path)
if not proxy then
if not api then
print('proxy: invalid API')
return
end
local methods = { }
for k,v in pairs(proxy) do
for k,v in pairs(api) do
if type(v) == 'function' then
table.insert(methods, k)
end
end
socket:write(methods)
while true do
local data = socket:read()
if not data then
print('proxy: lost connection from ' .. socket.dhost)
break
local s, m = pcall(function()
while true do
local data = socket:read()
if not data then
print('proxy: lost connection from ' .. socket.dhost)
break
end
socket:write({ api[data[1]](table.unpack(data, 2)) })
end
socket:write({ proxy[data[1]](table.unpack(data, 2)) })
end)
if not s and m then
_G.printError(m)
end
end
end)