MattiasBuelens

MCNet router

Jun 8th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  
  3.     Minecraft Intranet
  4.     Router program
  5.  
  6. --]]
  7.  
  8. print("Minecraft Intranet :: Router")
  9. print()
  10.  
  11. -- Load dependencies
  12. dofile("/common/all")
  13. program.load("message", "names", "device")
  14. program.load("devices/router")
  15.  
  16. -- Create router
  17. local configPath = "/router.conf"
  18. local defaultConfig = {
  19.     device = "router",
  20.     domain = nil,
  21.     detectTimeout = 1
  22. }
  23. local config = common.loadStore(configPath, defaultConfig)
  24. local router = mcnet.device.Router:new{
  25.     domain = config.domain
  26. }
  27. print("  Address: "..mcnet.address.format(router.address))
  28. print("  Computer ID: "..tostring(os.computerID()))
  29. print()
  30.  
  31. -- Register handlers
  32. router:on("registeredServant", function(clientId, clientName)
  33.     print("Registered "..clientName.." at "..clientId)
  34. end)
  35.  
  36. router:on("unregisteredServant", function(clientId, clientName)
  37.     print("Unregistered "..clientName.." at "..clientId)
  38. end)
  39.  
  40. router:on("registerServer", function(mainframeId)
  41.     print("Registering mainframe at "..mainframeId.."...")
  42. end)
  43.  
  44. router:on("registeredServer", function()
  45.     print("  Successfully registered at mainframe")
  46. end)
  47.  
  48. router:on("unregisteredServer", function(mainframeId)
  49.     print("Unregistered mainframe at "..mainframeId)
  50. end)
  51.  
  52. router:on("char", function(char)
  53.     if char == "q" then
  54.         -- Confirm quit
  55.         write("Confirm quit? [y/n] > ")
  56.         local event, char = os.pullEvent("char")
  57.         -- Check response
  58.         local response = (string.lower(char) == "y" and "y") or "n"
  59.         print(response)
  60.         if (response == "y") then
  61.             router:stop()
  62.         end
  63.     end
  64. end)
  65.  
  66. router:on("error", function(errorType, errorMessage)
  67.     print("[ERROR] "..errorMessage)
  68. end)
  69.  
  70. -- Start
  71. print("Starting...")
  72. router:start(config.detectTimeout)
  73. write("  Detected:")
  74. for i,deviceId in pairs(router:neighbours()) do
  75.     write(" "..deviceId)
  76. end
  77. print()
  78. print()
  79.  
  80. -- Main loop
  81. print("Listening...")
  82. print("  Press Q to quit")
  83. print()
  84.  
  85. while router.isRunning do
  86.     router:trigger(os.pullEvent())
  87. end
  88.  
  89. -- Stop
  90. print()
  91. print("Stopped")
  92. print()
Advertisement
Add Comment
Please, Sign In to add comment