Skip to content

Commit

Permalink
moved the routing away from the C2S server
Browse files Browse the repository at this point in the history
  • Loading branch information
julien51 committed Oct 2, 2011
1 parent 75c24b9 commit 74c7337
Showing 1 changed file with 6 additions and 82 deletions.
88 changes: 6 additions & 82 deletions lib/xmpp/c2s.js
Expand Up @@ -32,9 +32,9 @@ function C2SServer(options) {
if (options.tls) {
this.credentials = { key: fs.readFileSync(options.tls.keyPath, 'ascii'), cert: fs.readFileSync(options.tls.certPath, 'ascii')};
}

// By user bare JID
this.sessions = {};
// Router
this.router = {};
}

util.inherits(C2SServer, EventEmitter);
Expand All @@ -50,90 +50,14 @@ C2SServer.prototype.acceptConnection = function(socket, options) {

var self = this;
client.addListener('online', function() {
self.registerRoute(client.jid, client);
self.router.registerRoute(client.jid, client);

client.addListener('disconnect', function() {
self.unregisterRoute(client.jid, client);
self.router.unregisterRoute(client.jid, client);
});
});
};

/**
* C2S Router */
C2SServer.prototype.route = function(stanza) {
var self = this;
if (stanza.attrs && stanza.attrs.to) {
var toJid = new JID(stanza.attrs.to);
var userJid = toJid.bare().toString();
var userSessions = this.sessions.hasOwnProperty(userJid) ?
this.sessions[userJid] : [];
if (stanza.is('message')) {
/* Route <message/> to highest priority or most recent */
// TODO: requires presence tracking and should probably move a layer up
} else if (stanza.is('presence')) {
/* Broadcast <presence/> */
userSessions.forEach(function(session) {
session.client.send(stanza);
});
} else {
/* <iq/> only directed */
var sent = false;
userSessions.forEach(function(session) {
if (toJid.resource === session.resource) {
session.client.send(stanza);
sent = true;
}
});
if (!sent) {
// TODO: reply back w/ error
}
}
} else {
// Huh? Who is it for? and why did it end up here?
// TODO: reply with error
}
};

/**
* Registers a route (jid => specific client connection)
*/
C2SServer.prototype.registerRoute = function(jid, client) {
// What if we have a conflict! TOFIX
if (!this.sessions.hasOwnProperty(jid.bare().toString()))
this.sessions[jid.bare().toString()] = {};
this.sessions[jid.bare().toString()][jid.resource] = client;
return true;
};

/**
* Returns the list of jids connected for a specific jid.
*/
C2SServer.prototype.connectedClientsForJid = function(jid) {
jid = new JID(jid);
if (!this.sessions.hasOwnProperty(jid.bare().toString())) {
return [];
}
else {
var jids = [];
for(var resource in this.sessions[jid.bare().toString()]) {
jids.push(new JID(jid.bare().toString() + "/" + resource));
}
return jids;
}
};

/**
* Unregisters a route (jid => specific client connection)
*/
C2SServer.prototype.unregisterRoute = function(jid, client) {
if (!this.sessions.hasOwnProperty(jid.bare().toString())) {
// Hum. What? That can't be.
} else {
delete this.sessions[jid.bare().toString()][jid.resource];
}
return true;
};


function C2SStream(socket, server) {
var self = this;
Expand Down Expand Up @@ -164,7 +88,7 @@ function C2SStream(socket, server) {
});

this.addListener('outStanza', function(stanza) {
self.send(stanza)
self.send(stanza);
});

return self;
Expand Down

0 comments on commit 74c7337

Please sign in to comment.