class MaZMQ::SocketHandler

Public Class Methods

new() click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 5
def initialize
  @socket = MaZMQ::context.socket(@socket_type)
  @socket.setsockopt(ZMQ::LINGER, 0)
  @addresses = []

  if EM.reactor_running?
    build_connection
  else
    @connection = false
  end
  
  @state = :unavailable
end

Protected Class Methods

valid_address(protocol, address, port=nil) click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 96
def self.valid_address(protocol, address, port=nil)
  case protocol
    when :tcp
      if port.is_a? Numeric
        return "#{protocol}://#{address}:#{port.to_s}"
      else
        return false
      end
    when :ipc
      # Chequear socket file
      if port.is_a? NilClass
        return "#{protocol}://#{address}"
      else
        return false
      end
    when :inproc
      if port.is_a? NilClass
        return "#{protocol}://#{address}"
      else
        return false
      end
  end
end
valid_protocol?(protocol) click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 120
def self.valid_protocol?(protocol)
  @@protocols.include? protocol
end

Public Instance Methods

addresses() click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 56
def addresses
  @addresses
end
bind(protocol, address, port=nil) click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 34
def bind(protocol, address, port=nil)
  # check once binded should not bind anymore
  zmq_address = MaZMQ::SocketHandler.valid_address(protocol, address, port)
  return false if not zmq_address.is_a? String

  @socket.bind(zmq_address)

  @addresses << zmq_address

  if @state == :unavailable
    @state = :idle
  end
  @state
end
close() click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 49
def close
  @socket.close
  #if use_eventmachine
  #  @connection.detach
  #end
end
connect(protocol, address, port=nil) click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 19
def connect(protocol, address, port=nil)
  # check multiple connects for ZMQ::REQ should RoundRobin
  zmq_address = MaZMQ::SocketHandler.valid_address(protocol, address, port)
  return false if not zmq_address.is_a? String

  @socket.connect(zmq_address)

  @addresses << zmq_address
  
  if @state == :unavailable
    @state = :idle
  end
  @state
end
identity() click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 85
def identity
  arr = []
  @socket.getsockopt(ZMQ::IDENTITY, arr)
  arr[0].to_sym rescue nil
end
identity=(identity) click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 91
def identity=(identity)
  @socket.setsockopt(ZMQ::IDENTITY, identity.to_s)
end
on_read(&block) click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 75
def on_read(&block)
  return false if not @connection or block.arity != 1
  @connection.on_read(block)
end
on_write(&block) click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 80
def on_write(&block)
  return false if not @connection or block.arity != 1
  @connection.on_write(block)
end
recv_string(flags=nil) click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 64
def recv_string(flags=nil)
  msg = ''
  flags ||= ZMQ::NOBLOCK
  @socket.recv_string(msg, flags)
  return msg
end
send_string(msg) click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 60
def send_string(msg)
  @socket.send_string(msg)
end
socket_type() click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 71
def socket_type
  @socket_type
end

Private Instance Methods

build_connection() click to toggle source
# File lib/ma-zmq/socket_handler.rb, line 125
def build_connection
  fd = []
  @socket.getsockopt(ZMQ::FD, fd)

  return nil if not ZMQ::Util.resultcode_ok? fd[0]

  @connection = EM.watch(fd[0], MaZMQ::ConnectionHandler, self)
  #@connection.notify_readable = true
  @connection.notify_writable = true
  @connection
end