Class: ZMQ::Device::ZSocket

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ctx = nil, conf = {}) ⇒ ZSocket

Returns a new instance of ZSocket.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/zdevice.rb', line 80

def initialize(name, ctx = nil, conf = {})
  raise 'invalid name' if name == 'type'
  raise 'missing type' if !conf.key? :type

  @name = name
  conf[:option] ||= {}
  @type = case conf.delete(:type).downcase
    when :pub then ZMQ::PUB
    when :sub then ZMQ::SUB
    else 1
  end

  # if no filter is specified, then accept all messages by default
  if @type == ZMQ::SUB
    conf[:option][:subscribe] = '' if !conf[:option][:subscribe]
  end

  @socket = ctx.socket @type
  (conf[:bind]    || []).each { |addr| @socket.bind addr }
  (conf[:connect] || []).each { |addr| @socket.connect(addr) }

  conf[:option].each do |k, v|
    flag = case k
    when :subscribe then ZMQ::SUBSCRIBE
    when :hwm       then ZMQ::HWM
    when :swap      then ZMQ::SWAP
    end

    [v].flatten.map {|val| @socket.setsockopt(flag, val) }
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



79
80
81
# File 'lib/zdevice.rb', line 79

def name
  @name
end

#socketObject (readonly)

Returns the value of attribute socket.



79
80
81
# File 'lib/zdevice.rb', line 79

def socket
  @socket
end

#typeObject (readonly)

Returns the value of attribute type.



79
80
81
# File 'lib/zdevice.rb', line 79

def type
  @type
end

Instance Method Details

#closeObject



112
113
114
# File 'lib/zdevice.rb', line 112

def close
  @socket.close
end