[Xmpp4js-commit] SF.net SVN: xmpp4js:[753] trunk/src/main/javascript
Status: Beta
Brought to you by:
h-iverson
From: <h-i...@us...> - 2008-07-27 20:22:30
|
Revision: 753 http://xmpp4js.svn.sourceforge.net/xmpp4js/?rev=753&view=rev Author: h-iverson Date: 2008-07-27 20:22:38 +0000 (Sun, 27 Jul 2008) Log Message: ----------- added initial stream error handling code Modified Paths: -------------- trunk/src/main/javascript/XmppConnection.js trunk/src/main/javascript/transport/BOSH.js trunk/src/main/javascript/transport/Base.js trunk/src/main/javascript/transport/Script.js Modified: trunk/src/main/javascript/XmppConnection.js =================================================================== --- trunk/src/main/javascript/XmppConnection.js 2008-07-26 20:39:04 UTC (rev 752) +++ trunk/src/main/javascript/XmppConnection.js 2008-07-27 20:22:38 UTC (rev 753) @@ -112,6 +112,7 @@ scope: this, termerror: this.onTerminalError, error: this.onError, + streamerror: this.onStreamError, beginsession: this.onBeginSession, endsession: this.onEndSession, pause: function(pauseStruct) { @@ -251,6 +252,16 @@ }, /** + * Shutdown before firing error event to give the listener an opportunity + * to reconnect. + * @private + */ + onStreamError: function(packetNode, errorNode, errorCode, text) { + this.shutdown(); + this.fireEvent( "error", true, packetNode, errorCode, text ); + }, + + /** * Handle non-terminal errors * @param {DomElement} packetNode * @private Modified: trunk/src/main/javascript/transport/BOSH.js =================================================================== --- trunk/src/main/javascript/transport/BOSH.js 2008-07-26 20:39:04 UTC (rev 752) +++ trunk/src/main/javascript/transport/BOSH.js 2008-07-27 20:22:38 UTC (rev 753) @@ -125,10 +125,9 @@ session is still active. */ if( response.status == -1 ) { - // we aborted the transaction, do nothing. + // we aborted the connection, do nothing. close? } else if( !success || response.status != 200 ) { - // 17.2 Terminal Binding Conditions - this.shutdown(); + // Deprecated HTTP error conditions var condition = null; if( packetNode != null ) { @@ -140,25 +139,28 @@ } else { condition = "undefined-condition"; } - - var title = Xmpp4Js.PacketFilter.TerminalErrorPacketFilter.conditions[ condition ].title; - var message = Xmpp4Js.PacketFilter.TerminalErrorPacketFilter.conditions[ condition ].message; - this.fireEvent( "termerror", title, message, packetNode ); - } else if( packetNode.getAttribute("type").toString() == "error" ) { - // 17.3 Recoverable Binding Conditions + var title = "Unknown Error"; + var message = "There was an unknown error with the connection."; - // TODO this should attempt to resend all packets back - // to the one that created the error. This could be - // implemented by putting each sent packet into a queue - // and removing it upon a successful response. - // - // Ideally this error event would not even be visible beyond - // the the BOSH transport. - - this.fireEvent( "error", packetNode ); + var errorPacketNode = this.createPacketNode(); + errorPacketNode.setAttribute( "type", "terminate" ); + errorPacketNode.setAttribute( "condition", condition ); + + try { + this.handleErrors( errorPacketNode ); + } catch(e) { + this.shutdown(); + } } else { - this.fireEvent( "recv", packetNode ); + try { + // this will throw an exception if there is an error. + this.handleErrors( packetNode ); + + this.fireEvent( "recv", packetNode ); + } catch(e) { + this.shutdown(); + } } } } Modified: trunk/src/main/javascript/transport/Base.js =================================================================== --- trunk/src/main/javascript/transport/Base.js 2008-07-26 20:39:04 UTC (rev 752) +++ trunk/src/main/javascript/transport/Base.js 2008-07-27 20:22:38 UTC (rev 753) @@ -187,6 +187,9 @@ */ termerror : true, + + streamerror : true, + /** * @event sessionStarted * @@ -304,6 +307,7 @@ * @private */ shutdown: function() { +;;; console.debug( "Transport Shutdown (stopping tasks)" ); this.isSessionOpen = false; this.taskRunner.stop( this.sendQueueTask ); this.taskRunner.stop( this.sendPollTask ); @@ -372,6 +376,8 @@ * @private */ sendPoll: function() { +;;; console.debug( "Send Poll" ); + // if we're trying to poll too frequently /*var now = new Date().getTime(); if( this.lastPoll != undefined && this.polling != 0 && (now - this.lastPoll < (this.polling * 1000)) ) { @@ -429,7 +435,7 @@ */ addRid: function( packetNode ) { if( this.rid !== null ) { - packetNode.setAttribute( "rid", this.rid++ ); + packetNode.setAttribute( "rid", ++this.rid ); } }, @@ -546,6 +552,52 @@ // give others an opportunity to deserialize properties this.fireEvent( "resume", pauseStruct ); + }, + + handleErrors: function(packetNode) { + var errorNode = packetNode.getElementsByTagNameNS("http://etherx.jabber.org/streams","error"); + errorNode = errorNode.getLength() > 0 ? errorNode.item(0) : null; + + // HACK these errors should be given with terminate / remote-stream-error but in Openfire they are not. + if( errorNode == null && packetNode.getAttribute("type").toString() == "terminate" ) { + var condition = packetNode.getAttribute( "condition" ).toString(); + + var title = Xmpp4Js.PacketFilter.TerminalErrorPacketFilter.conditions[ condition ].title; + var message = Xmpp4Js.PacketFilter.TerminalErrorPacketFilter.conditions[ condition ].message; + + this.fireEvent( "termerror", packetNode, title, message ); + throw new Error( "Error in packet" ); + } else if( packetNode.getAttribute("type").toString() == "error" ) { + // 17.3 Recoverable Binding Conditions + + // TODO this should attempt to resend all packets back + // to the one that created the error. This could be + // implemented by putting each sent packet into a queue + // and removing it upon a successful response. + // + // Ideally this error event would not even be visible beyond + // the the BOSH transport. + + this.fireEvent( "error", packetNode ); + throw new Error( "Error in packet" ); + } else if(errorNode != null) { + // loop through stream nodes to find the condition and + // optionally text + var childNodes = errorNode.getChildNodes(); + for( var i = 0; i < childNodes.getLength(); i++ ) { + var node = childNodes.item(i); + if( node.getNamespaceURI() == "urn:ietf:params:xml:ns:xmpp-streams" ) { + if( node.getLocalName() == "text" ) { + var text = node.getText(); + } else { + var errorCode = node.getLocalName(); + } + } + } + + this.fireEvent( "streamerror", packetNode, errorNode, errorCode, text ); + throw new Error( "Error in packet" ); + } } } Modified: trunk/src/main/javascript/transport/Script.js =================================================================== --- trunk/src/main/javascript/transport/Script.js 2008-07-26 20:39:04 UTC (rev 752) +++ trunk/src/main/javascript/transport/Script.js 2008-07-27 20:22:38 UTC (rev 753) @@ -135,14 +135,18 @@ // we can't find out anything about what the error is. var condition = "undefined-condition"; - var title = Xmpp4Js.PacketFilter.TerminalErrorPacketFilter.conditions[ condition ].title; - var message = Xmpp4Js.PacketFilter.TerminalErrorPacketFilter.conditions[ condition ].message; + var title = "Unknown Error"; + var message = "There was an unknown error with the connection."; var packetNode = this.createPacketNode(); packetNode.setAttribute( "type", "terminate" ); packetNode.setAttribute( "condition", condition ); - this.fireEvent( "termerror", title, message, packetNode ); + try { + this.handleErrors( packetNode ); + } catch(e) { + this.shutdown(); + } }, /** @@ -152,45 +156,19 @@ * @private */ onWriteResponse: function( xml ) { - this.openRequestCount--; - + this.openRequestCount--; + // TODO character replacement (18.3)? + var packetNode = new DOMImplementation().loadXML( xml ).documentElement; - var packetNode = new DOMImplementation().loadXML( xml ).documentElement; - - if(packetNode.getAttribute("type").toString() == "terminal") { + try { + // this will throw an exception if there is an error. + this.handleErrors( packetNode ); + + this.fireEvent( "recv", packetNode ); + } catch(e) { this.shutdown(); - - var condition = null; - if( packetNode != null ) { - condition = packetNode.getAttribute( "condition" ).toString(); - } else if( !response.status ) { - condition = "undefined-condition"; - } else if( response.status != 200 ){ - condition = "status."+response.status; - } else { - condition = "undefined-condition"; - } - - var title = Xmpp4Js.PacketFilter.TerminalErrorPacketFilter.conditions[ condition ].title; - var message = Xmpp4Js.PacketFilter.TerminalErrorPacketFilter.conditions[ condition ].message; - - this.fireEvent( "termerror", title, message, packetNode ); - } else if( packetNode.getAttribute("type").toString() == "error" ) { - // 17.3 Recoverable Binding Conditions - - // TODO this should attempt to resend all packets back - // to the one that created the error. This could be - // implemented by putting each sent packet into a queue - // and removing it upon a successful response. - // - // Ideally this error event would not even be visible beyond - // the the BOSH transport. - - this.fireEvent( "error", packetNode ); - } else { - this.fireEvent( "recv", packetNode ); - } + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |