Guest User

Server

a guest
Mar 1st, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.15 KB | None | 0 0
  1. print("I am the server")
  2. -- server
  3. local modemSide = "right"
  4. local filesList = {
  5.   cake  = "/cake",
  6.   can   = "/can",
  7.   case  = "/case",
  8.   computer = "/computer",
  9.   foot  = "system/foot",
  10.   krypt = "/krypt",
  11.   mouse = "home/mouse",
  12.   notepad = "/notepad",
  13.   pen = "/pen",
  14.   test = "/test"
  15. }
  16.  
  17. rednet.open(modemSide)
  18.  
  19. while true do
  20.   local event, senderId, message, distanceSent = os.pullEvent("rednet_message") -- only pull rednet events
  21.   local packet -- forward declaration
  22.   print('got a request from '..senderId..' for '..message)
  23.   if filesList[message] then -- if the requested file exists
  24.     local handle = fs.open(filesList[message], "r")
  25.     if not handle then -- if we cannot open the file for some reason
  26.       packet = { success=false, message="Could not open desired file" }
  27.     else
  28.       packet = { success=true, message=handle.readAll(), path=filesList[message] } -- read the file into our packet
  29.       handle.close() -- close the file
  30.     end
  31.   else
  32.     packet = { success=false, message="No program with that name" }
  33.   end
  34.   rednet.send(senderId, textutils.serialize(packet)) -- send our packet
  35. end
  36.  
  37. rednet.close(modemSide)
Advertisement
Add Comment
Please, Sign In to add comment