Class: XMPP4EM::BaseConnection

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::XmlPushParser
Defined in:
lib/xmpp4em/base_connection.rb

Direct Known Subclasses

ClientConnection, ComponentConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 5222) ⇒ BaseConnection

Returns a new instance of BaseConnection.



15
16
17
18
# File 'lib/xmpp4em/base_connection.rb', line 15

def initialize host, port=5222
  @host, @port = host, port
  @client = nil
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



19
20
21
# File 'lib/xmpp4em/base_connection.rb', line 19

def client
  @client
end

#hostObject

Returns the value of attribute host.



19
20
21
# File 'lib/xmpp4em/base_connection.rb', line 19

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



19
20
21
# File 'lib/xmpp4em/base_connection.rb', line 19

def logger
  @logger
end

#portObject

Returns the value of attribute port.



19
20
21
# File 'lib/xmpp4em/base_connection.rb', line 19

def port
  @port
end

#stream_featuresObject (readonly)

Returns the value of attribute stream_features.



28
29
30
# File 'lib/xmpp4em/base_connection.rb', line 28

def stream_features
  @stream_features
end

Instance Method Details

#characters(text) ⇒ Object



64
65
66
# File 'lib/xmpp4em/base_connection.rb', line 64

def characters text
  @current.text = @current.text.to_s + encode2utf8(text) if @current
end

#connection_completedObject



21
22
23
24
25
26
27
# File 'lib/xmpp4em/base_connection.rb', line 21

def connection_completed
  @logger.debug{'connected'} if @logger
  @stream_features, @stream_mechanisms = {}, []
  @keepalive = EM::Timer.new(60){ send_data("\n") }
  @client.on(:connected)
  init
end

#encode2utf8(text) ⇒ Object



32
33
34
# File 'lib/xmpp4em/base_connection.rb', line 32

def encode2utf8(text)
  text.respond_to?(:force_encoding) ? text.force_encoding("utf-8") : text
end

#end_element(name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/xmpp4em/base_connection.rb', line 51

def end_element name
  if name == 'stream:stream' and @current.nil?
    @started = false
  else
    if @current.parent
      @current = @current.parent
    else
      process
      @current = nil
    end
  end
end

#error(*args) ⇒ Object



68
69
70
# File 'lib/xmpp4em/base_connection.rb', line 68

def error *args
  p ['error', *args]
end

#initObject



95
96
97
98
99
# File 'lib/xmpp4em/base_connection.rb', line 95

def init
  send "<?xml version='1.0' ?>" unless @started
  @started = false
  send "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='#{namespace;}' xml:lang='en' version='1.0' to='#{@host}'>"
end

#receive_data(data) ⇒ Object



72
73
74
75
# File 'lib/xmpp4em/base_connection.rb', line 72

def receive_data data
  @logger.debug{"<< #{data}"} if @logger
  super
end

#reconnect(host = @host, port = @port) ⇒ Object



91
92
93
# File 'lib/xmpp4em/base_connection.rb', line 91

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

#send(data, &blk) ⇒ Object



77
78
79
80
# File 'lib/xmpp4em/base_connection.rb', line 77

def send data, &blk
  @logger.debug{ ">> #{data}"} if @logger
  send_data data.to_s     
end

#start_element(name, attrs) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xmpp4em/base_connection.rb', line 36

def start_element name, attrs
  e = REXML::Element.new(name)
  attrs.each { |name, value|
    e.add_attribute(name, encode2utf8(value))
  }
  
  @current = @current.nil? ? e : @current.add_element(e)
  
  if @current.name == 'stream' and not @started
    @started = true
    process
    @current = nil
  end
end

#unbindObject



82
83
84
85
86
87
88
89
# File 'lib/xmpp4em/base_connection.rb', line 82

def unbind
  if @keepalive
    @keepalive.cancel
    @keepalive = nil
  end
  @client.on(:disconnect)
  @logger.debug{'disconnected'} if @logger
end