Permalink
Browse files

router is now a module as well

  • Loading branch information...
1 parent 4a53fdc commit e49cdb5a34f469f503c57665341876ba2eafd1ed @julien51 julien51 committed Dec 4, 2011
Showing with 29 additions and 22 deletions.
  1. +2 −20 lib/server.js
  2. +27 −2 modules/router.js
View
@@ -2,7 +2,7 @@ var xmpp = require('node-xmpp');
// Loading all modules needed
var Logger = require('../modules/logger');
-var Router = require('../modules/router').Router;
+var Router = require('../modules/router');
var Offline = require('../modules/offline');
var Version = require('../modules/version');
var Presence = require('../modules/presence');
@@ -18,7 +18,6 @@ exports.run = function(config, ready) {
// Creates the server.
var server = new xmpp.C2SServer(config);
- server.router = new Router(server); // Using the right C2S Router.
var s2s = new xmpp.Router(config.s2sPort, config.bindAddress); // We only use 5269 has the default S2S port.
// S2S plugged to C2S.
@@ -28,6 +27,7 @@ exports.run = function(config, ready) {
server.s2s = s2s;
// Configure the mods at the server level!
+ Router.configure(server, config.router);
Logger.configure(server, config.logger);
Offline.configure(server, config.offline);
Version.configure(server, config.version);
@@ -64,24 +64,6 @@ exports.run = function(config, ready) {
}
});
});
-
- // When the user is online, let's register the route. there could be other things involed here... like presence!
- client.on('online', function() {
- server.router.registerRoute(client.jid, client);
- });
-
- // When the user is offline, we remove him from the router.
- client.on('end', function() {
- if(client.jid) {
- // We may not have a jid just yet if the client never connected before
- server.router.unregisterRoute(client.jid, client);
- }
- });
-
- // this callback is called when the client sends a stanza.
- client.on('stanza', function(stanza) {
- server.router.route(stanza, client); // Let's send the stanza to the router and let the router decide what to do with it.
- });
});
// On Disconnect event. When a client disconnects
View
@@ -3,11 +3,14 @@ var ltx = require('ltx');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
+// server.router = new Router(server); // Using the right C2S Router.
+
+
/**
* C2S Router */
function Router(server) {
- this.server = server;
this.sessions = {};
+ this.server = server;
}
util.inherits(Router, EventEmitter);
@@ -97,4 +100,26 @@ Router.prototype.unregisterRoute = function(jid, client) {
};
-exports.Router = Router;
+exports.configure = function(server, config) {
+ var router = new Router(server); // Using the right C2S Router.
+ server.on('connect', function(client) {
+ // When the user is online, let's register the route. there could be other things involed here... like presence!
+ client.on('online', function() {
+ router.registerRoute(client.jid, client);
+ });
+
+ // When the user is offline, we remove him from the router.
+ client.on('end', function() {
+ if(client.jid) {
+ // We may not have a jid just yet if the client never connected before
+ router.unregisterRoute(client.jid, client);
+ }
+ });
+
+ // this callback is called when the client sends a stanza.
+ client.on('stanza', function(stanza) {
+ router.route(stanza, client); // Let's send the stanza to the router and let the router decide what to do with it.
+ });
+ });
+ server.router = router; // We attach the router to the server. (Maybe we want to use an event for this actually to indicate that a new router was attached to the server?)
+}

0 comments on commit e49cdb5

Please sign in to comment.