Advertisement
Guest User

ServerStartup.lua

a guest
Oct 18th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. function setHost()
  2. modemState = rednet.isOpen(checkForModem_modem)
  3. if modemState ==  false or nil then
  4.     setHost_fail = true
  5. else
  6.  
  7. rednet.host(server1_protocol, server1_host)
  8. setHost_fail = false
  9. end
  10. end
  11.  
  12. function peerCheck(peerCheck_protocol, peerCheck_host, peerCheck_var)
  13. local id = rednet.lookup(peerCheck_protocol, peerCheck_host)
  14. return id
  15. end
  16.  
  17. function checkForModem()
  18.     checkForModem_fail = false
  19.     checkForModem_modem = "top"
  20. end
  21.  
  22. function checkForCommand()
  23. local id, message, protocol = rednet.receive(protocolLv2, 10)
  24. if message == '' then return message, id
  25. else
  26.     checkForCommand_attempts = checkForCommand_attempts + 1 --Waits a bit to check for a command again, prints number of attempts and the clock
  27.     runningtime = os.clock()
  28.     os.sleep(10)
  29.     customMessage(nil, "Current computer runtime: "..runningtime)
  30.     customMessage(nil, "Command check Attempts: "..checkForCommand_attempts)
  31.     end
  32. end
  33.  
  34. function customMessage(mode, message) --For fancy messages
  35. if aY == 19 then
  36. term.clear()
  37. term.setCursorPos(1, 1)
  38. end
  39. if mode == nil then mode = "Server" end
  40. aX, aY = term.getCursorPos()
  41. term.write("["..tostring(mode).."]: "..tostring(message))
  42. term.setCursorPos(1, aY+1)
  43. end
  44.  
  45. function errorHandle() --Just to ennumarate the errors
  46.  
  47. if setHost_fail == true then
  48. customMessage("Error", "The function setHost() failed.")
  49. errorCount = errorCount + 1
  50. end
  51.  
  52. if checkForModem_fail == true then
  53. customMessage("Error", "The function checkForModem() failed.")
  54. errorCount = errorCount + 1
  55. end
  56.  
  57. if furnaceChecker_fail == true then
  58. customMessage("Error", "Unable to find the computer furnaceChecker")
  59. errorCount = errorCount + 1
  60. end
  61. end
  62.  
  63. --[[Action Functions]]--
  64.     function action_ping()
  65.         rednet.send(senderID, "pong!")
  66.     end
  67.            
  68. --[[Action Functions end]]--
  69.  
  70. if setHost_fail or checkForModem_fail == true then
  71. startup_fail = true
  72. end
  73.  
  74. function startup()
  75.     -- protocol hierarchy list MUST BE TOP ROW IN EVERY startup() FUNCTION
  76.         protocolLv1 = "alpha"
  77.         protocolLv2 = "beta"
  78.         protocolLv3 = "gamma"
  79.         protocolLv4 = "delta"
  80.     -- end
  81.  
  82.     -- local host settings
  83.         server1_protocol = protocolLv1
  84.         server1_host = "houseServer"
  85.     -- end
  86.    
  87.     -- static hosts settings
  88.         furnaceChecker = rednet.lookup(protocolLv3,"furnaceCheck")
  89.             if furnaceChecker == nil then
  90.             furnaceChecker_fail = true
  91.             end
  92.     -- end
  93.    
  94.     -- global variables
  95.         errorCount = 0
  96.         checkForCommand_attempts = 0
  97.     -- end
  98.  
  99.     --peripherals
  100.         monitor = peripheral.wrap("front")
  101.         term.redirect(monitor)
  102.         term.clear()
  103.         term.setCursorPos(1,1)
  104.         checkForModem()
  105.         rednet.open(checkForModem_modem)
  106.     -- end
  107.     errorHandle()
  108.         if errorCount == 1 then
  109.         customMessage(nil, "Succesfully started with "..errorCount.." error.")
  110.     elseif errorCount == 0 then
  111.         customMessage(nil, "Succesfully started with no errors")
  112.     elseif errorCount >= 2 then
  113.         customMessage(nil, "Succesfully started with "..errorCount.." errors.")
  114.         end
  115. end
  116.  
  117. if startup_fail == true then errorCount = errorCount + 1
  118.     else startup()
  119.     end
  120.    
  121. while true do --Main loop
  122.     received_command, senderID = checkForCommand()
  123.        
  124.         if received_command == "ping" or "Ping" then
  125.         action_ping()
  126.         received_command = nil
  127.         senderID = nil
  128.         end
  129. end
  130.  
  131. --DiegoG
  132. --Base server OS A.1.2.0
  133. --11/10/2016
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement