-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbevents.js
More file actions
229 lines (197 loc) · 7.85 KB
/
bevents.js
File metadata and controls
229 lines (197 loc) · 7.85 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var events = require('events');
var http = require('http');
var url = require('url');
var config = {
domain : 'localhost',
machineList : {},
hserver : null, //Will contain the event handler.
isListening : false,
port : 3868,
announcement : "/bevents/hi/",
eventpost : "/bevents/postevent/",
//Assign ourself a random ID... this should be good enough.
idString : Math.floor(Math.random(new Date().getTime()) * 10000000000) +
"-" + Math.floor(Math.random(new Date().getTime()) * 10000000000),
baseEmitter: null
};
function ipToHash(ipString) {
return (ipString + "").replace(/\.|:|-/g,"l");
}
function announceResHandler(res, callback) {
if ( callback )
callback();
res.on('data', function (chunk) {
var asString = ""+chunk;
if (( asString.match(/^\[|{/) ) && ( asString != "[[object Object]]" )) {
var dataPassed = JSON.parse(asString);
//Is array?
if ( chunk.constructor == [].constructor ) {
//Chunk should be list of machines they know about. We say "hi",
//to them, if we don't already know about them.
var machines = JSON.parse(chunk);
if ( machines && typeof machines == 'object' ) {
for ( var machine in machines ) {
if ( config.machineList[machine] == null ) {
config.machineList[machine] = machines[machine]; //Place holder object for that machine
//Put an announcement on the queue.
setTimeout((function(machine) {
return (function() { announceSelf(machine); });
})(machine), 0);
}
}
}
}
}
else
{
console.log("Not something we know how to parse...");
console.log(chunk + "");
console.log("got that?");
}
});
}
function announceSelf(machine, callback) {
var options = {
host: (machine ? machine : config.domain),
port: config.port,
path: config.announcement + "?id="+config.idString
}
this.options = options;
var getReq = http.get(options, (function(callback) {
return (function( res ) {
announceResHandler(res, callback);
});
})(callback));
getReq.on('error', (function(callback) { return ( function(e) {
console.log("Got error: " + e.message);
setTimeout(function() { announceSelf(); }, 1000);
if ( callback )
callback();
}); })(callback));
}
function serverTrigger(eventObj) {
config.baseEmitter.emit(eventObj.type + ".bvent", eventObj);
}
function postEvent(eventObj) {
for ( var machine in config.machineList ) {
var options = {
host: (config.machineList[machine] ? config.machineList[machine].ip : config.domain),
port: config.port,
path: config.eventpost + "?id="+config.idString + "&e=" + JSON.stringify(eventObj)
}
http.get(options, function(res) {
res.on('data', function (chunk) { });
}).on('error', function(e) {
console.log("Got post event error: " + e.message);
});
}
}
function connectionHandler(req, res) {
var urlObj = url.parse(req.url, true);
//Check to see if someone is just registering with us.
if ((urlObj.pathname + "").match(/.*\/hi\/.*/)) {
//if the id passed is our own... we're talking
//to ourself. Try again to talk to someone else in
//5 seconds.
if ( urlObj.query.id == config.idString ) {
//Whomever is first onto the network, could potentially
//be the only guy broadcasting... but at least one server
//will be letting everyone else know about everyone else as
//they come online.
setTimeout(announceSelf, 10000);
res.end();
return; //We're done talking to ourself.
}
//So, we're not talking to ourself. Tell them what we know.
var buildList = JSON.stringify(config.machineList);
//Store the connector to the machinelist.
var ipAddress = req.socket.remoteAddress;
var ipString = ipToHash(ipAddress);
if (!config.machineList[ipString]) {
config.machineList[ipString] = {ip:ipAddress};
}
//Send out the list as valid js.
res.end(buildList);
}
//Or maybe they are trying to hand us an event from the
//other part of the fleet.
else if ((urlObj.pathname + "").match(/.*postevent.*/)) {
//Check to see if this event was sent by us.
if ( urlObj.query.id == config.idString ) {
//We generated the event. Bail.
res.end();
return;
}
//So... we didn't create the event. Trigger baby, let the
//standard event emitter do the work.
if ( urlObj.query.e ) {
var eventObj = JSON.parse(urlObj.query.e);
serverTrigger(eventObj);
}
//And we're done.
res.end();
}
else {
res.end("Uh... sorry:" + urlObj.pathname);
return;
}
}
function beventsObj() {
config.baseEmitter = new events.EventEmitter();
this.config = config;
}
//Starts our event http server. Specifies which domain to ping
//and port to listen on.
beventsObj.prototype.createServer = function(port, domain, optionalMachineList) {
//Check to see if the port is overriden.
if ( port ) config.port = port;
//For auto-discovery of machines
config.domain = ( domain ? domain : 'localhost' );
//And if auto-discovery can't work... see if they just gave us a fleet
//to work amongst.
if ( optionalMachineList ) {
for ( var i = 0; i < optionalMachineList.length; i++ ) {
config.machineList[ipToHash(optionalMachineList[i])] = {ip:optionalMachineList[i]};
}
}
//This is the server that will start handling events and notifications.
config.hserver = http.createServer(connectionHandler);
//Make sure you announce yourself before you start listening.
//This means you'll always talk to someone else before
//accidentally talking to yourself and being secluded.
announceSelf(null, function() {
//Now that we tried to get someone elses response start up our server.
if ( !config.isListening ) {
config.isListening = true;
config.hserver.listen(config.port);
}
});
}
beventsObj.prototype.on = function(eventName, callback) {
//Mirror EventEmitter, but be smart about other machines.
config.baseEmitter.on(eventName+".bvent", (function(callback ) {
return (function(eventObj) {
if ( eventObj.id && eventObj.id != config.idString ) {
callback(eventObj);
}
else
postEvent(eventObj);
});
})(callback));
}
beventsObj.prototype.removeListener = function(eventName, listener) {
//Mirror removeListener
config.baseEmitter.removeListener(eventName+".bvent", listener);
}
function trigger (eventName, data) {
if ( data == null )
data = {};
var eventObj = {};
eventObj.id = config.idString;
eventObj.type = eventName;
eventObj.data = data;
config.baseEmitter.emit(eventName+".bvent", eventObj);
}
beventsObj.prototype.emit = trigger;
beventsObj.prototype.trigger = trigger;
exports.bevents = new beventsObj();