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)
print('samba: connection from ' .. socket.dhost) Event.addRoutine('samba_connection', function()
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 fs.mount(fs.combine('network', computer.label), 'netfs', computer.id)
local e, computer = os.pullEvent() end)
if e == 'network_attach' then Event.on('network_detach', function(e, computer)
fs.mount(fs.combine('network', computer.label), 'netfs', computer.id) print('samba: detaching ' .. computer.label)
elseif e == 'network_detach' then fs.unmount(fs.combine('network', computer.label))
print('samba: detaching ' .. 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,42 +77,37 @@ 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) print('discovery: listening on port 999')
--os.sleep(1) -- allow services a chance to startup Event.on('modem_message', function(e, s, sport, id, info, distance)
print('discovery: listening on port 999')
while true do if sport == 999 and tonumber(id) and type(info) == 'table' then
local e, s, sport, id, info, distance = os.pullEvent('modem_message') if not network[id] then
network[id] = { }
end
Util.merge(network[id], info)
network[id].distance = distance
network[id].timestamp = os.clock()
if sport == 999 and tonumber(id) and type(info) == 'table' then if not network[id].active then
if not network[id] then network[id].active = true
network[id] = { } os.queueEvent('network_attach', network[id])
end
Util.merge(network[id], info)
network[id].distance = distance
network[id].timestamp = os.clock()
if not network[id].active then
network[id].active = true
os.queueEvent('network_attach', network[id])
end
end end
end end
end) end)
@ -135,33 +130,20 @@ 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) sendInfo()
for _,c in pairs(_G.network) do
while true do local elapsed = os.clock()-c.timestamp
sendInfo() if c.active and elapsed > 15 then
c.active = false
for _,c in pairs(_G.network) do os.queueEvent('network_detach', c)
local elapsed = os.clock()-c.timestamp
if c.active and elapsed > 15 then
c.active = false
os.queueEvent('network_detach', c)
end
end end
os.sleep(10)
end end
end) end)
if os.isTurtle() then Event.on('turtle_response', function()
process:newThread('turtle_heartbeat', function() if turtle.status ~= info.status or
turtle.fuel ~= info.fuel then
while true do sendInfo()
os.pullEvent('turtle_response') end
if turtle.status ~= info.status or end)
turtle.fuel ~= info.fuel then
sendInfo()
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) multishell.openTab({
if termInfo then fn = telnetHost,
multishell.openTab({ args = { socket },
fn = telnetHost, env = getfenv(1),
args = { socket, termInfo }, title = 'Telnet Client',
env = getfenv(1), hidden = true,
title = 'Telnet Client', })
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')
@ -82,4 +73,4 @@ process:newThread('vnc_server', function()
socket:close() socket:close()
end end
end end
end) end)

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()
for _,c in pairs(network) do
c.active = false
os.queueEvent('network_detach', c)
end
os.queueEvent('network_down')
process:pullEvent('network_down')
process:threadEvent('terminate')
break
end end
end)
Event.pullEvents()
for _,c in pairs(network) do
c.active = false
os.queueEvent('network_detach', c)
end end
os.queueEvent('network_down')
Event.pullEvent('network_down')
Util.clear(_G.network) Util.clear(_G.network)
end end