Advertisement
ChaosBeing

Server startup

Mar 4th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. function send(ID, path)
  2.  
  3.  if not fs.exists(path) then
  4.   rednet.send(ID, "ERROR: File does not exist!")
  5.   return
  6.  end
  7.  
  8.  file = io.open(path, "r")
  9.  print(path)
  10.  fileBody = file:read("*a")
  11.  file:close()
  12.  
  13.  rednet.send(ID, fileBody)
  14.  
  15. end
  16.  
  17.  
  18.  
  19. function receive(ID, path)
  20.  
  21.  if fs.exists(path) then
  22.   count = 0
  23.   while count < 10 do
  24.    rednet.send(ID, "File exists on server. Overwrite? (y/n): ")
  25.    ID2, msg = rednet.receive(1)
  26.  
  27.    if ID2 and ID ~= ID2 then
  28.     rednet.send(ID2, "Server is busy. Try again in a moment.")
  29.  
  30.    elseif ID == ID2 and msg ~= "y" then
  31.     return
  32.    
  33.    elseif ID == ID2 and msg == "y" then
  34.     count = 20
  35.    end
  36.    
  37.    if count == 10 then
  38.     return
  39.    end
  40.   end
  41.  end
  42.  
  43.  for count = 0, 10 do
  44.   ID3, fBody = rednet.receive(1)
  45.   if ID3 and ID3 ~= ID then
  46.    rednet.send(ID3, "Server is busy. Try again in a moment.")
  47.   end
  48.  
  49.   if ID3 == ID then
  50.    file = io.open(path, "w")
  51.    file:write(fBody)
  52.    file:close()
  53.    rednet.send(ID, "finished")
  54.    return
  55.   end
  56.  end
  57. end
  58.  
  59.  
  60.  
  61. rednet.open("right")
  62.  
  63. while true do
  64.  ID, msg = rednet.receive()
  65.  
  66.  if msg:sub(1, 2) == "dl" then
  67.   send(ID, msg:sub(4))
  68.  
  69.  elseif msg:sub(1, 2) == "ul" then
  70.   receive(ID, msg:sub(4))
  71.  
  72.  else
  73.   rednet.send(ID, "ERROR: Invalid Command!")
  74.   print("Invalid command!")
  75.  end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement