Skip to content
Browse files

initial work, local proto working

  • Loading branch information...
1 parent 67b9015 commit e051ac51802a49f07ae3cc515ef20a31274c2d25 @outerim outerim committed Jun 17, 2009
Showing with 93 additions and 12 deletions.
  1. +9 −3 lib/pelvis/actor.rb
  2. +8 −3 lib/pelvis/agent.rb
  3. +13 −0 lib/pelvis/protocol.rb
  4. +16 −1 lib/pelvis/protocols/local.rb
  5. +22 −5 lib/pelvis/protocols/xmpp.rb
  6. +2 −0 spec/helpers.rb
  7. +23 −0 spec/protocol_spec.rb
View
12 lib/pelvis/actor.rb
@@ -10,6 +10,15 @@ class << self
extend Callbacks
callbacks :resources_changed
+ attr_reader :agent
+
+ def post_init
+ #override me
+ end
+
+ def set_agent(agent)
+ @agent = agent
+ end
def operation(name, options={})
options[:name] = name
@@ -59,9 +68,6 @@ def operations_for(job)
def resources_for(op)
nil
end
-
- def added_to_agent(agent)
- end
end
# TODO: Enable config and deployment resources
View
11 lib/pelvis/agent.rb
@@ -62,9 +62,15 @@ class ConfiguredActor < SimpleDelegator
extend Callbacks
callbacks :readvertising
- def initialize(klass, block)
+ def initialize(agent, klass, block)
super(klass)
@block = block || proc {|actor|}
+ set_agent agent
+ post_init
+ end
+
+ def post_init
+ super
end
def start(*a)
@@ -83,9 +89,8 @@ def readvertised_with(a)
end
def add_actor(klass, &block)
- actors << ConfiguredActor.new(klass, block)
+ actors << ConfiguredActor.new(self, klass, block)
it = actors.last
- it.added_to_agent(self)
it.on_resources_changed {
logger.debug "got resources changed for #{it}, readvertising"
a = Advertiser.new(self, [it])
View
13 lib/pelvis/protocol.rb
@@ -42,6 +42,19 @@ def advertise?
options[:advertise]
end
+ def presence_handlers
+ @presence_handlers ||= {}
+ end
+
+ def subscribe_presence(identity, &block)
+ presence_handlers[identity] = block
+ handle_subscribe_presence(identity)
+ end
+
+ def handle_subscribe_presence(ident)
+ raise "Implement #handle_subscribe_presence on #{self.class}"
+ end
+
def connect
raise "Implement #connect on #{self.class}"
end
View
17 lib/pelvis/protocols/local.rb
@@ -6,9 +6,16 @@ class Local < Protocol
register :local
SET = []
+ PRESENCE_HANDLERS = {}
def connect
- logger.debug "connecting using #{self.class}: identity=#{identity.inspect}"
+ logger.debug "connecting using #{self}: identity=#{identity.inspect}"
+
+ if presence_handlers[identity]
+ p presence_handlers[identity]
+ presence_handlers[identity].send(:call)
+ end
+
on_spawned do |agent|
SET << agent
end
@@ -37,6 +44,14 @@ def start
end
end
+ def presence_handlers
+ PRESENCE_HANDLERS
+ end
+
+ def handle_subscribe_presence(ident)
+ # nothing to do
+ end
+
def agent_for(identity)
SET.find do |a|
a.identity == identity
View
27 lib/pelvis/protocols/xmpp.rb
@@ -64,13 +64,30 @@ def close
def receive_data(stanza)
logger.debug "got a stanza for #{identity}:\n#{stanza.inspect}"
- if stanza.is_a?(Blather::BlatherError) || stanza.is_a?(Blather::SASLError)
- failed stanza
- return
+ case stanza
+ when Blather::BlatherError, Blather::SASLError
+ failed stanza
+ return
+# when Blather::Presence::Status
+# when Blather::Presence::Subscription
+# if stanza.subscribe?
+# logger.debug "Got subscription request from #{stanza.from}"
+# val = if identity == herault
+# logger.warn "Approving subscription request from #{stanza.from}"
+# stanza.approve!
+# else
+# logger.warn "Refusing subscription request from #{stanza.from}"
+# stanza.refuse!
+# end
+# @stream.send val
+# end
+ else
+ process_stanza stanza
end
+ end
- from = stanza["from"]
- remote_agent = agent_for(from)
+ def process_stanza(stanza)
+ remote_agent = agent_for(stanza['from'])
node = stanza.find("job").first
token = node["token"]
View
2 spec/helpers.rb
@@ -26,6 +26,7 @@ def identity_for(name)
def start_em(after=nil)
EM.run(nil, after) do
Protocols::Local::SET.clear
+ Protocols::Local::PRESENCE_HANDLERS.clear
yield
end
end
@@ -43,6 +44,7 @@ def connect(agent, &block)
def agent_connect(agents, &block)
agent, actors = *agents.shift
+ actors ||= []
connection = connect(agent) do |agent|
actors.each do |actor|
agent.add_actor actor
View
23 spec/protocol_spec.rb
@@ -0,0 +1,23 @@
+require File.dirname(__FILE__) + '/spec_helper'
+require 'examples/actors/herault'
+
+class DummyActor < Pelvis::Actor; end
+
+describe "A protocol" do
+ include Pelvis::Helpers
+
+ describe "#subscribe_presence" do
+ it "should call the block when the specified identity advertises" do
+ block = Proc.new { @called_me = true }
+
+ @agents = [[:herault, [DummyActor]], [:foo]]
+ start_agents { |agent|
+ agent.protocol.subscribe_presence(identity_for(:bar), &block)
+ connect(:bar) {
+ @called_me.should == true
+ EM.stop
+ }
+ }
+ end
+ end
+end

0 comments on commit e051ac5

Please sign in to comment.
Something went wrong with that request. Please try again.