Advertisement
Derek1017

VFS - Client

Jun 19th, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.29 KB | None | 0 0
  1. local nextSeq = 0
  2.  
  3. local timeout = 2
  4.  
  5. for _,v in ipairs(redstone.getSides()) do
  6.         if peripheral.getType(v) == "modem" then
  7.                 rednet.open(v)
  8.         end
  9. end
  10.  
  11. local fs_rfs = {
  12.         send = function(self, cmd, ...)
  13.                 local seq = nextSeq
  14.                 nextSeq = nextSeq + 1
  15.                 cmd = {"RFS", self.password, seq, cmd, ...}
  16.                 cmd = textutils.serialize(cmd)
  17.                 rednet.send(self.server, cmd)
  18.                 --print("sent ",cmd," to ",self.server)
  19.                 local _end = os.clock() + timeout
  20.                 --print("end ",_end," ",rednet.receive," ",coroutine.running())
  21.                 while true do
  22.                         local s, m = rednet.receive(_end - os.clock())
  23.                         --print(tostring(s)," ",tostring(m))
  24.                         if s == nil or m == nil then
  25.                                 error("RFS timeout")
  26.                         end
  27.                         local t = textutils.unserialize(m)
  28.                         if s == self.server and type(t) == "table" and t[2] == seq then
  29.                                 if t[1] == "RFS" then
  30.                                         table.remove(t, 1)
  31.                                         table.remove(t, 1)
  32.                                         return unpack(t)
  33.                                 elseif t[1] == "RFS-error" then
  34.                                         error("Remote error: " .. t[3])
  35.                                 end
  36.                         end
  37.                 end
  38.         end,
  39.  
  40.         trpath = function(self, path)
  41.                 return self.prefix .. path
  42.         end,
  43.  
  44.         isReadOnly = function(self, path) return self:send("isReadOnly", self:trpath(path)) end,
  45.         exists = function(self, path)
  46.                 local ok, rv = pcall(self.send, self, "exists", self:trpath(path))
  47.                 return ok and rv
  48.         end,
  49.         isDir = function(self, path)
  50.                 local ok, rv = pcall(self.send, self, "isDir", self:trpath(path))
  51.                 return ok and rv
  52.         end,
  53.         list = function(self, path) return {self:send("list", self:trpath(path))} end,
  54.         getDrive = function(self, path) return "RFS-"..self.server end,
  55.         mkdir = function(self, path) self:send("mkdir", self:trpath(path)) end,
  56.         delete = function(self, path) self:send("delete", self:trpath(path)) end,
  57.         getSize = function(self, path) return self:send("getSize", self:trpath(path)) end,
  58.         getFreeSpace = function(self, path) return self:send("getFreeSpace", self:trpath(path)) end,
  59. -- move/copy?
  60.  
  61.         open = function(self, path, mode)
  62.                 local id = self:send("open", self:trpath(path), mode)
  63.                 if id == nil then return nil end
  64.                 --print("Got ID "..tostring(id))
  65.                 local t = {}
  66.                 t.close = function() self:send("close", id) end
  67.                 if mode == "r" then
  68.                         t.readLine = function() return self:send("readLine", id) end
  69.                         t.readAll = function() return self:send("readAll", id) end
  70.                 elseif mode == "w" or mode == "a" then
  71.                         t.write = function(s) return self:send("write", id, s) end
  72.                         t.writeLine = function(s) return self:send("writeLine", id, s) end
  73.                 elseif mode == "rb" then
  74.                         t.read = function() return self:send("read", id) end
  75.                 elseif mode == "wb" or mode == "ab" then
  76.                         t.write = function(b) return self:send("write", id, b) end
  77.                 else
  78.                         error("invalid mode "..tostring(mode))
  79.                 end
  80.                 return t
  81.         end,
  82. }
  83.  
  84. vfs.registerFS("rfs", function(mount, opts)
  85.         assert(type(opts.server) == "number", "server field must be number")
  86.         assert(type(opts.password) == "string", "password field must be string")
  87.         assert(type(opts.prefix) == "string" or opts.prefix == nil, "prefix field must be string or nil")
  88.         local t = {server=opts.server, password=opts.password, prefix=opts.prefix or ""}
  89.         setmetatable(t, {__index=fs_rfs})
  90.         return t
  91. end)
  92.  
  93. --vfs.unmount("/rfs/")
  94. --vfs.mount("/rfs/", "rfs", {server=131, password="rfs-test"})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement