Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use EventMachine::XmlPushParser instead of SAX2Parser and StringIO

  • Loading branch information...
commit 7cffd77d3c5f812ad1363e2027e941d0b24324b2 1 parent ea21170
@tmm1 authored
Showing with 44 additions and 49 deletions.
  1. +4 −0 README
  2. +2 −0  em.rb
  3. +2 −1  stress_test.rb
  4. +36 −48 xmpp4em.rb
View
4 README
@@ -0,0 +1,4 @@
+Simple XMPP client built on EventMachine.
+
+Requires eventmachine-xmlpushparser. To install on OSX:
+ gem install eventmachine-xmlpushparser -- --with-xml2-include=/usr/include/libxml2
View
2  em.rb
@@ -26,4 +26,6 @@ class << self
end unless EM.respond_to? :old_run
end
+require 'evma_xmlpushparser'
+
EM.epoll
View
3  stress_test.rb
@@ -3,7 +3,8 @@
started = Time.now
users = {}
connected = 0
-num = ARGV[0].to_i || 1000
+num = Integer(ARGV[0])
+num = 1000 if num < 1
EM.epoll
View
84 xmpp4em.rb
@@ -13,17 +13,6 @@
require 'em'
module XMPP4EM
- class Buffer < StringIO
- def append(s)
- self.string[0..pos] = ''
- self.pos = 0
- self.string << s
- end
-
- def stat() self end
- def pipe?() false end
- end
-
class NotConnected < Exception; end
class Connection < EventMachine::Connection
@@ -40,46 +29,46 @@ def connection_completed
end
attr_reader :stream_features
- def receive_data data
- log "<< #{data}"
-
- @buffer.append(data)
-
- unless @parser
- @parser = REXML::Parsers::SAX2Parser.new(@buffer)
-
- @parser.listen(:start_element) do |uri, localname, qname, attributes|
- e = REXML::Element::new(qname)
- e.add_attributes attributes
+ include EventMachine::XmlPushParser
- @current = @current.nil? ? e : @current.add_element(e)
-
- if @current.name == 'stream' and not @started
- @started = true
- process
- @current = nil
- end
- end
-
- @parser.listen(:end_element) do |uri, localname, qname|
- if qname == 'stream:stream' and @current.nil?
- @started = false
- else
- if @current.parent
- @current = @current.parent
- else
- process
- @current = nil
- end
- end
- end
+ def start_document
+ end
+
+ def end_document
+ end
+
+ def start_element name, attrs
+ e = REXML::Element.new(name)
+ e.add_attributes attrs
+
+ @current = @current.nil? ? e : @current.add_element(e)
- @parser.listen(:characters) do |text|
- @current.text = @current.text.to_s + text if @current
+ if @current.name == 'stream' and not @started
+ @started = true
+ process
+ @current = nil
+ end
+ end
+
+ 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
+
+ def characters text
+ @current.text = @current.text.to_s + text if @current
+ end
- @parser.parse
+ def error *args
+ p ['error', *args]
end
def send data, &blk
@@ -92,9 +81,7 @@ def unbind
end
def init
- @buffer = Buffer.new
@started = false
- @parser = nil
send "<?xml version='1.0'?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en' version='1.0' to='#{@host}'>"
end
@@ -289,6 +276,7 @@ def receive stanza
when 'success', 'failure'
if stanza.name == 'success'
@authenticated = true
+ @connection.reset_parser
@connection.init
end
Please sign in to comment.
Something went wrong with that request. Please try again.