Sha256: 389221f539bb9b494ba8a0bbc892555c391852adeda6a3812ac4cbe4fa672fa9

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

require 'm2r'
require 'ostruct'

module M2R
  # {Connection} factory so that every thread can use it generate its own
  # {Connection} for communication with Mongrel2.
  #
  # @api public
  class ConnectionFactory
    class Options < Struct.new(:sender_id, :recv_addr, :send_addr)
      # @param [String, nil] sender_id {ZMQ::IDENTITY} option for response socket
      # @param [String] recv_addr ZMQ connection address. This is the
      #   send_spec option from Handler configuration in mongrel2.conf
      # @param [String] send_addr ZMQ connection address. This is the
      #   recv_spec option from Handler configuration in mongrel2.conf
      def initialize(sender_id, recv_addr, send_addr)
        super
      end
    end

    # @param [Options] options ZMQ connections options
    # @param [ZMQ::Context] context Context for creating new ZMQ sockets
    def initialize(options, context = M2R.zmq_context)
      @options = options
      @context = context
    end

    # Builds new Connection which can be used to receive
    # Mongrel2 requests and send responses.
    #
    # @return [Connection]
    def connection
      request_socket = @context.socket(ZMQ::PULL)
      request_socket.connect(@options.recv_addr)

      response_socket = @context.socket(ZMQ::PUB)
      response_socket.connect(@options.send_addr)
      response_socket.setsockopt(ZMQ::IDENTITY, @options.sender_id) if @options.sender_id

      Connection.new(request_socket, response_socket)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
m2r-2.1.0.pre lib/m2r/connection_factory.rb
m2r-2.0.2 lib/m2r/connection_factory.rb
m2r-2.0.1 lib/m2r/connection_factory.rb
m2r-2.0.0 lib/m2r/connection_factory.rb