OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9 KiB

local net = require("net")
local computer = computer or require("computer")
local component = component or require("component")
local sides = sides or require("sides")
local os = os or require("os")
local WORKER_PORT = 31
local function meet_server(port)
while true do
print("broadcast SYN")
net.broadcast(port, "SYN")
computer.beep(200)
local function ack(name, ...)
-- local addr, remote addr, port, distance, msg
local t = {...}
return t[5] == "ACK"
end
print("recv ACK")
local pkt, err = net.recv{port, filter = ack, timeout = 5}
if pkt ~= nil then
print("found server " .. pkt.raddr)
if pkt ~= nil then
return pkt.raddr
end
end
end
end
local function respond(pkt, code, error)
print("SEND", pkt.raddr, pkt.port, code, error)
net.send(pkt.raddr, pkt.port, code, error)
computer.beep(200)
end
-- Waits to recv a function asking to flash the current EEPROM
local function wait_for_job(port, server)
local function filter(name, ...)
-- local addr, remote addr, port, distance, msg
local t = {...}
return t[5] == "flash"
end
local pkt, err = net.recv{port, filter=filter}
local eeprom = require("component").eeprom
print("Found eeprom", eeprom.getData())
print("Flashing EEPROM " .. tostring(#pkt.data[2]) .. " bytes")
eeprom.set(pkt.data[2])
computer.beep(500)
if pkt.data[3] ~= nil then
eeprom.setLabel(pkt.data[3])
computer.beep(500)
end
print("Bytes written", #eeprom.get())
respond(pkt, "flash-good")
computer.beep(1000)
end
local function start_client(port)
server = meet_server(port)
while true do
wait_for_job(port, server)
os.sleep(5)
end
end
start_client(WORKER_PORT)