Multitasking & Some bugs fixed

This commit is contained in:
Abdussamed ULUTAŞ 2023-03-04 22:57:51 +03:00
parent e4f644a346
commit c630e20b46
10 changed files with 148 additions and 45 deletions

View File

@ -93,7 +93,7 @@ Client.prototype.pairList = function(){
Client.prototype.send = function(obj){
if(this.isProxy)
{
CLIENT_SEND_MESSAGE(this.id, obj)
CLIENT_SEND_MESSAGE(this.id, obj, this.proxyProcess)
}else{
this.socket.sendUTF(JSON.stringify(obj));
}

View File

@ -1,7 +1,7 @@
const { Room } = require("./Services/Room");
process.on('message',data => {
const {Client} = require("./Client.js")
const { Client } = require("./Client.js");
const { Room } = require("./Services/Room");
switch(data.type)
{
case "CLIENT_CREATED":{
@ -22,7 +22,10 @@ process.on('message',data => {
case "CLIENT_SEND_MESSAGE":{
slog("CLIENT_SEND_MESSAGE");
let client = Client.clients.get(data.uuid);
client.send(data.message)
if(client.isProxy != true)
{
client.send(data.message)
}
break;
}
case "CLIENT_DESTROY":{
@ -36,6 +39,34 @@ process.on('message',data => {
Room.rooms.set(room.id, room);
break;
}
case "ROOM_UPDATE_PROP":{
slog("ROOM_UPDATE_PROP");
let room = Room.rooms.get(data.uuid);
room[data.name] = data.value;
break;
}
case "ROOM_JOIN_CLIENT":{
slog("ROOM_JOIN_CLIENT");
let room = Room.rooms.get(data.uuid);
let client = Client.clients.get(data.client);
if(room && client)
{
client.rooms.add(room.id);
room.clients.set(client.id, client);
}
break;
}
case "ROOM_EJECT_CLIENT":{
slog("ROOM_EJECT_CLIENT");
let room = Room.rooms.get(data.uuid);
let client = Client.clients.get(data.client);
if(room && client)
{
client.rooms.delete(room.id);
room.clients.delete(client.id, client);
}
break;
}
case "ROOM_DESTROY":{
slog("ROOM_DESTROY");
Room.rooms.delete(data.value);
@ -62,13 +93,14 @@ function CLIENT_UPDATE_PROP(uuid, name, value)
value
})
};
function CLIENT_SEND_MESSAGE(uuid, message)
function CLIENT_SEND_MESSAGE(uuid, message, clusterPid)
{
mlog("CLIENT_SEND_MESSAGE");
process.send({
type:'CLIENT_SEND_MESSAGE',
uuid: uuid,
message
message,
process:clusterPid
})
};
function CLIENT_DESTROY(uuid)
@ -91,6 +123,36 @@ function ROOM_CREATED(room)
})
};
function ROOM_UPDATE_PROP(uuid, name, value)
{
mlog("ROOM_UPDATE_PROP");
process.send({
type:'ROOM_UPDATE_PROP',
uuid: uuid,
name,
value
})
};
function ROOM_JOIN_CLIENT(uuid, client)
{
mlog("ROOM_JOIN_CLIENT");
process.send({
type:'ROOM_JOIN_CLIENT',
uuid: uuid,
client
})
};
function ROOM_EJECT_CLIENT(uuid, client)
{
mlog("ROOM_EJECT_CLIENT");
process.send({
type:'ROOM_EJECT_CLIENT',
uuid: uuid,
client
})
};
function ROOM_DESTROY(room)
{
mlog("ROOM_DESTROY");
@ -115,6 +177,9 @@ exports.CLIENT_UPDATE_PROP = CLIENT_UPDATE_PROP;
exports.CLIENT_DESTROY = CLIENT_DESTROY;
exports.CLIENT_SEND_MESSAGE = CLIENT_SEND_MESSAGE;
exports.ROOM_CREATED = ROOM_CREATED;
exports.ROOM_UPDATE_PROP = ROOM_UPDATE_PROP;
exports.ROOM_JOIN_CLIENT = ROOM_JOIN_CLIENT;
exports.ROOM_EJECT_CLIENT = ROOM_EJECT_CLIENT;
exports.ROOM_DESTROY = ROOM_DESTROY;
exports.mlog = mlog;
exports.slog = slog;

View File

@ -87,6 +87,7 @@ addService(({
let {to,pack, handshake,wom} = message;
if(Room.rooms.has(to))
{
console.log("Oda da ", Room.rooms.get(to).clients.size,"kişi var")
if(!client.rooms.has(to))
{
return handshake && end({
@ -94,9 +95,9 @@ addService(({
})
};
Room.rooms.get(to).send([{
from: client.id,
from: to,
pack: pack
}, 'pack'], wom ? client.id : void 0);
}, 'pack/room'], wom ? client.id : void 0);
handshake && end({
type: 'success'
})

View File

@ -96,12 +96,15 @@ class APShortCode{
}
}
static release(code){
APShortCode.busyCodes.delete(code);
process.send({
type: 'AP_SHORTCODE/RELEASE',
uuid: APShortCode.busyCodes.get(code).id,
value: code
})
if(APShortCode.busyCodes.has(code))
{
process.send({
type: 'AP_SHORTCODE/RELEASE',
uuid: APShortCode.busyCodes.get(code).id,
value: code
})
APShortCode.busyCodes.delete(code);
}
}
static whois(num){
return APShortCode.busyCodes.get(num)?.id;
@ -183,12 +186,15 @@ class APIPAddress{
}
}
static release(code){
process.send({
type: 'AP_IPADDRESS/RELEASE',
uuid: APIPAddress.busyIP.get(code).id,
value: code
})
APIPAddress.busyIP.delete(code);
if(APIPAddress.busyIP.has(code))
{
process.send({
type: 'AP_IPADDRESS/RELEASE',
uuid: APIPAddress.busyIP.get(code).id,
value: code
})
APIPAddress.busyIP.delete(code);
}
}
static whois(num){
return APIPAddress.busyIP.get(num)?.id;
@ -381,8 +387,8 @@ process.on('message',({type, uuid, value}) => {
{
case "AP_NUMBER/LOCK":{
console.log("S",process.pid, 'IPPressure SYNCED')
APNumber.busyNumbers.set(value, client);
let client = Client.clients.get(uuid);
APNumber.busyNumbers.set(value, client);
if(client)
{
client.APNumber = value;
@ -401,8 +407,8 @@ process.on('message',({type, uuid, value}) => {
}
case "AP_SHORTCODE/LOCK":{
console.log("S",process.pid, 'IPPressure SYNCED')
APShortCode.busyCodes.set(value, client);
let client = Client.clients.get(uuid);
APShortCode.busyCodes.set(value, client);
if(client)
{
client.APShortCode = value;
@ -421,8 +427,8 @@ process.on('message',({type, uuid, value}) => {
}
case "AP_IPADDRESS/LOCK":{
console.log("S",process.pid, 'IPPressure SYNCED')
APIPAddress.busyIP.set(value, client);
let client = Client.clients.get(uuid);
APIPAddress.busyIP.set(value, client);
if(client)
{
client.APIPAddress = value;

View File

@ -3,7 +3,7 @@ let {randomUUID,createHash} = require("crypto");
const joi = require("joi");
let {addService,addListener} = require("../WebSocket.js");
const { termoutput } = require("../config.js");
const { ROOM_CREATED, ROOM_DESTROY } = require("../IPC.js");
const { ROOM_CREATED, ROOM_DESTROY, ROOM_UPDATE_PROP, ROOM_JOIN_CLIENT, ROOM_EJECT_CLIENT } = require("../IPC.js");
let term = require("terminal-kit").terminal;
function Sha256(update)
@ -65,6 +65,14 @@ function Room()
* @type {string[]}
*/
this.waitingInvited = new Set();
this.sync = function(...args){
process.nextTick(()=>{
for (const name of args) {
ROOM_UPDATE_PROP(this.id, name, this[name]);
}
})
};
}
/**
* @param {Room} room
@ -107,7 +115,10 @@ Room.fromJSON = function(data, room){
room.description = data.description;
room.joinType = data.joinType;
room.name = data.name;
room.owner = data.owner.id;
if(data.owner && Client.clients.has(data.owner))
{
room.owner = Client.clients.get(data.owner);
}
room.waitingInvited = new Set(data.waitingInvited);
obj.credential = data.credential;
obj.notifyActionInvite = data.notifyActionInvite;
@ -145,6 +156,7 @@ Room.prototype.join = function(client){
};
client.rooms.add(this.id);
this.clients.set(client.id, client);
ROOM_JOIN_CLIENT(this.id, client.id);
termoutput && term.green("Client Room joined ").white(this.name," in ").yellow(this.clients.size + "").white(" clients")('\n');
};
Room.prototype.down = function(){
@ -170,6 +182,7 @@ Room.prototype.eject = function(client){
}
client.rooms.delete(this.id);
this.clients.delete(client.id);
ROOM_EJECT_CLIENT(this.id, client.id);
if(this.clients.size == 0)
{
@ -193,8 +206,8 @@ addListener('connect',(global, client)=>{
room.id = client.id;
room.name = "Your Room | " + client.id;
room.owner = client;
room.join(client);
room.publish();
room.join(client);
});
addListener('disconnect',(global, client)=>{
@ -335,8 +348,8 @@ addService(({
{
room.credential = Sha256(message.credential + "");
}
room.join(client);
room.publish();
room.join(client);
end({
status: "success",
room: room.toJSON()

View File

@ -70,9 +70,9 @@ export default class MWSE extends EventTarget {
this.peer(from, true).emit('request', scope);
this.peer('me').emit('request', scope);
})
this.EventPooling.signal('pack/room',(payload : {to:string,pack:any}) => {
let {to,pack} = payload;
this.room(to).emit('message', pack);
this.EventPooling.signal('pack/room',(payload : {from:string,pack:any}) => {
let {from,pack} = payload;
this.room(from).emit('message', pack);
})
this.EventPooling.signal('room/joined',(payload : {id:string,roomid:any,ownerid:string}) => {
let {id,roomid} = payload;

View File

@ -11,6 +11,7 @@ async function main()
{
if(cluster.isPrimary == false)
{
console.log("Slave", process.pid);
// This process is a worker / slave
// Compile source code and run
require("./Source/index");
@ -19,11 +20,12 @@ async function main()
};
// This process is a primary / master
console.log("Master", process.pid);
// Worker process list
const master = new Map();
const coreCount = 2 // os.cpus().length;
const coreCount = os.cpus().length;
for(let index = 0; index < coreCount; index++)
{
// Open slave process
@ -34,10 +36,18 @@ async function main()
worker.message(
// This process want to send payload to sibling process with IPC
(workerId, payload) =>{
for (const [siblingWorkerId,{send}] of master) {
// Check Target worker
if(payload.process)
{
master.get(payload.process).send({
...payload,
pid: worker.id
})
}else for (const [siblingWorkerId,{send}] of master) {
// No sending to itself
if(workerId !== siblingWorkerId)
{
// Send command to sibling with IPC
send({
...payload,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -7,6 +7,7 @@
<title>Document</title>
</head>
<body>
<h2><pre id="log"></pre></h2>
<h1 id="message"></h1>
<script src="./script/index.js"></script>
<script>
@ -30,22 +31,29 @@
});
await room.createRoom();
room.on('message',(...args)=>{
console.log(args)
gg.r++
gg()
});
iroom = room;
document.addEventListener("click",async () => {
const r = wsjs.virtualPressure.allocAPIPAddress();
debugger;
console.log(r);
})
setInterval(()=>{
room.send({
type: "merhaba"
}, true)
gg.w++;
gg()
}, 200)
});
wsjs.on('peer',(peer)=>{
peer.on('message',(...args)=>{
console.log(args);
})
})
};
function gg()
{
log.innerHTML = `${gg.w} packet writed\n${gg.r} packet recaived`
}
gg.w = 0;
gg.r = 0;
setInterval(()=>{
window.location.reload();
}, 30000)
main();
</script>
</body>