Multitasking completed for Client, only room left
This commit is contained in:
parent
715ab15a23
commit
e4f644a346
|
@ -1,4 +1,4 @@
|
||||||
const { CLIENT_SEND_MESSAGE } = require("./Notify");
|
const { CLIENT_SEND_MESSAGE, CLIENT_UPDATE_PROP } = require("./IPC");
|
||||||
|
|
||||||
function Client()
|
function Client()
|
||||||
{
|
{
|
||||||
|
@ -26,6 +26,14 @@ function Client()
|
||||||
|
|
||||||
this.isProxy = false;
|
this.isProxy = false;
|
||||||
this.proxyProcess = null;
|
this.proxyProcess = null;
|
||||||
|
|
||||||
|
this.sync = function(...args){
|
||||||
|
process.nextTick(()=>{
|
||||||
|
for (const name of args) {
|
||||||
|
CLIENT_UPDATE_PROP(this.id, name, this[name]);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* @type {Map<string, Client>}
|
* @type {Map<string, Client>}
|
||||||
|
@ -39,6 +47,7 @@ Client.prototype.peerRequest = function(client){
|
||||||
let info = {};
|
let info = {};
|
||||||
this.store.forEach((value, name) => info[name] = value);
|
this.store.forEach((value, name) => info[name] = value);
|
||||||
this.pairs.add(client.id);
|
this.pairs.add(client.id);
|
||||||
|
this.sync('pairs');
|
||||||
client.send([{
|
client.send([{
|
||||||
from: this.id,
|
from: this.id,
|
||||||
info
|
info
|
||||||
|
@ -50,6 +59,7 @@ Client.prototype.peerRequest = function(client){
|
||||||
*/
|
*/
|
||||||
Client.prototype.acceptPeerRequest = function(client){
|
Client.prototype.acceptPeerRequest = function(client){
|
||||||
this.pairs.add(client.id);
|
this.pairs.add(client.id);
|
||||||
|
this.sync('pairs');
|
||||||
client.send([{
|
client.send([{
|
||||||
from: this.id
|
from: this.id
|
||||||
},'accepted/pair']);
|
},'accepted/pair']);
|
||||||
|
@ -60,6 +70,7 @@ Client.prototype.peerRequest = function(client){
|
||||||
Client.prototype.rejectPeerRequest = function(client){
|
Client.prototype.rejectPeerRequest = function(client){
|
||||||
this.pairs.delete(client.id);
|
this.pairs.delete(client.id);
|
||||||
client.pairs.delete(this.id);
|
client.pairs.delete(this.id);
|
||||||
|
this.sync('pairs');
|
||||||
client.send([{
|
client.send([{
|
||||||
from: this.id
|
from: this.id
|
||||||
},'rejected/pair']);
|
},'rejected/pair']);
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
|
const { Room } = require("./Services/Room");
|
||||||
|
|
||||||
process.on('message',data => {
|
process.on('message',data => {
|
||||||
const {Client} = require("./Client.js")
|
const {Client} = require("./Client.js")
|
||||||
switch(data.type)
|
switch(data.type)
|
||||||
{
|
{
|
||||||
case "CLIENT_CREATED":{
|
case "CLIENT_CREATED":{
|
||||||
|
slog("CLIENT_CREATED");
|
||||||
let client = new Client();
|
let client = new Client();
|
||||||
client.isProxy = true;
|
client.isProxy = true;
|
||||||
client.proxyProcess = data.pid;
|
client.proxyProcess = data.pid;
|
||||||
|
@ -11,25 +14,39 @@ process.on('message',data => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "CLIENT_UPDATE_PROP":{
|
case "CLIENT_UPDATE_PROP":{
|
||||||
|
slog("CLIENT_UPDATE_PROP");
|
||||||
let client = Client.clients.get(data.uuid);
|
let client = Client.clients.get(data.uuid);
|
||||||
client[data.name] = value;
|
client[data.name] = data.value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "CLIENT_SEND_MESSAGE":{
|
case "CLIENT_SEND_MESSAGE":{
|
||||||
|
slog("CLIENT_SEND_MESSAGE");
|
||||||
let client = Client.clients.get(data.uuid);
|
let client = Client.clients.get(data.uuid);
|
||||||
client.send(data.message)
|
client.send(data.message)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "CLIENT_DESTROY":{
|
case "CLIENT_DESTROY":{
|
||||||
|
slog("CLIENT_DESTROY");
|
||||||
Client.clients.delete(data.uuid);
|
Client.clients.delete(data.uuid);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case "ROOM_CREATED":{
|
||||||
|
slog("ROOM_CREATED");
|
||||||
|
let room = Room.fromJSON(data.value);
|
||||||
|
Room.rooms.set(room.id, room);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "ROOM_DESTROY":{
|
||||||
|
slog("ROOM_DESTROY");
|
||||||
|
Room.rooms.delete(data.value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function CLIENT_CREATED(uuid)
|
function CLIENT_CREATED(uuid)
|
||||||
{
|
{
|
||||||
console.log(process.pid, "CLIENT_CREATED");
|
mlog("CLIENT_CREATED");
|
||||||
process.send({
|
process.send({
|
||||||
type:'CLIENT_CREATED',
|
type:'CLIENT_CREATED',
|
||||||
uuid: uuid
|
uuid: uuid
|
||||||
|
@ -37,6 +54,7 @@ function CLIENT_CREATED(uuid)
|
||||||
};
|
};
|
||||||
function CLIENT_UPDATE_PROP(uuid, name, value)
|
function CLIENT_UPDATE_PROP(uuid, name, value)
|
||||||
{
|
{
|
||||||
|
mlog("CLIENT_UPDATE_PROP");
|
||||||
process.send({
|
process.send({
|
||||||
type:'CLIENT_UPDATE_PROP',
|
type:'CLIENT_UPDATE_PROP',
|
||||||
uuid: uuid,
|
uuid: uuid,
|
||||||
|
@ -46,6 +64,7 @@ function CLIENT_UPDATE_PROP(uuid, name, value)
|
||||||
};
|
};
|
||||||
function CLIENT_SEND_MESSAGE(uuid, message)
|
function CLIENT_SEND_MESSAGE(uuid, message)
|
||||||
{
|
{
|
||||||
|
mlog("CLIENT_SEND_MESSAGE");
|
||||||
process.send({
|
process.send({
|
||||||
type:'CLIENT_SEND_MESSAGE',
|
type:'CLIENT_SEND_MESSAGE',
|
||||||
uuid: uuid,
|
uuid: uuid,
|
||||||
|
@ -54,13 +73,48 @@ function CLIENT_SEND_MESSAGE(uuid, message)
|
||||||
};
|
};
|
||||||
function CLIENT_DESTROY(uuid)
|
function CLIENT_DESTROY(uuid)
|
||||||
{
|
{
|
||||||
|
mlog("CLIENT_DESTROY");
|
||||||
process.send({
|
process.send({
|
||||||
type:'CLIENT_DESTROY',
|
type:'CLIENT_DESTROY',
|
||||||
uuid: uuid
|
uuid: uuid
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function ROOM_CREATED(room)
|
||||||
|
{
|
||||||
|
mlog("ROOM_CREATED");
|
||||||
|
let raw = room.toJSON(true);
|
||||||
|
process.send({
|
||||||
|
type:'ROOM_CREATED',
|
||||||
|
value: raw
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
function ROOM_DESTROY(room)
|
||||||
|
{
|
||||||
|
mlog("ROOM_DESTROY");
|
||||||
|
process.send({
|
||||||
|
type:'ROOM_DESTROY',
|
||||||
|
value: room.id
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
function mlog(command)
|
||||||
|
{
|
||||||
|
console.log("M",process.pid, command)
|
||||||
|
}
|
||||||
|
function slog(command)
|
||||||
|
{
|
||||||
|
console.log("S",process.pid, command)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
exports.CLIENT_CREATED = CLIENT_CREATED;
|
exports.CLIENT_CREATED = CLIENT_CREATED;
|
||||||
exports.CLIENT_UPDATE_PROP = CLIENT_UPDATE_PROP;
|
exports.CLIENT_UPDATE_PROP = CLIENT_UPDATE_PROP;
|
||||||
exports.CLIENT_DESTROY = CLIENT_DESTROY;
|
exports.CLIENT_DESTROY = CLIENT_DESTROY;
|
||||||
exports.CLIENT_SEND_MESSAGE = CLIENT_SEND_MESSAGE;
|
exports.CLIENT_SEND_MESSAGE = CLIENT_SEND_MESSAGE;
|
||||||
|
exports.ROOM_CREATED = ROOM_CREATED;
|
||||||
|
exports.ROOM_DESTROY = ROOM_DESTROY;
|
||||||
|
exports.mlog = mlog;
|
||||||
|
exports.slog = slog;
|
|
@ -1,4 +1,5 @@
|
||||||
const { Client } = require("../Client.js");
|
const { Client } = require("../Client.js");
|
||||||
|
const { CLIENT_UPDATE_PROP } = require("../IPC.js");
|
||||||
let {addService, addListener} = require("../WebSocket.js");
|
let {addService, addListener} = require("../WebSocket.js");
|
||||||
|
|
||||||
addService(({
|
addService(({
|
||||||
|
@ -15,11 +16,14 @@ addService(({
|
||||||
{
|
{
|
||||||
client.requiredPair = true;
|
client.requiredPair = true;
|
||||||
end({status:"success"});
|
end({status:"success"});
|
||||||
|
client.sync('requiredPair');
|
||||||
}
|
}
|
||||||
if(value == 'disable')
|
if(value == 'disable')
|
||||||
{
|
{
|
||||||
client.requiredPair = false;
|
client.requiredPair = false;
|
||||||
end({status:"success"});
|
end({status:"success"});
|
||||||
|
client.sync('requiredPair');
|
||||||
|
//CLIENT_UPDATE_PROP(client.id, 'requiredPair', client.requiredPair);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -29,6 +33,7 @@ addService(({
|
||||||
}
|
}
|
||||||
case 'auth/public':{
|
case 'auth/public':{
|
||||||
client.requiredPair = false;
|
client.requiredPair = false;
|
||||||
|
client.sync('requiredPair');
|
||||||
return end({
|
return end({
|
||||||
value: 'success',
|
value: 'success',
|
||||||
mode: 'public'
|
mode: 'public'
|
||||||
|
@ -36,6 +41,7 @@ addService(({
|
||||||
}
|
}
|
||||||
case 'auth/private':{
|
case 'auth/private':{
|
||||||
client.requiredPair = true;
|
client.requiredPair = true;
|
||||||
|
client.sync('requiredPair');
|
||||||
return end({
|
return end({
|
||||||
value: 'success',
|
value: 'success',
|
||||||
mode: 'private'
|
mode: 'private'
|
||||||
|
|
|
@ -3,6 +3,7 @@ let {addService, addListener} = require("../WebSocket.js");
|
||||||
let {
|
let {
|
||||||
randomBytes
|
randomBytes
|
||||||
} = require("node:crypto");
|
} = require("node:crypto");
|
||||||
|
const { slog } = require("../IPC");
|
||||||
|
|
||||||
class APNumber{
|
class APNumber{
|
||||||
/**
|
/**
|
||||||
|
@ -11,14 +12,20 @@ class APNumber{
|
||||||
static busyNumbers = new Map();
|
static busyNumbers = new Map();
|
||||||
/**
|
/**
|
||||||
* @type {number}
|
* @type {number}
|
||||||
|
* @param {Client} client
|
||||||
*/
|
*/
|
||||||
static lock()
|
static lock(client)
|
||||||
{
|
{
|
||||||
let c = 24;
|
let c = 24;
|
||||||
while(true){
|
while(true){
|
||||||
if(!APNumber.busyNumbers.has(c))
|
if(!APNumber.busyNumbers.has(c))
|
||||||
{
|
{
|
||||||
APNumber.busyNumbers.set(c,true);
|
APNumber.busyNumbers.set(c,client);
|
||||||
|
process.send({
|
||||||
|
type: 'AP_NUMBER/LOCK',
|
||||||
|
uuid: client.id,
|
||||||
|
value: c
|
||||||
|
})
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
c++;
|
c++;
|
||||||
|
@ -30,6 +37,11 @@ class APNumber{
|
||||||
static release(num)
|
static release(num)
|
||||||
{
|
{
|
||||||
APNumber.busyNumbers.delete(num);
|
APNumber.busyNumbers.delete(num);
|
||||||
|
process.send({
|
||||||
|
type: 'AP_NUMBER/RELEASE',
|
||||||
|
uuid: APNumber.busyNumbers.get(num).id,
|
||||||
|
value: num
|
||||||
|
})
|
||||||
}
|
}
|
||||||
static whois(num){
|
static whois(num){
|
||||||
return APNumber.busyNumbers.get(num)?.id;
|
return APNumber.busyNumbers.get(num)?.id;
|
||||||
|
@ -56,6 +68,11 @@ class APShortCode{
|
||||||
if(APShortCode.busyCodes.has(code) == false)
|
if(APShortCode.busyCodes.has(code) == false)
|
||||||
{
|
{
|
||||||
APShortCode.busyCodes.set(code, client);
|
APShortCode.busyCodes.set(code, client);
|
||||||
|
process.send({
|
||||||
|
type: 'AP_SHORTCODE/LOCK',
|
||||||
|
uuid: APShortCode.busyCodes.get(num).id,
|
||||||
|
value: code
|
||||||
|
})
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
if(!thirdLetter.end())
|
if(!thirdLetter.end())
|
||||||
|
@ -80,6 +97,11 @@ class APShortCode{
|
||||||
}
|
}
|
||||||
static release(code){
|
static release(code){
|
||||||
APShortCode.busyCodes.delete(code);
|
APShortCode.busyCodes.delete(code);
|
||||||
|
process.send({
|
||||||
|
type: 'AP_SHORTCODE/RELEASE',
|
||||||
|
uuid: APShortCode.busyCodes.get(code).id,
|
||||||
|
value: code
|
||||||
|
})
|
||||||
}
|
}
|
||||||
static whois(num){
|
static whois(num){
|
||||||
return APShortCode.busyCodes.get(num)?.id;
|
return APShortCode.busyCodes.get(num)?.id;
|
||||||
|
@ -122,6 +144,11 @@ class APIPAddress{
|
||||||
if(APIPAddress.busyIP.has(code) == false)
|
if(APIPAddress.busyIP.has(code) == false)
|
||||||
{
|
{
|
||||||
APIPAddress.busyIP.set(code, client);
|
APIPAddress.busyIP.set(code, client);
|
||||||
|
process.send({
|
||||||
|
type: 'AP_IPADDRESS/LOCK',
|
||||||
|
uuid: APIPAddress.busyIP.get(code).id,
|
||||||
|
value: code
|
||||||
|
})
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
if(D != 255)
|
if(D != 255)
|
||||||
|
@ -156,6 +183,11 @@ class APIPAddress{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static release(code){
|
static release(code){
|
||||||
|
process.send({
|
||||||
|
type: 'AP_IPADDRESS/RELEASE',
|
||||||
|
uuid: APIPAddress.busyIP.get(code).id,
|
||||||
|
value: code
|
||||||
|
})
|
||||||
APIPAddress.busyIP.delete(code);
|
APIPAddress.busyIP.delete(code);
|
||||||
}
|
}
|
||||||
static whois(num){
|
static whois(num){
|
||||||
|
@ -167,7 +199,6 @@ exports.APNumber = APNumber;
|
||||||
exports.APShortCode = APShortCode;
|
exports.APShortCode = APShortCode;
|
||||||
exports.APIPAddress = APIPAddress;
|
exports.APIPAddress = APIPAddress;
|
||||||
|
|
||||||
|
|
||||||
addService(({
|
addService(({
|
||||||
client,
|
client,
|
||||||
message,
|
message,
|
||||||
|
@ -186,6 +217,7 @@ addService(({
|
||||||
};
|
};
|
||||||
let value = APIPAddress.lock(client);
|
let value = APIPAddress.lock(client);
|
||||||
client.APIPAddress = value;
|
client.APIPAddress = value;
|
||||||
|
client.sync('APIPAddress');
|
||||||
end({
|
end({
|
||||||
status : "success",
|
status : "success",
|
||||||
ip : value
|
ip : value
|
||||||
|
@ -201,6 +233,7 @@ addService(({
|
||||||
};
|
};
|
||||||
let value = APNumber.lock(client);
|
let value = APNumber.lock(client);
|
||||||
client.APNumber = value;
|
client.APNumber = value;
|
||||||
|
client.sync('APNumber');
|
||||||
end({
|
end({
|
||||||
status : "success",
|
status : "success",
|
||||||
number : value
|
number : value
|
||||||
|
@ -216,6 +249,7 @@ addService(({
|
||||||
};
|
};
|
||||||
let value = APShortCode.lock(client);
|
let value = APShortCode.lock(client);
|
||||||
client.APShortCode = value;
|
client.APShortCode = value;
|
||||||
|
client.sync('APShortCode');
|
||||||
end({
|
end({
|
||||||
status : "success",
|
status : "success",
|
||||||
code : value
|
code : value
|
||||||
|
@ -230,6 +264,7 @@ addService(({
|
||||||
}
|
}
|
||||||
APIPAddress.release(client.APIPAddress);
|
APIPAddress.release(client.APIPAddress);
|
||||||
let value = APIPAddress.lock(client);
|
let value = APIPAddress.lock(client);
|
||||||
|
client.sync('APIPAddress');
|
||||||
end({
|
end({
|
||||||
status : "success",
|
status : "success",
|
||||||
ip : value
|
ip : value
|
||||||
|
@ -244,6 +279,7 @@ addService(({
|
||||||
}
|
}
|
||||||
APNumber.release(client.APNumber);
|
APNumber.release(client.APNumber);
|
||||||
let value = APNumber.lock(client);
|
let value = APNumber.lock(client);
|
||||||
|
client.sync('APNumber');
|
||||||
end({
|
end({
|
||||||
status : "success",
|
status : "success",
|
||||||
number : value
|
number : value
|
||||||
|
@ -258,6 +294,7 @@ addService(({
|
||||||
}
|
}
|
||||||
APShortCode.release(client.APShortCode);
|
APShortCode.release(client.APShortCode);
|
||||||
let value = APShortCode.lock(client);
|
let value = APShortCode.lock(client);
|
||||||
|
client.sync('APShortCode');
|
||||||
end({
|
end({
|
||||||
status : "success",
|
status : "success",
|
||||||
code : value
|
code : value
|
||||||
|
@ -266,6 +303,8 @@ addService(({
|
||||||
}
|
}
|
||||||
case "release/APIPAddress":{
|
case "release/APIPAddress":{
|
||||||
APIPAddress.release(client.APIPAddress);
|
APIPAddress.release(client.APIPAddress);
|
||||||
|
client.APIPAddress = void 0;
|
||||||
|
client.sync('APShortCode');
|
||||||
end({
|
end({
|
||||||
status : "success"
|
status : "success"
|
||||||
})
|
})
|
||||||
|
@ -273,6 +312,8 @@ addService(({
|
||||||
}
|
}
|
||||||
case "release/APNumber":{
|
case "release/APNumber":{
|
||||||
APNumber.release(client.APNumber);
|
APNumber.release(client.APNumber);
|
||||||
|
client.APNumber = void 0;
|
||||||
|
client.sync('APIPAddress');
|
||||||
end({
|
end({
|
||||||
status : "success"
|
status : "success"
|
||||||
})
|
})
|
||||||
|
@ -280,6 +321,8 @@ addService(({
|
||||||
}
|
}
|
||||||
case "release/APShortCode":{
|
case "release/APShortCode":{
|
||||||
APShortCode.release(client.APShortCode);
|
APShortCode.release(client.APShortCode);
|
||||||
|
client.APShortCode = void 0;
|
||||||
|
client.sync('APIPAddress');
|
||||||
end({
|
end({
|
||||||
status : "success"
|
status : "success"
|
||||||
})
|
})
|
||||||
|
@ -333,6 +376,72 @@ addService(({
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
process.on('message',({type, uuid, value}) => {
|
||||||
|
switch(type)
|
||||||
|
{
|
||||||
|
case "AP_NUMBER/LOCK":{
|
||||||
|
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||||
|
APNumber.busyNumbers.set(value, client);
|
||||||
|
let client = Client.clients.get(uuid);
|
||||||
|
if(client)
|
||||||
|
{
|
||||||
|
client.APNumber = value;
|
||||||
|
client.sync('APNumber');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "AP_NUMBER/RELEASE":{
|
||||||
|
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||||
|
APNumber.busyNumbers.delete(value);
|
||||||
|
let client = Client.clients.get(uuid);
|
||||||
|
if(client)
|
||||||
|
{
|
||||||
|
client.APNumber = void 0;
|
||||||
|
client.sync('APNumber');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "AP_SHORTCODE/LOCK":{
|
||||||
|
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||||
|
APShortCode.busyCodes.set(value, client);
|
||||||
|
let client = Client.clients.get(uuid);
|
||||||
|
if(client)
|
||||||
|
{
|
||||||
|
client.APShortCode = value;
|
||||||
|
client.sync('APShortCode');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "AP_SHORTCODE/RELEASE":{
|
||||||
|
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||||
|
APShortCode.busyCodes.delete(value);
|
||||||
|
let client = Client.clients.get(uuid);
|
||||||
|
if(client)
|
||||||
|
{
|
||||||
|
client.APShortCode = void 0;
|
||||||
|
client.sync('APShortCode');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "AP_IPADDRESS/LOCK":{
|
||||||
|
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||||
|
APIPAddress.busyIP.set(value, client);
|
||||||
|
let client = Client.clients.get(uuid);
|
||||||
|
if(client)
|
||||||
|
{
|
||||||
|
client.APIPAddress = value;
|
||||||
|
client.sync('APIPAddress');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "AP_IPADDRESS/RELEASE":{
|
||||||
|
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||||
|
APIPAddress.busyIP.delete(value);
|
||||||
|
let client = Client.clients.get(uuid);
|
||||||
|
if(client)
|
||||||
|
{
|
||||||
|
client.APIPAddress = void 0;
|
||||||
|
client.sync('APIPAddress');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
addListener('disconnect',(global, client)=>{
|
addListener('disconnect',(global, client)=>{
|
||||||
if(client.APIPAddress != 0)
|
if(client.APIPAddress != 0)
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,6 +3,7 @@ let {randomUUID,createHash} = require("crypto");
|
||||||
const joi = require("joi");
|
const joi = require("joi");
|
||||||
let {addService,addListener} = require("../WebSocket.js");
|
let {addService,addListener} = require("../WebSocket.js");
|
||||||
const { termoutput } = require("../config.js");
|
const { termoutput } = require("../config.js");
|
||||||
|
const { ROOM_CREATED, ROOM_DESTROY } = require("../IPC.js");
|
||||||
let term = require("terminal-kit").terminal;
|
let term = require("terminal-kit").terminal;
|
||||||
|
|
||||||
function Sha256(update)
|
function Sha256(update)
|
||||||
|
@ -70,9 +71,10 @@ function Room()
|
||||||
*/
|
*/
|
||||||
Room.prototype.publish = function(room){
|
Room.prototype.publish = function(room){
|
||||||
Room.rooms.set(this.id, this);
|
Room.rooms.set(this.id, this);
|
||||||
|
ROOM_CREATED(this);
|
||||||
termoutput && term.green("Room Published ").white(this.name," in ").yellow(this.clients.size).white(" clients")('\n');
|
termoutput && term.green("Room Published ").white(this.name," in ").yellow(this.clients.size).white(" clients")('\n');
|
||||||
};
|
};
|
||||||
Room.prototype.toJSON = function(){
|
Room.prototype.toJSON = function(detailed){
|
||||||
let obj = {};
|
let obj = {};
|
||||||
obj.id = this.id;
|
obj.id = this.id;
|
||||||
obj.accessType = this.accessType;
|
obj.accessType = this.accessType;
|
||||||
|
@ -82,8 +84,44 @@ Room.prototype.toJSON = function(){
|
||||||
obj.name = this.name;
|
obj.name = this.name;
|
||||||
obj.owner = this.owner.id;
|
obj.owner = this.owner.id;
|
||||||
obj.waitingInvited = [...this.waitingInvited];
|
obj.waitingInvited = [...this.waitingInvited];
|
||||||
|
if(detailed)
|
||||||
|
{
|
||||||
|
obj.credential = this.credential;
|
||||||
|
obj.notifyActionInvite = this.notifyActionInvite;
|
||||||
|
obj.notifyActionJoined = this.notifyActionJoined;
|
||||||
|
obj.notifyActionEjected = this.notifyActionEjected;
|
||||||
|
obj.clients = [...this.clients.keys()];
|
||||||
|
}
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* @param {Object} data
|
||||||
|
* @param {Room} room
|
||||||
|
*/
|
||||||
|
Room.fromJSON = function(data, room){
|
||||||
|
room = room || new Room();
|
||||||
|
let obj = {};
|
||||||
|
room.id = data.id;
|
||||||
|
room.accessType = data.accessType;
|
||||||
|
room.createdAt = data.createdAt;
|
||||||
|
room.description = data.description;
|
||||||
|
room.joinType = data.joinType;
|
||||||
|
room.name = data.name;
|
||||||
|
room.owner = data.owner.id;
|
||||||
|
room.waitingInvited = new Set(data.waitingInvited);
|
||||||
|
obj.credential = data.credential;
|
||||||
|
obj.notifyActionInvite = data.notifyActionInvite;
|
||||||
|
obj.notifyActionJoined = data.notifyActionJoined;
|
||||||
|
obj.notifyActionEjected = data.notifyActionEjected;
|
||||||
|
obj.clients = new Map(
|
||||||
|
data.clients.map(e => ([
|
||||||
|
e, // map key
|
||||||
|
Client.clients.get(e) // map value
|
||||||
|
])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return room;
|
||||||
|
};
|
||||||
Room.prototype.send = function(obj, withOut){
|
Room.prototype.send = function(obj, withOut){
|
||||||
for (const client of this.clients.values()) {
|
for (const client of this.clients.values()) {
|
||||||
if(client.id != withOut)
|
if(client.id != withOut)
|
||||||
|
@ -116,6 +154,7 @@ Room.prototype.down = function(){
|
||||||
ownerid: this.owner.id
|
ownerid: this.owner.id
|
||||||
},'room/closed']);
|
},'room/closed']);
|
||||||
Room.rooms.delete(this.id);
|
Room.rooms.delete(this.id);
|
||||||
|
ROOM_DESTROY(this)
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* @param {Client} client
|
* @param {Client} client
|
||||||
|
|
|
@ -5,7 +5,7 @@ let {http} = require("./HTTPServer");
|
||||||
let {randomUUID} = require("crypto");
|
let {randomUUID} = require("crypto");
|
||||||
const { Client } = require("./Client.js");
|
const { Client } = require("./Client.js");
|
||||||
const { termoutput } = require("./config");
|
const { termoutput } = require("./config");
|
||||||
const { CLIENT_CREATED, CLIENT_DESTROY } = require("./Notify");
|
const { CLIENT_CREATED, CLIENT_DESTROY } = require("./IPC");
|
||||||
termoutput && console.log("Web Socket Protocol is ready");
|
termoutput && console.log("Web Socket Protocol is ready");
|
||||||
|
|
||||||
http.addListener("upgrade",() => {
|
http.addListener("upgrade",() => {
|
||||||
|
@ -20,10 +20,6 @@ let wsServer = new websocket.server({
|
||||||
let global = new Map();
|
let global = new Map();
|
||||||
let clients = new Map();
|
let clients = new Map();
|
||||||
wsServer.addListener("connect",(socket) => {
|
wsServer.addListener("connect",(socket) => {
|
||||||
|
|
||||||
socket.send("worker id " + process.pid);
|
|
||||||
return;
|
|
||||||
|
|
||||||
let xClient = new Client();
|
let xClient = new Client();
|
||||||
let id = randomUUID();
|
let id = randomUUID();
|
||||||
socket.id = id;
|
socket.id = id;
|
||||||
|
@ -33,6 +29,8 @@ wsServer.addListener("connect",(socket) => {
|
||||||
Client.clients.set(id, xClient);
|
Client.clients.set(id, xClient);
|
||||||
clients.set(id, xClient);
|
clients.set(id, xClient);
|
||||||
|
|
||||||
|
console.log("Client:", id,"on worker pid:",process.pid)
|
||||||
|
|
||||||
CLIENT_CREATED(id);
|
CLIENT_CREATED(id);
|
||||||
|
|
||||||
emit("connect", global, xClient);
|
emit("connect", global, xClient);
|
||||||
|
|
74
index.js
74
index.js
|
@ -1,66 +1,78 @@
|
||||||
/** @type {import('node:cluster').Cluster} */
|
/** @type {import('node:cluster').Cluster} */
|
||||||
const cluster = require("cluster");
|
const cluster = require("cluster");
|
||||||
const os = require("os");
|
const os = require("os");
|
||||||
const {randomInt} = require("crypto");
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use Round Robin algorithm for cluster process load balancer
|
||||||
|
*/
|
||||||
cluster.schedulingPolicy = cluster.SCHED_RR;
|
cluster.schedulingPolicy = cluster.SCHED_RR;
|
||||||
|
|
||||||
async function main()
|
async function main()
|
||||||
{
|
{
|
||||||
let master = new Map();
|
if(cluster.isPrimary == false)
|
||||||
|
|
||||||
if(cluster.isPrimary)
|
|
||||||
{
|
{
|
||||||
let e = 0|0
|
// This process is a worker / slave
|
||||||
while (e < 10)
|
// Compile source code and run
|
||||||
{
|
require("./Source/index");
|
||||||
e++;
|
// stay here
|
||||||
let flow = await generateFlow();
|
return;
|
||||||
flow.send({
|
};
|
||||||
TYPE:"start"
|
|
||||||
})
|
// This process is a primary / master
|
||||||
master.set(flow.id, flow);
|
|
||||||
flow.message((_id, obj) =>{
|
// Worker process list
|
||||||
for (const [id,{send}] of master) {
|
const master = new Map();
|
||||||
if(_id !== id)
|
|
||||||
|
const coreCount = 2 // os.cpus().length;
|
||||||
|
for(let index = 0; index < coreCount; index++)
|
||||||
|
{
|
||||||
|
// Open slave process
|
||||||
|
let worker = await generateFlow();
|
||||||
|
// Save process with id
|
||||||
|
master.set(worker.id, worker);
|
||||||
|
// Listen process for commands
|
||||||
|
worker.message(
|
||||||
|
// This process want to send payload to sibling process with IPC
|
||||||
|
(workerId, payload) =>{
|
||||||
|
for (const [siblingWorkerId,{send}] of master) {
|
||||||
|
// No sending to itself
|
||||||
|
if(workerId !== siblingWorkerId)
|
||||||
{
|
{
|
||||||
|
// Send command to sibling with IPC
|
||||||
send({
|
send({
|
||||||
...obj,
|
...payload,
|
||||||
pid: flow.id
|
pid: worker.id
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
};
|
|
||||||
}else{
|
|
||||||
let Application;
|
|
||||||
let synced = [];
|
|
||||||
process.on("message",data => {
|
|
||||||
switch(data.TYPE)
|
|
||||||
{
|
|
||||||
case "start":{
|
|
||||||
Application = require("./Source/index");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async function generateFlow(N)
|
async function generateFlow(N)
|
||||||
{
|
{
|
||||||
|
// Mirror this process with for (low-level os multitasking)
|
||||||
const worker = cluster.fork();
|
const worker = cluster.fork();
|
||||||
|
// Wait process is online
|
||||||
await new Promise(ok => {
|
await new Promise(ok => {
|
||||||
worker.addListener("online",()=> {
|
worker.addListener("online",()=> {
|
||||||
ok()
|
ok()
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
// Get process pid on the os
|
||||||
let id = worker.process.pid;
|
let id = worker.process.pid;
|
||||||
|
|
||||||
|
// Simplification wrapping send and get events with IPC's event functions
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
send: message => worker.send(message),
|
send: message => worker.send(message),
|
||||||
message: (callback) => worker.addListener("message", e => callback(id,e))
|
message: (callback) => worker.addListener("message", e => callback(id,e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run immediately
|
||||||
process.nextTick(main);
|
process.nextTick(main);
|
24
test.html
24
test.html
|
@ -33,15 +33,11 @@
|
||||||
console.log(args)
|
console.log(args)
|
||||||
});
|
});
|
||||||
iroom = room;
|
iroom = room;
|
||||||
/*setInterval(()=>{
|
document.addEventListener("click",async () => {
|
||||||
wsjs.server.tranferToServer({
|
const r = wsjs.virtualPressure.allocAPIPAddress();
|
||||||
e0:Math.random(),
|
debugger;
|
||||||
e1:Math.random(),
|
console.log(r);
|
||||||
e2:Math.random(),
|
})
|
||||||
e3:Math.random(),
|
|
||||||
e4:Math.random()
|
|
||||||
})
|
|
||||||
}, 10)*/
|
|
||||||
});
|
});
|
||||||
wsjs.on('peer',(peer)=>{
|
wsjs.on('peer',(peer)=>{
|
||||||
peer.on('message',(...args)=>{
|
peer.on('message',(...args)=>{
|
||||||
|
@ -50,15 +46,7 @@
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
let ws = new WebSocket("ws://localhost:7707");
|
main();
|
||||||
ws.onmessage = function({data}){
|
|
||||||
message.innerText = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
// main();
|
|
||||||
/*for (const iterator of Array.from({length:90}).fill(0)) {
|
|
||||||
main();
|
|
||||||
}*/
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue