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,9 +1,5 @@
local Event = require('event')
local Socket = require('socket')
local Util = require('util')
local os = _G.os
local Peripheral = Util.shallowCopy(_G.peripheral)
function Peripheral.getList()
@ -121,122 +117,4 @@ function Peripheral.get(args)
end
end
local function getProxy(pi)
local socket, msg = Socket.connect(pi.host, 189)
if not socket then
error("Timed out attaching peripheral: " .. pi.uri .. '\n' .. msg)
end
-- write the uri of the periperal we are requesting...
-- ie. type/monitor
socket:write(pi.path)
local proxy = socket:read(3)
if not proxy then
error("Timed out attaching peripheral: " .. pi.uri)
end
if type(proxy) == 'string' then
error(proxy)
end
local methods = proxy.methods
proxy.methods = nil
for _,method in pairs(methods) do
proxy[method] = function(...)
socket:write({ fn = method, args = { ... } })
local resp = socket:read()
if not resp then
error("Timed out communicating with peripheral: " .. pi.uri)
end
return table.unpack(resp)
end
end
if proxy.blit then
local methods = { 'clear', 'clearLine', 'setCursorPos', 'write', 'blit',
'setTextColor', 'setTextColour', 'setBackgroundColor',
'setBackgroundColour', 'scroll', 'setCursorBlink', }
local queue = nil
for _,method in pairs(methods) do
proxy[method] = function(...)
if not queue then
queue = { }
Event.onTimeout(0, function()
if not socket:write({ fn = 'fastBlit', args = { queue } }) then
error("Timed out communicating with peripheral: " .. pi.uri)
end
queue = nil
socket:read()
end)
end
if not socket.connected then
error("Timed out communicating with peripheral: " .. pi.uri)
end
table.insert(queue, {
fn = method,
args = { ... },
})
end
end
end
if proxy.type == 'monitor' then
Event.addRoutine(function()
while true do
local data = socket:read()
if not data then
break
end
if data.fn and data.fn == 'event' then
os.queueEvent(table.unpack(data.data))
end
end
end)
end
return proxy
end
--[[
Parse a uri into it's components
Examples:
monitor = { name = 'monitor' }
side/top = { side = 'top' }
method/list = { method = 'list' }
12://name/monitor = { host = 12, name = 'monitor' }
]]--
local function parse(uri)
local pi = Util.split(uri:gsub('^%d*://', ''), '(.-)/')
if #pi == 1 then
pi = {
'name',
pi[1],
}
end
return {
host = uri:match('^(%d*)%:'), -- 12
uri = uri, -- 12://name/monitor
path = uri:gsub('^%d*://', ''), -- name/monitor
[ pi[1] ] = pi[2], -- name = 'monitor'
}
end
function Peripheral.lookup(uri)
local pi = parse(uri)
if pi.host and _G.device.wireless_modem then
return getProxy(pi)
end
return Peripheral.get(pi)
end
return Peripheral

32
sys/apis/proxy.lua Normal file
View File

@ -0,0 +1,32 @@
local Socket = require('socket')
local Proxy = { }
function Proxy.create(remoteId, uri)
local socket, msg = Socket.connect(remoteId, 188)
if not socket then
error(msg)
end
socket.co = coroutine.running()
socket:write(uri)
local methods = socket:read() or error('Timed out')
local hijack = { }
for _,method in pairs(methods) do
hijack[method] = function(...)
socket:write({ method, ... })
local resp = socket:read()
if not resp then
error('timed out: ' .. method)
end
return table.unpack(resp)
end
end
return hijack, socket
end
return Proxy

View File

