Class: Spectacular::Server::Events

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/spectacular/server.rb

Instance Method Summary collapse

Instance Method Details

#do_GET(request, response) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/spectacular/server.rb', line 19

def do_GET request, response
  response.status = 200
  response['Content-Type'] = 'text/event-stream'
  rd, rw = IO.pipe

  response.body = rd

  Thread.new {
    begin
      ip = ENV['ROUTER'] || '10.0.1.1'
      dev = Spectacular::Device.new ip

      rw.write "event: interfaces\n"
      rw.write "data: #{JSON.dump(dev.interfaces)}\n\n"
      rw.flush

      dev.monitor do |row|
        data = {
          :interface => row[1].value.to_s,
          :in_delta  => row[2].value,
          :out_delta => row[3].value,
        }

        rw.write "event: update\n"
        rw.write "data: #{JSON.dump(data)}\n\n"
        rw.flush
      end
    ensure
      rw.close
    end
  }
end