-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
134 lines (134 loc) · 5.48 KB
/
main.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
import { spawn } from "child_process";
import * as uuid from "uuid";
import WebSocket from 'ws';
import { mkdirSync, existsSync, writeFileSync, readFileSync, copyFileSync } from "fs";
import { rimraf } from "./lib/utils.js";
const config = JSON.parse(readFileSync('./resources/config.json', 'utf8'));
// at every restart we clear the simulations directory
rimraf(config.simulation_folder);
mkdirSync(config.simulation_folder);
//store the ox-view connection
var clients = new Array();
//Setup the SocketServer
var wss = new WebSocket.Server({ port: config.serverPort, host: config.serverIP });
console.log(`server listening on port ${config.serverPort}`);
wss.on('connection', (connection) => {
let settings;
var top_file, dat_file;
var oxDNA;
// we need to know client index to remove them on 'close' event
var index = clients.push(connection) - 1;
console.log(`processes connected: ${clients.length}`);
if (index >= config.allowed_connections) {
console.log("refused connection");
connection.close();
return;
} // limit connection numbers
var user_id = uuid.v1();
//create a work directory per connection
let dir = `${config.simulation_folder}/${user_id}`;
if (!existsSync(dir)) {
mkdirSync(dir);
}
connection.on('message', (message) => {
if (message === "abort") {
//handle simulation stop
if (oxDNA)
oxDNA.kill();
return;
}
//if relax is running kill it regardless of the message
if (oxDNA)
oxDNA.kill();
//parse incomming congiguration
var data = JSON.parse(message);
let useDNA = true;
top_file = data.top_file;
dat_file = data.dat_file;
//inject oxDNA configuration settings
settings = data.settings;
Object.assign(settings, config.default_oxDNA_settings);
console.log(settings["interaction_type"]);
if (settings["interaction_type"].includes("DNA")) {
useDNA = true;
settings["seq_dep_file_DNA"] = "oxDNA2_sequence_dependent_parameters.txt";
}
else if (settings["interaction_type"].includes("RNA")) {
settings["seq_dep_file_RNA"] = "rna_sequence_dependent_parameters.txt";
useDNA = false;
}
if ("trap_file" in data) {
settings["external_forces"] = "1";
settings["external_forces_file"] = "trap.txt";
//write forces file
writeFileSync(`${dir}/trap.txt`, data.trap_file);
}
if ("par_file" in data) {
settings["parfile"] = "par_file.par";
//write par file (for anm)
writeFileSync(`${dir}/par_file.par`, data.par_file);
}
//construct input file from transmitted settings
let input_file = [];
for (let [key, value] of Object.entries(settings)) {
input_file.push(`${key} = ${value}`);
}
//write topology and configuration into dedicated connection folder
writeFileSync(`${dir}/conf_file.dat`, dat_file);
writeFileSync(`${dir}/last_conf.dat`, dat_file);
writeFileSync(`${dir}/top_file.top`, top_file);
//write input and base parameter files
writeFileSync(`${dir}/${config.input_file}`, input_file.join('\n'));
if (useDNA) {
copyFileSync(`./resources/oxDNA2_sequence_dependent_parameters.txt`, `${dir}/oxDNA2_sequence_dependent_parameters.txt`);
}
else {
copyFileSync(`./resources/rna_sequence_dependent_parameters.txt`, `${dir}/rna_sequence_dependent_parameters.txt`);
}
// perform simulation @ cwd = current working dir
oxDNA = spawn(config.oxDNA, [`${config.input_file}`], { cwd: dir });
console.log(`connection ${index}\t|\trelax\t|\t started`);
// the trick is to have energy and conf file print settings to be the same
if (oxDNA.stdout) {
oxDNA.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
// than we can transfer data easily
if (existsSync(`${dir}/last_conf.dat`)) {
connection.send(JSON.stringify({
dat_file: readFileSync(`${dir}/last_conf.dat`, 'utf8'),
console_log: data.toString()
}));
}
});
}
if (oxDNA.stderr) {
oxDNA.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
}
oxDNA.on('close', (code) => {
console.log(`connection ${index}\t|\trelax\t|\tfinished\t|\tcode: ${code}`);
if (existsSync(`${dir}/last_conf.dat`)) {
connection.send(JSON.stringify({
dat_file: readFileSync(`${dir}/last_conf.dat`, 'utf8'),
console_log: data.toString()
}));
}
});
});
// user disconnected
connection.on('close', (code) => {
// remove user from the list of connected clients
clients.splice(index, 1);
if (oxDNA) {
//stop process just make sure no writing occures
oxDNA.kill();
//TODO: make somehow async or policy speciffic
rimraf(dir);
console.log(`client ${index} id disconnected\t|\tcode: ${code}`);
console.log(`removed assosiated working dir:`);
console.log(dir);
console.log(`processes connected: ${clients.length}`);
}
});
});