c3s 0.4.2 → 0.4.3a

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -12,22 +12,4 @@ lib/pubsub/nodetracker.rb
12
12
  lib/pubsub/publisher.rb
13
13
  lib/pubsub/subscriber.rb
14
14
  lib/republisher.rb
15
- pkg/c3s-0.4.1.gem
16
- pkg/c3s-0.4.1.tar.gz
17
- pkg/c3s-0.4.1/Manifest
18
- pkg/c3s-0.4.1/README.rdoc
19
- pkg/c3s-0.4.1/Rakefile
20
- pkg/c3s-0.4.1/c3s.gemspec
21
- pkg/c3s-0.4.1/lib/c3s.rb
22
- pkg/c3s-0.4.1/lib/c3s_logger.rb
23
- pkg/c3s-0.4.1/lib/component.rb
24
- pkg/c3s-0.4.1/lib/component_connection.rb
25
- pkg/c3s-0.4.1/lib/configreader.rb
26
- pkg/c3s-0.4.1/lib/databaseadapter.rb
27
- pkg/c3s-0.4.1/lib/model.rb
28
- pkg/c3s-0.4.1/lib/pubsub/node.rb
29
- pkg/c3s-0.4.1/lib/pubsub/nodetracker.rb
30
- pkg/c3s-0.4.1/lib/pubsub/publisher.rb
31
- pkg/c3s-0.4.1/lib/pubsub/subscriber.rb
32
- pkg/c3s-0.4.1/lib/republisher.rb
33
15
  Manifest
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('c3s', '0.4.2') do |p|
5
+ Echoe.new('c3s', '0.4.3a') do |p|
6
6
  p.description = "C3s library gem."
7
7
  p.url = "http://github.com/rikas/c3s"
8
8
  p.author = "Ricardo Otero"
data/c3s.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{c3s}
5
- s.version = "0.4.2"
5
+ s.version = "0.4.3a"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Ricardo Otero"]
9
- s.date = %q{2010-09-27}
9
+ s.date = %q{2010-11-10}
10
10
  s.description = %q{C3s library gem.}
11
11
  s.email = %q{oterosantos@gmail.com}
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/c3s.rb", "lib/c3s_logger.rb", "lib/component.rb", "lib/component_connection.rb", "lib/configreader.rb", "lib/databaseadapter.rb", "lib/model.rb", "lib/pubsub/node.rb", "lib/pubsub/nodetracker.rb", "lib/pubsub/publisher.rb", "lib/pubsub/subscriber.rb", "lib/republisher.rb"]
data/lib/c3s.rb CHANGED
@@ -27,7 +27,7 @@ rescue Exception => e
27
27
  end
28
28
 
29
29
  module C3s
30
- VERSION = "0.3.3"
30
+ VERSION = "0.4.3a"
31
31
 
32
32
  def self.included(base)
33
33
  base.extend ClassMethods
data/lib/component.rb CHANGED
@@ -125,12 +125,17 @@ module C3s
125
125
  end
126
126
 
127
127
  changed = true
128
- object = self.model.find_by_jid(data[:jid]) || self.model.new
128
+ if self.is_a?(ActiveRecord::Base)
129
+ object = self.model.find_by_jid(data[:jid]) || self.model.new
129
130
 
130
- $LOG.info "Saving #{config['name']} context for #{data[:jid]} #{data.inspect}"
131
- object.update_attributes(data)
132
- changed = object.changed?
133
- object.save!
131
+ $LOG.info "Saving #{config['name']} context for #{data[:jid]} #{data.inspect}"
132
+ object.update_attributes(data)
133
+ changed = object.changed?
134
+ object.save!
135
+ else
136
+ object = self.model.new
137
+ object.update_attributes(data)
138
+ end
134
139
 
135
140
  if object.errors.count == 0
136
141
  $LOG.info "Publishing data to node #{config['name']}:#{data[:jid]} (data changed: #{changed})"
data/lib/model.rb CHANGED
@@ -1,10 +1,44 @@
1
1
  require 'active_record'
2
2
 
3
3
  module C3sModel
4
+ def self.included(base)
5
+ if !base.ancestors.include?(ActiveRecord::Base)
6
+ base.class_eval do
7
+ cattr_accessor :attrs
8
+ def attributes
9
+ attr_hash = {}
10
+ attrs.each do |a|
11
+ attr_hash[a.to_s] = self.send(a.to_s) if self.respond_to?(a.to_s)
12
+ end
13
+ attr_hash
14
+ end
15
+
16
+ def attributes=(att, guard_protected_attributes)
17
+ att.each do |key, val|
18
+ self.attrs << key.to_s
19
+ self.send("#{key.to_s}=", val) if self.respond_to?(key.to_s)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ base.extend(ClassMethods)
25
+ end
26
+
27
+ module ClassMethods
28
+ def attribute(attribute)
29
+ self.attrs ||= []
30
+ self.attrs << attribute.to_s
31
+ attr_accessor attribute
32
+ end
33
+ end
34
+
4
35
  ##
5
36
  # Attributes ignored and not included on model xml by default
6
37
  IGNORE_ATTR = ["created_at", "updated_at", "id", "jid"]
7
38
 
39
+ def is_AR?
40
+ end
41
+
8
42
  ##
9
43
  # Updates multiple attributes at once
10
44
  # attributes:: [Hash] the attributes hash
@@ -57,6 +91,6 @@ module C3sModel
57
91
  ##
58
92
  # Returns a default namespace based on class name
59
93
  def xmlns
60
- "http://c3s.hng.av.it.pt/#{self._class_name}"
94
+ "http://c3s.av.it.pt/#{self._class_name}"
61
95
  end
62
96
  end
@@ -1,4 +1,5 @@
1
1
  require 'xmpp4r/pubsub'
2
+ require 'xmpp4r/pubsub/iq/pubsub'
2
3
  require 'pubsub/nodetracker'
3
4
 
4
5
  module C3s
@@ -15,16 +16,42 @@ module C3s
15
16
  end
16
17
 
17
18
  ##
18
- # Subscribes to collection node.
19
+ # Subscribes to a collection node.
19
20
  # nodename:: [String] the node name
20
21
  def subscribe_collection(nodename, options={})
21
- return if !is_collection?(nodename)
22
- subscription = @pubsub.subscribe_to(nodename)
22
+ $LOG.info "Going to subscribe #{nodename}"
23
+ iq = basic_pubsub_query(:set)
24
+ sub = REXML::Element.new('subscribe')
25
+ sub.attributes['node'] = nodename
26
+ sub.attributes['jid'] = @client.config['jid']
27
+ iq.pubsub.add(sub)
28
+
29
+ # Set the options to subscribe collection node
23
30
  options = {
24
31
  'pubsub#subscription_type' => options[:subscription_type] || 'items',
25
32
  'pubsub#subscription_depth' => options[:subscription_depth] || 'all'
26
33
  }
27
- @pubsub.set_options_for(nodename, @client.jid, options, subscription.subid)
34
+ iq.pubsub.add(Jabber::PubSub::SubscriptionConfig.new(nodename, @client.jid, options))
35
+
36
+ res = nil
37
+ @client.send_with_id(iq) do |reply|
38
+ pubsubanswer = reply.pubsub
39
+ if pubsubanswer.first_element('subscription')
40
+ res = PubSub::Subscription.import(pubsubanswer.first_element('subscription'))
41
+ end
42
+ end
43
+ res
44
+ end
45
+
46
+ def basic_pubsub_query(type,ownerusecase = false)
47
+ iq = Jabber::Iq.new(type, @client.config['pubsub'])
48
+ if ownerusecase
49
+ iq.add(Jabber::PubSub::IqPubSubOwner.new)
50
+ else
51
+ iq.add(Jabber::PubSub::IqPubSub.new)
52
+ end
53
+ iq.from = @client.jid
54
+ iq
28
55
  end
