Class: C3s::Republisher

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

Instance Attribute Summary

Attributes inherited from Component

#config, #model

Instance Method Summary collapse

Methods inherited from Component

#add_callbacks, #connect, #handle_disco_info, #handle_set, #initialize, #publish_to_pubsub, #send!, #send_error, #send_ok

Constructor Details

This class inherits a constructor from C3s::Component

Instance Method Details

#build_object(klass, item) ⇒ Object

Builds an object information based on pubsub item content. Each field of the published item is maped to an object attribute.

klass
Class

the object class

item
REXML::Element

the published item



84
85
86
87
88
89
90
91
92
93
# File 'lib/republisher.rb', line 84

def build_object(klass, item)
  return nil if item.nil?
  @object = klass.new
  klass.attributes.each do |att|
    @object.send("#{att}=", item.first_element(att).text)
  end
  @object
rescue Exception => e
  $LOG.error "Error on build_object(): #{e.inspect} #{e.backtrace.join("\n")}"
end

#handle_iq(iq) ⇒ Object

Block the set iqs because this provider doesn’t receive iq’s directly to set the information.



18
19
20
21
22
23
24
25
26
# File 'lib/republisher.rb', line 18

def handle_iq(iq)
  if iq.type == :set
    puts "WARNING: IQ of type 'set' received from #{iq.from}!"
    $LOG.warn("IQ of type 'set' received from #{iq.from}")
    send_error(iq, ['unexpected-request', nil])
    return
  end
  super
end

#handle_msg(msg) ⇒ Object

Handles messages. This can be subscribe or unsubscribe messages and pubsub events (most likely).



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/republisher.rb', line 45

def handle_msg(msg)
  if msg.body.eql?("sub")
    $LOG.info "Subscribing nodes: #{@subscribing_nodes.join(", ")}"
    subscribe_nodes()
  elsif msg.body.eql?("unsub")
    @subscriber.unsubscribe_all
  else
    event = msg.first_element("event")
    if event.kind_of?(Jabber::PubSub::Event)
      event.each_element("//item/") do |item|
        $LOG.debug("Received item from pubsub")
        begin
          c3snode = C3s::Node.new(item.attributes['node'])
          next unless c3snode
          $LOG.debug("Node built: #{c3snode.to_s}")
          c3snode.provider = config['name'] # TODO - assuming that provider_name.eql?(pubsub_root_node)
          $LOG.debug("Call republish() from child")
          
          
          Thread.abort_on_exception = true
          t = Thread.new do
            republish(c3snode, item)
          end
          t.join
        rescue Exception => e
          $LOG.error "Error on published item: #{e}"
        end
      end
    end
  end
rescue Exception => e
  $LOG.error "Error on handle_msg(): #{e.inspect} #{e.backtrace.join("\n")}"
end

#start(subscribing_nodes) ⇒ Object

Starts the component and sets the subscribing nodes.

subscribing_nodes
String or Array

the subscribing node(s)



9
10
11
12
13
# File 'lib/republisher.rb', line 9

def start(subscribing_nodes)
  @subscribing_nodes = subscribing_nodes.to_a
  @subscriber = C3s::Subscriber.new(self, config['pubsub'])
  super()
end

#subscribe_nodesObject

Subscribes to all nodes



31
32
33
34
35
36
37
38
39
40
# File 'lib/republisher.rb', line 31

def subscribe_nodes
  @subscribing_nodes.each do |node|
    begin
      @subscriber.subscribe_collection(node)
    rescue Exception => e
      $LOG.error "Could not subscribe to node #{node}"
      next
    end
  end
end