Advertisement
SuPeRMiNoR3

File_Server

Dec 8th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. fs = require("filesystem")
  2. serial = require("serialization")
  3. component = require("component")
  4. dns = require("dns")
  5. modem = component.modem
  6. result = false
  7.  
  8. function decode(data)
  9. status, result = pcall(serial.unserialize, data)
  10. return status, result
  11. end
  12.  
  13. function buildArray(input)
  14.   local arr = {}
  15.   for v in input do
  16.     arr[#arr + 1] = v
  17.   end
  18.   return arr
  19. end
  20.  
  21. function encode(data)
  22. return serial.serialize(data)
  23. end
  24.  
  25. function decodeFile(path)
  26. good = false
  27. data = nil
  28. if fs.exists(path) then
  29.  f = open(path)
  30.  data = f:read()
  31.  f:close()
  32.  good = true
  33. end
  34. if good then return data else return false end
  35. end
  36.  
  37. function send(addr, port, data)
  38. tmp = encode(data)
  39. --print(tmp)
  40. modem.send(addr, port, tmp)
  41. end
  42.  
  43. function broadcast(port, data)
  44. modem.broadcast(port, encode(data))
  45. end
  46.  
  47. file_table = {}
  48. function scanDir(path)
  49.   for item in fs.list(path) do
  50.     --print(item)
  51.     if fs.isDirectory(item) then
  52.       scanDir(fs.concat(path, item))
  53.     else
  54.       file_table[#file_table + 1] = string.sub(fs.concat(path, item), 7)
  55.     end
  56.   end
  57. end
  58.  
  59. print("Building file table")
  60. scanDir("/share")
  61. print("Done")
  62.  
  63. print("File Server Starting...")
  64. dns.register("FileServer")
  65. modem.open(80)
  66.  
  67. while true do
  68.   --a, b, c, r, p = event.pull("modem_")
  69.   e, _, address, port, distance, message = event.pull("modem_message")
  70.   result, msg = decode(message)
  71.   if result then
  72.  
  73.     if msg.action == "ping" then
  74.       print("Client "..address.." sent ping")
  75.       tmp_list = {action="ping"}
  76.       send(address, 81, tmp_list)
  77.     end
  78.  
  79.     if msg.action == "list" then
  80.       print("Client "..address.." requested list")
  81.       --tmp_list = fs.list("/share")
  82.       --tmp_list = buildArray(tmp_list)
  83.       send(address, 81, {action="list", data=file_table})
  84.     end
  85.  
  86.     if msg.action == "get" then
  87.       print("Client "..address.." requested get "..msg.data)
  88.       realfile = fs.concat("/share", msg.data)
  89.       if fs.exists(realfile) then
  90.          f = io.open(realfile)
  91.          print("Reading file")
  92.          tmp = f:read("*all")
  93.          f:close()
  94.          tmp = {action="get", data=tmp}
  95.          print("Sending file")
  96.          send(address, 81, tmp)
  97.          print("Done")
  98.       else
  99.         print("Error: file does not exist.")
  100.       end
  101.     end
  102.   end
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement