Made the initialization of the xmppClient an asynchronous call. This included modifying xmppClient.js so that connect() can be passed a callback function that will get called once the connection has succeeded or failed. For most of our purposes this is probably a better API than what we had before where you call waitForConnection() and it busy-waits until the connection has succeeded or failed.

This commit is contained in:
jonathandicarlo@jonathan-dicarlos-macbook-pro.local 2008-06-12 17:35:44 -07:00
Родитель 9e6d103089
Коммит 3f2b6465d8
2 изменённых файлов: 43 добавлений и 17 удалений

Просмотреть файл

@ -96,14 +96,14 @@ BookmarksEngine.prototype = {
this.__proto__.__proto__._init.call( this, pbeId );
if ( Utils.prefs.getBoolPref( "xmpp.enabled" ) ) {
dump( "Starting XMPP client for bookmark engine..." );
// TODO call startXmppClient asynchronously?
this._startXmppClient();
// TODO catch errors if connection fails.
this._startXmppClient.async(this);
//this._startXmppClient();
}
},
_startXmppClient: function BmkEngine__startXmppClient() {
// TODO this should probably be called asynchronously as it can take a while.
// To be called asynchronously.
let self = yield;
// Get serverUrl and realm of the jabber server from preferences:
let serverUrl = Utils.prefs.getCharPref( "xmpp.server.url" );
@ -127,7 +127,7 @@ BookmarksEngine.prototype = {
clientPassword,
transport,
auth );
let self = this;
let bmkEngine = this;
let messageHandler = {
handle: function ( messageText, from ) {
/* The callback function for incoming xmpp messages.
@ -145,16 +145,25 @@ BookmarksEngine.prototype = {
let commandWord = words[0];
let directoryName = words[1];
if ( commandWord == "share" ) {
self._incomingShareOffer( directoryName, from );
bmkEngine._incomingShareOffer( directoryName, from );
} else if ( commandWord == "stop" ) {
self._incomingShareWithdrawn( directoryName, from );
bmkEngine._incomingShareWithdrawn( directoryName, from );
}
}
}
this._xmppClient.registerMessageHandler( messageHandler );
this._xmppClient.connect( realm );
this._xmppClient.connect( realm, self.cb );
yield;
if ( this._xmppClient._connectionStatus == this._xmppClient.FAILED ) {
this._log.warn( "Weave can't log in to xmpp server: xmpp disabled." );
} else if ( this._xmppClient._connectionStatus == this._xmppClient.CONNECTED ) {
this._log.info( "Weave logged into xmpp OK." );
}
yield;
self.done();
},
_incomingShareOffer: function BmkEngine__incomingShareOffer( dir, user ) {
/* Called when we receive an offer from another user to share a
directory.
@ -167,6 +176,7 @@ BookmarksEngine.prototype = {
But since we don't have notification in place yet, I'm going to skip
right ahead to creating the incoming share.
*/
dump( "I was offered the directory " + dir + " from user " + dir );
},
@ -205,8 +215,8 @@ BookmarksEngine.prototype = {
// Create the outgoing share folder on the server
// TODO do I need to call these asynchronously?
this._createOutgoingShare.async( this, selectedFolder, username );
this._updateOutgoingShare.async( this, selectedFolder, username );
//this._createOutgoingShare.async( this, selectedFolder, username );
//this._updateOutgoingShare.async( this, selectedFolder, username );
/* Set the annotation on the folder so we know
it's an outgoing share: */
@ -218,9 +228,13 @@ BookmarksEngine.prototype = {
// Send an xmpp message to the share-ee
if ( this._xmppClient ) {
let msgText = "share " + folderName;
this._xmppClient.sendMessage( username, msgText );
}
if ( this._xmppClient._connectionStatus == this._xmppClient.CONNECTED ) {
let msgText = "share " + folderName;
this._xmppClient.sendMessage( username, msgText );
} else {
this._log.info( "XMPP connection not available for share notification." );
}
}
/* LONGTERM TODO: in the future when we allow sharing one folder
with many people, the value of the annotation can be a whole list

Просмотреть файл

@ -53,6 +53,7 @@ XmppClient.prototype = {
this._iqResponders = [];
this._nextIqId = 0;
this._pendingIqs = {};
this._callbackOnConnect = null;
},
__parser: null,
@ -81,10 +82,17 @@ XmppClient.prototype = {
namespace */
},
_finishConnectionAttempt: function() {
if ( this._callbackOnConnect ) {
this._callbackOnConnect.call();
}
},
setError: function( errorText ) {
LOG( "Error: " + errorText );
this._error = errorText;
this._connectionStatus = this.FAILED;
this._finishConnectionAttempt();
},
onIncomingData: function( messageText ) {
@ -148,7 +156,7 @@ XmppClient.prototype = {
this.setError( this._authenticationLayer.getError() );
} else if ( response == this._authenticationLayer.COMPLETION_CODE ){
this._connectionStatus = this.CONNECTED;
LOG( "We be connected!!" );
this._finishConnectionAttempt();
} else {
this._transportLayer.send( response );
}
@ -296,12 +304,16 @@ XmppClient.prototype = {
this.setError( errorText );
},
connect: function( host ) {
// Do the handshake to connect with the server and authenticate.
connect: function( host, callback ) {
/* Do the handshake to connect with the server and authenticate.
callback is optional: if provided, it will be called (with no arguments)
when the connection has either succeeded or failed. */
if ( callback ) {
this._callbackOnConnect = callback;
}
this._transportLayer.connect();
this._transportLayer.setCallbackObject( this );
this._transportLayer.send( this._makeHeaderXml( host ) );
this._connectionStatus = this.CALLED_SERVER;
// Now we wait... the rest of the protocol will be driven by
// onIncomingData.