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

View File

@ -1,6 +1,6 @@
local Socket = require('socket')
local GPS = require('gps')
local process = require('process')
local Event = require('event')
-- move this into gps api
local gpsRequested
@ -77,42 +77,37 @@ local function snmpConnection(socket)
end
end
process:newThread('snmp_server', function()
Event.addRoutine(function()
print('snmp: listening on port 161')
while true do
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)
print('snmp: closing connection to ' .. socket.dhost)
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
print('discovery: listening on port 999')
Event.on('modem_message', function(e, s, sport, id, info, distance)
while true do
local e, s, sport, id, info, distance = os.pullEvent('modem_message')
if sport == 999 and tonumber(id) and type(info) == 'table' then
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] then
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
if not network[id].active then
network[id].active = true
os.queueEvent('network_attach', network[id])
end
end
end)
@ -135,33 +130,20 @@ local function sendInfo()
end
-- every 10 seconds, send out this computer's info
process:newThread('discovery_heartbeat', function()
--os.sleep(1)
while true do
sendInfo()
for _,c in pairs(_G.network) do
local elapsed = os.clock()-c.timestamp
if c.active and elapsed > 15 then
c.active = false
os.queueEvent('network_detach', c)
end
Event.onInterval(10, function()
sendInfo()
for _,c in pairs(_G.network) do
local elapsed = os.clock()-c.timestamp
if c.active and elapsed > 15 then
c.active = false
os.queueEvent('network_detach', c)
end
os.sleep(10)
end
end)
if os.isTurtle() then
process:newThread('turtle_heartbeat', function()
while true do
os.pullEvent('turtle_response')
if turtle.status ~= info.status or
turtle.fuel ~= info.fuel then
sendInfo()
end
end
end)
end
Event.on('turtle_response', function()
if turtle.status ~= info.status or
turtle.fuel ~= info.fuel then
sendInfo()
end
end)

View File

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

View File

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

View File

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

View File

@ -6,7 +6,8 @@ multishell.setTitle(multishell.getCurrent(), 'Net Daemon')
_G.network = { }
local function netUp()
local process = require('process')
require = requireInjector(getfenv(1))
local Event = require('event')
local files = fs.list('/sys/network')
@ -19,19 +20,20 @@ local function netUp()
end
end
while true do
local e = process:pullEvent('device_detach')
if not device.wireless_modem or e == 'terminate' then
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
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