class Sponges::Listener

Constants

CRLF

Attributes

supervisor[R]

Public Class Methods

new(supervisor) click to toggle source
# File lib/sponges/listener.rb, line 7
def initialize(supervisor)
  @supervisor = supervisor
end

Public Instance Methods

call() click to toggle source
# File lib/sponges/listener.rb, line 11
def call
  Socket.tcp_server_loop("0.0.0.0", port) {|c| handle_connection c }
end

Private Instance Methods

handle_connection(connection) click to toggle source
# File lib/sponges/listener.rb, line 21
def handle_connection(connection)
  response = Response.new(supervisor).to_json
  connection.write headers(response)
  connection.write response
  connection.close_write
  connection.close_read
rescue SystemCallError
  # Resist to system errors when closing or writing to a socket that is not
  # opened.
end
headers(response) click to toggle source
# File lib/sponges/listener.rb, line 32
def headers(response)
  [
    "HTTP/1.1 200 OK",
    "Date: #{Time.now.utc}",
    "Status: OK",
    "Server: Sponges #{Sponges::VERSION} #{supervisor.name} edition",
    "Content-Type: application/json; charset=utf-8",
    "Content-Length: #{response.length}",
    CRLF
  ].join(CRLF)
end
port() click to toggle source
# File lib/sponges/listener.rb, line 17
def port
  Sponges::Configuration.port
end