Sha256: f3140d0f5aec757a563c5cc1b71391bde42747a5bb74e70eb875f97f15a6538c

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

module Mongrel2
  class Connection
    attr_reader :chroot
    @context = nil

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

    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

    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

    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

    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

    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

    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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
em-rack-mongrel2-0.2.3 lib/mongrel2/connection.rb
em-rack-mongrel2-0.2.2 lib/mongrel2/connection.rb
em-rack-mongrel2-0.2.1 lib/mongrel2/connection.rb
em-rack-mongrel2-0.2.0 lib/mongrel2/connection.rb