Advertisement
osmarks

monday server

Nov 11th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. -- monday - a thing for running and monitoring services - servery bit
  2.  
  3. local a=http.get"https://raw.githubusercontent.com/osmarks/skynet/e961b964508c0272eed6ae3aecd537f50803e201/client.lua"local b=fs.open("skynet","w")b.write(a.readAll())a.close()b.close()
  4.  
  5. local skynet = require "/skynet"
  6.  
  7. local chan = settings.get "monday.channel" or "monday"
  8. local m = peripheral.find "monitor"
  9.  
  10. local things = {}
  11. local by_ID = {}
  12.  
  13. local function send_command(computer, command)
  14.     skynet.send(chan, {
  15.         type = "command",
  16.         computer = computer,
  17.         command = command
  18.     })
  19. end
  20.  
  21. local function displayer()
  22.     m.setTextScale(0.5)
  23.     m.setTextColor(colors.black)
  24.     m.setBackgroundColor(colors.black)
  25.     m.clear()
  26.     while true do
  27.         local _, msg = skynet.receive(chan)
  28.         if type(msg) == "table" and msg.type == "update" then
  29.             things[msg.computer] = {
  30.                 state = msg.state,
  31.                 message = msg.message
  32.             }
  33.         end
  34.  
  35.         local i = 1
  36.         for k, v in pairs(things) do
  37.             m.setCursorPos(1, i)
  38.             if v.state == "running" then
  39.                 m.setBackgroundColor(colors.green)
  40.             elseif v.state == "restarting" then
  41.                 m.setBackgroundColor(colors.yellow)
  42.             else
  43.                 m.setBackgroundColor(colors.red)
  44.             end
  45.             m.clearLine()
  46.             m.write(("%s %s"):format(k, v.message or ""))
  47.             by_ID[i] = k
  48.             i = i + 1
  49.         end
  50.     end
  51. end
  52.  
  53. local function touch_handler()
  54.     while true do
  55.         local event, peripheral_name, x, y = os.pullEvent "monitor_touch"
  56.         local name = by_ID[y]
  57.         if name then
  58.             send_command(name, "restart")
  59.         end
  60.     end
  61. end
  62.  
  63. local function split_at_spaces(s)
  64.     local t = {}
  65.     for i in string.gmatch(s, "%S+") do
  66.        table.insert(t, i)
  67.     end
  68.     return t
  69. end
  70.  
  71. local function command_reader()
  72.     local hist = {}
  73.     while true do
  74.         write "|> "
  75.         local text = read(nil, hist)
  76.         table.insert(hist, text)
  77.         local command = split_at_spaces(text)
  78.         send_command(command[2], command[1])
  79.     end
  80. end
  81.  
  82. parallel.waitForAll(displayer, touch_handler, command_reader)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement