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.

71 lines
1.7 KiB

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