This commit is contained in:
2024-09-26 18:32:50 +05:00
parent 9167785656
commit 86864ac19e
11 changed files with 164 additions and 175 deletions
+52 -29
View File
@@ -30,7 +30,7 @@ require("dotenv/config");
const db_1 = __importDefault(require("./config/db"));
const express_1 = __importStar(require("express"));
const cors_1 = __importDefault(require("cors"));
const os_1 = __importDefault(require("os"));
const os_1 = __importStar(require("os"));
const util_1 = __importDefault(require("util"));
const child_process_1 = require("child_process");
const tree_kill_1 = __importDefault(require("tree-kill"));
@@ -45,6 +45,10 @@ const serverLocation = process.env.SERVER_LOCATION;
const serverName = process.env.SERVER_NAME;
const serverType = process.env.SERVER_TYPE;
const serverHostname = os_1.default.hostname();
const nets = (0, os_1.networkInterfaces)();
const localIP = Object.entries(nets)
.find((net) => net[0] === "Ethernet")?.[1]
?.find((obj) => obj.family === "IPv4")?.address;
async function updateSessionServerData(data) {
try {
await SessionServer_1.default.findOneAndUpdate({ hostname: serverHostname }, data);
@@ -74,7 +78,6 @@ async function getGpuMemoryFree() {
async function getAvailablePorts() {
const cirrusPorts = [14000, 14001, 14002];
for (const cirrusPort of cirrusPorts) {
console.log(cirrusPort);
const activeSession = await ActiveSession_1.default.exists({
location: serverLocation,
name: serverName,
@@ -86,8 +89,8 @@ async function getAvailablePorts() {
return { cirrusPort, uePort };
}
}
async function startSession(buildName) {
const filePath = `C:/PixelStreaming/builds/${buildName}/${buildName}.exe`;
async function startSession(buildName, ownerIp, endAt) {
const filePath = `C:/pixel-streaming/builds/${buildName}/${buildName}.exe`;
const availablePorts = await getAvailablePorts();
if (!availablePorts) {
console.log("No available ports");
@@ -95,7 +98,7 @@ async function startSession(buildName) {
}
const { cirrusPort, uePort } = availablePorts;
const cirrusProcess = (0, child_process_1.execFile)("node", [
`C:/PixelStreaming/signalling-server/cirrus.js`,
`C:/pixel-streaming/signalling-server/cirrus.js`,
`--StreamerPort`,
`${uePort}`,
`--HttpPort`,
@@ -127,27 +130,43 @@ async function startSession(buildName) {
console.log("UE application was not started");
return;
}
const activeSession = await ActiveSession_1.default.create({
location: serverLocation,
name: serverName,
buildName,
cirrusPort,
uePort,
cirrusProcessId,
ueProcessId,
});
const activeSessionId = activeSession.id;
return activeSessionId;
const type = serverType;
try {
const activeSession = await ActiveSession_1.default.create({
location: serverLocation,
name: serverName,
buildName,
type,
cirrusPort,
uePort,
cirrusProcessId,
ueProcessId,
ownerIp,
endAt,
localIP,
});
return activeSession;
}
catch (error) {
(0, tree_kill_1.default)(cirrusProcessId);
(0, tree_kill_1.default)(ueProcessId);
console.log(error.message);
}
}
async function endSession(activeSessionId) {
const activeSession = await ActiveSession_1.default.findByIdAndDelete(activeSessionId);
if (!activeSession) {
console.log("Session with this ID not found");
return;
try {
const activeSession = await ActiveSession_1.default.findByIdAndDelete(activeSessionId);
if (!activeSession) {
console.log("Session with this ID not found");
return;
}
(0, tree_kill_1.default)(activeSession.cirrusProcessId);
(0, tree_kill_1.default)(activeSession.ueProcessId);
console.log("Kill session");
}
catch (error) {
console.log(error.message);
}
(0, tree_kill_1.default)(activeSession.cirrusProcessId);
(0, tree_kill_1.default)(activeSession.ueProcessId);
console.log("Kill session");
}
async function init() {
try {
@@ -156,6 +175,7 @@ async function init() {
name: serverName,
type: serverType,
hostname: serverHostname,
localIP,
}, { upsert: true, new: true });
getGpuMemoryFree();
}
@@ -166,14 +186,17 @@ async function init() {
}
}
app.post("/start", async (req, res) => {
console.log(req.query);
const buildName = req.body.buildName;
if (!buildName) {
for (const [key, value] of Object.entries(req.body)) {
if (value === "null")
req.body[key] = undefined;
}
const { buildName, ownerIp, endAt } = req.body;
console.log("req.body", req.body);
if (!buildName || !ownerIp) {
return res.json({ error: 1 });
}
const result = await startSession(buildName);
console.log("Result: ", result);
res.json({ ok: 1 });
const activeSession = await startSession(buildName, ownerIp, endAt);
res.json(activeSession);
});
app.post("/end", async (req, res) => {
const activeSessionId = req.body.activeSessionId;