-
Notifications
You must be signed in to change notification settings - Fork 8
/
SessionManager.js
40 lines (31 loc) · 916 Bytes
/
SessionManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const Session = require("./Session");
function SessionManager() {
var self = this;
self.sessions = {};
}
SessionManager.prototype.getSession = function (args, cb) {
/* args = {ID: nickname, DESTINATION: privkey} */
var self = this;
if (args.ID != undefined && self.sessions[args.ID] != undefined) {
var session = self.sessions[args.ID];
session.reference();
cb(session);
} else {
var session = new Session();
session.on('connect', function () {
session.on("end", function (data) {
delete self.sessions[session.ID];
});
self.sessions[session.ID] = session;
cb(session);
});
session.connect(args);
}
}
SessionManager.prototype.releaseSession = function (args) {
var self = this;
var session = self.sessions[args.ID];
session.dereference();
}
module.exports = new SessionManager();
module.exports.SessionManager = SessionManager;