Class: OMQ::Routing::Sub

Inherits:
Object
  • Object
show all
Defined in:
lib/omq/routing/sub.rb

Overview

SUB socket routing: subscription-based receive from PUB peers.

Sends SUBSCRIBE/CANCEL commands to connected PUB peers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ Sub

Returns a new instance of Sub.

Parameters:



18
19
20
21
22
23
24
# File 'lib/omq/routing/sub.rb', line 18

def initialize(engine)
  @engine        = engine
  @connections   = Set.new
  @recv_queue    = Routing.build_queue(engine.options.recv_hwm, :block)
  @subscriptions = Set.new
  @tasks         = []
end

Instance Attribute Details

#recv_queueAsync::LimitedQueue (readonly)

Returns:

  • (Async::LimitedQueue)


13
14
15
# File 'lib/omq/routing/sub.rb', line 13

def recv_queue
  @recv_queue
end

Instance Method Details

#connection_added(connection) ⇒ Object

Parameters:

  • connection (Connection)


47
48
49
50
51
52
53
54
55
56
# File 'lib/omq/routing/sub.rb', line 47

def connection_added(connection)
  @connections << connection

  @subscriptions.each do |prefix|
    connection.send_command(Protocol::ZMTP::Codec::Command.subscribe(prefix))
  end

  task = @engine.start_recv_pump(connection, @recv_queue)
  @tasks << task if task
end

#connection_removed(connection) ⇒ Object

Parameters:

  • connection (Connection)


61
62
63
# File 'lib/omq/routing/sub.rb', line 61

def connection_removed(connection)
  @connections.delete(connection)
end

#dequeue_recvArray<String>?

Dequeues the next received message. Blocks until one is available.

Returns:

  • (Array<String>, nil)


31
32
33
# File 'lib/omq/routing/sub.rb', line 31

def dequeue_recv
  @recv_queue.dequeue
end

#enqueue(_parts) ⇒ Object

SUB is read-only.



68
69
70
# File 'lib/omq/routing/sub.rb', line 68

def enqueue(_parts)
  raise "SUB sockets cannot send"
end

#stopvoid

This method returns an undefined value.

Stops all background tasks.



101
102
103
104
# File 'lib/omq/routing/sub.rb', line 101

def stop
  @tasks.each(&:stop)
  @tasks.clear
end

#subscribe(prefix) ⇒ Object

Subscribes to a topic prefix.

Parameters:

  • prefix (String)


77
78
79
80
81
82
# File 'lib/omq/routing/sub.rb', line 77

def subscribe(prefix)
  @subscriptions << prefix
  @connections.each do |conn|
    conn.send_command(Protocol::ZMTP::Codec::Command.subscribe(prefix))
  end
end

#unblock_recvvoid

This method returns an undefined value.

Wakes a blocked #dequeue_recv with a nil sentinel.



40
41
42
# File 'lib/omq/routing/sub.rb', line 40

def unblock_recv
  @recv_queue.enqueue(nil)
end

#unsubscribe(prefix) ⇒ Object

Unsubscribes from a topic prefix.

Parameters:

  • prefix (String)


89
90
91
92
93
94
# File 'lib/omq/routing/sub.rb', line 89

def unsubscribe(prefix)
  @subscriptions.delete(prefix)
  @connections.each do |conn|
    conn.send_command(Protocol::ZMTP::Codec::Command.cancel(prefix))
  end
end