29
56
 
30
57
  ##
data/lib/republisher.rb CHANGED
@@ -12,6 +12,14 @@ module C3s
12
12
  super()
13
13
  end
14
14
 
15
+ ##
16
+ # When connects our republisher must subscribe nodes from which
17
+ # will receive context.
18
+ def connect
19
+ super
20
+ #subscribe_nodes()
21
+ end
22
+
15
23
  ##
16
24
  # Block the set iqs because this provider doesn't receive iq's
17
25
  # directly to set the information.
@@ -27,50 +35,45 @@ module C3s
27
35
 
28
36
  ##
29
37
  # Subscribes to all nodes
30
- #
31
38
  def subscribe_nodes
32
39
  @subscribing_nodes.each do |node|
33
40
  begin
34
41
  @subscriber.subscribe_collection(node)
35
42
  rescue Exception => e
36
- $LOG.error "Could not subscribe to node #{node}"
43
+ $LOG.error "Could not subscribe to node #{node} (#{e.inspect})"
37
44
  next
38
45
  end
39
46
  end
40
47
  end
41
48
 
42
49
  ##
43
- # Handles messages. This can be subscribe or unsubscribe messages
50
+ # Handles messages. I only deal with pubsub messages (publication notify)
44
51
  # and pubsub events (most likely).
52
+ # msg:: The message received
45
53
  def handle_msg(msg)
46
- if msg.body.eql?("sub")
47
- $LOG.info "Subscribing nodes: #{@subscribing_nodes.join(", ")}"
48
- subscribe_nodes()
49
- elsif msg.body.eql?("unsub")
50
- @subscriber.unsubscribe_all
51
- else
52
- event = msg.first_element("event")
53
- if event.kind_of?(Jabber::PubSub::Event)
54
- event.each_element("//item/") do |item|
55
- $LOG.debug("Received item from pubsub")
56
- begin
57
- c3snode = C3s::Node.new(item.attributes['node'])
58
- next unless c3snode
59
- $LOG.debug("Node built: #{c3snode.to_s}")
60
- c3snode.provider = config['name'] # TODO - assuming that provider_name.eql?(pubsub_root_node)
61
- $LOG.debug("Call republish() from child")
62
-
63
- Thread.abort_on_exception = true
64
- t = Thread.new do
65
- # This must be implemented on the republisher
66
- # because the strategy may vary from component to component.
67
- republish(c3snode, item)
68
- end
69
- t.join
70
- rescue Exception => e
71
- $LOG.error "Error on published item: #{e}"
72
- end
54
+ event = msg.first_element("event")
55
+ return if !event.kind_of?(Jabber::PubSub::Event)
56
+ event.each_element("//items/") do |items|
57
+ c3snode = C3s::Node.new(items.attributes['node'])
58
+ next unless c3snode
59
+ handle_items_publication(items)
60
+ end
61
+ end
62
+
63
+ ##
64
+ # Handles the publicated items and the republishing on new node(s)
65
+ def handle_items_publication(items)
66
+ items.each_element("//item/") do |item|
67
+ begin
68
+ c3snode.provider = config['name'] # TODO - assuming that provider_name.eql?(pubsub_root_nodes)
69
+ t = Thread.new do
70
+ # This must be implemented on the republisher
71
+ # because the strategy may vary from component to component.
72
+ republish(c3snode, item)
73
73
  end
74
+ t.join
75
+ rescue Exception => e
76
+ $LOG.error "Error on published item: #{e}"
74
77
  end
75
78
  end
76
79
  rescue Exception => e
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: c3s
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
5
- prerelease: false
4
+ hash: 3450203
5
+ prerelease: true
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 2
10
- version: 0.4.2
9
+ - 3a
10
+ version: 0.4.3a
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ricardo Otero
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-27 00:00:00 +01:00
18
+ date: 2010-11-10 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies: []
21
21