Advertisement
Treyzania

RedMesh - DediRouter

Feb 17th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. -- DediRouter v1.1 (Part of RedMesh)
  2. -- Author: Treyzania
  3.  
  4. args = {...}
  5.  
  6. --== PROGRAM ==--
  7. local name = args[1]
  8. local monitorPeri = nil
  9.  
  10. local function waiter()
  11.     while true do os.sleep(60) end
  12. end
  13.  
  14. function handler(data)
  15.    
  16.     local sender = data["sender"]
  17.     local proto = data["protocol"]
  18.     local msg = data["message"]
  19.     local symbol = "?"
  20.    
  21.     if data["type"] == "p2p" then symbol = "P2P" end
  22.     if data["type"] == "broadcast" then symbol = "PGT" end
  23.    
  24.     print("M:" .. sender .. "/" .. base64.from_base64(proto) .. "(" .. symbol .. "):" .. base64.from_base64(msg))
  25.    
  26. end
  27.  
  28. function monitorHandler(data)
  29.    
  30.     local mp = monitorPeri -- Alias
  31.     local c = monitorPeri.isColor()
  32.    
  33.     term.redirect(mp)
  34.    
  35.     local w, h = mp.getSize()
  36.     local x, y = term.getCursorPos()
  37.     if (y >= h) then
  38.         term.scroll(1)
  39.         y = y - 1
  40.     end
  41.     term.setCursorPos(1, y + 1)
  42.    
  43.     if c then mp.setTextColor(colors.white) end
  44.     term.write("P:")
  45.    
  46.     if c then mp.setTextColor(colors.red) end
  47.     term.write(data["sender"])
  48.     if c then mp.setTextColor(colors.gray) end
  49.     term.write("->")
  50.     if c then mp.setTextColor(colors.lime) end
  51.     term.write(data["dest"])
  52.    
  53.     if c then mp.setTextColor(colors.green) end
  54.     term.write(" @" .. tostring(os.day()) .. "-" .. textutils.formatTime(os.time(), true))
  55.    
  56.     if c then mp.setTextColor(colors.blue) end
  57.     term.write(" ")
  58.     term.write(base64.from_base64(data["protocol"]))
  59.     if c then mp.setTextColor(colors.white) end
  60.     term.write("(")
  61.     if c then mp.setTextColor(colors.yellow) end
  62.     term.write(string.sub(base64.from_base64(data["message"]), 1, 8))
  63.     if c then mp.setTextColor(colors.white) end
  64.     term.write(")")
  65.    
  66.     term.redirect(term.native())
  67.    
  68. end
  69.  
  70. -- Args check.
  71. if #args <= 1 then
  72.     print("Usage: DediRouter <RedMesh name> <router1> [router2 [router3[...]]]")
  73.     error("Exiting...")
  74.     shell.exit()
  75. end
  76.  
  77. print("== RedMesh Network Dedicated Router ==")
  78. print("Computer ID:    " .. os.getComputerID())
  79. print("Computer Label: " .. os.getComputerLabel())
  80. print("MeshNet Name:   " .. name)
  81.  
  82. os.loadAPI("redmesh")
  83. redmesh.setVerbose(true)
  84. redmesh.registerHandler(handler)
  85.  
  86. local monitored = false
  87. local b = true
  88. for i, v in ipairs(args) do
  89.    
  90.     if b then
  91.         b = false
  92.     else
  93.        
  94.         local first2 = string.sub(v, 1, 2)
  95.         local after = string.sub(v, 3, -1)
  96.        
  97.         if first2 == "-D" then
  98.             monitorPeri = peripheral.wrap(after)
  99.             monitorPeri.setTextScale(0.5)
  100.             monitorPeri.clear()
  101.             if monitorPeri.isColor() then monitorPeri.setTextColor(colors.lightGray) end
  102.             monitorPeri.setCursorPos(1, 1)
  103.             monitorPeri.write("Beginning log...")
  104.             monitored = true
  105.         elseif first2 == "-M" then
  106.             -- Add the sides to the list.
  107.             redmesh.addSide(after, "*")
  108.         else
  109.             print("Ignoring argument \"" .. v .. "\".")
  110.         end
  111.        
  112.     end
  113.    
  114. end
  115.  
  116. if monitored then redmesh.registerHandler(monitorHandler) end
  117.  
  118. redmesh.init(name)
  119.  
  120. print("All init complete, proceding to main...")
  121. print("=================================================")
  122.  
  123. if pcall(waiter) then
  124.     print("wtf? y u exit?")
  125.     os.reboot()
  126. else
  127.     os.reboot()
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement