PaymentOption

downloadServer

Apr 25th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. -- File Server by PaymentOption for NeXuS
  2. VERSION = 1.0
  3. ---------------------------------------------
  4.  
  5. serverID = os.getComputerID()
  6. rednet.open("top")
  7. rednet.open("right")
  8. rednet.open("left")
  9. rednet.open("back")
  10. rednet.open("bottom")
  11.  
  12. -- Create File Directories
  13. shell.run("mkdir", "NeXuS/FileServer")
  14. shell.run("mkdir", "NeXuS/FileServer/Files")
  15. --
  16.  
  17. tFiles = {}
  18. sFiles = "" --For sending
  19.  
  20. -- LUA FUNCTIONS NOT WRITTEN BY ME --
  21. function TBLval_to_str ( v )
  22.   if "string" == type( v ) then
  23.     v = string.gsub( v, "\n", "\\n" )
  24.     if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
  25.       return "'" .. v .. "'"
  26.     end
  27.     return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
  28.   else
  29.     return "table" == type( v ) and TBLtostring( v ) or
  30.       tostring( v )
  31.   end
  32. end
  33.  
  34. function TBLkey_to_str ( k )
  35.   if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
  36.     return k
  37.   else
  38.     return "[" .. TBLval_to_str( k ) .. "]"
  39.   end
  40. end
  41.  
  42. function TBLtostring( tbl )
  43.   local result, done = {}, {}
  44.   for k, v in ipairs( tbl ) do
  45.     table.insert( result, TBLval_to_str( v ) )
  46.     done[ k ] = true
  47.   end
  48.   for k, v in pairs( tbl ) do
  49.     if not done[ k ] then
  50.       table.insert( result,
  51.         TBLkey_to_str( k ) .. "=" .. TBLval_to_str( v ) )
  52.     end
  53.   end
  54.   return table.concat( result, "\n" )
  55. end
  56. -- END --
  57.  
  58. function downloadFile(filename, origSender)
  59.         if fs.exists("NeXuS/FileServer/Files/"..filename) == false then
  60.                 cPrint(10, "Failed: File does not exist")
  61.                 rednet.send(origSender, "failed")
  62.                 sleep(2)
  63.         else
  64.                 file = fs.open("NeXuS/FileServer/Files/"..filename, "r")
  65.                 fileContents = file.readAll()
  66.                 file.close()
  67.                
  68.                 rednet.send(origSender, fileContents)
  69.                 cPrint(10, "Successfull send")
  70.                 sleep(2)
  71.         end
  72. end
  73.  
  74. function uploadFile(origSender)
  75.         rednet.send(origSender, "ready")
  76.         sender, filename = rednet.receive(0.8)
  77.        
  78.         if sender == origSender then
  79.                 local file = fs.open("NeXuS/FileServer/Files/"..filename, "w")
  80.                 rednet.send(origSender, "ready")
  81.                 sender, fileContents = rednet.receive(0.8)
  82.                
  83.                 if sender == origSender then
  84.                         file.write(tostring(fileContents))
  85.                         file.close()
  86.                 else cPrint(7, "Failure: Request conjestion"); sleep(0.5); end
  87.         else cPrint(7, "Failure: Request conjestion"); sleep(0.5); end
  88. end
  89.  
  90. function cPrint(height, string)
  91.         local w, h = term.getSize()
  92.         local xPosition = w/2 - string.len(string)/2
  93.        
  94.         term.setCursorPos(xPosition, height)
  95.         term.write(string)
  96. end
  97.  
  98. function rPrint(height, string)
  99.         local w,h = term.getSize()
  100.         local xPos = w - string.len(string)
  101.        
  102.         term.setCursorPos(xPos, height)
  103.         term.write(string)
  104. end
  105.  
  106. function printMenu()
  107.         rPrint(4, "NeXuS File Server   ")
  108.         rPrint(5, "Version: "..VERSION.."   ")
  109. end
  110.  
  111.  function printLogo()
  112.     term.clear(); term.setCursorPos(1,1)
  113.     print("     _______          ____  ___      _________")
  114.     print("     \\      \\   ____  \\   \\/  /__ __/   _____/")
  115.     print("     /   |   \\_/ __ \\  \\     /|  |  \\_____  \\ ")
  116.     print("    /    |    \\  ___/  /     \\|  |  /        \\")
  117.     print("    \\____|__  /\\___  >/___/\\  \\____/_______  /")
  118.     print("            \\/     \\/       \\_/            \\/ ")
  119.     cPrint(15, "File Server by PaymentOption"); sleep(3)
  120. end
  121.  
  122. printLogo()
  123. while true do
  124.         term.clear()
  125.         term.setCursorPos(1,1)
  126.        
  127.         tFiles = fs.list("NeXuS/FileServer/Files/")
  128.         sFiles = TBLtostring(tFiles)
  129.        
  130.         printMenu()
  131.         cPrint(8, "Awaiting requests...")
  132.         sender, message = rednet.receive()
  133.         if message == "download" then
  134.                 rednet.send(sender, "ready")
  135.                 sender, filename = rednet.receive(0.8)
  136.                 downloadFile(filename, sender)
  137.         end
  138.        
  139.         if message == "upload" then
  140.                 cPrint(7, "Upload request from "..sender)
  141.                 uploadFile(sender)
  142.         end
  143.        
  144.         if message == "files" then
  145.                 rednet.send(sender, sFiles)
  146.         end
  147. end
Add Comment
Please, Sign In to add comment