class RedfishTools::SseServer

Constants

HEADERS

Public Class Methods

new(source, address, port) click to toggle source
# File lib/redfish_tools/sse_server.rb, line 14
def initialize(source, address, port)
  @events = load_events(source)
  @server = TCPServer.open(address, port)
end

Public Instance Methods

start() click to toggle source
# File lib/redfish_tools/sse_server.rb, line 19
def start
  loop { Thread.start(@server.accept) { |client| handle_client(client) } }
end

Private Instance Methods

handle_client(socket) click to toggle source
# File lib/redfish_tools/sse_server.rb, line 29
def handle_client(socket)
  socket.print(HEADERS)
  id = 0
  loop do
    event = @events.sample
    make_events_unique(event["Events"])
    socket.print("id: #{id}\ndata: #{event.to_json}\n\n")
    sleep(rand(1..60))
    id += 1
  end
ensure
  socket.close
end
load_events(source) click to toggle source
# File lib/redfish_tools/sse_server.rb, line 25
def load_events(source)
  JSON.parse(File.read(source))
end
make_events_unique(events) click to toggle source
# File lib/redfish_tools/sse_server.rb, line 43
def make_events_unique(events)
  events.each do |event|
    event["EventTimestamp"] = Time.now.utc.to_s
    event["EventId"] = SecureRandom.uuid
  end
end