-
Notifications
You must be signed in to change notification settings - Fork 0
/
weasel.js
134 lines (110 loc) · 4.32 KB
/
weasel.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Generated by CoffeeScript 1.7.1
(function() {
var CONFIG, DrowsyPersistence, Weasel, argv, events, faye, fs, http, weasel,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
http = require('http');
faye = require('faye');
fs = require('fs');
events = require('events');
DrowsyPersistence = require('./drowsy_persistence').DrowsyPersistence;
argv = require('optimist').usage('Usage:\n\t$0 [-c config.json]').argv;
if (argv.c != null) {
CONFIG = argv.c;
} else {
CONFIG = './config.json';
}
Weasel = (function(_super) {
__extends(Weasel, _super);
function Weasel() {
return Weasel.__super__.constructor.apply(this, arguments);
}
Weasel.prototype.setupLogging = function() {
this.bayeux.bind('handshake', function(cid) {
return console.log("" + (Date.now()) + " [handshk] " + cid);
});
this.bayeux.bind('subscribe', function(cid, channel) {
return console.log("" + (Date.now()) + " [sub ] " + cid + " " + channel);
});
this.bayeux.bind('unsubscribe', function(cid, channel) {
return console.log("" + (Date.now()) + " [unsub ] " + cid + " " + channel);
});
this.bayeux.bind('publish', function(cid, channel, data) {
return console.log("" + (Date.now()) + " [pub ] " + cid + " " + channel, data);
});
this.bayeux.bind('disconnect', function(cid, channel, data) {
return console.log("" + (Date.now()) + " [disconn] " + cid);
});
this.on('persist_success', function(cid, channel, data, res) {
return console.log("" + (Date.now()) + " [drwsy.save] " + cid + " " + channel + " (" + res.statusCode + ")");
});
return this.on('persist_failure', function(cid, channel, data, res) {
return console.warn("" + (Date.now()) + " [drwsy.fail] " + cid + " " + channel + " (" + res.statusCode + ")");
});
};
Weasel.prototype.setupPersistence = function() {
var drowsy;
if (this.config.drowsy) {
drowsy = new DrowsyPersistence(this.config.drowsy);
} else {
console.warn("Drowsy persistence will be disabled because no 'drowsy' config was provided!");
return;
}
drowsy.on('persist_success', (function(_this) {
return function(cid, channel, data, res) {
return _this.emit('persist_success', cid, channel, data, res);
};
})(this));
drowsy.on('persist_failure', (function(_this) {
return function(cid, channel, data, res) {
return _this.emit('persist_failure', cid, channel, data, res);
};
})(this));
return this.bayeux.addExtension(drowsy);
};
Weasel.prototype.loadConfig = function() {
var config, configPath, defaults, key, val;
defaults = {
port: 7777,
mount: '/faye',
timeout: 30
};
configPath = CONFIG;
if (fs.existsSync(configPath)) {
config = JSON.parse(fs.readFileSync(configPath));
for (key in defaults) {
val = defaults[key];
if (config[key] == null) {
config[key] = defaults[key];
}
}
console.log("Configuration loaded from '" + CONFIG + "':", config);
} else {
config = defaults;
console.warn("Configuration file '" + CONFIG + "' not found! Using defaults:", config);
}
return this.config = config;
};
Weasel.prototype.setupFaye = function() {
return this.bayeux = new faye.NodeAdapter({
mount: this.config.mount,
timeout: this.config.timeout
});
};
Weasel.prototype.start = function() {
var server;
server = http.createServer();
this.bayeux.attach(server);
server.listen(this.config.port);
return console.log("... awake and listening on http://localhost:" + this.config.port + this.config.mount);
};
return Weasel;
})(events.EventEmitter);
console.log("Waking the Weasel...");
weasel = new Weasel();
weasel.loadConfig();
weasel.setupFaye();
weasel.setupLogging();
weasel.setupPersistence();
weasel.start();
}).call(this);