Class: XmppGateway::XmppInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp_gateway/xmpp_interface.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, password) ⇒ XmppInterface

Returns a new instance of XmppInterface.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/xmpp_gateway/xmpp_interface.rb', line 18

def initialize(user,password)
  @jid = Blather::JID.new(user)
  
  f = Fiber.current
  
  @client = Blather::Client.setup @jid, password  
  @client.register_handler(:ready) do
    @connected = true
    f.resume( @connected ) if f.alive?
  end
  @client.register_handler(:disconnected) do
    XmppGateway.logger.debug "XMPP connection #{@jid} disconnected"
    @connected = false
    f.resume( @connected ) if f.alive?
    # Prevent EventMachine from stopping by returning true on disconnected
    true
  end
  @client.clear_handlers(:error)
  @client.register_handler(:error){ f.resume( false ) if f.alive? }
   
  begin
    @client.run
  rescue
    @connected = false
  end
  
  # Waits until it has connected unless an error was thrown
  @connected = Fiber.yield if @connected.nil?
  
  schedule_activity_monitor
  
  XmppGateway.logger.debug "XMPP connection #{@jid} #{@connected ? '' : 'not '}connected"
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/xmpp_gateway/xmpp_interface.rb', line 7

def client
  @client
end

#connectedObject (readonly)

Returns the value of attribute connected.



8
9
10
# File 'lib/xmpp_gateway/xmpp_interface.rb', line 8

def connected
  @connected
end

Class Method Details

.stanza(stanza) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/xmpp_gateway/xmpp_interface.rb', line 10

def self.stanza(stanza)
  noko = Nokogiri::XML::Document.parse( stanza ).root
  return nil unless noko
  blather = Blather::XMPPNode.import( noko )
  return nil if blather.class == Blather::XMPPNode # This means it isn't a valid presence, iq or message stanza
  return blather
end

Instance Method Details

#write(stanza) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xmpp_gateway/xmpp_interface.rb', line 52

def write(stanza)
  XmppGateway.logger.debug "Sending stanza #{stanza.to_s}"
  
  f = Fiber.current

  @client.write_with_handler( stanza ){|result|
    f.resume(result) if f.alive?
  }
  
  if reply_expected stanza
    # Remove timer while we wait for response
    cancel_timer
    reply = Fiber.yield
    XmppGateway.logger.debug "Received #{reply}"
    # Schedule new timer
    schedule_activity_monitor
    return reply
  end
  
  schedule_activity_monitor
end