Skip to content

Commit

Permalink
use EventMachine::XmlPushParser instead of SAX2Parser and StringIO
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Jun 5, 2008
1 parent ea21170 commit 7cffd77
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 49 deletions.
4 changes: 4 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions em.rb
Expand Up @@ -26,4 +26,6 @@ class << self
end unless EM.respond_to? :old_run end unless EM.respond_to? :old_run
end end


require 'evma_xmlpushparser'

EM.epoll EM.epoll
3 changes: 2 additions & 1 deletion stress_test.rb
Expand Up @@ -3,7 +3,8 @@
started = Time.now started = Time.now
users = {} users = {}
connected = 0 connected = 0
num = ARGV[0].to_i || 1000 num = Integer(ARGV[0])
num = 1000 if num < 1


EM.epoll EM.epoll


Expand Down
84 changes: 36 additions & 48 deletions xmpp4em.rb
Expand Up @@ -13,17 +13,6 @@
require 'em' require 'em'


module XMPP4EM 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 NotConnected < Exception; end


class Connection < EventMachine::Connection class Connection < EventMachine::Connection
Expand All @@ -40,46 +29,46 @@ def connection_completed
end end
attr_reader :stream_features attr_reader :stream_features


def receive_data data include EventMachine::XmlPushParser
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


@current = @current.nil? ? e : @current.add_element(e) def start_document

end
if @current.name == 'stream' and not @started
@started = true def end_document
process end
@current = nil
end def start_element name, attrs
end e = REXML::Element.new(name)

e.add_attributes attrs
@parser.listen(:end_element) do |uri, localname, qname|
if qname == 'stream:stream' and @current.nil? @current = @current.nil? ? e : @current.add_element(e)
@started = false
else
if @current.parent
@current = @current.parent
else
process
@current = nil
end
end
end


@parser.listen(:characters) do |text| if @current.name == 'stream' and not @started
@current.text = @current.text.to_s + text if @current @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 end
end

def characters text
@current.text = @current.text.to_s + text if @current
end


@parser.parse def error *args
p ['error', *args]
end end


def send data, &blk def send data, &blk
Expand All @@ -92,9 +81,7 @@ def unbind
end end


def init def init
@buffer = Buffer.new
@started = false @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}'>" 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 end


Expand Down Expand Up @@ -289,6 +276,7 @@ def receive stanza
when 'success', 'failure' when 'success', 'failure'
if stanza.name == 'success' if stanza.name == 'success'
@authenticated = true @authenticated = true
@connection.reset_parser
@connection.init @connection.init
end end


Expand Down

0 comments on commit 7cffd77

Please sign in to comment.