const database = require('../database/database') const not_found_error = require('../../lib/src/http/errors/not_found_error') const {get_fastest_session_server, run_session} = require('../modules/session_server') const create_session = async (req, res, next) => { const {title} = req.body var free_servers = await database.get_free_session_servers(title) if (!free_servers) { next(new not_found_error('no free session server or title not exists')) return } var session_server = await get_fastest_session_server(free_servers) if (!session_server) { next(new not_found_error('all session servers are not available')) return } var session_id = await database.generate_session_id() if (!session_id) { next(new not_found_error('can not generate session id')) return } var websocket_url = await run_session(session_server.url, session_id, title) if (!websocket_url) { next(new not_found_error('websocket_url not valid')) return } var add_active_session_result = await database.add_active_session(session_server.url, session_id, websocket_url) if (!add_active_session_result) { next(new not_found_error('add_active_session error')) return } res.json({session_id:session_id}) } const connect_session = async (req, res, next) => { var websocket_url = await database.get_session_websocket_url(req.query.session_id) if (!websocket_url) { next(new not_found_error('session not exists')) return } res.json({websocket_url:websocket_url}) } const schedule_session = async (req, res, next) => { } module.exports = { create_session, schedule_session, connect_session }