util.xmppstream, util.xmllex: Basic test passes

Tue, 04 Jan 2011 00:23:51 +0000

author
Daurnimator <quae@daurnimator.com>
date
Tue, 04 Jan 2011 00:23:51 +0000
changeset 3995
7214dc7a5642
parent 3994
b3dffb6bc1aa
child 3996
ea0ac2fb7c9f

util.xmppstream, util.xmllex: Basic test passes

util/xmllex.lua file | annotate | diff | comparison | revisions
util/xmppstream.lua file | annotate | diff | comparison | revisions
--- a/util/xmllex.lua	Mon Jan 03 22:20:49 2011 +0000
+++ b/util/xmllex.lua	Tue Jan 04 00:23:51 2011 +0000
@@ -29,6 +29,7 @@
 
 local function handleoutside ( str , r , initial )
 	local a , b , close = str:find ( "<(/?)" , initial )
+
 	if not a then
 		r.state = "outside"
 		return false
@@ -88,6 +89,8 @@
 local function index ( str , r )
 	r = r or { depth = 0, state = "outside" }
 	
+	local initial = 1
+	
 	if not r[#r] then
 		r[1] = setmetatable ( {
 			msgs = { str } ;
@@ -98,17 +101,18 @@
 		tblinsert ( r[#r].msgs , str )
 	end
 	
-	repeat
+	while true do
 		if r.state == "outside" then
-			if not handleoutside ( str , r , r[#r].start ) then
+			if not handleoutside ( str , r , initial ) then
 				break
 			end
 		else
-			if not handleinside ( str , r , r[#r].start ) then
+			if not handleinside ( str , r , initial ) then
 				break
 			end
 		end
-	until false
+		initial = r[#r].start
+	end
 
 	return r
 end
@@ -148,10 +152,17 @@
 end
 
 local function tagindex_to_tree(indices)
+	if not start then
+		start = 1
+		finish = #indices
+	end
+	
 	local root = { tags = { } }
 	local leaf = root
 	
-	for k ,v in ipairs ( indices ) do
+	for i = start , finish do
+		local v = indices [ i ]
+		
 		if v.type == "selfclosing" then
 			local newleaf = new_stanza ( )
 			newleaf.opentag = v
@@ -182,6 +193,7 @@
 end
 
 return {
-	index = index;
-	tagindex_to_tree = tagindex_to_tree;
+	index = index ;
+	tagindex_to_tree = tagindex_to_tree ;
+	process_starttag = process_starttag ;
 };
--- a/util/xmppstream.lua	Mon Jan 03 22:20:49 2011 +0000
+++ b/util/xmppstream.lua	Tue Jan 04 00:23:51 2011 +0000
@@ -6,44 +6,60 @@
 -- COPYING file in the source package for more information.
 --
 
-
-local lxp = require "lxp";
-local lex = require "util.xmllex";
-local st = require "util.stanza";
-local stanza_mt = st.stanza_mt;
+local tblinsert = table.insert
 
-local tostring = tostring;
-local t_insert = table.insert;
-local t_concat = table.concat;
-local t_remove = table.remove;
-local setmetatable = setmetatable;
+local lex = require "util.xmllex";
+local index = lex.index
+local tagindex_to_tree  = lex.tagindex_to_tree
+local process_starttag = lex.process_starttag
 
 local default_log = require "util.logger".init("xmppstream");
 
-local error = error;
-
-module "xmppstream"
-
 local xmlns_streams = "http://etherx.jabber.org/streams";
 
-function new(session, stream_callbacks)
-	local partial;
+local function new(session, stream_callbacks)
+	local partial
+	local openindexes = { }
+	local lastindex = 0
 	
-	local function feed(stream, data)
-		index(data, 
-	end
-
 	return {
-		reset = function ()
-			parser = new_parser(handlers, ns_separator);
-			parse = parser.parse;
-			meta.reset();
-		end,
-		feed = function (self, data)
-			return parse(parser, data);
-		end,
-		set_session = meta.set_session;
+		reset = function ( )
+			partial = nil
+			openindexes = { }
+		end ;
+		feed = function ( stream , data )
+			while true do
+				partial = index ( data , partial )
+				for i = lastindex + 1 , #partial - 1 do
+					local v = partial [ i ]
+					
+					if v.finish then
+						if v.type == "open" then
+							tblinsert ( openindexes , i )
+							if #openindexes == 1 then
+								local name , attr = process_starttag ( v )
+								stream_callbacks.streamopened ( session , attr )
+							end
+						elseif v.type == "close" then
+							if #openindexes == 2 then -- If closing level 2...
+								local stanza = tagindex_to_tree ( partial , openindexes [ #openindexes ] , i )
+								stream_callbacks.handlestanza ( session , stanza )
+							elseif #openindexes == 1 then
+								stream_callbacks.streamclosed ( session )
+							end
+							--print ( "Closed level " .. #openindexes )
+							openindexes [ #openindexes ] = nil
+						end
+					end
+				end
+				lastindex = #partial - 1
+				break
+			end
+		end ;
+		set_session = function ( newsession ) session = newsession end
 	};
 end
 
-return _M;
+return {
+	new = new ;
+}

mercurial