Guest User

Untitled

a guest
Dec 9th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. function call()
  2. local dg = string.format("%s $", "call")
  3. rednet.broadcast(dg)
  4. id, msg = rednet.receive(10)
  5. if msg then
  6. cmd, etc = msg:match("^(%S*) (.*)")
  7. if cmd == "call" then
  8. rednet.send(id, "call")
  9. return id
  10. end
  11. end
  12. end
  13.  
  14. function open(timeout)
  15. if timeout >= 100 then
  16. error("Timeout too high")
  17. else
  18. for k, v in pairs(rs.getSides()) do
  19. rednet.open(v)
  20. end
  21. os.startTimer(timeout)
  22. while true do
  23. event, p1, p2 = os.pullEvent()
  24. if event == "rednet_message" then
  25. -- p1 = id
  26. -- p2 = msg
  27. if p2 then
  28. cmd, etc = p2:match("^(%S*) (.*)")
  29. if cmd == "view" then
  30. local val = fs.list("/")
  31. local _val = textutils.serialize(val)
  32. local dg = string.format("%s %s", "view", _val)
  33. rednet.send(p1, dg)
  34. elseif cmd == "transfer" then
  35. local file = fs.open(etc, "r")
  36. local dg = string.format("%s %s", "transfer", file.readAll())
  37. file.close()
  38. rednet.send(p1, dg)
  39. elseif cmd == "call" then
  40. local dg = string.format("%s $", "call")
  41. rednet.send(p1, dg)
  42. end
  43. end
  44. elseif event == "timeout" then
  45. break
  46. end
  47. end
  48. end
  49. end
  50.  
  51. function view(id)
  52. local dg = string.format("%s $", "view")
  53. rednet.send(id, dg)
  54. id, msg = rednet.receive(10)
  55. if msg then
  56. cmd, etc = msg:match("^(%S*) (.*)")
  57. if cmd == "view" then
  58. tbl = textutils.unserialize(etc)
  59. return tbl
  60. end
  61. end
  62. end
  63.  
  64. function transfer(id, file)
  65. local dg = string.format("%s %s", "transfer", file)
  66. rednet.send(id, dg)
  67. id, msg = rednet.receive(10)
  68. if msg then
  69. cmd, etc = msg:match("^(%S*) (.*)")
  70. if cmd == "transfer" then
  71. if fs.exists(file) then
  72. return false
  73. else
  74. hFile = fs.open(file, "w")
  75. hFile.write(etc)
  76. hFile.close()
  77. return true
  78. end
  79. end
  80. end
  81. end
Add Comment
Please, Sign In to add comment