Advertisement
ozybrum

BRUM'S SLAVE COMPUTER 0.2

Jul 27th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. --#########BRUM'S SLAVE COMPUTER ############
  2.  
  3. -- System Variables
  4. local sides = {"left", "bottom", "top", "right", "back"}
  5. local myId = os.getComputerID()
  6. local version = 0.2
  7. local connected = false
  8.  
  9.  
  10.  
  11. -- Find what side the modem is on and open the modem to the network
  12. function peripherals()
  13.     for i = 1, 5 do
  14.         if peripheral.getType(sides[i]) == "modem" then
  15.                 modem = peripheral.wrap(sides[i])
  16.                 rednet.open(sides[i])
  17.                 modemside = (sides[i])
  18.         end
  19.     end
  20. end
  21.  
  22. peripherals()
  23.  
  24. --######### USER VARIABLES ############
  25. local myName = "Slave 1"
  26. --#####################################
  27.  
  28.  
  29. function startup()
  30.     print("#########BRUM'S SLAVE COMPUTER ############")
  31.     print("Version: " ..version)
  32.     print("Computer ID: " ..myId)
  33.     print("Computer Name: " ..myName)
  34.     print("Modem Side: " ..modemside)
  35.     findMaster()
  36. end
  37.  
  38. function findMaster()
  39.     while connected == false do
  40.         print("INFO: Attempting to find master computer...")
  41.         rednet.broadcast(myName)
  42.         local id, response = rednet.receive(4)
  43.         if response == "hello from master" then
  44.             masterId = id
  45.             print("Successfully Connected to master")
  46.             print("Master computer ID: "..masterId)
  47. --[[            masterId = tonumber(id)   -- try to convert it to a number
  48.                 if masterId == nil then
  49.                 error("Error: "..masterId .." is not a valid number")
  50.                 else
  51.                 print("INFO: Successfully converted id to number")
  52.                 end
  53. --]]
  54.             connected = true
  55.             pingMaster()
  56.         else
  57.         sleep(3)
  58.         end
  59.     end
  60. end
  61.  
  62. function pingMaster()
  63. sleep(10)
  64.     while connected == true do
  65.         print("Pinging master...")
  66.         rednet.send(masterId, "still there?")
  67.         local id, response = rednet.receive(4)
  68.         if id == masterId and response == "Yep! still here." then
  69.             print("INFO: Master responded:" ..response)
  70.             sleep(10)
  71.             connected = true
  72.         else
  73.             connected = false
  74.         end
  75.     end
  76. end
  77.  
  78. sleep(3)
  79. startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement