1
0
mirror of https://github.com/kepler155c/opus synced 2025-01-24 06:06:54 +00:00

convert to event api

This commit is contained in:
kepler155c@gmail.com 2017-08-03 01:46:39 -04:00
parent 252fb16853
commit a1660fd073
6 changed files with 80 additions and 108 deletions

View File

@ -1,5 +1,5 @@
local Socket = require('socket') local Socket = require('socket')
local process = require('process') local Event = require('event')
local fileUid = 0 local fileUid = 0
local fileHandles = { } local fileHandles = { }
@ -57,31 +57,26 @@ local function sambaConnection(socket)
print('samba: Connection closed') print('samba: Connection closed')
end end
process:newThread('samba_server', function() Event.addRoutine(function()
print('samba: listening on port 139') print('samba: listening on port 139')
while true do while true do
local socket = Socket.server(139) local socket = Socket.server(139)
Event.addRoutine('samba_connection', function()
print('samba: connection from ' .. socket.dhost) print('samba: connection from ' .. socket.dhost)
process:newThread('samba_connection', function()
sambaConnection(socket) sambaConnection(socket)
print('samba: closing connection to ' .. socket.dhost) print('samba: closing connection to ' .. socket.dhost)
end) end)
end end
end) end)
process:newThread('samba_manager', function() Event.on('network_attach', function(e, computer)
while true do
local e, computer = os.pullEvent()
if e == 'network_attach' then
fs.mount(fs.combine('network', computer.label), 'netfs', computer.id) fs.mount(fs.combine('network', computer.label), 'netfs', computer.id)
elseif e == 'network_detach' then end)
Event.on('network_detach', function(e, computer)
print('samba: detaching ' .. computer.label) print('samba: detaching ' .. computer.label)
fs.unmount(fs.combine('network', computer.label)) fs.unmount(fs.combine('network', computer.label))
end
end
end) end)

View File

@ -1,6 +1,6 @@
local Socket = require('socket') local Socket = require('socket')
local GPS = require('gps') local GPS = require('gps')
local process = require('process') local Event = require('event')
-- move this into gps api -- move this into gps api
local gpsRequested local gpsRequested
@ -77,29 +77,25 @@ local function snmpConnection(socket)
end end
end end
process:newThread('snmp_server', function() Event.addRoutine(function()
print('snmp: listening on port 161') print('snmp: listening on port 161')
while true do while true do
local socket = Socket.server(161) local socket = Socket.server(161)
print('snmp: connection from ' .. socket.dhost)
process:newThread('snmp_connection', function() Event.addRoutine(function()
print('snmp: connection from ' .. socket.dhost)
snmpConnection(socket) snmpConnection(socket)
print('snmp: closing connection to ' .. socket.dhost) print('snmp: closing connection to ' .. socket.dhost)
end) end)
end end
end) end)
process:newThread('discovery_server', function()
device.wireless_modem.open(999) device.wireless_modem.open(999)
--os.sleep(1) -- allow services a chance to startup
print('discovery: listening on port 999') print('discovery: listening on port 999')
while true do Event.on('modem_message', function(e, s, sport, id, info, distance)
local e, s, sport, id, info, distance = os.pullEvent('modem_message')
if sport == 999 and tonumber(id) and type(info) == 'table' then if sport == 999 and tonumber(id) and type(info) == 'table' then
if not network[id] then if not network[id] then
@ -114,7 +110,6 @@ process:newThread('discovery_server', function()
os.queueEvent('network_attach', network[id]) os.queueEvent('network_attach', network[id])
end end
end end
end
end) end)
local info = { local info = {
@ -135,12 +130,8 @@ local function sendInfo()
end end
-- every 10 seconds, send out this computer's info -- every 10 seconds, send out this computer's info
process:newThread('discovery_heartbeat', function() Event.onInterval(10, function()
--os.sleep(1)
while true do
sendInfo() sendInfo()
for _,c in pairs(_G.network) do for _,c in pairs(_G.network) do
local elapsed = os.clock()-c.timestamp local elapsed = os.clock()-c.timestamp
if c.active and elapsed > 15 then if c.active and elapsed > 15 then
@ -148,20 +139,11 @@ process:newThread('discovery_heartbeat', function()
os.queueEvent('network_detach', c) os.queueEvent('network_detach', c)
end end
end end
os.sleep(10)
end
end) end)
if os.isTurtle() then Event.on('turtle_response', function()
process:newThread('turtle_heartbeat', function()
while true do
os.pullEvent('turtle_response')
if turtle.status ~= info.status or if turtle.status ~= info.status or
turtle.fuel ~= info.fuel then turtle.fuel ~= info.fuel then
sendInfo() sendInfo()
end end
end
end) end)
end

View File

@ -1,7 +1,7 @@
local Socket = require('socket') local Socket = require('socket')
local process = require('process') local Event = require('event')
local function telnetHost(socket, termInfo) local function telnetHost(socket)
require = requireInjector(getfenv(1)) require = requireInjector(getfenv(1))
local Event = require('event') local Event = require('event')
@ -9,6 +9,12 @@ local function telnetHost(socket, termInfo)
'setTextColor', 'setTextColour', 'setBackgroundColor', 'setTextColor', 'setTextColour', 'setBackgroundColor',
'setBackgroundColour', 'scroll', 'setCursorBlink', } 'setBackgroundColour', 'scroll', 'setCursorBlink', }
local termInfo = socket:read(5)
if not termInfo then
printtError('read failed')
return
end
socket.term = term.current() socket.term = term.current()
local oldWindow = Util.shallowCopy(socket.term) local oldWindow = Util.shallowCopy(socket.term)
@ -47,7 +53,6 @@ local function telnetHost(socket, termInfo)
Event.exitPullEvents() Event.exitPullEvents()
break break
end end
shellThread:resume(table.unpack(data)) shellThread:resume(table.unpack(data))
end end
end) end)
@ -58,7 +63,7 @@ local function telnetHost(socket, termInfo)
shellThread:terminate() shellThread:terminate()
end end
process:newThread('telnet_server', function() Event.addRoutine(function()
print('telnet: listening on port 23') print('telnet: listening on port 23')
while true do while true do
@ -66,15 +71,12 @@ process:newThread('telnet_server', function()
print('telnet: connection from ' .. socket.dhost) print('telnet: connection from ' .. socket.dhost)
local termInfo = socket:read(5)
if termInfo then
multishell.openTab({ multishell.openTab({
fn = telnetHost, fn = telnetHost,
args = { socket, termInfo }, args = { socket },
env = getfenv(1), env = getfenv(1),
title = 'Telnet Client', title = 'Telnet Client',
hidden = true, hidden = true,
}) })
end end
end
end) end)

View File

@ -1,8 +1,8 @@
local Socket = require('socket') local Socket = require('socket')
local process = require('process') local Event = require('event')
local Crypto = require('crypto') local Crypto = require('crypto')
process:newThread('trust_server', function() Event.addRoutine(function()
print('trust: listening on port 19') print('trust: listening on port 19')
while true do while true do

View File

@ -1,5 +1,5 @@
local Socket = require('socket') local Socket = require('socket')
local process = require('process') local Event = require('event')
local function wrapTerm(socket, termInfo) local function wrapTerm(socket, termInfo)
local methods = { 'blit', 'clear', 'clearLine', 'setCursorPos', 'write', local methods = { 'blit', 'clear', 'clearLine', 'setCursorPos', 'write',
@ -13,7 +13,10 @@ local function wrapTerm(socket, termInfo)
socket.term[k] = function(...) socket.term[k] = function(...)
if not socket.queue then if not socket.queue then
socket.queue = { } socket.queue = { }
os.queueEvent('vnc_flush') Event.onTimeout(0, function()
socket:write(socket.queue)
socket.queue = nil
end)
end end
table.insert(socket.queue, { table.insert(socket.queue, {
f = k, f = k,
@ -32,16 +35,6 @@ local function vncHost(socket, termInfo)
wrapTerm(socket, termInfo) wrapTerm(socket, termInfo)
local queueThread = process:newThread('queue_writer', function()
while true do
os.pullEvent('vnc_flush')
if socket.queue then
socket:write(socket.queue)
socket.queue = nil
end
end
end)
os.queueEvent('term_resize') os.queueEvent('term_resize')
while true do while true do
@ -56,15 +49,13 @@ local function vncHost(socket, termInfo)
end end
end end
queueThread:terminate()
for k,v in pairs(socket.oldTerm) do for k,v in pairs(socket.oldTerm) do
socket.term[k] = v socket.term[k] = v
end end
os.queueEvent('term_resize') os.queueEvent('term_resize')
end end
process:newThread('vnc_server', function() Event.addRoutine(function()
print('vnc: listening on port 5900') print('vnc: listening on port 5900')

View File

@ -6,7 +6,8 @@ multishell.setTitle(multishell.getCurrent(), 'Net Daemon')
_G.network = { } _G.network = { }
local function netUp() local function netUp()
local process = require('process') require = requireInjector(getfenv(1))
local Event = require('event')
local files = fs.list('/sys/network') local files = fs.list('/sys/network')
@ -19,19 +20,20 @@ local function netUp()
end end
end end
while true do Event.on('device_detach', function()
local e = process:pullEvent('device_detach') if not device.wireless_modem then
if not device.wireless_modem or e == 'terminate' then Event.exitPullEvents()
end
end)
Event.pullEvents()
for _,c in pairs(network) do for _,c in pairs(network) do
c.active = false c.active = false
os.queueEvent('network_detach', c) os.queueEvent('network_detach', c)
end end
os.queueEvent('network_down') os.queueEvent('network_down')
process:pullEvent('network_down') Event.pullEvent('network_down')
process:threadEvent('terminate')
break
end
end
Util.clear(_G.network) Util.clear(_G.network)
end end