Class: Vines::Config::PubSub

Inherits:
Object
  • Object
show all
Defined in:
lib/vines/config/pubsub.rb

Overview

Provides the configuration DSL to conf/config.rb for pubsub subdomains and exposes the storage and notification systems that the pubsub stanzas need to process. This class hides the complexity of determining pubsub behavior in a standalone vs. clustered chat server environment from the stanzas.

Instance Method Summary collapse

Constructor Details

#initialize(config, name) ⇒ PubSub

Returns a new instance of PubSub.



10
11
12
13
# File 'lib/vines/config/pubsub.rb', line 10

def initialize(config, name)
  @config, @name = config, name
  @nodes = {}
end

Instance Method Details

#add_node(id) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/vines/config/pubsub.rb', line 15

def add_node(id)
  if @config.cluster?
    @config.cluster.add_pubsub_node(@name, id)
  else
    @nodes[id] ||= Set.new
  end
end

#delete_node(id) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/vines/config/pubsub.rb', line 23

def delete_node(id)
  if @config.cluster?
    @config.cluster.delete_pubsub_node(@name, id)
  else
    @nodes.delete(id)
  end
end

#node?(node) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/vines/config/pubsub.rb', line 60

def node?(node)
  if @config.cluster?
    @config.cluster.pubsub_node?(@name, node)
  else
    @nodes.key?(node)
  end
end

#publish(node, stanza) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vines/config/pubsub.rb', line 77

def publish(node, stanza)
  stanza['id'] = Kit.uuid
  stanza['from'] = @name

  local, remote = subscribers(node).partition {|jid| @config.local_jid?(jid) }

  local.flat_map do |jid|
    @config.router.connected_resources(jid, @name)
  end.each do |recipient|
    stanza['to'] = recipient.user.jid.to_s
    recipient.write(stanza)
  end

  remote.each do |jid|
    el = stanza.clone
    el['to'] = jid.to_s
    @config.router.route(el) rescue nil # ignore RemoteServerNotFound
  end
end

#subscribe(node, jid) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/vines/config/pubsub.rb', line 31

def subscribe(node, jid)
  return unless node?(node) && @config.allowed?(jid, @name)
  if @config.cluster?
    @config.cluster.subscribe_pubsub(@name, node, jid)
  else
    @nodes[node] << JID.new(jid)
  end
end

#subscribed?(node, jid) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
# File 'lib/vines/config/pubsub.rb', line 68

def subscribed?(node, jid)
  return false unless node?(node)
  if @config.cluster?
    @config.cluster.pubsub_subscribed?(@name, node, jid)
  else
    @nodes[node].include?(JID.new(jid))
  end
end

#unsubscribe(node, jid) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/vines/config/pubsub.rb', line 40

def unsubscribe(node, jid)
  return unless node?(node)
  if @config.cluster?
    @config.cluster.unsubscribe_pubsub(@name, node, jid)
  else
    @nodes[node].delete(JID.new(jid))
    delete_node(node) if subscribers(node).empty?
  end
end

#unsubscribe_all(jid) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/vines/config/pubsub.rb', line 50

def unsubscribe_all(jid)
  if @config.cluster?
    @config.cluster.unsubscribe_all_pubsub(@name, jid)
  else
    @nodes.keys.each do |node|
      unsubscribe(node, jid)
    end
  end
end