Skip to content

Commit

Permalink
add #reconnect and on(:disconnect) callback
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Jul 7, 2008
1 parent 81e5d2d commit 0593bc2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
3 changes: 2 additions & 1 deletion spec/spec_runner.rb
Expand Up @@ -6,13 +6,14 @@
$bacon_thread = Thread.current
def wait
Thread.stop
EM.add_timer(10) do
@timer = EM::Timer.new(10) do
wake
should.flunk('waited too long')
end
end
def wake
$bacon_thread.wakeup
@timer.cancel if @timer
end
end

Expand Down
19 changes: 18 additions & 1 deletion spec/xmpp4em_spec.rb
Expand Up @@ -37,4 +37,21 @@

received.should == 'hello'
end
end

should 'fire disconnect callback and reconnect' do
user = XMPP4EM::Client.new('user@localhost', 'user', :auto_register => true)
user.on(:disconnect){ wake }
user.connect 'localhost', 5333 # invalid port
wait

user.should.not.be.connected?

user.instance_variable_get('@callbacks')[:disconnect] = []
user.connection.port = 5222
user.on(:login){ wake }
user.reconnect
wait

user.should.be.connected?
end
end
54 changes: 31 additions & 23 deletions xmpp4em.rb
Expand Up @@ -16,11 +16,11 @@ module XMPP4EM
class NotConnected < Exception; end

class Connection < EventMachine::Connection
def initialize host
@host = host
def initialize host, port
@host, @port = host, port
@client = nil
end
attr_accessor :client
attr_accessor :client, :host, :port

def connection_completed
log 'connected'
Expand Down Expand Up @@ -81,9 +81,14 @@ def unbind
@keepalive.cancel
@keepalive = nil
end
@client.on(:disconnect)
log 'disconnected'
end

def reconnect host = @host, port = @port
super
end

def init
send "<?xml version='1.0' ?>" unless @started
@started = false
Expand Down Expand Up @@ -156,11 +161,12 @@ def initialize user, pass, opts = {}
@id_callbacks = {}

@callbacks = {
:message => [],
:presence => [],
:iq => [],
:exception => [],
:login => []
:message => [],
:presence => [],
:iq => [],
:exception => [],
:login => [],
:disconnect => []
}

@opts = { :auto_register => false }.merge(opts)
Expand All @@ -177,13 +183,17 @@ def jid

def connect host = jid.domain, port = 5222
EM.run {
EM.connect host, port, Connection, host do |conn|
EM.connect host, port, Connection, host, port do |conn|
@connection = conn
conn.client = self
end
}
end

def reconnect
@connection.reconnect
end

def connected?
@connection and !@connection.error?
end
Expand Down Expand Up @@ -268,9 +278,7 @@ def receive stanza
send(iq){ |reply|
if reply.type == :result

@callbacks[:login].each do |blk|
blk.call(stanza)
end
on(:login, stanza)
end
}
end
Expand All @@ -291,25 +299,25 @@ def receive stanza

case stanza
when Jabber::Message
@callbacks[:message].each do |blk|
blk.call(stanza)
end
on(:message, stanza)

when Jabber::Iq
@callbacks[:iq].each do |blk|
blk.call(stanza)
end
on(:iq, stanza)

when Jabber::Presence
@callbacks[:presence].each do |blk|
blk.call(stanza)
end
on(:presence, stanza)
end

end

def on type, &blk
@callbacks[type] << blk
def on type, *args, &blk
if blk
@callbacks[type] << blk
else
@callbacks[type].each do |blk|
blk.call(*args)
end
end
end

def add_message_callback (&blk) on :message, &blk end
Expand Down

0 comments on commit 0593bc2

Please sign in to comment.