From 13ec8ea04fa0a137d3e959640e7402c19a7c19a7 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sat, 6 Jan 2018 22:25:33 -0500 Subject: [PATCH] peripheral overhaul --- sys/apis/peripheral.lua | 24 +++++++++++++----------- sys/apis/socket.lua | 6 ++---- sys/apis/terminal.lua | 4 ++-- sys/apis/ui.lua | 6 +++++- sys/extensions/device.lua | 3 +++ sys/network/peripheral.lua | 6 ++++-- sys/services/transort.lua | 27 ++++++++++++++++++++------- 7 files changed, 49 insertions(+), 27 deletions(-) diff --git a/sys/apis/peripheral.lua b/sys/apis/peripheral.lua index b8791b5..6e4e794 100644 --- a/sys/apis/peripheral.lua +++ b/sys/apis/peripheral.lua @@ -92,8 +92,8 @@ function Peripheral.get(args) args = { type = args } end - if args.device then - return _G.device[args.device] + if args.name then + return _G.device[args.name] end if args.type then @@ -157,7 +157,9 @@ local function getProxy(pi) if not queue then queue = { } Event.onTimeout(0, function() - socket:write({ fn = 'fastBlit', args = { queue } }) + if not socket:write({ fn = 'fastBlit', args = { queue } }) then + error("Timed out communicating with peripheral: " .. pi.uri) + end queue = nil socket:read() end) @@ -192,26 +194,26 @@ end Parse a uri into it's components Examples: - monitor = { device = 'monitor' } - side/top = { side = 'top' } - method/list = { method = 'list' } - 12://device/monitor = { host = 12, device = 'monitor' } + 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 = { - 'device', + 'name', pi[1], } end return { host = uri:match('^(%d*)%:'), -- 12 - uri = uri, -- 12://device/monitor - path = uri:gsub('^%d*://', ''), -- device/monitor - [ pi[1] ] = pi[2], -- device = 'monitor' + uri = uri, -- 12://name/monitor + path = uri:gsub('^%d*://', ''), -- name/monitor + [ pi[1] ] = pi[2], -- name = 'monitor' } end diff --git a/sys/apis/socket.lua b/sys/apis/socket.lua index 6adb847..fecc817 100644 --- a/sys/apis/socket.lua +++ b/sys/apis/socket.lua @@ -37,6 +37,7 @@ function socketClass:read(timeout) break end timerId = os.startTimer(5) + self:ping() end end end @@ -54,10 +55,7 @@ end function socketClass:ping() if self.connected then - _G.transport.write(self, { - type = 'PING', - seq = self.wseq, - }) + _G.transport.ping(self) return true end end diff --git a/sys/apis/terminal.lua b/sys/apis/terminal.lua index 5314d8e..6b662cc 100644 --- a/sys/apis/terminal.lua +++ b/sys/apis/terminal.lua @@ -149,8 +149,8 @@ function Terminal.mirror(ct, dt) ct[k] = function(...) local ret = { f(...) } if dt[k] then - dt[k](...) - end + dt[k](...) + end return unpack(ret) end end diff --git a/sys/apis/ui.lua b/sys/apis/ui.lua index ca9bed3..fc9f5d7 100644 --- a/sys/apis/ui.lua +++ b/sys/apis/ui.lua @@ -1600,7 +1600,9 @@ function UI.Grid:setPage(pageNo) end function UI.Grid:eventHandler(event) - if event.type == 'mouse_click' or event.type == 'mouse_doubleclick' then + if event.type == 'mouse_click' or + event.type == 'mouse_rightclick' or + event.type == 'mouse_doubleclick' then if not self.disableHeader then if event.y == 1 then local col = 2 @@ -1628,6 +1630,8 @@ function UI.Grid:eventHandler(event) self:setIndex(row) if event.type == 'mouse_doubleclick' then self:emit({ type = 'key_enter' }) + elseif event.type == 'mouse_rightclick' then + self:emit({ type = 'grid_select_right', selected = self.selected, element = self }) end return true end diff --git a/sys/extensions/device.lua b/sys/extensions/device.lua index 2b53733..4b9cfda 100644 --- a/sys/extensions/device.lua +++ b/sys/extensions/device.lua @@ -6,3 +6,6 @@ _G.device = Peripheral.getList() -- register the main term in the devices list _G.device.terminal = _G.term.current() +_G.device.terminal.side = 'terminal' +_G.device.terminal.type = 'terminal' +_G.device.terminal.name = 'terminal' diff --git a/sys/network/peripheral.lua b/sys/network/peripheral.lua index ceec9f8..1ec79cc 100644 --- a/sys/network/peripheral.lua +++ b/sys/network/peripheral.lua @@ -8,8 +8,8 @@ local Socket = require('socket') local Util = require('util') Event.addRoutine(function() + print('peripheral: listening on port 189') while true do - print('peripheral: listening on port 189') local socket = Socket.server(189) print('peripheral: connection from ' .. socket.dhost) @@ -19,7 +19,9 @@ Event.addRoutine(function() if uri then local peripheral = Peripheral.lookup(uri) - if peripheral then + if not peripheral then + print('peripheral: invalid peripheral ' .. uri) + else print('peripheral: proxing ' .. uri) local proxy = { methods = { } diff --git a/sys/services/transort.lua b/sys/services/transort.lua index 8855ec3..07ac754 100644 --- a/sys/services/transort.lua +++ b/sys/services/transort.lua @@ -31,11 +31,10 @@ function transport.read(socket) end function transport.write(socket, data) - --debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq })) socket.transmit(socket.dport, socket.dhost, data) - local timerId = os.startTimer(2) + local timerId = os.startTimer(3) transport.timers[timerId] = socket socket.timers[socket.wseq] = timerId @@ -43,6 +42,18 @@ function transport.write(socket, data) socket.wseq = socket.wseq + 1 end +function transport.ping(socket) + --debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq })) + socket.transmit(socket.dport, socket.dhost, { + type = 'PING', + seq = -1, + }) + + local timerId = os.startTimer(3) + transport.timers[timerId] = socket + socket.timers[-1] = timerId +end + function transport.close(socket) transport.sockets[socket.sport] = nil end @@ -54,6 +65,7 @@ while true do if e == 'timer' then local socket = transport.timers[timerId] + if socket and socket.connected then print('transport timeout - closing socket ' .. socket.sport) socket:close() @@ -72,11 +84,12 @@ while true do socket:close() elseif msg.type == 'ACK' then - local timerId = socket.timers[msg.seq] - - os.cancelTimer(timerId) - socket.timers[msg.seq] = nil - transport.timers[timerId] = nil + local ackTimerId = socket.timers[msg.seq] + if ackTimerId then + os.cancelTimer(ackTimerId) + socket.timers[msg.seq] = nil + transport.timers[ackTimerId] = nil + end elseif msg.type == 'PING' then socket.transmit(socket.dport, socket.dhost, {