Operating system for OpenComputers
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.

80 lines
2.7KB

  1. local computer = require "computer"
  2. local minitel = require "minitel"
  3. local event = require "event"
  4. local rpc = require "rpc"
  5. local netutil = {}
  6. function netutil.importfs(host,rpath,lpath) -- string string string -- boolean -- Import filesystem *rpath* from *host* and attach it to *lpath*.
  7. local px = rpc.proxy(host,"fs_"..rpath.."_")
  8. function px.getLabel()
  9. return host..":"..rpath
  10. end
  11. px.address = host..":"..rpath
  12. return fs.mount(lpath,px)
  13. end
  14. function netutil.exportfs(path) -- string -- boolean -- Export the directory *path* over RPC.
  15. local path = "/"..table.concat(fs.segments(path),"/")
  16. local px = require("unionfs").create(path)
  17. function px.dirstat(p)
  18. local rt = {}
  19. for k,v in ipairs(px.list(p)) do
  20. local fp = p.."/"..v
  21. rt[v] = {px.isDirectory(fp), px.size(fp), px.lastModified(fp)}
  22. end
  23. return rt
  24. end
  25. for k,v in pairs(px) do
  26. rpc.register("fs_"..path.."_"..k,v)
  27. print("fs_"..path.."_"..k)
  28. end
  29. return true
  30. end
  31. function netutil.ping(addr,times,timeout,silent) -- string number number boolean -- boolean number number number -- Request acknowledgment from *addr*, waiting *timeout* seconds each try, and try *times* times. If *silent* is true, don't print status. Returns true if there was at least one successful ping, the number of successes, the number of failures, and the average round trip time.
  32. local times, timeout = times or 5, timeout or 30
  33. local success, fail, time, avg = 0, 0, 0, 0
  34. for i = 1, times do
  35. local ipt = computer.uptime()
  36. local pid = minitel.genPacketID()
  37. computer.pushSignal("net_send",1,addr,0,"ping",pid)
  38. local t,a = event.pull(timeout,"net_ack")
  39. if t == "net_ack" and a == pid then
  40. if not silent then print("Ping reply: "..tostring(computer.uptime()-ipt).." seconds.") end
  41. success = success + 1
  42. time = time + computer.uptime()-ipt
  43. avg = time / success
  44. else
  45. if not silent then print("Timed out.") end
  46. fail = fail + 1
  47. end
  48. end
  49. if not silent then print(string.format("%d packets transmitted, %d received, %0.0f%% packet loss, %0.1fs",times,success,fail/times*100,time)) end
  50. return success > 0, success, fail, avg
  51. end
  52. function netutil.nc(host,port) -- string number -- boolean -- Starts an interactive Minitel socket connection to *host* on *port*, primarily for remote login. Returns whether the attempt was successful.
  53. port = port or 22
  54. local socket = minitel.open(host,port)
  55. if not socket then return false end
  56. local b = ""
  57. os.spawn(function()
  58. repeat
  59. local b = socket:read("*a")
  60. if b and b:len() > 0 then
  61. io.write(b)
  62. end
  63. coroutine.yield()
  64. until socket.state ~= "open"
  65. end)
  66. repeat
  67. local b = io.read()
  68. if b and b:len() > 0 then
  69. socket:write(b.."\n")
  70. end
  71. until socket.state ~= "open"
  72. return true
  73. end
  74. return netutil