Class: MaZMQ::ConnectionHandler

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/ma-zmq/connection_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(socket_handler) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



3
4
5
6
7
8
9
10
# File 'lib/ma-zmq/connection_handler.rb', line 3

def initialize(socket_handler)
  @socket_handler = socket_handler
  @socket_type = socket_handler.socket_type

  @on_read_lambda = lambda {|m|}
  @on_write_lambda = lambda {|m|}
  @on_timeout_lambda = lambda {}
end

Instance Method Details

#notify_readableObject



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

def notify_readable
  #if @socket_handler.socket_type == ZMQ::REP
  #  if @socket_handler.state == :idle
  #    msg = try_read
  #    if msg
  #      @on_read_lambda.call(msg)
  #    end
  #  end
  #end
end

#notify_writableObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ma-zmq/connection_handler.rb', line 35

def notify_writable
  case @socket_type
    when ZMQ::REP
      if @socket_handler.state == :idle
        msg = @socket_handler.recv_string
        if msg and not msg.empty?
          @on_read_lambda.call(msg)
        end
      end
    when ZMQ::REQ
      if @socket_handler.state == :sending
        msg = @socket_handler.recv_string
      end
      case @socket_handler.state
        when :idle
          if msg and not msg.empty?
            @on_read_lambda.call(msg)
          end
        when :timeout
          @on_timeout_lambda.call #(@socket_handler.identity)
          puts "SocketHandler: #{@socket_handler.identity} timeout!"
          self.detach
      end
    when ZMQ::PULL
      msg = @socket_handler.recv_string
      case @socket_handler.state
        when :idle
          if msg and not msg.empty?
            @on_read_lambda.call(msg)
          end
        when :timeout
          @on_timeout_lambda.call #(@socket_handler.identity)
          puts "SocketHandler: #{@socket_handler.identity} timeout!"
          self.detach
      end
  end
end

#on_read(block) ⇒ Object



12
13
14
# File 'lib/ma-zmq/connection_handler.rb', line 12

def on_read(block)
  @on_read_lambda = block
end

#on_timeout(block) ⇒ Object



20
21
22
# File 'lib/ma-zmq/connection_handler.rb', line 20

def on_timeout(block)
  @on_timeout_lambda = block
end

#on_write(block) ⇒ Object



16
17
18
# File 'lib/ma-zmq/connection_handler.rb', line 16

def on_write(block)
  @on_write_lambda = block
end