1
0
mirror of https://github.com/TheGreyDiamond/Enlight.git synced 2025-07-17 20:33:48 +02:00

Working discovery

This commit is contained in:
TheGreyDiamond
2020-12-26 22:07:45 +01:00
parent b0eeb7e12e
commit 53be9a8566
4 changed files with 48 additions and 33 deletions

View File

@ -16,6 +16,7 @@ const express = require("express");
const { time } = require("console");
const { nanoid } = require("nanoid");
const restApp = express();
const diont = require('diont')();
const restPort = 33334;
const PORT = 33333;
@ -46,6 +47,16 @@ pageLookup["header"] = "header.html";
// 4 Connection failed
// 5 Searching
var service = {
name: "Enlight Default Session",
host: "127.0.0.1", // when omitted, defaults to the local IP
port: PORT,
memberCount: 0
// any additional information is allowed and will be propagated
};
var sessionState = -1;
var sessionStateGoal = -1;
@ -81,11 +92,6 @@ function preloadPages(){
// Prepare session existence broadcast
function prepBroadcast() {
server.bind(PORT, function () {
server.setBroadcast(true);
server.setMulticastTTL(128);
server.addMembership(MULTICAST_ADDR);
});
runy = true;
while (runy) {
if (networkInterfaces.length >= 1) {
@ -107,7 +113,7 @@ function prepBroadcast() {
}
// Broadcasts the presence of a session
function broadcastNew() {
function broadcastNewOff() {
var message = new Buffer.from(
"ENLIGHT_NEW_SESSION$" + String(mainNetworkInterface.ip4)
);
@ -204,6 +210,7 @@ function init() {
sysInf.networkInterfaces(function (data) {
networkInterfaces = data;
});
setTimeout(prepBroadcast, 200)
var langs = new Config("./lang/langs_v1.js");
@ -214,6 +221,22 @@ function init() {
console.log(`Restful is running on http://localhost:${restPort}`);
});
diont.on("serviceAnnounced", function(serviceInfo) {
// A service was announced
// This function triggers for services not yet available in diont.getServiceInfos()
// serviceInfo is an Object { isOurService : Boolean, service: Object }
// service.name, service.host and service.port are always filled
console.log("A new service was announced", serviceInfo.service);
// List currently known services
console.log("All known services", diont.getServiceInfos());
});
diont.on("serviceRenounced", function(serviceInfo) {
console.log("A service was renounced", serviceInfo.service);
console.log("All known services", diont.getServiceInfos());
});
restApp.get("/", (req, res) => {
res.send("Hello World! The RestFul API of Enlight is up and working!");
});
@ -248,6 +271,7 @@ function init() {
};
mySession.memberList.push();
mySession.members++;
service.memberCount++;
res.json({ state: "Succes", uid: uid });
} else {
res.json({
@ -266,14 +290,6 @@ function init() {
});
}, 20);
// Handling sessions
setInterval(function () {
if (sessionStateGoal == 2) {
broadcastNew();
sessionState = 2;
}
}, 400);
setTimeout(doneLoading, 2000);
ipcMain.on("synchronous-message", (event, arg) => {
@ -343,30 +359,23 @@ function init() {
} else if (String(arg).includes("SESSION:createNew")) {
// Creates a new session
setTimeout(prepBroadcast, 5);
service.host = mainNetworkInterface.ip4
mySession.name = String(arg).split("|")[1];
service.name = String(arg).split("|")[1];
console.log("Annouce start")
diont.announceService(service);
console.log("Annouce done")
mySession.joinable = true;
sessionState = 1;
sessionState = 2;
sessionStateGoal = 2;
event.returnValue = "";
sessionReAnn = setInterval(function() {
diont.renounceService(service);
}, 5000);
} else if(String(arg).includes("SESSION:startSearch")){
console.log("starting search")
client.bind(PORT);
client.on('listening', function () {
console.log("look")
// Get and print udp server listening ip address and port number in log console.
var address = client.address();
console.log('UDP client started and listening on ' + address.address + ":" + address.port);
});
client.on("message", function (message) {
// Create output message.
var output = "Udp server receive message : " + message + "\n";
// Print received message in stdout, here is log console.
console.log(output)
});
sessionState = 5;
event.returnValue = "";
} else {

5
package-lock.json generated
View File

@ -351,6 +351,11 @@
"integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==",
"optional": true
},
"diont": {
"version": "0.9.7",
"resolved": "https://registry.npmjs.org/diont/-/diont-0.9.7.tgz",
"integrity": "sha1-k7WrBj00WAnJSbwK27DaDYDGkOM="
},
"duplexer3": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz",

View File

@ -22,6 +22,7 @@
"homepage": "https://github.com/TheGreyDiamond/Enlight#readme",
"dependencies": {
"config-js": "^1.1.14",
"diont": "^0.9.7",
"ejs": "^3.1.5",
"electron": "^11.0.2",
"express": "^4.17.1",

View File

@ -1 +1 @@
Loopback Pseudo-Interface 1
Intel(R) I211 Gigabit Network Connection