Files
pixel-streaming-coordinator/src/controllers/session.js
T

60 lines
1.7 KiB
JavaScript

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
let 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
}
let session_server = await get_fastest_session_server(free_servers)
if (!session_server) {
next(new not_found_error('all session servers are not available'))
return
}
let session_id = await database.generate_session_id()
if (!session_id) {
next(new not_found_error('can not generate session id'))
return
}
let websocket_url = await run_session(session_server.url, session_id, title)
if (!websocket_url) {
next(new not_found_error('can not run session'))
return
}
let add_active_session_result = await database.add_active_session(title, 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) => {
let 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
}