mirror of
https://github.com/kepler155c/opus
synced 2025-10-28 22:27:39 +00:00
move multishell functionality to kernel
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
_G.requireInjector()
|
||||
|
||||
local Event = require('event')
|
||||
local Peripheral = require('peripheral')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
local multishell = _ENV.multishell
|
||||
local os = _G.os
|
||||
local term = _G.term
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'Devices')
|
||||
|
||||
local attachColor = colors.green
|
||||
local detachColor = colors.red
|
||||
|
||||
if not term.isColor() then
|
||||
attachColor = colors.white
|
||||
detachColor = colors.lightGray
|
||||
end
|
||||
|
||||
Event.on('peripheral', function(_, side)
|
||||
if side then
|
||||
local dev = Peripheral.addDevice(device, side)
|
||||
if dev then
|
||||
term.setTextColor(attachColor)
|
||||
Util.print('[%s] %s attached', dev.side, dev.name)
|
||||
os.queueEvent('device_attach', dev.name)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
Event.on('peripheral_detach', function(_, side)
|
||||
if side then
|
||||
local dev = Util.find(device, 'side', side)
|
||||
if dev then
|
||||
term.setTextColor(detachColor)
|
||||
Util.print('[%s] %s detached', dev.side, dev.name)
|
||||
os.queueEvent('device_detach', dev.name)
|
||||
device[dev.name] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
print('waiting for peripheral changes')
|
||||
Event.pullEvents()
|
||||
@@ -3,6 +3,7 @@ _G.requireInjector()
|
||||
local Terminal = require('terminal')
|
||||
local Util = require('util')
|
||||
|
||||
local keyboard = _G.device.keyboard
|
||||
local multishell = _ENV.multishell
|
||||
local os = _G.os
|
||||
local term = _G.term
|
||||
@@ -25,7 +26,7 @@ end
|
||||
print('Debug started')
|
||||
print('Press ^d to activate debug window')
|
||||
|
||||
multishell.addHotkey('control-d', function()
|
||||
keyboard.addHotkey('control-d', function()
|
||||
local currentId = multishell.getFocus()
|
||||
if currentId ~= tabId then
|
||||
previousId = currentId
|
||||
@@ -40,4 +41,4 @@ os.pullEventRaw('terminate')
|
||||
print('Debug stopped')
|
||||
|
||||
_G.debug = function() end
|
||||
multishell.removeHotkey('control-d')
|
||||
keyboard.removeHotkey('control-d')
|
||||
|
||||
@@ -1,73 +1,45 @@
|
||||
_G.requireInjector()
|
||||
|
||||
local Util = require('util')
|
||||
local Event = require('event')
|
||||
local Util = require('util')
|
||||
|
||||
local device = _G.device
|
||||
local fs = _G.fs
|
||||
local multishell = _ENV.multishell
|
||||
local network = _G.network
|
||||
local os = _G.os
|
||||
local printError = _G.printError
|
||||
|
||||
local network = { }
|
||||
_G.network = network
|
||||
if not device.wireless_modem then
|
||||
return
|
||||
end
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'Net Daemon')
|
||||
|
||||
local function netUp()
|
||||
_G.requireInjector()
|
||||
|
||||
local Event = require('event')
|
||||
|
||||
for _,file in pairs(fs.list('sys/network')) do
|
||||
local fn, msg = Util.run(_ENV, 'sys/network/' .. file)
|
||||
if not fn then
|
||||
printError(msg)
|
||||
end
|
||||
end
|
||||
|
||||
Event.on('device_detach', function()
|
||||
if not device.wireless_modem then
|
||||
Event.exitPullEvents()
|
||||
end
|
||||
end)
|
||||
|
||||
Event.pullEvents()
|
||||
|
||||
for _,c in pairs(network) do
|
||||
c.active = false
|
||||
os.queueEvent('network_detach', c)
|
||||
end
|
||||
os.queueEvent('network_down')
|
||||
Event.pullEvent('network_down')
|
||||
|
||||
Util.clear(_G.network)
|
||||
end
|
||||
|
||||
print('Net daemon started')
|
||||
|
||||
local function startNetwork()
|
||||
print('Starting network services')
|
||||
|
||||
local success, msg = Util.runFunction(
|
||||
Util.shallowCopy(_ENV), netUp)
|
||||
|
||||
if not success and msg then
|
||||
for _,file in pairs(fs.list('sys/network')) do
|
||||
local fn, msg = Util.run(_ENV, 'sys/network/' .. file)
|
||||
if not fn then
|
||||
printError(msg)
|
||||
end
|
||||
print('Network services stopped')
|
||||
end
|
||||
|
||||
if device.wireless_modem then
|
||||
startNetwork()
|
||||
else
|
||||
print('No modem detected')
|
||||
end
|
||||
|
||||
while true do
|
||||
local _, deviceName = os.pullEvent('device_attach')
|
||||
if deviceName == 'wireless_modem' then
|
||||
startNetwork()
|
||||
Event.on('device_detach', function()
|
||||
if not device.wireless_modem then
|
||||
Event.exitPullEvents()
|
||||
end
|
||||
end)
|
||||
|
||||
Event.pullEvents()
|
||||
|
||||
for _,c in pairs(network) do
|
||||
c.active = false
|
||||
os.queueEvent('network_detach', c)
|
||||
end
|
||||
os.queueEvent('network_down')
|
||||
Event.pullEvent('network_down')
|
||||
|
||||
Util.clear(_G.network)
|
||||
|
||||
print('Net daemon stopped')
|
||||
|
||||
@@ -21,6 +21,7 @@ _G.transport = transport
|
||||
|
||||
function transport.open(socket)
|
||||
transport.sockets[socket.sport] = socket
|
||||
socket.activityTimer = os.clock()
|
||||
end
|
||||
|
||||
function transport.read(socket)
|
||||
@@ -34,24 +35,27 @@ function transport.write(socket, data)
|
||||
--debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq }))
|
||||
socket.transmit(socket.dport, socket.dhost, data)
|
||||
|
||||
local timerId = os.startTimer(3)
|
||||
--local timerId = os.startTimer(3)
|
||||
|
||||
transport.timers[timerId] = socket
|
||||
socket.timers[socket.wseq] = timerId
|
||||
--transport.timers[timerId] = socket
|
||||
--socket.timers[socket.wseq] = timerId
|
||||
|
||||
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,
|
||||
})
|
||||
if os.clock() - socket.activityTimer > 10 then
|
||||
socket.activityTimer = os.clock()
|
||||
socket.transmit(socket.dport, socket.dhost, {
|
||||
type = 'PING',
|
||||
seq = -1,
|
||||
})
|
||||
|
||||
local timerId = os.startTimer(3)
|
||||
transport.timers[timerId] = socket
|
||||
socket.timers[-1] = timerId
|
||||
local timerId = os.startTimer(3)
|
||||
transport.timers[timerId] = socket
|
||||
socket.timers[-1] = timerId
|
||||
end
|
||||
end
|
||||
|
||||
function transport.close(socket)
|
||||
@@ -67,7 +71,7 @@ while true do
|
||||
local socket = transport.timers[timerId]
|
||||
|
||||
if socket and socket.connected then
|
||||
print('transport timeout - closing socket ' .. socket.sport)
|
||||
debug('transport timeout - closing socket ' .. socket.sport)
|
||||
socket:close()
|
||||
transport.timers[timerId] = nil
|
||||
end
|
||||
@@ -88,18 +92,21 @@ while true do
|
||||
if ackTimerId then
|
||||
os.cancelTimer(ackTimerId)
|
||||
socket.timers[msg.seq] = nil
|
||||
socket.activityTimer = os.clock()
|
||||
transport.timers[ackTimerId] = nil
|
||||
end
|
||||
|
||||
elseif msg.type == 'PING' then
|
||||
socket.activityTimer = os.clock()
|
||||
socket.transmit(socket.dport, socket.dhost, {
|
||||
type = 'ACK',
|
||||
seq = msg.seq,
|
||||
})
|
||||
|
||||
elseif msg.type == 'DATA' and msg.data then
|
||||
socket.activityTimer = os.clock()
|
||||
if msg.seq ~= socket.rseq then
|
||||
print('transport seq error - closing socket ' .. socket.sport)
|
||||
debug('transport seq error - closing socket ' .. socket.sport)
|
||||
socket:close()
|
||||
else
|
||||
socket.rseq = socket.rseq + 1
|
||||
@@ -111,10 +118,10 @@ while true do
|
||||
end
|
||||
|
||||
--debug('>> ' .. Util.tostring({ type = 'ACK', seq = msg.seq }))
|
||||
socket.transmit(socket.dport, socket.dhost, {
|
||||
type = 'ACK',
|
||||
seq = msg.seq,
|
||||
})
|
||||
--socket.transmit(socket.dport, socket.dhost, {
|
||||
-- type = 'ACK',
|
||||
-- seq = msg.seq,
|
||||
--})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user