upd
This commit is contained in:
+62
-33
@@ -2,7 +2,7 @@ import "dotenv/config";
|
||||
import connectDB from "./config/db";
|
||||
import express, { json } from "express";
|
||||
import cors from "cors";
|
||||
import os from "os";
|
||||
import os, { networkInterfaces } from "os";
|
||||
import util from "util";
|
||||
import { exec, execFile } from "child_process";
|
||||
import treeKill from "tree-kill";
|
||||
@@ -22,6 +22,12 @@ const serverName = process.env.SERVER_NAME;
|
||||
const serverType = process.env.SERVER_TYPE;
|
||||
const serverHostname = os.hostname();
|
||||
|
||||
const nets = networkInterfaces();
|
||||
|
||||
const localIP = Object.entries(nets)
|
||||
.find((net) => net[0] === "Ethernet")?.[1]
|
||||
?.find((obj) => obj.family === "IPv4")?.address;
|
||||
|
||||
async function updateSessionServerData(data: any) {
|
||||
try {
|
||||
await SessionServer.findOneAndUpdate({ hostname: serverHostname }, data);
|
||||
@@ -59,8 +65,6 @@ async function getAvailablePorts() {
|
||||
const cirrusPorts = [14000, 14001, 14002];
|
||||
|
||||
for (const cirrusPort of cirrusPorts) {
|
||||
console.log(cirrusPort);
|
||||
|
||||
const activeSession = await ActiveSession.exists({
|
||||
location: serverLocation,
|
||||
name: serverName,
|
||||
@@ -75,8 +79,12 @@ async function getAvailablePorts() {
|
||||
}
|
||||
}
|
||||
|
||||
async function startSession(buildName: string) {
|
||||
const filePath = `C:/PixelStreaming/builds/${buildName}/${buildName}.exe`;
|
||||
async function startSession(
|
||||
buildName: string,
|
||||
ownerIp: string,
|
||||
endAt?: string
|
||||
) {
|
||||
const filePath = `C:/pixel-streaming/builds/${buildName}/${buildName}.exe`;
|
||||
|
||||
const availablePorts = await getAvailablePorts();
|
||||
|
||||
@@ -88,7 +96,7 @@ async function startSession(buildName: string) {
|
||||
const { cirrusPort, uePort } = availablePorts;
|
||||
|
||||
const cirrusProcess = execFile("node", [
|
||||
`C:/PixelStreaming/signalling-server/cirrus.js`,
|
||||
`C:/pixel-streaming/signalling-server/cirrus.js`,
|
||||
`--StreamerPort`,
|
||||
`${uePort}`,
|
||||
`--HttpPort`,
|
||||
@@ -126,33 +134,50 @@ async function startSession(buildName: string) {
|
||||
return;
|
||||
}
|
||||
|
||||
const activeSession = await ActiveSession.create({
|
||||
location: serverLocation,
|
||||
name: serverName,
|
||||
buildName,
|
||||
cirrusPort,
|
||||
uePort,
|
||||
cirrusProcessId,
|
||||
ueProcessId,
|
||||
});
|
||||
const type = serverType;
|
||||
|
||||
const activeSessionId = activeSession.id;
|
||||
try {
|
||||
const activeSession = await ActiveSession.create({
|
||||
location: serverLocation,
|
||||
name: serverName,
|
||||
buildName,
|
||||
type,
|
||||
cirrusPort,
|
||||
uePort,
|
||||
cirrusProcessId,
|
||||
ueProcessId,
|
||||
ownerIp,
|
||||
endAt,
|
||||
localIP,
|
||||
});
|
||||
|
||||
return activeSessionId;
|
||||
return activeSession;
|
||||
} catch (error) {
|
||||
treeKill(cirrusProcessId);
|
||||
treeKill(ueProcessId);
|
||||
|
||||
console.log((error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function endSession(activeSessionId: string) {
|
||||
const activeSession = await ActiveSession.findByIdAndDelete(activeSessionId);
|
||||
try {
|
||||
const activeSession = await ActiveSession.findByIdAndDelete(
|
||||
activeSessionId
|
||||
);
|
||||
|
||||
if (!activeSession) {
|
||||
console.log("Session with this ID not found");
|
||||
return;
|
||||
if (!activeSession) {
|
||||
console.log("Session with this ID not found");
|
||||
return;
|
||||
}
|
||||
|
||||
treeKill(activeSession.cirrusProcessId as number);
|
||||
treeKill(activeSession.ueProcessId as number);
|
||||
|
||||
console.log("Kill session");
|
||||
} catch (error) {
|
||||
console.log((error as Error).message);
|
||||
}
|
||||
|
||||
treeKill(activeSession.cirrusProcessId as number);
|
||||
treeKill(activeSession.ueProcessId as number);
|
||||
|
||||
console.log("Kill session");
|
||||
}
|
||||
|
||||
async function init() {
|
||||
@@ -164,6 +189,7 @@ async function init() {
|
||||
name: serverName,
|
||||
type: serverType,
|
||||
hostname: serverHostname,
|
||||
localIP,
|
||||
},
|
||||
{ upsert: true, new: true }
|
||||
);
|
||||
@@ -177,18 +203,21 @@ async function init() {
|
||||
}
|
||||
|
||||
app.post("/start", async (req, res) => {
|
||||
console.log(req.query);
|
||||
const buildName: string = req.body.buildName;
|
||||
for (const [key, value] of Object.entries(req.body)) {
|
||||
if (value === "null") req.body[key] = undefined;
|
||||
}
|
||||
|
||||
if (!buildName) {
|
||||
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);
|
||||
const activeSession = await startSession(buildName, ownerIp, endAt);
|
||||
|
||||
console.log("Result: ", result);
|
||||
|
||||
res.json({ ok: 1 });
|
||||
res.json(activeSession);
|
||||
});
|
||||
|
||||
app.post("/end", async (req, res) => {
|
||||
|
||||
@@ -11,6 +11,9 @@ const activeSessionSchema = new Schema(
|
||||
buildName: {
|
||||
type: String,
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
},
|
||||
uePort: {
|
||||
type: Number,
|
||||
},
|
||||
@@ -23,6 +26,15 @@ const activeSessionSchema = new Schema(
|
||||
cirrusProcessId: {
|
||||
type: Number,
|
||||
},
|
||||
ownerIp: {
|
||||
type: String,
|
||||
},
|
||||
endAt: {
|
||||
type: Date || null,
|
||||
},
|
||||
localIP: {
|
||||
type: String,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
|
||||
@@ -18,6 +18,9 @@ const sessionServerSchema = new Schema(
|
||||
gpuMemoryFree: {
|
||||
type: Number,
|
||||
},
|
||||
localIP: {
|
||||
type: String,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ import { Router } from "express";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
router.get("/", async (_req, res) => {
|
||||
res.json({ ok: 1 });
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user