Guest User

repeater

a guest
Jun 8th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. --[[
  2.  
  3.     Minecraft Intranet
  4.     Repeater program
  5.  
  6. --]]
  7.  
  8. print("Minecraft Intranet :: Repeater")
  9. print()
  10.  
  11. -- Program directory
  12. local progDir = shell.resolve("/"..shell.getRunningProgram().."/..")
  13.  
  14. -- Load APIs
  15. dofile(progDir.."/config")
  16.  
  17. -- Load repeater config
  18. local configPath = "/repeater.conf"
  19. local defaultConfig = {
  20.     modemSide = "back",
  21.     detectTimeout = 5
  22. }
  23. local config = mcnet.config.load(configPath, defaultConfig)
  24.  
  25. print("  Computer ID: "..tostring(os.computerID()))
  26. print("  Modem side: "..config.modemSide)
  27. print()
  28. rednet.open(config.modemSide)
  29.  
  30. -- Detect surrounding devices
  31. print("Detecting surrounding devices...")
  32. rednet.broadcast("deviceOn")
  33.  
  34. local surroundings = {}
  35.  
  36. local detectTimer = os.startTimer(config.detectTimeout)
  37. while true do
  38.     local event, p1, p2 = os.pullEvent()
  39.     local refresh = false
  40.  
  41.     if (event == "rednet_message") then
  42.         -- Add and notify if device started
  43.         if (p1 == "deviceOn") then
  44.             surroundings[p1] = true
  45.             rednet.send(p1, "deviceDetect")
  46.             refresh = true
  47.         -- Add if device detected
  48.         elseif (p1 == "deviceDetect") then
  49.             surroundings[p1] = true
  50.             refresh = true
  51.         -- Remove if device stopped
  52.         elseif (p1 == "deviceOff") then
  53.             surroundings[p1] = nil
  54.             refresh = true
  55.         end
  56.     elseif (event == "timer" and p1 == detectTimer) then
  57.         -- Detection timed out
  58.         break
  59.     end
  60.  
  61.     if refresh then
  62.         -- Refresh timer
  63.         detectTimer = os.startTimer(config.detectTimeout)
  64.     end
  65. end
  66.  
  67. write("Detected:")
  68. for deviceId,v in pairs(surroundings) do
  69.     write(" "..deviceId")
  70. end
  71. print()
  72.  
  73. -- Start listening
  74. print("Repeating...")
  75. print("Press Q to quit")
  76. print()
  77. while true do
  78.    local event, p1, p2 = os.pullEvent()
  79.  
  80.    if (event == "rednet_message") then
  81.        -- Forward message to other devices
  82.        for deviceId,v in pairs(surroundings) do
  83.            if (deviceId ~= p1) then
  84.                rednet.broadcast(p2)
  85.            end
  86.        end
  87.    elseif (event == "char" and p1 == "q") then
  88.        -- Confirm quit
  89.        write("Confirm quit? [y/n] > ")
  90.        local event, response = os.pullEvent("char")
  91.        print(response)
  92.  
  93.        if (response == "y") then
  94.            break
  95.        end
  96.    end
  97. end
  98.  
  99. rednet.broadcast("repeaterOff")
  100. rednet.close(config.modemSide)
  101. print("Stopped")
  102. print()
Advertisement
Add Comment
Please, Sign In to add comment