Sha256: 26bb3414beb8e6b5bb1f035cd3d09dd3946199b6526d1113271fc2b2a14b15d5

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'miu'
require 'ffi-rzmq'

module Miu
  class Socket
    attr_reader :host, :port
    attr_reader :context, :socket

    def initialize(socket_type, options = {})
      @host = options[:host] || '127.0.0.1'
      @port = options[:port]

      if options[:context]
        @context = options[:context]
        @terminate_context = false
      else
        @context = ZMQ::Context.new(options[:io_threads] || 1)
        @terminate_context = true
      end

      @socket = @context.socket socket_type
    end

    def bind
      rc = @socket.bind "tcp://#{@host}:#{@port}"
      error_check rc
      self
    end

    def connect
      rc = @socket.connect "tcp://#{@host}:#{@port}"
      error_check rc
      self
    end

    def forward(forwarder)
      loop do
        message = ZMQ::Message.new
        @socket.recvmsg message
        more = @socket.more_parts?
        forwarder.socket.sendmsg message, (more ? ZMQ::SNDMORE : 0)
        break unless more
      end
    end

    def close
      @socket.close
      @context.terminate if @terminate_context
    end

    protected

    def error_check(rc, source = nil)
      unless ZMQ::Util.resultcode_ok? rc
        raise ZMQ::ZeroMQError.new source, rc, ZMQ::Util.errno, ZMQ::Util.error_string
      end
      true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
miu-0.0.6 lib/miu/socket.rb