[Pkg-javascript-commits] [ltx] 66/469: router refactorized

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:01:01 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository ltx.

commit 5ea709677e1949cdc5a9856ad0d56bfa390f8264
Author: Astro <astro at spaceboyz.net>
Date:   Sun Sep 5 16:55:43 2010 +0200

    router refactorized
---
 lib/xmpp/router.js | 138 ++++++++++++++++++++++++++++++++---------------------
 lib/xmpp/server.js |  50 +++++++++----------
 2 files changed, 106 insertions(+), 82 deletions(-)

diff --git a/lib/xmpp/router.js b/lib/xmpp/router.js
index a498878..29ce999 100644
--- a/lib/xmpp/router.js
+++ b/lib/xmpp/router.js
@@ -20,30 +20,84 @@ dbgStream = function(tag, stream) {
     };
 };
 
+/**
+ * Represents a domain we host with connections to federated services
+ */
 function DomainContext(domain) {
     this.domain = domain;
     this.s2sIn = {};
     this.s2sOut = {};
 }
 
-DomainContext.prototype.getOut = function(domain) {
+/**
+ * Buffers until stream has been verified via Dialback
+ */
+DomainContext.prototype.send = function(stanza) {
+    if (stanza.root)
+	stanza = stanza.root();
+
+    // TODO: return on empty to
+    destDomain = new JID.JID(stanza.attrs.to).domain;
+    var outStream = this.getOutStream(destDomain);
+
+    if (outStream.isVerified)
+	outStream.send(stanza);
+    else {
+	outStream.queue = outStream.queue || [];
+	outStream.queue.push(stanza);
+    }
+};
+
+/**
+ * Does only buffer until stream is established, used for Dialback
+ * communication itself.
+ *
+ * returns the stream
+ */
+DomainContext.prototype.sendRaw = function(stanza, destDomain) {
+    if (stanza.root)
+	stanza = stanza.root();
+
+    var outStream = this.getOutStream(destDomain);
+    var send = function() {
+	outStream.send(stanza)
+    };
+
+    if (outStream.isOnline)
+	send();
+    else
+	outStream.addListener('online', send);
+
+    return outStream;
+};
+
+DomainContext.prototype.getOutStream = function(domain) {
     var self = this;
 
+    // TODO: check incoming as well
     if (this.s2sOut.hasOwnProperty(domain)) {
+	// There's one already
 	return this.s2sOut[domain];
     } else {
+	// Setup a new outgoing connection
 	var outStream = this.s2sOut[domain] =
 	    Server.makeOutgoingServer(domain);
 	dbgStream('outgoing', outStream);
-	outStream.dbKey = generateKey();
+	outStream.addListener('error', function() {
+	    // TODO: purge queue
+	    delete self.s2sOut[domain];
+	    outStream.end();
+	});
+
+	// Prepare dialback
 	outStream.addListener('online', function() {
-	    outStream.dialbackKey(self.domain, domain, outStream.dbKey);
+	    outStream.isOnline = true;
+	    outStream.dbKey = generateKey();
+	    outStream.send(Server.dialbackKey(self.domain, domain, outStream.dbKey));
 	});
 	outStream.addListener('dialbackResult', function(from, to, isValid) {
-	    console.log({outDialbackResult:arguments});
 	    if (isValid) {
-		outStream.isValid = true;
-		console.log({outStreamQueue:outStream.queue});
+		outStream.isVerified = true;
 		if (outStream.queue) {
 		    outStream.queue.forEach(function(stanza) {
 			outStream.send(stanza);
@@ -54,29 +108,11 @@ DomainContext.prototype.getOut = function(domain) {
 		outStream.emit('error', new Error('Dialback failure'));
 	    }
 	});
-	outStream.addListener('error', function() {
-	    // TODO: purge queue
-	    delete self.s2sOut[domain];
-	});
 
 	return outStream;
     }
 };
 
-DomainContext.prototype.send = function(stanza) {
-    var self = this;
-    // TODO: return on empty to
-    var domain = (new JID.JID(stanza.attrs.to)).domain;
-
-    var outStream = this.getOut(domain);
-    if (outStream.isValid)
-	outStream.send(stanza);
-    else {
-	outStream.queue = outStream.queue || [];
-	outStream.queue.push(stanza);
-    }
-};
-
 DomainContext.prototype.verifyDialback = function(domain, id, key) {
     var outStream;
     if (this.s2sOut.hasOwnProperty(domain) &&
@@ -85,7 +121,7 @@ DomainContext.prototype.verifyDialback = function(domain, id, key) {
 	var isValid = outStream.streamAttrs.id === id &&
 	    outStream.dbKey === key;
 
-	outStream.dialbackResult(this.domain, domain, isValid);
+	outStream.send(Server.dialbackResult(this.domain, domain, isValid));
 	return isValid;
     } else
 	return false;
@@ -105,48 +141,40 @@ DomainContext.prototype.verifyDialback = function(domain, id, key) {
  * * keepAlive
  */
 function Router(s2sPort) {
+    var self = this;
     this.ctxs = {};
 
-    net.createServer(this.acceptConnection).
-	listen(s2sPort || 5269);
+    net.createServer(function(stream) {
+	self.acceptConnection(stream);
+    }).listen(s2sPort || 5269);
 }
 exports.Router = Router;
 
 Router.prototype.acceptConnection = function(stream) {
+    var self = this;
     dbgStream('incoming', stream);
-    var domain;
-    stream.verifyDialback = function(from, to, id, key) {
-	domain = from;
-	return self.verifyDialback(from, to, id, key);
-    };
+
     Server.makeIncomingServer(stream);
+
+    // incoming server wants to verify an outgoing connection of ours
     stream.addListener('dialbackVerify', function(from, to, id, key) {
-	isValid = self.verifyDialback(from, to, id, key);
-	stream.dialbackVerified(to, from, id, isValid);
+	var isValid = self.verifyDialback(from, to, id, key);
+	stream.send(Server.dialbackVerified(to, from, id, isValid));
     });
+    // incoming server wants us to verify this connection
     stream.addListener('dialbackKey', function(from, to, key) {
-	var outStream = self.getContext(to).getOut(from);
-	var sendVerify = function() {
-	    outStream.dialbackVerify(to, from, stream.streamId, key);
-
-	    var onVerified;
-	    onVerified = function(from, to, id, isValid) {
-		stream.dialbackResult(to, from, isValid);
-		
-		outStream.removeListener('dialbackVerified', onVerified);
-	    };
-	    outStream.addListener('dialbackVerified', onVerified);
+	var ctx = self.getContext(to);
+	var outStream = ctx.sendRaw(Server.dialbackVerify(to, from, stream.streamId, key),
+				    from);
+
+	var onVerified;
+	onVerified = function(from, to, id, isValid) {
+	    // TODO: what if outgoing connection is gone?
+	    ctx.sendRaw(Server.dialbackResult(to, from, isValid), from);
+	    outStream.removeListener('dialbackVerified', onVerified);
 	};
-	if (outStream.writable)
-	    sendVerify();
-	else {
-	    var connectHook;
-	    var connectHook = function() {
-		sendVerify();
-		outStream.removeListener('connect', step);
-	    };
-	    outStream.addListener('connect', connectHook);
-	}
+	outStream.addListener('dialbackVerified', onVerified);
+    });
 };
 
 Router.prototype.send = function(stanza) {
diff --git a/lib/xmpp/server.js b/lib/xmpp/server.js
index b2582ae..c1967e8 100644
--- a/lib/xmpp/server.js
+++ b/lib/xmpp/server.js
@@ -53,35 +53,31 @@ function initServer(self) {
 	} else
 	    self.emit('stanza', stanza);
     });
-    
-    self.dialbackKey = function(from, to, key) {
-	self.send(new xml.Element('db:result', { to: to,
-						 from: from }).
-		  t(key)
-		 );	
-    };
-    self.dialbackVerify = function(from, to, id, key) {
-	self.send(new xml.Element('db:verify', { to: to,
-						 from: from,
-						 id: id }).
-		  t(key)
-		 );
-    };
-    self.dialbackVerified = function(from, to, id, isValid) {
-	self.send(new xml.Element('db:verify', { to: to,
-						 from: from,
-						 id: id,
-						 type: isValid ? 'valid' : 'invalid' })
-		 );
-    };
-    self.dialbackResult = function(from, to, isValid) {
-	self.send(new xml.Element('db:result', { to: to,
-						 from: from,
-						 type: isValid ? 'valid' : 'invalid' })
-		 );	
-    };
 }
 
+exports.dialbackKey = function(from, to, key) {
+    return new xml.Element('db:result', { to: to,
+					  from: from }).
+	t(key);
+};
+exports.dialbackVerify = function(from, to, id, key) {
+    return new xml.Element('db:verify', { to: to,
+					  from: from,
+					  id: id }).
+	t(key);
+};
+exports.dialbackVerified = function(from, to, id, isValid) {
+    return new xml.Element('db:verify', { to: to,
+					  from: from,
+					  id: id,
+					  type: isValid ? 'valid' : 'invalid' });
+};
+exports.dialbackResult = function(from, to, isValid) {
+    return new xml.Element('db:result', { to: to,
+					  from: from,
+					  type: isValid ? 'valid' : 'invalid' });
+};
+
 exports.makeIncomingServer = function(self) {
     Connection.makeConnection(self);
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git



More information about the Pkg-javascript-commits mailing list