Class: MaZMQ::SocketHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ma-zmq/socket_handler.rb

Direct Known Subclasses

Pull, Push, Reply, Request

Constant Summary collapse

@@protocols =

, :pgm]

[:tcp, :inproc, :ipc]

Instance Method Summary collapse

Constructor Details

#initializeSocketHandler

Returns a new instance of SocketHandler.



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Method Details

#addressesObject



56
57
58
# File 'lib/ma-zmq/socket_handler.rb', line 56

def addresses
  @addresses
end

#bind(protocol, address, port = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

#closeObject



49
50
51
52
53
54
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

#identityObject



85
86
87
88
89
# 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) ⇒ Object



91
92
93
# File 'lib/ma-zmq/socket_handler.rb', line 91

def identity=(identity)
  @socket.setsockopt(ZMQ::IDENTITY, identity.to_s)
end

#on_read(&block) ⇒ Object



75
76
77
78
# 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) ⇒ Object



80
81
82
83
# 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) ⇒ Object



64
65
66
67
68
69
# 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) ⇒ Object



60
61
62
# File 'lib/ma-zmq/socket_handler.rb', line 60

def send_string(msg)
  @socket.send_string(msg)
end

#socket_typeObject



71
72
73
# File 'lib/ma-zmq/socket_handler.rb', line 71

def socket_type
  @socket_type
end