Class: Kibosh::XMPP::Client::Connection

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/kibosh/xmpp/client/connection.rb

Defined Under Namespace

Classes: StreamParser

Instance Method Summary collapse

Constructor Details

#initialize(stream, success, failure) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
# File 'lib/kibosh/xmpp/client/connection.rb', line 7

def initialize stream, success, failure
  super
  @stream = stream
  @success = success
  @failure = failure
end

Instance Method Details

#closeObject



201
202
203
204
205
# File 'lib/kibosh/xmpp/client/connection.rb', line 201

def close
  puts "> #{@suffix}"
  send_data @suffix
  close_connection_after_writing
end

#connection_completedObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/kibosh/xmpp/client/connection.rb', line 157

def connection_completed
  stream_element = Nokogiri::XML::Builder.new do |xml|
    xml.send :"stream:stream", :to => @stream.to,
                               :xmlns => "jabber:client",
                               :"xmlns:stream" => "http://etherx.jabber.org/streams",
                               :version => @stream.version do
      xml.text "*"
    end
  end.to_xml :indent => 0
  midway = stream_element.index "*"
  prefix = stream_element[0..midway-1]
  puts "> (prefix) #{prefix}"
  @suffix = stream_element[midway+1..-1]
  @parser = StreamParser.new self, @stream, lambda { |attributes|
    @stream.xmpp_id = attributes["id"]
    @stream.from = attributes["from"]
    @success.call self if @success
    @success = @failure = nil
  }, lambda {
    @failiure.call self if @failure
    @success = @failure = nil
  }
  send_data prefix
end

#post_initObject



14
15
# File 'lib/kibosh/xmpp/client/connection.rb', line 14

def post_init
end

#receive_data(data) ⇒ Object



182
183
184
185
# File 'lib/kibosh/xmpp/client/connection.rb', line 182

def receive_data data
  puts "< #{data}"
  @parser << data
end

#restart(stream) ⇒ Object



192
193
194
195
# File 'lib/kibosh/xmpp/client/connection.rb', line 192

def restart stream
  @stream = stream
  connection_completed
end

#terminateObject



197
198
199
# File 'lib/kibosh/xmpp/client/connection.rb', line 197

def terminate
  close
end

#unbindObject



187
188
189
190
# File 'lib/kibosh/xmpp/client/connection.rb', line 187

def unbind
  @failure.call self if @failure
  @success = @failure = nil
end