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

Use randomised port for communication with iTasks server

parent f0c3402d
......@@ -24,8 +24,6 @@ import iTasks
import iTasks.Internal.IWorld
import iTasks.Internal.Task
PORT :== 1234
derive JSONEncode TransportMethod
derive JSONDecode TransportMethod
......@@ -106,22 +104,24 @@ serveElectron ::
!([String] EngineOptions -> EngineOptions)
!(ElectronProcess UniversalConnectionDetails -> Task Connection)
!*World -> *World
serveElectron modOpts startConnection w =
doTasksWithOptions
(\cli opts -> Ok (onStartup task,modOpts cli opts))
w
serveElectron modOpts startConnection w = doTasksWithOptions
(\cli opts
# (cli,opts) = parseBaseOptions (tl cli) opts
# opts = modOpts cli opts
-> Ok (onStartup (task opts.serverPort),opts))
w
where
task :: Task ()
task =
task :: !Int -> Task ()
task port =
getPIE >>- \pie ->
(connect pie -&&- loop pie) @!
(connect port pie -&&- loop pie) @!
()
getPIE :: Task PrelinkedInterpretationEnvironment
getPIE = mkInstantTask \_ iworld=:{IWorld | abcInterpreterEnv} -> (Ok abcInterpreterEnv,iworld)
connect :: !PrelinkedInterpretationEnvironment -> Task ()
connect pie = tcplisten PORT True tcpShare
connect :: !Int !PrelinkedInterpretationEnvironment -> Task ()
connect port pie = tcplisten port True tcpShare
{ ConnectionHandlers
| onConnect = onConnect
, onData = onData
......@@ -234,6 +234,11 @@ where
-> w
}
parseBaseOptions :: ![String] !EngineOptions -> (![String], !EngineOptions)
parseBaseOptions ["--port":p:rest] opts =
parseBaseOptions rest {opts & serverPort=toInt p}
parseBaseOptions cli opts = (cli,opts)
runInElectron :: !ElectronProcess !(JSVal *JSWorld -> *JSWorld) -> Task ()
runInElectron processId f = sendSerializedTCPMsg processId "run" (wrapInitFunction f)
......
......@@ -2,7 +2,7 @@ const fs=require ('fs').promises;
const net=require ('net');
const path=require ('path');
const spawn=require ('child_process').spawn;
const {app}=require ('electron');
const {app,ipcMain,ipcRenderer}=require ('electron');
/* This module is assumed to be called from the iTasks -www directory. This
* directory also contains abc-interpreter.js, so we can do a relative import.
......@@ -124,28 +124,53 @@ const DEFAULTS={
debug: true
};
function run (_opts) {
const opts=DEFAULTS;
Object.assign (opts,_opts);
const server=spawn ('./'+opts.app,{stdio: 'inherit'});
server.on ('close',code => {
if (code!==0 && code!==null)
console.log ('iTasks exited with exit code '+code);
app.quit(code);
var wait_for_tcp_port;
if (typeof ipcRenderer=='undefined'){
/* main process; pick a random TCP port */
const temp_server=net.createServer();
wait_for_tcp_port=new Promise ((resolve,reject) => {
temp_server.listen (0,() => {
DEFAULTS.port=temp_server.address().port;
temp_server.close(() => resolve());
});
});
process.on ('exit',() => server.kill (9));
// TODO: this assumes that the iTasks server has started up by the time we
// have initialized the ABC interpreter, which might not be the case
return Component.instantiate (opts);
ipcMain.on ('synchronous-message',(ev,arg) => {
if (arg=='port')
ev.returnValue=DEFAULTS.port;
});
} else {
/* renderer process; get TCP port from main process */
DEFAULTS.port=ipcRenderer.sendSync ('synchronous-message','port');
wait_for_tcp_port=Promise.resolve();
}
function run (_opts) {
return wait_for_tcp_port.then(() => {
const opts=DEFAULTS;
Object.assign (opts,_opts);
const server=spawn ('./'+opts.app,['--port',opts.port],{stdio: 'inherit'});
server.on ('close',code => {
if (code!==0 && code!==null)
console.log ('iTasks exited with exit code '+code);
app.quit(code);
});
process.on ('exit',() => server.kill (9));
// TODO: this assumes that the iTasks server has started up by the time we
// have initialized the ABC interpreter, which might not be the case
return Component.instantiate (opts);
});
}
function connect (_opts) {
const opts=DEFAULTS;
Object.assign (opts,_opts);
return wait_for_tcp_port.then(() => {
const opts=DEFAULTS;
Object.assign (opts,_opts);
return Component.instantiate (opts);
return Component.instantiate (opts);
});
}
module.exports={
......
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