Class: Pronghorn::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/pronghorn/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
# File 'lib/pronghorn/server.rb', line 10

def initialize(app, &block)
  @app = app
  @app = Rack::Builder.new(&block).to_app if block
  @connections = []
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



4
5
6
# File 'lib/pronghorn/server.rb', line 4

def app
  @app
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/pronghorn/server.rb', line 6

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/pronghorn/server.rb', line 8

def port
  @port
end

Instance Method Details

#finish_connection(connection) ⇒ Object



49
50
51
# File 'lib/pronghorn/server.rb', line 49

def finish_connection(connection)
  @connections.delete(connection)
end

#start(host, port) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pronghorn/server.rb', line 16

def start(host, port)
  @host = host
  @port = port
  starter = proc do
    @signature = EventMachine.start_server @host, @port, Connection, &method(:initialize_connection)
    @running = true
  end

  Signal.trap("INT")  { stop }
  Signal.trap("TERM") { stop }

  puts "Proghorn #{::Pronghorn::VERSION} starting..."
  puts "* Listening on http://#{@host}:#{@port} *"

  EventMachine.error_handler{ |error|
    puts error.message
  }

  if EventMachine.reactor_running?
    starter.call
  else
    EventMachine.run(&starter)
  end
end

#stopObject



41
42
43
44
45
46
47
# File 'lib/pronghorn/server.rb', line 41

def stop
  puts "* Stopping server *"
  return unless @running
  @running = false
  EventMachine.stop_server(@signature)
  EventMachine.stop if EventMachine.reactor_running?
end