mirror of
https://github.com/TheGreyDiamond/Enlight.git
synced 2026-03-31 14:50:24 +02:00
Cleanup and more session
This commit is contained in:
91
index.js
91
index.js
@@ -9,14 +9,11 @@ var Config = require("config-js");
|
|||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const { win32 } = require("path");
|
const { win32 } = require("path");
|
||||||
const sysInf = require("systeminformation");
|
const sysInf = require("systeminformation");
|
||||||
const dgram = require("dgram");
|
|
||||||
var server = dgram.createSocket("udp4");
|
|
||||||
var client = dgram.createSocket("udp4");
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const { time } = require("console");
|
const { time } = require("console");
|
||||||
const { nanoid } = require("nanoid");
|
const { nanoid } = require("nanoid");
|
||||||
const restApp = express();
|
const restApp = express();
|
||||||
const diont = require('diont')();
|
const diont = require("diont")();
|
||||||
|
|
||||||
const restPort = 33334;
|
const restPort = 33334;
|
||||||
const PORT = 33333;
|
const PORT = 33333;
|
||||||
@@ -51,12 +48,11 @@ var service = {
|
|||||||
name: "Enlight Default Session",
|
name: "Enlight Default Session",
|
||||||
host: "127.0.0.1", // when omitted, defaults to the local IP
|
host: "127.0.0.1", // when omitted, defaults to the local IP
|
||||||
port: PORT,
|
port: PORT,
|
||||||
memberCount: 0
|
memberCount: 1,
|
||||||
|
UUID: ""
|
||||||
// any additional information is allowed and will be propagated
|
// any additional information is allowed and will be propagated
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var sessionState = -1;
|
var sessionState = -1;
|
||||||
var sessionStateGoal = -1;
|
var sessionStateGoal = -1;
|
||||||
|
|
||||||
@@ -73,23 +69,23 @@ var mySession = {
|
|||||||
var mainConn = "";
|
var mainConn = "";
|
||||||
var mainNetworkInterface = undefined;
|
var mainNetworkInterface = undefined;
|
||||||
|
|
||||||
var knownSessions = [];
|
//var knownSessions = [];
|
||||||
|
var knownSessionsByUUID = {};
|
||||||
|
|
||||||
diont.on("serviceAnnounced", function(serviceInfo) {
|
diont.on("serviceAnnounced", function (serviceInfo) {
|
||||||
console.log("A new session was announced", serviceInfo.service);
|
console.log("A new session was announced", serviceInfo.service);
|
||||||
// List currently known services
|
// List currently known services
|
||||||
knownSessions.push(serviceInfo.service)
|
//knownSessions.push(serviceInfo.service);
|
||||||
|
knownSessionsByUUID[serviceInfo.service.UUID] = serviceInfo.service
|
||||||
});
|
});
|
||||||
|
|
||||||
diont.on("serviceRenounced", function(serviceInfo) {
|
diont.on("serviceRenounced", function (serviceInfo) {
|
||||||
//console.log("A session was renounced", serviceInfo.service);
|
//console.log("A session was renounced", serviceInfo.service);
|
||||||
knownSessions.push(serviceInfo.service)
|
knownSessionsByUUID[serviceInfo.service.UUID] = serviceInfo.service
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Preload all pages
|
// Preload all pages
|
||||||
function preloadPages(){
|
function preloadPages() {
|
||||||
for (const [key, value] of Object.entries(pageLookup)) {
|
for (const [key, value] of Object.entries(pageLookup)) {
|
||||||
// check if the property/key is defined in the object itself, not in parent
|
// check if the property/key is defined in the object itself, not in parent
|
||||||
if (pageLookup.hasOwnProperty(key)) {
|
if (pageLookup.hasOwnProperty(key)) {
|
||||||
@@ -157,7 +153,8 @@ function createWindow() {
|
|||||||
return win;
|
return win;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createStartupInfo() { // aka. loading screen
|
function createStartupInfo() {
|
||||||
|
// aka. loading screen
|
||||||
const win2 = new BrowserWindow({
|
const win2 = new BrowserWindow({
|
||||||
width: 400,
|
width: 400,
|
||||||
height: 200,
|
height: 200,
|
||||||
@@ -215,7 +212,7 @@ function init() {
|
|||||||
sysInf.networkInterfaces(function (data) {
|
sysInf.networkInterfaces(function (data) {
|
||||||
networkInterfaces = data;
|
networkInterfaces = data;
|
||||||
});
|
});
|
||||||
setTimeout(prepBroadcast, 200)
|
setTimeout(prepBroadcast, 200);
|
||||||
|
|
||||||
var langs = new Config("./lang/langs_v1.js");
|
var langs = new Config("./lang/langs_v1.js");
|
||||||
|
|
||||||
@@ -226,8 +223,6 @@ function init() {
|
|||||||
console.log(`Restful is running on http://localhost:${restPort}`);
|
console.log(`Restful is running on http://localhost:${restPort}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
restApp.get("/", (req, res) => {
|
restApp.get("/", (req, res) => {
|
||||||
res.send("Hello World! The RestFul API of Enlight is up and working!");
|
res.send("Hello World! The RestFul API of Enlight is up and working!");
|
||||||
});
|
});
|
||||||
@@ -284,7 +279,6 @@ function init() {
|
|||||||
setTimeout(doneLoading, 2000);
|
setTimeout(doneLoading, 2000);
|
||||||
|
|
||||||
ipcMain.on("synchronous-message", (event, arg) => {
|
ipcMain.on("synchronous-message", (event, arg) => {
|
||||||
|
|
||||||
if (String(arg).includes("hasBattery")) {
|
if (String(arg).includes("hasBattery")) {
|
||||||
// Retrieve if the device has a battery
|
// Retrieve if the device has a battery
|
||||||
sysInf.battery(function (data) {
|
sysInf.battery(function (data) {
|
||||||
@@ -300,7 +294,6 @@ function init() {
|
|||||||
} else if (String(arg).includes("getNetworks")) {
|
} else if (String(arg).includes("getNetworks")) {
|
||||||
// Retrieve all saved networks
|
// Retrieve all saved networks
|
||||||
event.returnValue = networkInterfaces;
|
event.returnValue = networkInterfaces;
|
||||||
|
|
||||||
} else if (String(arg).includes("set:newNetwork")) {
|
} else if (String(arg).includes("set:newNetwork")) {
|
||||||
// Sets the new main network
|
// Sets the new main network
|
||||||
fs.writeFile(
|
fs.writeFile(
|
||||||
@@ -312,7 +305,6 @@ function init() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
event.returnValue = "";
|
event.returnValue = "";
|
||||||
|
|
||||||
} else if (String(arg).includes("getMainNetwork")) {
|
} else if (String(arg).includes("getMainNetwork")) {
|
||||||
// Retrieves the main network definded by the user
|
// Retrieves the main network definded by the user
|
||||||
try {
|
try {
|
||||||
@@ -320,14 +312,12 @@ function init() {
|
|||||||
//console.log(last.toString())
|
//console.log(last.toString())
|
||||||
event.returnValue = last.toString();
|
event.returnValue = last.toString();
|
||||||
mainConn = last.toString();
|
mainConn = last.toString();
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
sysInf.networkInterfaces(function (data) {
|
sysInf.networkInterfaces(function (data) {
|
||||||
fs.writeFileSync("usrStore/lastNetwork.data", data[0].ifaceName);
|
fs.writeFileSync("usrStore/lastNetwork.data", data[0].ifaceName);
|
||||||
event.returnValue = data[0].ifaceName;
|
event.returnValue = data[0].ifaceName;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (String(arg).includes("PAGE:change")) {
|
} else if (String(arg).includes("PAGE:change")) {
|
||||||
// Change to a diffrent page of the programm
|
// Change to a diffrent page of the programm
|
||||||
|
|
||||||
@@ -342,45 +332,64 @@ function init() {
|
|||||||
win.loadFile("ui_templates/temp.html");
|
win.loadFile("ui_templates/temp.html");
|
||||||
const timestamp3 = Date.now();
|
const timestamp3 = Date.now();
|
||||||
event.returnValue = "";
|
event.returnValue = "";
|
||||||
|
|
||||||
} else if (String(arg).includes("SESSION:get.state")) {
|
} else if (String(arg).includes("SESSION:get.state")) {
|
||||||
// Retrives the session state
|
// Retrives the session state
|
||||||
|
|
||||||
event.returnValue = sessionState;
|
event.returnValue = sessionState;
|
||||||
|
|
||||||
} else if (String(arg).includes("SESSION:createNew")) {
|
} else if (String(arg).includes("SESSION:createNew")) {
|
||||||
// Creates a new session
|
// Creates a new session
|
||||||
service.host = mainNetworkInterface.ip4
|
i = 0;
|
||||||
|
sessinUids = Object.keys(knownSessionsByUUID);
|
||||||
|
// Make sure its a unique (at least to me) UUID
|
||||||
|
rounds = 0;
|
||||||
|
uidSes = nanoid();
|
||||||
|
while (sessinUids.includes(uidSes)) {
|
||||||
|
uidSes = nanoid();
|
||||||
|
rounds++;
|
||||||
|
if(rounds >= 10000){
|
||||||
|
// This takes a lot of rounds, there are two possible reasons to this:
|
||||||
|
// A lot of sessions or a broken nanoid() random generation
|
||||||
|
console.warn("It takes very long to find a unique session UUID")
|
||||||
|
}
|
||||||
|
if(rounds >= 40000){
|
||||||
|
// Okay, no. We shall give up now. Something is very, very wrong here.
|
||||||
|
console.error("Took to long to find a session UUID")
|
||||||
|
sessionState = 4;
|
||||||
|
return("")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
service.UUID = uidSes;
|
||||||
|
service.host = mainNetworkInterface.ip4;
|
||||||
mySession.name = String(arg).split("|")[1];
|
mySession.name = String(arg).split("|")[1];
|
||||||
service.name = String(arg).split("|")[1];
|
service.name = String(arg).split("|")[1];
|
||||||
console.log("Annouce start")
|
console.log("Annouce start");
|
||||||
diont.announceService(service);
|
diont.announceService(service);
|
||||||
console.log("Annouce done")
|
console.log("Annouce done");
|
||||||
mySession.joinable = true;
|
mySession.joinable = true;
|
||||||
sessionState = 2;
|
sessionState = 2;
|
||||||
sessionStateGoal = 2;
|
sessionStateGoal = 2;
|
||||||
event.returnValue = "";
|
event.returnValue = "";
|
||||||
|
|
||||||
sessionAnn = setInterval(function() {
|
sessionAnn = setInterval(function () {
|
||||||
console.log("announce")
|
console.log("announce");
|
||||||
diont.announceService(service);
|
diont.announceService(service);
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
||||||
sessionReAnn = setInterval(function() {
|
sessionReAnn = setInterval(function () {
|
||||||
console.log("REANNOUCNE")
|
console.log("REANNOUCNE");
|
||||||
diont.renounceService(service);
|
diont.renounceService(service);
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
} else if (String(arg).includes("SESSION:startSearch")) {
|
||||||
} else if(String(arg).includes("SESSION:startSearch")){
|
console.log("starting search");
|
||||||
console.log("starting search")
|
|
||||||
|
|
||||||
sessionState = 5;
|
sessionState = 5;
|
||||||
event.returnValue = "";
|
event.returnValue = "";
|
||||||
} else if(String(arg).includes("SESSION:getAll")){
|
} else if (String(arg).includes("SESSION:getAll")) {
|
||||||
console.log("GET ALL SESSION")
|
console.log("GET ALL SESSION");
|
||||||
event.returnValue = knownSessions
|
event.returnValue = knownSessionsByUUID;
|
||||||
console.log(knownSessions)
|
console.log(knownSessionsByUUID);
|
||||||
console.log("GET ALL SESSIOON DONE")
|
console.log("GET ALL SESSIOON DONE");
|
||||||
} else {
|
} else {
|
||||||
event.returnValue = "ERR:UNKNOW_CMD";
|
event.returnValue = "ERR:UNKNOW_CMD";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user