Alyssa

FS_Server

May 31st, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.16 KB | None | 0 0
  1. privChan = 31000 + os.getComputerID()
  2. pubChan = 31000
  3. continue = false
  4. modemFound = peripheral.find("modem")
  5. if modemFound then
  6.     modem = modemFound
  7.     modem.open(privChan)
  8.     modem.open(pubChan)
  9.     continue = true
  10. end
  11. while continue do
  12.     e = {os.pullEvent("modem_message")}
  13.     msg = textutils.unserialize(e[5])
  14.     chan = e[3]
  15.     if chan == pubChan then
  16.         cmd = msg[1]
  17.         args = msg[2]
  18.         if cmd == "Find" then
  19.             if fs.exists(args) then
  20.                 modem.transmit(e[4],privChan, textutils.serialize({"success","File found"}))
  21.             end
  22.         elseif cmd == "getFreeSpace" then
  23.             freeSpace = fs.getFreeSpace("/")
  24.             modem.transmit(e[4],privChan, textutils.serialize({"success",freeSpace}))
  25.         end
  26.     elseif chan == privChan then
  27.         cmd = msg[1]
  28.         args = msg[2]
  29.         if cmd == "Request" then
  30.             if fs.exists(args) then
  31.                 f = fs.open(args,"r")
  32.                 modem.transmit(e[4],privChan, f.readAll())
  33.                 f.close()
  34.             else
  35.                 modem.transmit(e[4],privChan,textutils.serialize({"error","File not found"}))
  36.             end
  37.         elseif cmd == "SaveFile" then
  38.             f = fs.open(args,"w")
  39.             f.write(msg[3])
  40.             f.close()
  41.             modem.transmit(e[4],privChan, textutils.serialize({"success","File saved"}))
  42.         end
  43.     end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment