Module: Hatetepe::Server

Includes:
Connection
Defined in:
lib/hatetepe/server.rb,
lib/hatetepe/server/pipeline.rb,
lib/hatetepe/server/rack_app.rb,
lib/hatetepe/server/keep_alive.rb

Defined Under Namespace

Classes: KeepAlive, Pipeline, RackApp

Constant Summary collapse

CONFIG_DEFAULTS =
{
  :timeout => 5.0,
  :app     => [ Pipeline, KeepAlive, RackApp ]
}

Instance Attribute Summary collapse

Attributes included from Connection

#processing_enabled

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Connection

#close_connection, #closed?, #closed_by_connect_timeout?, #closed_by_remote?, #closed_by_self?, #closed_by_timeout?, #comm_inactivity_timeout=, #connected?, #connection_completed, #pending_connect_timeout=, #remote_address, #remote_port, #sockaddr

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/hatetepe/server.rb', line 15

def config
  @config
end

#requestsObject (readonly)

Returns the value of attribute requests.



15
16
17
# File 'lib/hatetepe/server.rb', line 15

def requests
  @requests
end

Class Method Details

.start(config) ⇒ Object



23
24
25
# File 'lib/hatetepe/server.rb', line 23

def self.start(config)
  EM.start_server(config[:host], config[:port], self, config)
end

Instance Method Details

#initialize(config) ⇒ Object



28
29
30
# File 'lib/hatetepe/server.rb', line 28

def initialize(config)
  @config = CONFIG_DEFAULTS.merge(config).freeze
end

#post_initObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/hatetepe/server.rb', line 33

def post_init
  @parser, @builder = Hatetepe::Parser.new, Hatetepe::Builder.new
  @parser.on_request &method(:process_request)
  @builder.on_write  &method(:send_data)
  # @builder.on_write {|data| p "<--| #{data}" }

  @app = build_app(config[:app])

  self.comm_inactivity_timeout = config[:timeout]
end

#process_request(request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
57
58
59
60
# File 'lib/hatetepe/server.rb', line 54

def process_request(request)
  Fiber.new do
    @app.call(request) do |response|
      send_response(request, response)
    end
  end.resume
end

#receive_data(data) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/hatetepe/server.rb', line 45

def receive_data(data)
  # p "-->| #{data}"
  @parser << data
rescue Object => ex
  puts [ex.message, *ex.backtrace].join("\n\t")
  close_connection
end

#send_response(request, response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hatetepe/server.rb', line 63

def send_response(request, response)
  self.comm_inactivity_timeout = 0
  @builder.response(response.to_a)
  self.comm_inactivity_timeout = config[:timeout]

  if response.failure?
    request.fail(response)
  else
    request.succeed(response)
  end
end

#stopObject



81
82
# File 'lib/hatetepe/server.rb', line 81

def stop
end

#stop!Object



85
86
87
# File 'lib/hatetepe/server.rb', line 85

def stop!
  close_connection_after_writing
end

#unbind(reason) ⇒ Object



76
77
78
# File 'lib/hatetepe/server.rb', line 76

def unbind(reason)
  super
end