const port_alloc = require('../../lib/port_alloc.js') const titles = require('../titles.json') const config = require('../config.json') const { spawn } = require('node:child_process') const child_process = require('child_process') const http_client = require('../../lib/http_client.js') const app_port_begin = config.session_ports.app_begin const http_port_begin = config.session_ports.http_begin const app_args_static = config.app_args_static const app_args_ip = config.app_args_runtime.ip const server_ip = '127.0.0.1'//config.ip const external_domain = config.external_domain const app_args_port = config.app_args_runtime.port const start_nginx = config.start_nginx const server_id = config.server_id const webrtc_cirrus_path = config.webrtc_args_static.cirrus_path const coordinator_domain = config.coordinator_domain // const http_client = require('../lib/http_client.js') // SHALL FIX IT IN FUTURE BECAUSE IT IS NOT THE REST API const ports_count = 50 var app_port_alloc = new port_alloc(app_port_begin, ports_count) var http_port_alloc = new port_alloc(http_port_begin, ports_count) // close all own sessions on session server if emergency shutdown http_client.gets({ host: coordinator_domain, path: `/session_server/emergency_shutdown?id=${server_id}` }, async function(answer) { }, function(err) { //next(Error('session server not working')) }) const create_session = async (req, res, next) => { //create?title=name&next_var=0 try { var app_info titles.forEach((title) => { if (title.title == req.query.title) { app_info = title } }) if (!app_info) throw new Error('app info not exists') var app_port var http_port try { app_port = app_port_alloc.get() http_port = http_port_alloc.get() } catch(e) { //console.log('count error') throw new Error(e) } // start app_proc const app_proc = spawn(app_info.path, [].concat(app_args_static, [app_args_ip + server_ip, app_args_port + app_port.toString()]), { detached: true }) app_proc.on('error', function(err) { throw new Error('create proc error') }) app_proc.on('close', (code) => { app_port_alloc.free(app_port) }) // start webrtc server const webrtc_proc = child_process.fork(webrtc_cirrus_path, [http_port.toString(), app_port.toString()], { detached: true }) webrtc_proc.on('error', function(err) { throw new Error('create webrtc error') }) webrtc_proc.on('close', (code) => { http_port_alloc.free(http_port) // if webrtc server closed, kill app proc and send session end message to server if (process.platform === 'win32') { spawn('taskkill', ['/pid', app_proc.pid, '/f', '/t']) } else { app_proc.kill('SIGINT') } http_client.gets({ host: coordinator_domain, path: `/session/close?session_id=${req.query.session_id}` }, async function(answer) { }, function(err) { next(Error('session server not working')) }) }) setTimeout(() => { res.json({msg:'SESSION_CREATED', link:`https://${external_domain}/${start_nginx}${http_port}/`}) }, 1000) } catch (e) { next(e) } } const connect_session = async (req, res, next) => { } const close_session = async (req, res, next) => { } module.exports = { create_session, connect_session, close_session }