Permalink
Browse files

agents subscribe to herault status and readvertise on join/leave, ver…

…sion 0.2
  • Loading branch information...
1 parent dbeff02 commit 35ed120caaee7821ad6ecc421d6c60170667ad4d @outerim outerim committed Jun 19, 2009
@@ -6,14 +6,10 @@ def chained
results = ProxyBack.new(self)
# FIXME: changing from a timer to a 10.times loop breaks the following
# it appears to be an issue with blather's handling of the message from jabber
- timer = EM::PeriodicTimer.new(1) {
+ 10.times do |number|
send_data :message => "requesting inner #{number}"
request(:all, "/inner", {:number => number}, :delegate => results)
- number -= 1
- if number <= 0
- timer.cancel
- end
- }
+ end
end
class ProxyBack
@@ -1,5 +1,10 @@
class Debugger
include Pelvis::Delegate
+ attr_accessor :name
+
+ def initialize(name='')
+ @name = name
+ end
def received(data)
log "Received data: #{data.inspect}"
@@ -14,7 +19,7 @@ def failed(error)
end
def log(message)
- puts "%0.7f: %s" % [Time.now.to_f, message]
+ puts "%0.7f: #{name} - %s" % [Time.now.to_f, message]
end
end
View
@@ -5,6 +5,14 @@ class Agent
include Logging
extend Callbacks
+ def self.readvertise_wait_interval
+ @readvertise_wait_interval || 60
+ end
+
+ def self.readvertise_wait_interval=(amt)
+ @readvertise_wait_interval = amt
+ end
+
callbacks :advertised
def initialize(protocol)
@@ -14,6 +22,14 @@ def initialize(protocol)
def start
logger.debug "Starting an agent: #{@protocol.inspect}"
+ if herault != identity
+ protocol.subscribe_presence(herault) do |id, state|
+ if state == :available and @advertised #should have sent our initial advertisement, we exit if we don't this prevents double initial ads
+ logger.warn "Herault joined, readvertising #{identity}"
+ actors.each { |a| EM.add_timer(rand(self.class.readvertise_wait_interval)) { readvertise(a) } }
+ end
+ end
+ end
end
def initial_job
@@ -93,18 +109,23 @@ def add_actor(klass, &block)
it = actors.last
it.on_resources_changed {
logger.debug "got resources changed for #{it}, readvertising"
- a = Advertiser.new(self, [it])
- a.on_succeeded {
- logger.debug "readvertisement successful"
- }
- a.on_failed {
- logger.debug "readvertisement failed"
- }
- it.readvertised_with(a)
+ readvertise(it)
}
it
end
+ def readvertise(agent)
+ logger.warn "Readvertising #{agent}"
+ a = Advertiser.new(self, [agent])
+ a.on_succeeded {
+ logger.debug "readvertisement successful"
+ }
+ a.on_failed {
+ logger.debug "readvertisement failed"
+ }
+ agent.readvertised_with(a)
+ end
+
def advertise
if !@protocol.advertise? || actors.empty?
logger.debug "Not advertising"
@@ -114,7 +135,7 @@ def advertise
# initial advertisement
a = Advertiser.new(self, actors)
- a.on_succeeded { advertised }
+ a.on_succeeded { @advertised = true; advertised }
a.on_failed { raise "unable to perform advertisement" }
end
@@ -44,12 +44,12 @@ def receive_data(stanza)
when Blather::Stanza::Iq::Roster
# ignore
when Blather::Stanza::Presence::Status
- logger.debug "Got presence announcement from #{stanza.from} state #{stanza.state}"
+ logger.debug "#{identity} Got presence announcement from #{stanza.from} state #{stanza.state}"
call_presence_handlers stanza.from.to_s, stanza.state
when Blather::Stanza::Presence::Subscription
if stanza.subscribe?
logger.debug "Got subscription request from #{stanza.from}"
- val = if stanza.from.stripped == herault_jid.stripped
+ val = if stanza.from.stripped == herault_jid.stripped || identity == herault
logger.warn "Approving subscription request from #{stanza.from}"
stanza.approve!
else
View
@@ -1,3 +1,3 @@
module Pelvis
- VERSION = "0.1.6"
+ VERSION = "0.2.0"
end
View
@@ -13,7 +13,7 @@ require File.dirname(__FILE__) + '/examples/delegates/debugger'
if ENV["DEBUGGER"]
Pelvis.logger.level = Logger::DEBUG
- Blather::LOG.level = Logger::DEBUG
+ #Blather::LOG.level = Logger::DEBUG
end
def connect_herault
@@ -54,8 +54,8 @@ def connect_bar
agent.on_advertised do
puts "bar is ready"
- agent.request(:all, "/do/random", {}, :delegate => Debugger.new)
- agent.request(:all, "/chained", {}, :delegate => Debugger.new)
+ agent.request(:all, "/do/random", {}, :delegate => Debugger.new('RANDOM'))
+ agent.request(:all, "/chained", {}, :delegate => Debugger.new('CHAINED'))
end
end
end
View
@@ -0,0 +1,40 @@
+require File.dirname(__FILE__) + '/spec_helper'
+require 'examples/actors/herault'
+
+class Herault
+ def self.reset
+ @operation_map = nil
+ end
+end
+
+describe "An Agent" do
+ include Pelvis::Helpers
+ Pelvis::Agent.readvertise_wait_interval = 0
+
+ # FIXME: this is awful complex but it works
+ it "should readvertise it's resources when herault returns from an absence" do
+ @agents = [[:herault]]
+ start_agents do |herault|
+ herault.add_actor Herault
+ connect(:foo) do |foo|
+ simple = foo.add_actor Simple
+ foo.on_advertised do
+ foo.protocol.subscribe_presence(identity_for(:herault)) do |i, s|
+ if s == :available
+ simple.on_readvertising do |ad|
+ ad.on_succeeded do
+ Herault.operation_map['/echo'].keys.should == [ identity_for(:foo) ]
+ EM.stop
+ end
+ end
+ else
+ Herault.reset
+ connect(:herault) { |h| h.add_actor Herault }
+ end
+ end
+ herault.protocol.stop
+ end
+ end
+ end
+ end
+end
View
@@ -3,8 +3,6 @@
describe "Herault" do
include Pelvis::Helpers
- before(:each) do
- end
it "should subscribe to the presence messages of agents that advertise to it and remove advertisements on unavailable" do
@agents = [[:herault]]
@@ -13,8 +11,8 @@
connect(:foo) do |foo|
foo.add_actor Simple
foo.on_advertised {
- herault.protocol.presence_handlers.keys.should == [ identity_for(:foo) ]
- Herault.operation_map['/echo'].should == { identity_for(:foo) => nil }
+ herault.protocol.presence_handlers.keys.should include(identity_for(:foo))
+ Herault.operation_map['/echo'].keys.should == [ identity_for(:foo) ]
herault.protocol.subscribe_presence(identity_for(:foo)) do |i, s|
if s == :unavailable

0 comments on commit 35ed120

Please sign in to comment.