Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Minecraft Intranet
- Router program
- --]]
- print("Minecraft Intranet :: Router")
- print()
- -- Load dependencies
- dofile("/common/all")
- program.load("message", "names", "device")
- program.load("devices/router")
- -- Create router
- local configPath = "/router.conf"
- local defaultConfig = {
- device = "router",
- domain = nil,
- detectTimeout = 1
- }
- local config = common.loadStore(configPath, defaultConfig)
- local router = mcnet.device.Router:new{
- domain = config.domain
- }
- print(" Address: "..mcnet.address.format(router.address))
- print(" Computer ID: "..tostring(os.computerID()))
- print()
- -- Register handlers
- router:on("registeredServant", function(clientId, clientName)
- print("Registered "..clientName.." at "..clientId)
- end)
- router:on("unregisteredServant", function(clientId, clientName)
- print("Unregistered "..clientName.." at "..clientId)
- end)
- router:on("registerServer", function(mainframeId)
- print("Registering mainframe at "..mainframeId.."...")
- end)
- router:on("registeredServer", function()
- print(" Successfully registered at mainframe")
- end)
- router:on("unregisteredServer", function(mainframeId)
- print("Unregistered mainframe at "..mainframeId)
- end)
- router:on("char", function(char)
- if char == "q" then
- -- Confirm quit
- write("Confirm quit? [y/n] > ")
- local event, char = os.pullEvent("char")
- -- Check response
- local response = (string.lower(char) == "y" and "y") or "n"
- print(response)
- if (response == "y") then
- router:stop()
- end
- end
- end)
- router:on("error", function(errorType, errorMessage)
- print("[ERROR] "..errorMessage)
- end)
- -- Start
- print("Starting...")
- router:start(config.detectTimeout)
- write(" Detected:")
- for i,deviceId in pairs(router:neighbours()) do
- write(" "..deviceId)
- end
- print()
- print()
- -- Main loop
- print("Listening...")
- print(" Press Q to quit")
- print()
- while router.isRunning do
- router:trigger(os.pullEvent())
- end
- -- Stop
- print()
- print("Stopped")
- print()
Advertisement
Add Comment
Please, Sign In to add comment