Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

a bit more new arch

  • Loading branch information...
commit 07315d5f4d5b856e19737316c92f4d6b7a72a55f 1 parent 782dfda
@astro astro authored
Showing with 57 additions and 24 deletions.
  1. +2 −2 lib/server.js
  2. +47 −18 lib/session.js
  3. +6 −2 lib/users.js
  4. +2 −2 xmpp-server.js
View
4 lib/server.js
@@ -26,7 +26,7 @@ exports.run = function(config) {
// Creates the server.
var s2sRouter = new xmpp.Router();
- var c2s = new xmpp.C2SServer(config);
+ var c2s = new xmpp.C2SServer(config.c2s);
var sessions = new SessionManager();
c2s.on('connect', function(conn) {
@@ -34,7 +34,7 @@ exports.run = function(config) {
var jid;
conn.on('authenticate', function(opts, cb) {
var jid = opts.jid;
- User.find(jid, function(user) {
+ User.find(jid.toString(), function(user) {
if (user && user.attrs.password === opts.password) {
cb();
} else
View
65 lib/session.js
@@ -6,36 +6,65 @@
*
* TODO: inherit from EventEmitter
*/
-function SessionManager() {
-}
+exports.SessionManager = function() {
+ this.userSessions = {};
+};
-SessionManager.prototype = {
- addClient: function(jid, conn) {
- },
+exports.SessionManager.prototype.addClient = function(jid, conn) {
+ var userSession = this.userSessions[jid];
+ if (!userSession)
+ userSession = this.userSessions[jid] = new exports.UserSession(jid);
- send: function(stanza) {
- }
+ userSession.addClient(conn);
};
/**
* Aggregates ClientSession instances
*/
-function UserSession(jid) {
+exports.UserSession = function(jid) {
+ this.jid = jid;
+ this.clientSessions = [];
}
+exports.UserSession.prototype.addClient = function(conn) {
+ this.clientSessions.push(new exports.ClientSession(conn));
+};
+
/**
* One client instance of a user
*/
-function ClientSession(conn) {
+exports.ClientSession = function(conn) {
+ var that = this;
+ this.conn = conn;
-}
+ this.conn.on('stanza', function(stanza) {
+ that.onStanza(stanza);
+ });
+};
+
+var stanzaHooks = [];
+exports.ClientSession.prototype.onStanza = function(stanza) {
+ var that = this;
+ var hooks = stanzaHooks.slice(0); // clone
-ClientSession.prototype = {
- /**
- * Handle stanza from client
- *
- * @param {Function} cb Callback called with (stanza, nextHookCb, replyCb)
- */
- addStanzaHook: function(cb) {
- }
+ var replyCb = function(reply) {
+ that.conn.send(reply);
+ };
+
+ var nextCb = function() {
+ var cb;
+ if ((cb = hooks.shift())) {
+ cb(stanza, nextCb, replyCb);
+ }
+ };
+ nextCb();
+};
+
+/**
+ * Handle stanza from client
+ *
+ * @param {Function} cb Callback called with (stanza, nextHookCb, replyCb)
+ */
+exports.addStanzaHook = function(cb) {
+ stanzaHooks.push(cb);
};
View
8 lib/users.js
@@ -18,11 +18,15 @@ User.key = function(jid) {
};
User.find = function(jid, cb) {
+ if (jid === "test@localhost")
+ // REMOVE THIS LATER
+ return cb(new User(jid, { password: "test" }));
+
var self = this;
client.hgetall(User.key(jid), function(err, obj) {
if(isEmpty(obj)) {
cb(null);
- }
+ }
else {
cb(new User(jid, obj));
}
@@ -57,7 +61,7 @@ User.register = function(jid, password, options) {
});
}
});
-
+
}
exports.User = User;
View
4 xmpp-server.js
@@ -5,10 +5,10 @@ if(process.argv.length === 2) {
process.exit(1);
}
-var configuration = require(process.argv[process.argv.length -1]);
+var config = require(process.argv[process.argv.length -1]);
var server = require('./lib/server.js');
-server.run(configuration.config);
+server.run(config);
Please sign in to comment.
Something went wrong with that request. Please try again.