File: topic_manager.rb

package info (click to toggle)
stompserver 0.9.9gem-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 372 kB
  • sloc: ruby: 2,801; sh: 171; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 758 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# topic - non persistent, sent to all interested parties

module StompServer
class TopicManager
  attr_accessor :frame_index
  def initialize
    @frame_index =0
    @topics = Hash.new { Array.new }
    puts "TopicManager initialized"
  end  

  def index
    @frame_index
  end

  def next_index
    @frame_index += 1
  end

  def subscribe(topic, user)
    @topics[topic] += [user]
  end
  
  def unsubscribe(topic, user)
    @topics[topic].delete(user) 
  end
  
  def disconnect(user)
    @topics.each do |dest, queue|
      queue.delete_if { |qu| qu == user }
    end
  end
  
  def sendmsg(msg)
    msg.command = "MESSAGE"
    topic = msg.headers['destination']
    @topics[topic].each do |user|
      user.stomp_send_data(msg)
    end
  end  
end
end