@ -2,7 +2,6 @@ local Canvas = require('ui.canvas')
local class = require('class')
local Event = require('event')
local Input = require('input')
local Peripheral = require('peripheral')
local Transition = require('ui.transition')
local Util = require('util')
@ -12,6 +11,7 @@ local colors = _G.colors
local device = _G.device
local fs = _G.fs
local os = _G.os
local peripheral = _G.peripheral
local term = _G.term
--[[
@ -178,13 +178,12 @@ function Manager:configure(appName, ...)
Util.merge(defaults.device, optionValues)
if defaults.device.name then
local dev
if defaults.device.name == 'terminal' then
dev = term.current()
else
dev = Peripheral.lookup(defaults.device.name) --- device[defaults.device.name]
dev = peripheral.wrap(defaults.device.name)
end
if not dev then

View File

@ -1,5 +1,3 @@
_G.requireInjector(_ENV)
local Security = require('security')
local SHA1 = require('sha1')
local Terminal = require('terminal')

View File

@ -1,5 +1,3 @@
_G.requireInjector(_ENV)
local Event = require('event')
local Socket = require('socket')
local Terminal = require('terminal')

View File

@ -1,5 +1,3 @@
_G.requireInjector(_ENV)
local Crypto = require('crypto')
local Security = require('security')
local SHA1 = require('sha1')

View File

@ -1,5 +1,3 @@
_G.requireInjector(_ENV)
local Event = require('event')
local Socket = require('socket')
local Terminal = require('terminal')

View File

@ -1,5 +1,3 @@
_G.requireInjector(_ENV)
local Util = require('util')
local kernel = _G.kernel
@ -21,10 +19,12 @@ keyboard.addHotkey('control-backspace', function()
local tab = kernel.find(uid)
if not tab.isOverview then
multishell.terminate(uid)
tab = Util.shallowCopy(tab)
tab.isDead = false
tab.focused = true
multishell.openTab(tab)
multishell.openTab({
path = tab.path,
env = tab.env,
args = tab.args,
focused = true,
})
end
end)

View File

@ -24,7 +24,7 @@ local multishell = { }
shell.setEnv('multishell', multishell)
multishell.term = parentTerm --deprecated
multishell.term = parentTerm --deprecated use device.terminal
local config = {
standard = {

View File

@ -1,86 +0,0 @@
--[[
Allow sharing of local peripherals.
]]--
local Event = require('event')
local Peripheral = require('peripheral')
local Socket = require('socket')
Event.addRoutine(function()
print('peripheral: listening on port 189')
while true do
local socket = Socket.server(189)
print('peripheral: connection from ' .. socket.dhost)
Event.addRoutine(function()
local uri = socket:read(2)
if uri then
local peripheral = Peripheral.lookup(uri)
-- only 1 proxy of this device can happen at one time
-- need to prevent multiple shares
if not peripheral then
print('peripheral: invalid peripheral ' .. uri)
socket:write('Invalid peripheral: ' .. uri)
else
print('peripheral: proxing ' .. uri)
local proxy = {
methods = { }
}
if peripheral.blit then
--peripheral = Util.shallowCopy(peripheral)
peripheral.fastBlit = function(data)
for _,v in ipairs(data) do
peripheral[v.fn](unpack(v.args))
end
end
end
for k,v in pairs(peripheral) do
if type(v) == 'function' then
table.insert(proxy.methods, k)
else
proxy[k] = v
end
end
socket:write(proxy)
if proxy.type == 'monitor' then
peripheral.eventChannel = function(...)
socket:write({
fn = 'event',
data = { ... }
})
end
end
while true do
local data = socket:read()
if not data then
print('peripheral: lost connection from ' .. socket.dhost)
break
end
if not _G.device[peripheral.name] then
print('periperal: detached')
socket:close()
break
end
if peripheral[data.fn] then
-- need to trigger an error on the other end
-- local s, m = pcall()
socket:write({ peripheral[data.fn](table.unpack(data.args)) })
else
socket:write({ false, "Invalid function: " .. data.fn })
end
end
peripheral.eventChannel = nil
peripheral.fastBlit = nil
end
end
end)
end
end)

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)