Class: LoadReporter

Inherits:
Object
  • Object
show all
Includes:
WEBrick
Defined in:
lib/load/load_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ LoadReporter

Returns a new instance of LoadReporter.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/load/load_reporter.rb', line 7

def initialize(params)
  puts "Initializing Load Reporter"
  @started = false
  @server_address = params[:server]
  @current_load = 0
  @out_file = File.new("out.txt", "w")
  port = params[:port] || 2233
  puts "Starting server: http://#{Socket.gethostname}:#{port}"
  server = HTTPServer.new(:Port=>2233,:DocumentRoot=>Dir::pwd )
  trap("INT") {
    puts "Server going down"
    server.shutdown
  }

  server.mount_proc '/' do |request, response|
    response.body = process_request(request)
  end
  server.start
  @started = true
end

Instance Attribute Details

#startedObject (readonly)

Returns the value of attribute started.



4
5
6
# File 'lib/load/load_reporter.rb', line 4

def started
  @started
end

Instance Method Details

#process_request(request) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/load/load_reporter.rb', line 29

def process_request(request)
response = "
    Current Load: #{@current_load}
    Query String: #{request.query_string}
  "

  request.query.collect { | key, value |
    #f.write("#{key}: #{value}\n")
    if (key == "load")
      @current_load = value.to_i
      response += "Current load changed to: #{@current_load}"
    end
    response += "#{key}: #{value}\n"
  }
  puts "RESPONSE: #{response}"
  return response
end