-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfanout.js
191 lines (156 loc) · 4.95 KB
/
fanout.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
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
/*
* fanout.js
* a fanout messaging server for node.js
* by @jazzychad - Chad Etzel
* MIT Licensed
*/
var tcp = require("net"),
sys = require("sys");
// The .bind method from Prototype.js
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
Array.prototype.remove = function(e) {
for(var i = 0; i < this.length; i++)
if(e == this[i]) this.splice(i, 1);
};
// Array Remove - By John Resig (MIT Licensed)
Array.remove = function(array, from, to) {
var rest = array.slice((to || from) + 1 || array.length);
array.length = from < 0 ? array.length + from : from;
return array.push.apply(array, rest);
};
var msgEmitter = new process.EventEmitter();
var handleMessage = function handleMessage(conn, socket, data) {
sys.puts('[' + conn.name + ']' + ' data: ' + data);
if (data == "time") {
socket.write(Date.now() + "\n");
}
if (data.indexOf("subscribe ") == 0) {
conn.addchannel(data.split(' ')[1]);
conn.subscribe();
} else if (data.indexOf("unsubscribe ") == 0) {
conn.removechannel(data.split(' ')[1]);
/* update subscriptions by calling subscribe */
conn.subscribe();
}
};
var handleControllerMessage = function handleControllerMessage(socket, channel, data) {
msgEmitter.emit(channel, channel, data);
};
function Client(connection) {
this.socket = connection;
this.name = null;
this.timer = null;
this.channels = [];
this.listeners = [];
}
/* adds channel. must use "subscribe" to take effect */
Client.prototype.addchannel = function(channel) {
sys.puts('adding sub: ' + channel);
this.removechannel(channel);
this.channels.push(channel);
};
/* removes channel. also removes associated listener immediately */
Client.prototype.removechannel = function(channel) {
//sys.puts('removing sub');
/* remove channel if it exists */
this.channels.remove(channel);
/* remove listener */
var listener = this.listeners[channel];
if (listener) {
msgEmitter.removeListener(channel, listener);
}
};
Client.prototype.subscribe = function() {
sys.puts('subs:' + JSON.stringify(this.channels));
this.channels.forEach(function(channel) {
var listener = this.listeners[channel];
if (listener) {
msgEmitter.removeListener(channel, listener);
}
}.bind(this));
this.listeners = [];
this.channels.forEach(function(channel) {
var listener = function(c, msg) {
this.socket.write(c + "!" + msg + "\n");
}.bind(this);
this.listeners[channel] = listener;
msgEmitter.addListener(channel, listener);
}.bind(this));
};
Client.prototype.deconstruct = function() {
this.channels.forEach(function(channel) {
var listener = this.listeners[channel];
if (listener) {
msgEmitter.removeListener(channel, listener);
}
}.bind(this));
};
var connections = [];
var cnt = 0;
var server = tcp.createServer(function(socket) {
var conn = new Client(socket);
connections.push(conn);
conn.name = ++cnt;
socket.setTimeout(0);
socket.setNoDelay();
socket.setEncoding("utf8");
sys.puts("client connected!");
conn.addchannel("all");
conn.subscribe();
socket.addListener("connect", function() {
socket.write("debug!connected...\n");
});
socket.addListener("data", function(data) {
//sys.puts('raw data ' + data);
var dataarr = data.split("\n");
var l = dataarr.length;
for (var jj = 0; jj < dataarr.length-1; jj++) {
var dataline = dataarr[jj];
handleMessage(conn, socket, dataline);
}
});
socket.addListener("eof", function() {
socket.close();
});
socket.addListener("end", function() {
/* unsubscribe from all here (remove all listeners) */
conn.deconstruct();
connections.remove(conn);
conn = null;
sys.puts("Client connection closed.");
});
});
var controller = tcp.createServer(function(socket) {
sys.puts("Controller connected");
socket.setTimeout(0);
socket.setEncoding("ascii");
socket.setNoDelay();
socket.addListener("eof", function() {
socket.close();
});
socket.addListener("end", function() {
sys.puts("Controller closed.");
});
socket.addListener("data", function(data) {
sys.puts('raw data: ' + data);
var dataarr = data.split("\n");
var l = dataarr.length;
for (var jj = 0; jj < dataarr.length-1; jj++) {
var dataline = dataarr[jj];
var i = dataline.indexOf(' ');
var channel = dataline.slice(0,i);
var msg = dataline.slice(i+1);
handleControllerMessage(socket, channel, msg);
}
});
});
var client_port = 8880;
var controller_port = 8890;
server.listen(client_port);
controller.listen(controller_port);