Class: EventMachine::Protocols::Zmq2::Sub

Inherits:
Socket
  • Object
show all
Defined in:
lib/em/protocols/zmq2/pub_sub.rb

Overview

ZMQ socket which acts as SUB subscriptions are done by subscribe method

Defined Under Namespace

Classes: DefaultAction

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Sub

Returns a new instance of Sub.



19
20
21
22
23
24
# File 'lib/em/protocols/zmq2/pub_sub.rb', line 19

def initialize(opts = {})
  super
  @subscriptions = {}
  @default_action = DefaultAction.new(self)
  subscribe_many [*opts[:subscribe]]
end

Instance Method Details

#receive_message(message) ⇒ Object

Raises:

  • (NoMethodError)


47
48
49
# File 'lib/em/protocols/zmq2/pub_sub.rb', line 47

def receive_message(message)
  raise NoMethodError
end

#receive_message_and_peer(message, peer) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/em/protocols/zmq2/pub_sub.rb', line 34

def receive_message_and_peer(message, peer)
  for sub, callback in @subscriptions
    matched = if String === sub
                message.first.start_with?(sub)
              elsif sub.respond_to?(:call)
                sub.call(message.first)
              else
                sub === message.first  # Regexp and anything you want
              end
    callback.call(message) if matched
  end
end

#subscribe(s, cb = nil, &block) ⇒ Object



30
31
32
# File 'lib/em/protocols/zmq2/pub_sub.rb', line 30

def subscribe(s, cb = nil, &block)
  @subscriptions[s] = cb || block || @default_action
end

#subscribe_many(subscriptions) ⇒ Object



26
27
28
# File 'lib/em/protocols/zmq2/pub_sub.rb', line 26

def subscribe_many(subscriptions)
  subscriptions.each{|sub| subscribe *sub}  if subscriptions
end