-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualiser.js
137 lines (114 loc) · 4.61 KB
/
visualiser.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
//---------------------------------------------------------------------
//
// DPD visualiser
// created for the POETS project
//
//---------------------------------------------------------------------
// creates a node.js based server used to render the incoming particle movements from the simulation
var fs = require('fs');
var path = require('path');
var cdir = process.cwd();
// load in the config file
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
console.log("Loaded in config file");
console.log("---------------------------------");
console.log("frames_location: "+config.frames);
console.log("state filename: "+config.state);
console.log("---------------------------------");
// copy over the node_modules directory
// https://stackoverflow.com/questions/13786160/copy-folder-recursively-in-node-js
function copyFileSync( source, target ) {
var targetFile = target;
//if target is a directory a new file with the same name will be created
if ( fs.existsSync( target ) ) {
if ( fs.lstatSync( target ).isDirectory() ) {
targetFile = path.join( target, path.basename( source ) );
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync( source, target ) {
var files = [];
//check if folder needs to be created or integrated
var targetFolder = path.join( target, path.basename( source ) );
if ( !fs.existsSync( targetFolder ) ) {
fs.mkdirSync( targetFolder );
}
//copy
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );
files.forEach( function ( file ) {
var curSource = path.join( source, file );
if ( fs.lstatSync( curSource ).isDirectory() ) {
copyFolderRecursiveSync( curSource, targetFolder );
} else {
copyFileSync( curSource, targetFolder );
}
} );
}
}
// actually copy the files
copyFolderRecursiveSync(__dirname+'/node_modules/', './');
// config loaded now load in the libraries
const express = require('express');
const app = express();
const WebSocket = require('ws');
// reading from STDIN: https://stackoverflow.com/questions/20086849/how-to-read-from-stdin-line-by-line-in-node
var readline = require('readline');
var r1 = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
// open our web socket
const wss = new WebSocket.Server({port: 8079});
// send a particle position update to the rendered instance
function sendUpdate(wss, update_str) {
// https://github.com/websockets/ws#simple-server
wss.clients.forEach(function each(client) {
if(client !== wss && client.readyState == WebSocket.OPEN) {
client.send(update_str);
//console.log(update_str);
}
});
}
// updates the frames folder
function update_frames(f) {
//copy state.json to config.frames directory
copyFileSync(config.state, config.frames+'/state_'+f+'.json');
app.get('/_frames/state_'+f+'.json', (req,res) => res.sendFile(path.join(cdir+'/'+config.frames+'/state_'+f+'.json')));
}
// create the _frames folder if it does not exist
if(!fs.existsSync(config.frames)){
fs.mkdirSync(config.frames);
}
// for initial setup find out how many frames are there already and save it as metadata in _meta.json
// get the number of already completed frames from the frames folder and store it in an _meta.json file
var frame_cnt;
fs.readdir(config.frames, (err, files) => {
frame_cnt = files.length;
files.forEach( function(file) {
//console.log("Adding get rule for ./frames/" + file);
app.get('/_frames/'+file, (req,res) => res.sendFile(path.join(cdir+'/'+config.frames+'/'+file)));
});
// write the frame_cnt to the _meta.json file
frame_cnt_json = "{ \"num_frames\":"+frame_cnt+"}"
fs.writeFile("./_meta.json", frame_cnt_json, function (err) {
if(err) {
return console.log(err);
}
console.log("The _meta.json file was saved\n");
});
// provide a GET rule for the _meta.json file
app.get('/meta.json', (req, res) => res.sendFile(path.join(cdir+'/_meta.json')));
});
// get stdin data which will be passed to the rendered graph
r1.on('line', function(line) {
sendUpdate(wss, line);
update_frames(frame_cnt);
frame_cnt = frame_cnt + 1;
});
app.get('/', (req, res) => res.sendFile(path.join(__dirname+'/src/index.html')));
app.get('/state.json', (req, res) => res.sendFile(path.join(cdir+'/'+config.state)));
app.get('/d3-3d.js', (req, res) => res.sendFile(path.join(__dirname+'/src/d3-3d.js')));
app.listen(3000, () => console.log('listening on port 3000'))