Class: MaZMQ::Request

Inherits:
SocketHandler show all
Defined in:
lib/ma-zmq/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SocketHandler

#addresses, #bind, #close, #connect, #identity, #identity=, #on_read, #on_write, #socket_type

Constructor Details

#initializeRequest

Returns a new instance of Request.



5
6
7
8
9
10
11
12
13
# File 'lib/ma-zmq/request.rb', line 5

def initialize
  @socket_type = ZMQ::REQ

  @last_try = nil

  @timeout = false
  # @cooldown
  super
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/ma-zmq/request.rb', line 3

def state
  @state
end

Instance Method Details

#on_timeout(&block) ⇒ Object



61
62
63
64
# File 'lib/ma-zmq/request.rb', line 61

def on_timeout(&block)
  return false if not @connection or block.arity != -1
  @connection.on_timeout(block)
end

#reconnectObject



15
16
17
18
19
20
21
# File 'lib/ma-zmq/request.rb', line 15

def reconnect
  close
  @socket = MaZMQ::context.socket(@socket_type)
  @addresses.each do |zmq_address|
    @socket.connect zmq_address
  end
end

#recv_stringObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ma-zmq/request.rb', line 34

def recv_string
  case @state
    when :idle
      return false
    when :sending
      @last_try ||= Time.now if @timeout

      msg = super

      if msg.empty?
        if @timeout and (Time.now - @last_try) > @timeout
          @state = :timeout
        end
      else
        @last_try = nil if @timeout
        @state = :idle
      end
      return msg
    when :timeout
      return false
  end
end

#send_string(msg) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/ma-zmq/request.rb', line 23

def send_string(msg)
  case @state
    when :idle
      super(msg)
      @state = :sending
      return @state
    else
      return false
  end
end

#timeout(secs) ⇒ Object



57
58
59
# File 'lib/ma-zmq/request.rb', line 57

def timeout(secs)
  @timeout = secs
end