Commit 9eecfc58 authored by LIGNEUL CLEMENT's avatar LIGNEUL CLEMENT
Browse files

add remote islands file

parent 04398c86
import { spawnSync } from "child_process";
import { createSocket, Socket } from "dgram";
import { readFileSync } from "fs-extra";
import { exit } from "process";
import { AbortController } from "node-abort-controller";
import { existsSync } from "fs";
import ip from 'ip';
// output color
let zero = '\x1b[0m';
let red = '\x1b[1;31m';
let orange = '\x1b[1;33m';
let green = '\x1b[1;32m';
export class Remote_Island {
ez_filename:string = "";
executable_name: string = "";
run_options: string[] = [];
compilation_options: string[] = [];
ez_dir: string = "";
connector:Socket; // socket
controller: AbortController;
my_ipv4: string = "127.0.0.1";
my_port: number = 6000;
/**
* @param ez_path Path to the ez file
* @param config_file Path to the config file
*/
constructor(ez_path:string, config_file:string){
if(!existsSync(ez_path)){
console.log(`${ez_path} doesn't exists`);
exit(1);
}
if(!existsSync(config_file)){
console.log(`${ez_path} doesn't exists`);
exit(1);
}
var splitted = ez_path.split("/");
this.ez_filename = splitted[splitted.length - 1];
this.executable_name = this.ez_filename.slice(0,-3);
this.ez_dir = splitted.splice(0, splitted.length - 1).join("/")+"/";
// read config file
try{
var data = JSON.parse(readFileSync(config_file, 'utf-8'));
} catch (error) {
console.log(error)
}
this.compilation_options = data.compilation.join(" ");
this.run_options = data.run_options.join(" ");
/////////// mettre ici les autres options comme taille de batch, fichier ip ?
// this.my_ipv4 = child.stdout.toString(); //"127.0.0.1";//ip.address(undefined, 'ipv4');
this.my_ipv4 = ip.address(undefined, 'ipv4');
console.log(this.my_ipv4);
this.controller = new AbortController();
const signal = this.controller.signal;
this.connector = createSocket({type:'udp4', signal});
this.connector.on('error', (err)=>{
console.log(err);
this.connector.close();
});
this.connector.on('listening', ()=>{
console.log(`\nSocket opened and listenning on ${orange}${this.connector.address().address}:${this.connector.address().port}${zero}`);
});
this.connector.on('message', (msg, rinfo)=>{
console.log(`\nReceive "${orange}${msg}${zero}" from ${rinfo.address}:${rinfo.port}`);
var txt = msg.toString();
if(txt === 'exit'){
console.log(`\n${green}Received exit => end of program${zero}\n`);
this.connector.close();
exit(0);
}
});
this.connector.on('close', ()=>{
console.log(`port closed`);
exit(0);
});
this.connector.bind(6000);
}
/**
* @brief Compile the file
*/
compile(){
console.log("\n\n########## Compilation ##########\n")
this.compilation_options.push(this.ez_filename);
console.log(`\n${orange}Running : easena ${this.compilation_options.join(" ")}${zero}\n`);
var child = spawnSync('easena', this.compilation_options, {cwd:this.ez_dir});
if(child.status != 0){
console.log(`${red}${child.output.join("")}${zero}`);
} else {
console.log(`${child.output.join("")}`);
}
if(child.status === 0){
// make
console.log(`\n${orange}Running : make -C ${this.ez_dir} -f ${this.executable_name}.mak${zero}\n`);
child = spawnSync('make', ["-C", this.ez_dir, "-f", this.executable_name + '.mak'], {cwd:this.ez_dir, shell:true});
if(child.status != 0){
console.log(`${red}${child.output.join("")}${zero}`);
} else {
console.log(`${child.stdout.toString()}`);
console.log(`${red}${child.stderr.toString()}${zero}`);
console.log("\n########## Compilation succeed ##########\n\n");
}
}
}
send_message(msg:string){
this.connector.send(msg, this.my_port, this.my_ipv4)
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment