-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
453 lines (364 loc) · 10.9 KB
/
Copy pathapp.js
File metadata and controls
453 lines (364 loc) · 10.9 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/**
* Module dependencies.
*/
var express = require('express')
, http = require('http')
, path = require('path')
, io = require('socket.io')
, osc = require('node-osc')
, _ = require('underscore')
, conf = require('./config');
var app = express()
, host = '0.0.0.0'
, server = http.createServer(app)
, connection
, oscClient
, cachedRegExes = []
, cachedText = '';
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
// we only hae one route so lets not get too fancy
app.get('/', function(req, res){
res.render('index', conf);
});
app.get('/watch', function(req, res) {
res.render('watch', conf);
});
app.get('/wordfinder/:letters', require('./routes/word-finder'));
// setup the osc server
oscClient = new osc.Client(conf.osc.host, conf.osc.port);
console.log('OSC client running for host "'+conf.osc.host+'", port '+conf.osc.port);
// setup the socket.io connection
connection = io.listen(server);
// listen for connection event
connection.sockets.on('connection', onSocketConnect);
// start the web server
server.listen(app.get('port'), host, onServerStart);
/**
* Event handler for when the express server is started
*/
function onServerStart() {
console.log("Express server listening on port " + app.get('port'));
}
/**
* Event handler for socket.io connection
* @param {Object} socket Socket object for this connection
*/
function onSocketConnect(socket) {
console.log("Socket.io running on port " + app.get('port'));
// register events
socket.on('/com/notioncollective/rules', onRulesUpdate);
socket.on('/com/notioncollective/text', onTextUpdate);
socket.on('/com/notioncollective/key', onTextKeyPress);
socket.on('/com/notioncollective/conf/oschost', onOscHostChange);
socket.on('/com/notioncollective/conf/oscport', onOscPortChange);
socket.on('/com/notioncollective/reset', onReset);
}
function onOscHostChange(data) {
console.log('change osc host to '+data.value)
if(oscClient && data.value) {
oscClient.host = data.value;
}
}
function onOscPortChange(data) {
console.log('change osc port to '+data.value)
if(oscClient && data.value) {
oscClient.port = data.value;
}
}
function onReset() {
console.log('reset sent');
// things that should happen on reset from websocket
// - clear both input fields (this should happen on client)
// - relay reset message to osc
}
/**
* Event handler for rules update from client
* @param {Object} data Data received from client
*/
function onRulesUpdate(data) {
var regExes;
console.log('rules update', data);
regExes = processRegExes(data.text)
if(regExes && regExes.length) {
processText(cachedText, setRules(regExes, data.text));
}
}
/**
* Handler for keypress event from client
*/
function onTextKeyPress() {
console.log('text keypress');
oscClient.send(getChannel('key'), 1);
}
/**
* Event handler for text update from client
* @param {Object} data Data recieved from client
*/
function onTextUpdate(data) {
console.log('text update', data);
processText(setText(data.text), cachedRegExes);
}
/**
* Process a regular expressions string returned from the client. This method
* also validates the given string.
* @param {String} regExesStr A string of space-separated regular expressions
* @return {Array} Will return the array of regular expressions if valid, or `undefined` if invalid.
*/
function processRegExes(regExesStr) {
// @todo: this will currently disallow using the `/` character in any regular expressions
var regExesParser = /(\/.+?\/)+?/g,
regExStrings,
isValid = true,
regExes = [];
console.log('check ' + regExesStr);
// a basic test to make sure we are parsing correctly
if(!regExesParser.test(regExesStr)) {
rulesInvalid(regExesStr);
return;
} else {
// get matches from our rules string -- each of these
// should be a regular expression
regExStrings = regExesStr.match(regExesParser);
console.log('regExStrings', regExStrings);
// cycle through and make sure they're valid,
// and if they are add the regex to our array
regExStrings.forEach(function(reStr) {
var re;
if(isValid) {
try {
// we're slicing off the beginning and end slashes
re = new RegExp(reStr.slice(1, -1), 'g');
regExes.push(re);
} catch(e) {
isValid = false;
}
}
});
// if none were invalidated, let's update
// our cached regexes
if(!isValid) {
rulesInvalid(regExesStr);
return;
}
}
console.log('returning regexes', regExes);
// create regular expressions and return as array
return regExes;
}
/**
* Process the text after updated in the client
* @param {String} text The new text
* @param {Array} regExes An array of regular expressions for processing
*/
function processText(text, regExes) {
var globalCount = 0,
charsCount = text.length,
wordsCount = text.split(/\s+/).length;
// send global counts
processChars(text, regExes);
processWords(text, regExes);
// parse for note messages
parseNotes(text, regExes);
// evaluate individual regexes
regExes.forEach(function(re, i) {
var matches = text.match(re),
count;
if(matches) {
count = matches.length;
globalCount += count;
console.log('test re for exp '+i, matches, count);
// send matches count for this regex
sendToAll(getChannel('count', i), count, {count: count, rule: i, regExp: re.toString()});
// oscClient.send(getChannel('count', i), count);
}
});
console.log('send global matches count: ', globalCount);
// send global matches count
oscClient.send(getChannel('count'), globalCount);
}
function processWords(text, regExes) {
var splitWordsRegEx = /\s+/,
globalCount = text.split(splitWordsRegEx).length;
sendToAll(getChannel('words'), globalCount, {count:globalCount});
regExes.forEach(function(re, i) {
var matches = text.match(re),
count;
if(matches) {
count = _(matches)
.chain()
.map(function(m) {
return m.split(splitWordsRegEx)
})
.flatten()
.value().length;
console.log('send word count for exp '+i, count);
sendToAll(getChannel('words', i), count, {count:count, rule: i, regExp: re.toString()});
}
});
}
function processChars(text, regExes) {
var globalCount = text.length;
sendToAll(getChannel('chars'), globalCount, {count:globalCount});
regExes.forEach(function(re, i) {
var matches = text.match(re),
count;
if(matches) {
count = matches.join().length;
console.log('send char count for exp '+i, count);
sendToAll(getChannel('chars', i), count, {count:count, rule: i, regExp: re.toString()});
}
});
}
/**
* Sends characters count to osc and to client
* @param {Number} count Number of characters
*/
function sendCharsCount(count) {
console.log('send chars count: '+count);
oscClient.send(getChannel('chars'), count);
connection.emit(getChannel('chars'), {count: count});
}
/**
* Sends words count to osc and to client
* @param {Number} count Number of words
*/
function sendWordsCount(count) {
console.log('send words count: '+count);
oscClient.send(getChannel('words'), count);
connection.emit(getChannel('words'), {count: count});
}
/**
* Get a channel name.
* @param {String} name Name of the channel ('notes')
* @param {Number} rule Expression number for channel (optional)
* @return {String} Channel name.
*/
function getChannel(name, rule) {
var ns = '/com/notioncollective/',
channel;
if(_.isFinite(rule)) {
channel = ns+rule+'/'+name;
} else {
channel = ns + name;
}
console.log('channel '+channel);
return channel;
}
/**
* Send information to osc and the socket.io client on the same channel.
* @param {String} channel The channel to send to ('/com/notioncollective/notes')
* @param {Object} oscData Data to use in osc message
* @param {Object} ioData Data to send as socket.io message
*/
function sendToAll(channel, oscData, ioData) {
console.log('send to '+channel+':', oscData, ioData);
oscClient.send(channel, oscData);
connection.emit(channel, ioData);
}
/**
* Parse note values given text and regular expressions. Will send osc messages
* for global notes and notes within specific regular expression matches
* @param {String} text Current text value
* @param {Array} regExes Array of regular expressions
*/
function parseNotes(text, regExes) {
var notesRegEx = /[abcdefg]/g,
globalNotes = _.uniq(text.match(notesRegEx));
if(globalNotes && globalNotes.length) {
oscClient.send(createNotesMessage(getChannel('notes'), globalNotes));
}
regExes.forEach(function(re, i) {
var matches = text.match(re)
, notes;
if(matches) {
notes = _.uniq(matches.join().match(notesRegEx));
if(notes && notes.length) {
oscClient.send(createNotesMessage(getChannel('notes', i), notes));
}
}
});
}
/**
* Create an osc message with note values based on an array of
* note characters.
* @param {String} endpoint Endpoint for osc message
* @param {Array} notes An array of note characters (`['a', 'b', 'f']);
* @return {Object} An osc message with note values.
*/
function createNotesMessage(endpoint, notes) {
var msg = new osc.Message(endpoint),
noteMapping = {
'c' : 60
, 'd' : 62
, 'e' : 64
, 'f' : 65
, 'g' : 67
, 'a' : 69
, 'b' : 71
};
notes.forEach(function(note) {
if(noteMapping[note]) {
msg.append(noteMapping[note]);
}
});
console.log('created notes message for '+endpoint+':', notes);
return msg;
}
/**
* Sends a message to the client for invalid rules. This is so
* that the regular expression can remain synced in clients
* even if it isn't valid.
* @param {String} str The regular expressions string
* @return {String} The regular expressions string
*/
function rulesInvalid(str) {
var msg = {
valid: false
, text: str
};
console.log('send rules invalid update', str)
connection.emit('/com/notioncollective/rules', msg);
return str;
}
/**
* Setter for the stored rules (an array of regular expressions).
* Also sends rules update event to the client.
* @param {Array} re An array of regular expressions
* @param {String} str The original string of regular expressions
* @return {re} The new array of regular expressions
*/
function setRules(re, str) {
var msg = {
valid: true
, text: str
};
cachedRegExes = re;
console.log('send rules update', msg);
connection.emit('/com/notioncollective/rules', msg);
return re;
}
/**
* Setter for the stored text value (also sends update event to client)
* @param {String} str String to set text to
* @return {String} The new text
*/
function setText(str) {
var msg = { text : str };
cachedText = str;
console.log('send text update', msg);
connection.emit('/com/notioncollective/text', msg);
return str;
}