Sha256: 399df5ea1c412b2dd4ae53b54e1c79d3d5145b464481f0f8ad8914b08ca77cdf

Contents?: true

Size: 856 Bytes

Versions: 1

Compression:

Stored size: 856 Bytes

Contents

require 'ffi-rzmq'
require 'mongrel2/request'
require 'mongrel2/response'

module Mongrel2
  class Connection
    CTX = ZMQ::Context.new(1)

    def initialize(uuid, sub, pub, block = true)
      @uuid, @sub, @pub, @block = uuid, sub, pub, block

      # Connect to receive requests
      @reqs = CTX.socket(ZMQ::PULL)
      @reqs.connect(sub)

      # Connect to send responses
      @resp = CTX.socket(ZMQ::PUB)
      @resp.connect(pub)
      @resp.setsockopt(ZMQ::IDENTITY, uuid)
    end

    def recv
      msg = @reqs.recv_string(@block ? 0 : ZMQ::NOBLOCK)
      msg.nil? ? nil : Request.parse(msg)
    end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-mongrel2-0.2.1 lib/mongrel2/connection.rb