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.

77 lines
1.9 KiB

1 year ago
1 year ago
1 year ago
  1. local net = require("net")
  2. local computer = computer or require("computer")
  3. local component = component or require("component")
  4. local sides = sides or require("sides")
  5. local os = os or require("os")
  6. local WORKER_PORT = 31
  7. local function meet_server(port)
  8. while true do
  9. print("broadcast SYN")
  10. net.broadcast(port, "SYN")
  11. computer.beep(200)
  12. local function ack(name, ...)
  13. -- local addr, remote addr, port, distance, msg
  14. local t = {...}
  15. return t[5] == "ACK"
  16. end
  17. print("recv ACK")
  18. local pkt, err = net.recv{port, filter = ack, timeout = 5}
  19. if pkt ~= nil then
  20. print("found server " .. pkt.raddr)
  21. if pkt ~= nil then
  22. return pkt.raddr
  23. end
  24. end
  25. end
  26. end
  27. local function respond(pkt, code, error)
  28. print("SEND", pkt.raddr, pkt.port, code, error)
  29. net.send(pkt.raddr, pkt.port, code, error)
  30. computer.beep(200)
  31. end
  32. -- Waits to recv a function asking to flash the current EEPROM
  33. local function wait_for_job(port, server)
  34. local function filter(name, ...)
  35. -- local addr, remote addr, port, distance, msg
  36. local t = {...}
  37. return t[5] == "flash"
  38. end
  39. local pkt, err = net.recv{port, filter=filter}
  40. local eeprom = require("component").eeprom
  41. print("Found eeprom", eeprom.getData())
  42. print("Flashing EEPROM " .. tostring(#pkt.data[2]) .. " bytes")
  43. eeprom.set(pkt.data[2])
  44. computer.beep(500)
  45. if pkt.data[3] ~= nil then
  46. eeprom.setLabel(pkt.data[3])
  47. computer.beep(500)
  48. end
  49. print("Bytes written", #eeprom.get())
  50. respond(pkt, "flash-good")
  51. computer.beep(1000)
  52. end
  53. local function start_client(port)
  54. server = meet_server(port)
  55. while true do
  56. wait_for_job(port, server)
  57. os.sleep(5)
  58. end
  59. end
  60. start_client(WORKER_PORT)