From a9d03b06e9e068538b5744c9427020f2fa1155da Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Fri, 8 Mar 2019 16:25:56 -0500 Subject: [PATCH] phase out peripheral.lookup --- sys/apis/peripheral.lua | 122 -------------------------------- sys/apis/proxy.lua | 32 +++++++++ sys/apis/ui.lua | 5 +- sys/apps/password.lua | 2 - sys/apps/telnet.lua | 2 - sys/apps/trust.lua | 2 - sys/apps/vnc.lua | 2 - sys/autorun/hotkeys.lua | 12 ++-- sys/extensions/7.multishell.lua | 2 +- sys/network/peripheral.lua | 86 ---------------------- sys/network/proxy.lua | 39 +++++++--- 11 files changed, 69 insertions(+), 237 deletions(-) create mode 100644 sys/apis/proxy.lua delete mode 100644 sys/network/peripheral.lua diff --git a/sys/apis/peripheral.lua b/sys/apis/peripheral.lua index 33a1df1..a176268 100644 --- a/sys/apis/peripheral.lua +++ b/sys/apis/peripheral.lua @@ -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 diff --git a/sys/apis/proxy.lua b/sys/apis/proxy.lua new file mode 100644 index 0000000..4603d57 --- /dev/null +++ b/sys/apis/proxy.lua @@ -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 diff --git a/sys/apis/ui.lua b/sys/apis/ui.lua index 712b600..70b6dbc 100644 --- a/sys/apis/ui.lua +++ b/sys/apis/ui.lua @@ -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 diff --git a/sys/apps/password.lua b/sys/apps/password.lua index eb3c5b0..ca0f5b5 100644 --- a/sys/apps/password.lua +++ b/sys/apps/password.lua @@ -1,5 +1,3 @@ -_G.requireInjector(_ENV) - local Security = require('security') local SHA1 = require('sha1') local Terminal = require('terminal') diff --git a/sys/apps/telnet.lua b/sys/apps/telnet.lua index 25c6c05..8a736a3 100644 --- a/sys/apps/telnet.lua +++ b/sys/apps/telnet.lua @@ -1,5 +1,3 @@ -_G.requireInjector(_ENV) - local Event = require('event') local Socket = require('socket') local Terminal = require('terminal') diff --git a/sys/apps/trust.lua b/sys/apps/trust.lua index b8cb705..da8ff55 100644 --- a/sys/apps/trust.lua +++ b/sys/apps/trust.lua @@ -1,5 +1,3 @@ -_G.requireInjector(_ENV) - local Crypto = require('crypto') local Security = require('security') local SHA1 = require('sha1') diff --git a/sys/apps/vnc.lua b/sys/apps/vnc.lua index 5b17700..0540125 100644 --- a/sys/apps/vnc.lua +++ b/sys/apps/vnc.lua @@ -1,5 +1,3 @@ -_G.requireInjector(_ENV) - local Event = require('event') local Socket = require('socket') local Terminal = require('terminal') diff --git a/sys/autorun/hotkeys.lua b/sys/autorun/hotkeys.lua index 9c9dfce..3c97e0e 100644 --- a/sys/autorun/hotkeys.lua +++ b/sys/autorun/hotkeys.lua @@ -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) diff --git a/sys/extensions/7.multishell.lua b/sys/extensions/7.multishell.lua index d5de390..9bbfb2c 100644 --- a/sys/extensions/7.multishell.lua +++ b/sys/extensions/7.multishell.lua @@ -24,7 +24,7 @@ local multishell = { } shell.setEnv('multishell', multishell) -multishell.term = parentTerm --deprecated +multishell.term = parentTerm --deprecated use device.terminal local config = { standard = { diff --git a/sys/network/peripheral.lua b/sys/network/peripheral.lua deleted file mode 100644 index 89e2b86..0000000 --- a/sys/network/peripheral.lua +++ /dev/null @@ -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) diff --git a/sys/network/proxy.lua b/sys/network/proxy.lua index 6aaddef..c721896 100644 --- a/sys/network/proxy.lua +++ b/sys/network/proxy.lua @@ -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)