Class: Mongrel2::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mongrel2/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid, sub, pub, chroot, app) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
15
16
17
18
# File 'lib/mongrel2/connection.rb', line 10

def initialize(uuid, sub, pub, chroot, app)
  @uuid, @sub, @pub, @chroot, @app = uuid, sub, pub, chroot, app

  # Connect to receive requests
  @reqs = self.class.context.connect(ZMQ::PULL, sub, self)

  # Connect to send responses
  @resp = self.class.context.connect(ZMQ::PUB, pub, nil, :identity => uuid)
end

Instance Attribute Details

#chrootObject (readonly)

Returns the value of attribute chroot.



3
4
5
# File 'lib/mongrel2/connection.rb', line 3

def chroot
  @chroot
end

Class Method Details

.contextObject



6
7
8
# File 'lib/mongrel2/connection.rb', line 6

def self.context
   @context ||= EM::ZeroMQ::Context.new(1)
end

Instance Method Details

#closeObject



66
67
68
69
# File 'lib/mongrel2/connection.rb', line 66

def close
  # I think I should be able to just close the context
  self.class.context.close rescue nil
end

#on_readable(socket, messages) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/mongrel2/connection.rb', line 20

def on_readable(socket, messages)
  messages.each do |msg|
    req = msg.nil? ? nil : Request.parse(msg.copy_out_string, self)
    next if req.nil? || req.disconnect?
    process req
  end
end

#post_process(response, req) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/mongrel2/connection.rb', line 50

def post_process(response, req)
  status, headers, rack_response = *response
  # Status code -1 indicates that we're going to respond later (async).
  return if -1 == status

  body = ''
  rack_response.each { |b| body << b }
  reply req, body, status, headers
end

#pre_process(req) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/mongrel2/connection.rb', line 40

def pre_process(req)
  status, headers, rack_response = -1, {}, []

  catch(:async) do
    status, headers, rack_response = @app.call(req.env)
  end

  [status, headers, rack_response]
end

#process(req) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongrel2/connection.rb', line 28

def process(req)
  pre = Proc.new do
    method(:pre_process).call(req)
  end

  post = Proc.new do |resp|
    method(:post_process).call(resp, req)
  end

  EM.defer pre, post
end

#reply(req, body, status = 200, headers = {}) ⇒ Object



60
61
62
63
64
# File 'lib/mongrel2/connection.rb', line 60

def reply(req, body, status = 200, headers = {})
  resp = Response.new(@resp)
  resp.send_http req, body, status, headers
  resp.close req if req.close?
end