Advertisement
Guest User

FTP

a guest
Mar 20th, 2015
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.55 KB | None | 0 0
  1. -----------------------------
  2. --[   Global varibales    ]--
  3. -----------------------------
  4. local protocolRequest = "FTP_request"
  5. local protocolData = "FTP_data"
  6. local protocolArrived = "FTP_arrived"
  7. local messageContinue = "continue"
  8. local messageStop = "stop"
  9.  
  10. local args = {...}
  11. local debug = true
  12. local modemSide
  13.  
  14. -----------------------------
  15. --[   Utility Functions   ]--
  16. -----------------------------
  17. local function getWirelessModemSide()
  18.   local sides = rs.getSides()
  19.   local side = nil
  20.  
  21.   for i = 1, #sides do
  22.     if peripheral.getType(sides[i]) == "modem" then
  23.       if peripheral.wrap(sides[i]).isWireless() then
  24.         side = sides[i]
  25.       end
  26.     end
  27.   end
  28.  
  29.   return side
  30. end
  31.  
  32. -----------------------------
  33. --[    Main Functions     ]--
  34. -----------------------------
  35. local function send(targetID, filePath, code)
  36.   -- Checks the targetID variable
  37.   if tonumber(targetID) == nil then
  38.     if debug then print("The ID argument must be a number.") end
  39.     return
  40.   else
  41.     if tonumber(targetID) < 0 then
  42.       if debug then print("The ID argument cannot be lower than 0.") end
  43.       return
  44.     end
  45.   end
  46.  
  47.   -- Checks the filePath variable
  48.   if not fs.exists(filePath) then
  49.     if debug then print("The file path you specified does not exist.") end
  50.     return
  51.   end
  52.   local file = fs.open(filePath, "r")
  53.   local fileContent = file.readAll()
  54.   file.close()
  55.  
  56.   -- Sends a request and waits for confirmation of arrival
  57.   local senderID, message, protocol = nil
  58.   local i = 0
  59.   while senderID == nil and i < 10 do
  60.     rednet.send(tonumber(targetID), code, protocolRequest)
  61.     senderID, message, protocol = rednet.receive(protocolArrived, 1)
  62.     i = i + 1
  63.   end
  64.   if senderID == nil and i == 10 then
  65.     if debug then print("We didn't receive a response, not sending any data.") end
  66.     return
  67.   elseif message == messageStop then
  68.     if debug then print("The codes didn't match, not sending any data.") end
  69.     return
  70.   end
  71.  
  72.   -- Sends the file content and waits for confirmation of arrival
  73.   local senderID, message, protocol = nil
  74.   local i = 0
  75.   while senderID == nil and i < 10 do
  76.     rednet.send(tonumber(targetID), fileContent, protocolData)
  77.     senderID, message, protocol = rednet.receive(protocolArrived, 1)
  78.     i = i + 1
  79.   end
  80.   if i == 10 then
  81.     if debug then print("We didn't receive a response, the data may have not arrived.") end
  82.     return
  83.   end
  84. end
  85. local function receive(time, code)
  86.   -- Checks the time variable
  87.   if tonumber(time) == nil then
  88.     if debug then print("The time argument must be a number.") end
  89.     return
  90.   else
  91.     if tonumber(time) < 0 then
  92.       if debug then print("The time argument cannot be lower than 0.") end
  93.       return
  94.     end
  95.   end
  96.  
  97.   -- Starts the timer
  98.   local timerID
  99.   if tonumber(time) > 0 then
  100.     timerID = os.startTimer(tonumber(time))
  101.   end
  102.  
  103.   local fileCount = 0
  104.   while true do
  105.     -- Waits for the next event
  106.     local event, p1, p2, p3 = os.pullEvent()
  107.    
  108.     if event == "rednet_message" and p3 == protocolRequest then
  109.       if p2 == code then
  110.         -- Sends a message to confirm arrival
  111.         rednet.send(p1, messageContinue, protocolArrived)
  112.        
  113.         -- Waits for the file content, saves it to a file (if the codes match) and sends a message to confirm arrival
  114.         local senderID, message, protocol = rednet.receive(protocolData, 10)
  115.         rednet.send(senderID, messageStop, protocolArrived)
  116.        
  117.         local file = fs.open("download" .. fileCount, "w")
  118.         file.write(message)
  119.         file.close()
  120.        
  121.         if debug then print("A file from computer " .. senderID .. " arrived and was saved in the path \"download" .. fileCount .. "\".") end
  122.        
  123.         fileCount = fileCount + 1
  124.       else
  125.         -- Sends a message to confirm arrival
  126.         rednet.send(p1, messageStop, protocolArrived)
  127.        
  128.         if debug then print("A file from computer " .. p1 .. " arrived and was discarded, because the codes didn't match.") end
  129.       end
  130.     elseif event == "key" then
  131.       -- Stops receiving files if the user presses any button
  132.       if debug then print("The user pressed a key, stopped receiving files.") end
  133.       return
  134.     elseif event == "timer" and p1 == timerID then
  135.       -- Stops receiving files if the timer went off
  136.       if debug then print("The time has passed, stopped receiving files.") end
  137.       return
  138.     end
  139.   end
  140. end
  141.  
  142. -----------------------------
  143. --[        Program        ]--
  144. -----------------------------
  145. -- Opens a wireless modem
  146. modemSide = getWirelessModemSide()
  147. if modemSide == nil then
  148.   if debug then print("Please attach a wireless modem to this computer.") end
  149.   return
  150. else
  151.   rednet.open(modemSide)
  152. end
  153.  
  154. -- Runs main program
  155. if args[1] == "send" and args[2] ~= nil and args[3] ~= nil and args[4] ~= nil then
  156.   send(args[2], args[3], args[4])
  157. elseif args[1] == "send" and args[2] ~= nil and args[3] ~= nil then
  158.   send(args[2], args[3], "")
  159. elseif args[1] == "receive" and args[2] ~= nil and args[3] ~= nil then
  160.   receive(args[2], args[3])
  161. elseif args[1] == "receive" and args[2] ~= nil then
  162.   receive(args[2], "")
  163. else
  164.   print("Usage (<> required, [] optional):")
  165.   print("  FTP send <ID> <file path> [code]")
  166.   print("    sends <file path> to <ID> (with [code])")
  167.   print("  FTP receive <time> [code]")
  168.   print("    receives files for <time> in seconds and saves")
  169.   print("    them (if they are sent with the right [code])")
  170.   print("    (set <time> to 0 for indefinite file receiving)")
  171. end
  172.  
  173. -- Closes the wireless modem
  174. rednet.close(modemSide)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement