[Xmpp4js-commit] SF.net SVN: xmpp4js:[742] trunk/src
Status: Beta
Brought to you by:
h-iverson
From: <h-i...@us...> - 2008-07-23 16:49:36
|
Revision: 742 http://xmpp4js.svn.sourceforge.net/xmpp4js/?rev=742&view=rev Author: h-iverson Date: 2008-07-23 16:49:45 +0000 (Wed, 23 Jul 2008) Log Message: ----------- commited first stab at pause/resume. it works using the example in doc, but not in soashable itself yet. the code needs to be cleaned up a ton, but it works. Modified Paths: -------------- trunk/src/main/javascript/KeySequence.js trunk/src/main/javascript/XmppConnection.js trunk/src/main/javascript/transport/Base.js trunk/src/site/xdoc/code-samples.xml Modified: trunk/src/main/javascript/KeySequence.js =================================================================== --- trunk/src/main/javascript/KeySequence.js 2008-07-21 19:11:43 UTC (rev 741) +++ trunk/src/main/javascript/KeySequence.js 2008-07-23 16:49:45 UTC (rev 742) @@ -25,6 +25,10 @@ * @constructor */ function KeySequence(length) { + this._keys = []; + this._idx = null; + this._seed = null; + this._initKeys( length ); } Modified: trunk/src/main/javascript/XmppConnection.js =================================================================== --- trunk/src/main/javascript/XmppConnection.js 2008-07-21 19:11:43 UTC (rev 741) +++ trunk/src/main/javascript/XmppConnection.js 2008-07-23 16:49:45 UTC (rev 742) @@ -75,24 +75,20 @@ stanzaProvider: this.stanzaProvider }); + this.setupTransport(); + + Xmpp4Js.Connection.superclass.constructor.call( this, superConfig ); + if( config.pauseStruct != undefined ) { + this.resume( config.pauseStruct ); + } + } Xmpp4Js.Connection.prototype = { - /** - * Connect to a given domain, port and server. Only domain is required. - */ - connect: function(domain, port, server) { - // TODO it would be nice to be able to give a connect - // event as a parameter, that registers on this and - // unregisters right after it's called. - if( this.isConnected() ) { - throw new Xmpp4Js.Error( "Already Connected" ); - } - this.domain = domain; - + setupTransport: function(server, port) { var transportClass = this.transportConfig.clazz; if( typeof transportClass != 'function' ) { @@ -117,6 +113,22 @@ }); this.transport = new transportClass(transportConfig); + }, + + /** + * Connect to a given domain, port and server. Only domain is required. + */ + connect: function(domain, server, port) { + // TODO it would be nice to be able to give a connect + // event as a parameter, that registers on this and + // unregisters right after it's called. + + if( this.isConnected() ) { + throw new Xmpp4Js.Error( "Already Connected" ); + } + this.domain = domain; + + this.setupTransport( server, port ); this.transport.beginSession(); }, @@ -270,6 +282,52 @@ */ getJid : function() { return new Xmpp4Js.Jid(this.jid); + }, + + /** + * Sends a pause command to the server and returns a struct that may be + * serialized and passed to resume. + * @param {Number} time Time in seconds to allow inactivity. No longer than maxpause. + * @public + */ + pause: function(time) { + + // serialize transport's junk + var pauseStruct = this.transport.pause(time); + + // serialize our junk (domain is covered by transport) + pauseStruct.jid = this.jid; + + // stop doing stuff + // will this cause problems collecting any last packet? + this.connected = false; + this.transport.un("recv", this.onRecv, this ); + + return pauseStruct; + }, + + /** + * Sends a pause command to the server and returns a struct that may be + * serialized and passed to resume. + * @param {Number} time Time in seconds to allow inactivity. No longer than maxpause. + * @public + */ + resume: function(pauseStruct) { + this.setupTransport( pauseStruct.server, pauseStruct.port ); + + // deserialize transport's junk + this.transport.resume(pauseStruct); + + // deserialize our junk + this.domain = pauseStruct.domain; + this.jid = pauseStruct.jid; + + // return to connected state + this.connected = true; + this.transport.on({ + scope: this, + recv: this.onRecv + }); } } Modified: trunk/src/main/javascript/transport/Base.js =================================================================== --- trunk/src/main/javascript/transport/Base.js 2008-07-21 19:11:43 UTC (rev 741) +++ trunk/src/main/javascript/transport/Base.js 2008-07-23 16:49:45 UTC (rev 742) @@ -477,6 +477,64 @@ */ createInitialRid: function() { return Math.floor( Math.random() * 10000 ); + }, + + pause: function(time) { + var pauseNode = this.createPacketNode(); + pauseNode.setAttribute( "pause", time ); + + /* + * the connection manager SHOULD respond immediately to all pending + * requests (including the pause request) and temporarily increase + * the maximum inactivity period to the requested time. + + this.on("recv", function(packet) { + this.fireEvent( "pause", pauseStruct ); + }, this, {single:true}); + */ + + this.write( pauseNode ); + + this.shutdown(); + + var pauseStruct = { + maxpause: 120, // TODO not hard code me + maxRequests: this.maxRequests, + hold: this.hold, + polling: this.polling, + server: this.server, + port: this.port, + domain: this.domain, + wait: this.wait, + sid: this.sid, + rid: this.rid, + endpoint: this.endpoint, // TODO subclass implementations should handle this + keysSeqKeys : this.keySeq._keys, + keySeqIdx: this.keySeq._idx + }; + + return pauseStruct; + }, + + resume: function(pauseStruct) { + + // this.maxpause = pauseStruct.maxpause; + this.maxpause = pauseStruct.maxpause; + this.hold = pauseStruct.hold; + this.polling = pauseStruct.polling; + this.server = pauseStruct.server; + this.port = pauseStruct.port; + this.wait = pauseStruct.wait; + this.sid = pauseStruct.sid; + this.rid = pauseStruct.rid; + this.domain = pauseStruct.domain; + this.endpoint = pauseStruct.endpoint; + this.maxRequests = pauseStruct.maxRequests; + + this.keySeq._keys = pauseStruct.keysSeqKeys; + this.keySeq._idx = pauseStruct.keySeqIdx; + + this.startup(); } } Modified: trunk/src/site/xdoc/code-samples.xml =================================================================== --- trunk/src/site/xdoc/code-samples.xml 2008-07-21 19:11:43 UTC (rev 741) +++ trunk/src/site/xdoc/code-samples.xml 2008-07-23 16:49:45 UTC (rev 742) @@ -47,12 +47,35 @@ ]]></pre> </subsection> -<subsection name="Suspend a connection"> -<p>... can't do it yet ...</p> -</subsection> +<subsection name="Pause / Resume a connection"> +<pre name="code" class="javascript"><![CDATA[ +// running all content at once will not work. I might update this to add listeners +// and the like someday. until then, use firebug's console and run these 1-by-1. -<subsection name="Resume a connection"> -<p>... can't do it yet ...</p> +// 1, 6 +sp = new Xmpp4Js.Packet.StanzaProvider(); +sp.registerDefaultProviders(); +con = new Xmpp4Js.Connection({transport: {clazz: BOSH_TRANSPORT,endpoint:BOSH_ENDPOINT,useKeys:true},stanzaProvider:sp}); + +// 2 +con.connect("soashable.com"); + +// 3 +loginFlow = new Xmpp4Js.Workflow.Login({con:con});loginFlow.start( "plaintext", "test", "test" ); + +// 4 +con.send( new Xmpp4Js.Packet.Presence() ); + +// 5 +ps = con.pause( 120 ); + +// 7 +con.resume( ps ); + +// 8 +con.send( new Xmpp4Js.Packet.Message( "ha...@so...", "normal", "yoooo I'm alive" ) ); + +]]></pre> </subsection> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |