PDA

View Full Version : Data Framing Filters for Web Sockets



xantus
16 Dec 2008, 11:44 AM
Several Releases here.

These are data framing filters mainly for use with Web Sockets (http://code.google.com/p/cometdesktop/wiki/WebSockets)

Here is an excerpt:

Data Framing Filters

You put data into an object, and zero or more full blocks of data are returned. It sounds simple, and is, somewhat. I used a familiar system, from POE (http://poe.perl.org/), that brings the power of tried and true code from a Perl project, to JavaScript? (http://code.google.com/p/cometdesktop/w/edit/JavaScript).
Currently, there are several filters available. They are written for the Extjs toolkit, but they can be easily be written to be framework agnostic. (See js.io (http://js.io/))
Ext.ux.Sprocket.Filter (http://xant.us/ext-ux/lib/Sprocket/Filter.js) - The base class for all filters
Ext.ux.Sprocket.Filter.Stream (http://xant.us/ext-ux/lib/Sprocket/Filter.js) - A simple filter, nothing is buffered. It is usually used as a placeholder in the absence of another filter.
Ext.ux.Sprocket.Filter.Line (http://xant.us/ext-ux/lib/Sprocket/Filter/Line.js) - A line type auto detecting filter. Supports most types of line combinations (\x0D\x0A?|\x0A\x0D?|\u2028) with the added benefit of being a delimiter splitting filter. You can specify anything as your delimiter.
Ext.ux.Sprocket.Filter.JSON (http://xant.us/ext-ux/lib/Sprocket/Filter/JSON.js) - A JSON filter. JSON goes in, objects come out, or objects go in, and JSON comes out.
Ext.ux.Sprocket.Filter.Stackable (http://xant.us/ext-ux/lib/Sprocket/Filter.js) - The beauty of using data framing filters starts here. You can stack any number of filters in any order, and magic comes out. See the next section.Stackable Filters

Consider this set of stacked filters:


var stack = new Ext.ux.Sprocket.Filter.Stackable({
filters: [
new Ext.ux.Sprocket.Filter.Line(),
new Ext.ux.Sprocket.Filter.JSON()
]
});That is a line filter filter combined with a JSON filter. Lines of JSON can be pushed into that filter stack and an array of json objects is returned:


var arrObjs = stack.get('{ "foo": "bar" }\r\n{ "bar": "baz" }\r\n{ "blitz": "boom" }\r\n');arrObjs is now an Array of 3 Objects. (The line filter detected that \r\n is the line break, and will now split on \r\n from now on.)


var strData = stack.put(arrObjs);strData is now:


'{"foo":"bar"}\r\n{"bar":"baz"}\r\n{"blitz":"boom"}';The stacked filters chain into each other both ways! There is also an interface for pushing, shifting, unshifting and popping filters on the fly.