Operating system for OpenComputers
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

72 Zeilen
2.2KB

  1. local computer = require "computer"
  2. local minitel = require "minitel"
  3. local event = require "event"
  4. local ufs = require "unionfs"
  5. local rpc = require "rpc"
  6. local netutil = {}
  7. function netutil.importfs(host,rpath,lpath) -- import filesystem *rpath* from *host* and attach it to *lpath*
  8. local px = rpc.proxy(host,rpath.."_")
  9. function px.getLabel()
  10. return host..":"..rpath
  11. end
  12. px.address = host..":"..rpath
  13. return fs.mount(lpath,px)
  14. end
  15. function netutil.exportfs(path) -- export the directory *path* over RPC
  16. local path = "/"..table.concat(fs.segments(path),"/")
  17. local px = ufs.create(path)
  18. for k,v in pairs(px) do
  19. rpc.register(path.."_"..k,v)
  20. print(path.."_"..k)
  21. end
  22. return true
  23. end
  24. function netutil.ping(addr,times,timeout, silent) -- 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.
  25. local times, timeout = times or 5, timeout or 30
  26. local success, fail, time, avg = 0, 0, 0, 0
  27. for i = 1, times do
  28. local ipt = computer.uptime()
  29. local pid = minitel.genPacketID()
  30. computer.pushSignal("net_send",1,addr,0,"ping",pid)
  31. local t,a = event.pull(timeout,"net_ack")
  32. if t == "net_ack" and a == pid then
  33. if not silent then print("Ping reply: "..tostring(computer.uptime()-ipt).." seconds.") end
  34. success = success + 1
  35. time = time + computer.uptime()-ipt
  36. avg = time / success
  37. else
  38. if not silent then print("Timed out.") end
  39. fail = fail + 1
  40. end
  41. end
  42. 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
  43. return success > 0, success, fail, avg
  44. end
  45. function netutil.nc(host,port)
  46. port = port or 22
  47. local socket = minitel.open(host,port)
  48. if not socket then return false end
  49. local b = ""
  50. os.spawn(function()
  51. repeat
  52. local b = socket:read("*a")
  53. if b and b:len() > 0 then
  54. io.write(b)
  55. end
  56. coroutine.yield()
  57. until socket.state ~= "open"
  58. end)
  59. repeat
  60. local b = io.read()
  61. if b and b:len() > 0 then
  62. socket:write(b.."\n")
  63. end
  64. until socket.state ~= "open"
  65. end
  66. return netutil