2018-01-06 11:07:49 +00:00
|
|
|
--[[
|
|
|
|
Allow sharing of local peripherals.
|
|
|
|
]]--
|
|
|
|
|
|
|
|
local Event = require('event')
|
|
|
|
local Peripheral = require('peripheral')
|
|
|
|
local Socket = require('socket')
|
|
|
|
|
|
|
|
Event.addRoutine(function()
|
2018-01-07 03:25:33 +00:00
|
|
|
print('peripheral: listening on port 189')
|
2018-01-06 11:07:49 +00:00
|
|
|
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)
|
|
|
|
|
2018-01-10 21:46:37 +00:00
|
|
|
-- only 1 proxy of this device can happen at one time
|
|
|
|
-- need to prevent multiple shares
|
2018-01-07 03:25:33 +00:00
|
|
|
if not peripheral then
|
|
|
|
print('peripheral: invalid peripheral ' .. uri)
|
2018-01-13 20:17:26 +00:00
|
|
|
socket:write('Invalid peripheral: ' .. uri)
|
2018-01-07 03:25:33 +00:00
|
|
|
else
|
2018-01-06 11:07:49 +00:00
|
|
|
print('peripheral: proxing ' .. uri)
|
|
|
|
local proxy = {
|
|
|
|
methods = { }
|
|
|
|
}
|
|
|
|
|
|
|
|
if peripheral.blit then
|
2018-01-10 21:46:37 +00:00
|
|
|
--peripheral = Util.shallowCopy(peripheral)
|
2018-01-06 11:07:49 +00:00
|
|
|
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
|
2018-01-10 21:46:37 +00:00
|
|
|
peripheral.eventChannel = function(...)
|
|
|
|
socket:write({
|
|
|
|
fn = 'event',
|
|
|
|
data = { ... }
|
|
|
|
})
|
|
|
|
end
|
2018-01-06 11:07:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
while true do
|
|
|
|
local data = socket:read()
|
|
|
|
if not data then
|
|
|
|
print('peripheral: lost connection from ' .. socket.dhost)
|
|
|
|
break
|
|
|
|
end
|
2018-01-13 20:17:26 +00:00
|
|
|
if not _G.device[peripheral.name] then
|
|
|
|
print('periperal: detached')
|
|
|
|
socket:close()
|
|
|
|
break
|
|
|
|
end
|
2018-01-10 21:46:37 +00:00
|
|
|
if peripheral[data.fn] then
|
|
|
|
socket:write({ peripheral[data.fn](table.unpack(data.args)) })
|
|
|
|
end
|
2018-01-06 11:07:49 +00:00
|
|
|
end
|
2018-01-10 21:46:37 +00:00
|
|
|
|
|
|
|
peripheral.eventChannel = nil
|
|
|
|
peripheral.fastBlit = nil
|
2018-01-06 11:07:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|