Class: TopicManager

Inherits:
Object
  • Object
show all
Defined in:
lib/topic_manager.rb

Overview

topic - non persistent, sent to all interested parties

Instance Method Summary collapse

Constructor Details

#initializeTopicManager

Returns a new instance of TopicManager.



4
5
6
# File 'lib/topic_manager.rb', line 4

def initialize
  @topics = Hash.new { Array.new }
end

Instance Method Details

#disconnect(user) ⇒ Object



16
17
18
19
20
# File 'lib/topic_manager.rb', line 16

def disconnect(user)
  @topics.each do |dest, queue|
    queue.delete_if { |qu| qu == user }
  end
end

#sendmsg(msg) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/topic_manager.rb', line 22

def sendmsg(msg)
  msg.command = "MESSAGE"
  topic = msg.headers['destination']
  payload = msg.to_s
  @topics[topic].each do |user|
    user.send_data(payload)
  end
end

#subscribe(topic, user) ⇒ Object



8
9
10
# File 'lib/topic_manager.rb', line 8

def subscribe(topic, user)
  @topics[topic] += [user]
end

#unsubscribe(topic, user) ⇒ Object



12
13
14
# File 'lib/topic_manager.rb', line 12

def unsubscribe(topic, user)
  @topics[topic].delete(user) 
end