-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathserver.js
252 lines (236 loc) · 7.96 KB
/
server.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
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
var http = require('http')
var path = require('path');
var finalhandler = require('finalhandler')
var serveStatic = require('serve-static')
require('./vvvv.js')
var ws = require("nodejs-websocket");
var fs = require("fs");
var vvvvjsservices = require('./backend/vvvvjs-services');
var argv = require('minimist')(process.argv);
var documentRoot = "." //__dirname;
if (argv._.length>2) {
documentRoot = argv._[2];
}
var edit_mode = false;
if (argv.e)
edit_mode = true;
var serve = serveStatic(path.join(documentRoot));
var map = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg'
}
VVVVContext.externalHandlers = [];
var http_hostname = "0.0.0.0";
var http_port = 5000;
var virtual_dir = "/vvvvjs"
try {
var appconf = JSON.parse(fs.readFileSync(process.cwd()+"/vvvvjsapp.json"));
if (appconf.externalHandlers) {
for (var i=0; i<appconf.externalHandlers.length; i++) {
VVVVContext.externalHandlers.push(require(process.cwd()+"/"+appconf.externalHandlers[i]));
}
}
if (appconf.httpHostname)
http_hostname = appconf.httpHostname;
if (appconf.httpPort)
http_port = appconf.httpPort;
if (appconf.virtualVVVVJsDir)
virtual_dir = appconf.virtualVVVVJsDir;
}
catch (e) { console.error(e.message)};
var virtual_dir_regex = new RegExp(virtual_dir+"/(.+)");
var server = http.createServer(function(req, res) {
var match;
if (match = /^\/vvvvjs-service\/([^\?]+)/.exec(req.url)) {
if (match[1]=="create_subpatch") {
vvvvjsservices.createSubpatch(req, res);
return;
}
}
for (var i=0; i<VVVVContext.externalHandlers.length; i++) {
if (VVVVContext.externalHandlers[i].process(req,res)) {
return;
}
}
if (match = virtual_dir_regex.exec(req.url)) {
fs.readFile(path.join(__dirname, match[1]), function(err, data){
if(err){
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
} else {
res.setHeader('Content-type', map[path.parse(match[1]).ext] || 'text/javascript' );
res.end(data);
}
});
return;
}
// only if not processed by external handler
var done = finalhandler(req, res)
serve(req, res, done);
});
server.listen(http_port, http_hostname);
console.log("HTTP Server listening on "+http_hostname+":"+http_port);
if (edit_mode)
console.log("EDIT MODE IS ENABLED.");
VVVVContext.init('./', 'full', function (vvvv) {
VVVVContext.DocumentRoot = documentRoot;
var websocket_server = ws.createServer(function (conn) {
console.log("New connection");
var patch = null;
var mainloop = null;
conn.on("text", function (str) {
var req = JSON.parse(str);
if (patch==null && req.patch && req.app_root) {
console.log("Spawning patch "+req.patch+" in "+req.app_root);
VVVVContext.AppRoot = req.app_root;
patch = new vvvv.Patch(req.patch, function() {
this.serverSync.socket = conn;
});
}
if (req.nodes) {
//console.log('-> '+str);
if (!mainloop)
mainloop = new vvvv.MainLoop(patch, 0.2);
var i=req.nodes.length;
var node = null;
var p = patch.serverSync.patchRegistry[req.patch];
while (i--) {
node = req.nodes[i];
if (!p.nodeMap[node.node_id]) // TODO: this handles the case when a synced nodes is created on the client side, and pin values are sent before the actual update arrived. Should be handled cleaner
continue;
for (var pinname in node.pinValues) {
p.nodeMap[node.node_id].inputPins[pinname].values = node.pinValues[pinname];
p.nodeMap[node.node_id].inputPins[pinname].markPinAsChanged();
}
}
if (mainloop) {
mainloop.stop();
mainloop.start();
}
}
if (req.command) {
if (!edit_mode)
return;
//console.log('receiving patch update for '+vvvv.Helpers.prepareFilePath(req.patch));
var patches = patch.executionContext.Patches[vvvv.Helpers.prepareFilePath(req.patch)];
var i = patches.length;
while (i--) {
patches[i].doLoad(req.command)
patches[i].afterUpdate();
}
}
if (req.save) {
if (!edit_mode)
return;
var p = patch.executionContext.Patches[vvvv.Helpers.prepareFilePath(req.patch)][0];
fs.writeFile(vvvv.Helpers.prepareFilePath(req.patch)+".xml", p.toXML(), function() {
console.log('saved '+req.patch+".xml");
});
fs.writeFile(vvvv.Helpers.prepareFilePath(req.patch), p.exportJSON(), function() {
console.log('saved '+req.patch);
})
}
if (req.message) {
var p = patch.serverSync.patchRegistry[req.patch]
if (typeof p.nodeMap[req.node].handleBackendMessage === 'function')
p.nodeMap[req.node].handleBackendMessage(req.message);
}
})
conn.on("binary", function(inStream) {
var chunks = [];
var totalByteLength = 0;
var meta_data = undefined;
inStream.on("readable", function() {
var newData = inStream.read();
if (!newData)
return;
var offset = 0;
if (meta_data==undefined) {
var meta_len = newData.readUInt16BE(0);
offset = 2;
var meta = "";
for (var i=0; i<meta_len; i++) {
meta += String.fromCharCode(newData.readInt16BE(offset));
offset += 2;
}
meta_data = JSON.parse(meta);
}
var data = newData.buffer.slice(newData.byteOffset, newData.byteOffset + newData.byteLength).slice(offset);
chunks.push(data);
totalByteLength += data.byteLength;
})
inStream.on("end", function() {
var p = patch.serverSync.patchRegistry[meta_data.patch];
if (p && p.nodeMap[meta_data.node] && (typeof p.nodeMap[meta_data.node].handleBackendMessage === 'function')) {
var buf = new Uint8Array(totalByteLength);
var offset=0;
for (var i=0; i<chunks.length; i++) {
var dv2 = new Uint8Array(chunks[i]);
for (var j=0; j<dv2.length; j++) {
buf[offset] = dv2[j];
offset++;
}
}
p.nodeMap[meta_data.node].handleBackendMessage(buf, meta_data);
}
})
})
conn.on("close", function (code, reason) {
if (patch) patch.destroy();
patch = undefined;
if (mainloop) mainloop.stop();
if (mainloop) mainloop.disposing = true;
mainloop = undefined;
console.log("Connection closed");
})
conn.on("error", function(err) {
if (patch) patch.destroy();
patch = undefined;
if (mainloop) mainloop.stop();
if (mainloop) mainloop.disposing = true;
mainloop = undefined;
console.log("Connection closed/reset");
})
}).listen(5001)
});
// fetch and register installed node.js packages
if (argv.e && VVVVContext.name=='nodejs') {
var npm = require('npm');
console.log('Checking for installed Node.js packages ...')
npm.load({loglevel: 'silent', progress: false, prefix: VVVVContext.Root}, function(err) {
if (err)
console.log(err);
else {
npm.commands.list([], function(err, res) {
if (err && err.indexOf("extraneous")!==0) {
console.log(err);
return;
}
(function registerDeps(p) {
for (var package_name in p.dependencies) {
VVVVContext.LoadedLibs[package_name] = true;
registerDeps(p.dependencies[package_name]);
}
})(res);
console.log('Node.js packages scanned. Ready if you are.\n');
})
}
})
}
// launch browser window in app mode
if (argv.mode=='app') {
var bl = require('james-browser-launcher');
bl(function(err, launch) {
launch('http://localhost:'+http_port, {browser: 'chrome'}, function(err, instance) {
console.log('Launched Chrome');
instance.on('stop', function(code) {
process.exit();
})
})
});
}