1
0
mirror of https://github.com/kepler155c/opus synced 2024-12-24 23:50:26 +00:00

peripheral overhaul

This commit is contained in:
kepler155c@gmail.com 2018-01-06 22:25:33 -05:00
parent 911fec9118
commit 13ec8ea04f
7 changed files with 49 additions and 27 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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'

View File

@ -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 = { }

View File

@ -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, {