Verified Commit 41705537 authored by Camil Staps's avatar Camil Staps 🚀

Randomise iTasks port as well

parent 63b7c411
...@@ -127,12 +127,16 @@ serveElectron :: ...@@ -127,12 +127,16 @@ serveElectron ::
!(String ElectronProcess UniversalConnectionDetails -> Task Connection) !(String ElectronProcess UniversalConnectionDetails -> Task Connection)
!*World -> *World !*World -> *World
serveElectron modOpts startConnection w = doTasksWithOptions serveElectron modOpts startConnection w = doTasksWithOptions
(\cli opts (\[arg0:argv] opts
# electron_opts = # electron_opts =
{ port = 0 { port = 0
, debug = False , debug = False
} }
# (cli,electron_opts,opts) = parseBaseOptions (tl cli) electron_opts opts # (cli,electron_opts,opts) = parseBaseOptions argv electron_opts opts
# mbOptions = defaultEngineCLIOptions () [arg0:cli] opts
| isError mbOptions
-> liftError mbOptions
# opts = snd (fromOk mbOptions)
# (electron_opts,opts,startup) = modOpts cli electron_opts opts # (electron_opts,opts,startup) = modOpts cli electron_opts opts
# main = onStartup (task electron_opts.ElectronOptions.port) # main = onStartup (task electron_opts.ElectronOptions.port)
| electron_opts.debug | electron_opts.debug
...@@ -272,7 +276,7 @@ where ...@@ -272,7 +276,7 @@ where
} }
parseBaseOptions :: ![String] !ElectronOptions !EngineOptions -> (![String], !ElectronOptions, !EngineOptions) parseBaseOptions :: ![String] !ElectronOptions !EngineOptions -> (![String], !ElectronOptions, !EngineOptions)
parseBaseOptions ["--port":p:rest] electron_opts opts = parseBaseOptions ["--electron-port":p:rest] electron_opts opts =
parseBaseOptions rest {ElectronOptions | electron_opts & port=toInt p} opts parseBaseOptions rest {ElectronOptions | electron_opts & port=toInt p} opts
parseBaseOptions cli electron_opts opts = (cli,electron_opts,opts) parseBaseOptions cli electron_opts opts = (cli,electron_opts,opts)
......
...@@ -157,37 +157,52 @@ class Component { ...@@ -157,37 +157,52 @@ class Component {
const DEFAULTS={ const DEFAULTS={
host: 'localhost', host: 'localhost',
itasks_port: 8080,
port: 1234, port: 1234,
debug: true debug: true
}; };
var wait_for_tcp_port; var wait_for_tcp_ports;
if (typeof ipcRenderer=='undefined'){ if (typeof ipcRenderer=='undefined'){
/* main process; pick a random TCP port */ /* main process; pick a random TCP port */
const temp_server=net.createServer(); const temp_server=net.createServer();
wait_for_tcp_port=new Promise ((resolve,reject) => { const temp_server_p=new Promise ((resolve,reject) => {
temp_server.listen (0,() => { temp_server.listen (0,() => {
DEFAULTS.port=temp_server.address().port; DEFAULTS.port=temp_server.address().port;
temp_server.close(() => resolve()); temp_server.close(() => resolve());
}); });
}); });
const temp_server_2=net.createServer();
const temp_server_p_2=new Promise ((resolve,reject) => {
temp_server_2.listen (0,() => {
DEFAULTS.itasks_port=temp_server_2.address().port;
temp_server_2.close(() => resolve());
});
});
wait_for_tcp_ports=Promise.all ([temp_server_p,temp_server_p_2]);
ipcMain.on ('synchronous-message',(ev,arg) => { ipcMain.on ('synchronous-message',(ev,arg) => {
if (arg=='port') if (arg=='itasks-port')
ev.returnValue=DEFAULTS.itasks_port;
else if (arg=='port')
ev.returnValue=DEFAULTS.port; ev.returnValue=DEFAULTS.port;
}); });
} else { } else {
/* renderer process; get TCP port from main process */ /* renderer process; get TCP port from main process */
DEFAULTS.itasks_port=ipcRenderer.sendSync ('synchronous-message','itasks-port');
DEFAULTS.port=ipcRenderer.sendSync ('synchronous-message','port'); DEFAULTS.port=ipcRenderer.sendSync ('synchronous-message','port');
wait_for_tcp_port=Promise.resolve(); wait_for_tcp_ports=Promise.resolve();
} }
function run (_opts) { function run (_opts) {
return wait_for_tcp_port.then(() => { return wait_for_tcp_ports.then(() => {
const opts=DEFAULTS; const opts=DEFAULTS;
Object.assign (opts,_opts); Object.assign (opts,_opts);
const server=spawn ('./'+opts.app+'.exe',['--port',opts.port],{stdio: 'inherit'}); const args=['--electron-port',opts.port,'--port',opts.itasks_port];
const server=spawn ('./'+opts.app+'.exe',args,{stdio: 'inherit'});
server.on ('close',code => { server.on ('close',code => {
if (code!==0 && code!==null) if (code!==0 && code!==null)
console.log ('iTasks exited with exit code '+code); console.log ('iTasks exited with exit code '+code);
...@@ -199,7 +214,7 @@ function run (_opts) { ...@@ -199,7 +214,7 @@ function run (_opts) {
} }
function connect (_opts) { function connect (_opts) {
return wait_for_tcp_port.then(() => { return wait_for_tcp_ports.then(() => {
const opts=DEFAULTS; const opts=DEFAULTS;
Object.assign (opts,_opts); Object.assign (opts,_opts);
......
Markdown is supported
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