Advertisement
PaymentOption

webserverSERVER

Apr 27th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.17 KB | None | 0 0
  1. -- Basic websites by PaymentOption for NeXuS --
  2. VERSION = "ALPHA 0.0.1"
  3. -----------------------------------------------
  4.  
  5. -- VARS --
  6. iConnections = 0
  7. tConnected = {}
  8.  
  9. sCodePath = "NeXuS/Web/Server/code"
  10. ----------
  11.  
  12. rednet.open("top")
  13. rednet.open("right")
  14. rednet.open("left")
  15. rednet.open("back")
  16. rednet.open("bottom")
  17.  
  18. shell.run("mkdir", "NeXuS/Web/Server/")
  19.  
  20. -- Helper Functions --
  21. function cPrint(height, string)
  22.     local w, h = term.getSize()
  23.     local xPosition = w/2 - string.len(string)/2
  24.    
  25.     term.setCursorPos(xPosition, height)
  26.     term.write(string)
  27. end
  28.  
  29. function rPrint(height, string)
  30.     local w,h = term.getSize()
  31.     local xPos = w - string.len(string)
  32.    
  33.     term.setCursorPos(xPos, height)
  34.     term.write(string)
  35. end
  36.  
  37. function clear()
  38.     term.clear()
  39.     term.setCursorPos(1,1)
  40. end
  41. -----------------------
  42.  
  43. -- Menues and other screens --
  44. function printMenu()
  45.     rPrint(17, "Version: "..VERSION)
  46.     cPrint(1, "NeXuS Website BETA")
  47.     term.setCursorPos(1,1)
  48.     write("Status: ")
  49.     term.setCursorPos(1,3)
  50.     write("Connected users: "..iConnections)
  51.     cPrint(6, "Awaiting requests...")
  52. end
  53. ------------------------------
  54.  
  55. -- Networking related functions --
  56. function connectHANDLE(connection) -- Connection being the userID who is trying to connect
  57.     local file = fs.open(sCodePath, "r") -- Get the code that the user will be running to interact with the server
  58.     local fileContents = file.readAll()
  59.     file.close()
  60.    
  61.     rednet.send(connection, fileContents) -- tostring() just to make sure that rednet doesn't through a fit
  62.     iConnections = iConnections+1
  63.     tConnected[iConnections] = tonumber(connection)
  64.     term.setCursorPos(1,2)
  65.     write("User "..connection.." connected")
  66. end
  67.  
  68. function disconnectHANDLE(connection)
  69.     for i=1, #tConnected do
  70.         if tConnected[i] == connection then
  71.             iConnections = iConnections-1
  72.             tConnected[i] = nil
  73.         end
  74.     end
  75.     term.setCursorPos(1,2)
  76.     write("User "..connection.." disconnected")
  77.     sleep(0.9)
  78. end
  79. ----------------------------------
  80.  
  81. while true do
  82.     clear()
  83.     printMenu()
  84.    
  85.     sender, message = rednet.receive()
  86.    
  87.     if message == "connect" then
  88.         connectHANDLE(sender)
  89.     end
  90.    
  91.     if message == "disconnect" then
  92.         disconnectHANDLE(sender)
  93.     end
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement