Shinobi/libs/childNode.js

119 lines
5.1 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');
const { createWebSocketServer, createWebSocketClient } = require('./basic/websocketTools.js')
2018-10-16 02:40:12 +00:00
module.exports = function(s,config,lang,app,io){
2020-08-27 22:39:39 +00:00
const { cameraDestroy } = require('./monitor/utils.js')(s,config,lang)
2018-10-18 04:45:15 +00:00
//setup Master for childNodes
if(config.childNodes.enabled === true && config.childNodes.mode === 'master'){
const {
initiateDataConnection,
initiateVideoTransferConnection,
onWebSocketData,
onDataConnectionDisconnect,
} = require('./childNode/utils.js')(s,config,lang,app,io)
2018-10-18 04:45:15 +00:00
s.childNodes = {};
const childNodesConnectionIndex = {};
const childNodeHTTP = express();
const childNodeServer = http.createServer(app);
const childNodeWebsocket = createWebSocketServer();
childNodeServer.on('upgrade', function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
if (pathname === '/childNode') {
childNodeWebsocket.handleUpgrade(request, socket, head, function done(ws) {
childNodeWebsocket.emit('connection', ws, request)
})
} else if (pathname.indexOf('/socket.io') > -1) {
return;
} else {
socket.destroy();
}
2018-10-18 04:45:15 +00:00
});
const childNodeBindIP = config.childNodes.ip || config.bindip;
childNodeServer.listen(config.childNodes.port,childNodeBindIP,function(){
console.log(lang.Shinobi+' - CHILD NODE SERVER : '+childNodeBindIP + ':' + config.childNodes.port);
});
s.debugLog('childNodeWebsocket.attach(childNodeServer)')
2021-11-25 17:20:50 +00:00
//send data to child node function
s.cx = function(data,connectionId,senderObject){
// if(senderObject){
// data.sentFrom = senderObject.id;
// childNodesConnectionIndex[y].sendJson(data)
// }else{
childNodesConnectionIndex[y].sendJson(data)
2021-11-25 17:20:50 +00:00
// }
}
2018-10-18 04:45:15 +00:00
//child Node Websocket
childNodeWebsocket.on('connection', function (client, req) {
2018-10-18 04:45:15 +00:00
//functions for dispersing work to child servers;
const connectionId = s.gid(10);
client.id = connectionId;
function onConnection(d){
const data = JSON.parse(d);
const childNodeKeyAccepted = config.childNodes.key.indexOf(data.socketKey) > -1;
if(!client.shinobiChildAlreadyRegistered && data.f === 'init' && childNodeKeyAccepted){
const connectionAddress = initiateDataConnection(client,req,data);
childNodesConnectionIndex[connectionId] = client;
client.removeListener('message',onConnection)
client.on('message',(d) => {
const data = JSON.parse(d);
onWebSocketData(client,data)
})
}else{
client.destroy()
2018-10-18 04:42:32 +00:00
}
}
client.on('message',onConnection)
client.on('disconnect',() => {
onDataConnectionDisconnect(client, req)
2018-09-28 05:37:08 +00:00
})
2018-10-18 04:45:15 +00:00
})
}else
//setup Child for childNodes
if(config.childNodes.enabled === true && config.childNodes.mode === 'child' && config.childNodes.host){
2021-11-25 17:20:50 +00:00
const {
initiateConnectionToMasterNode,
onDisconnectFromMasterNode,
onDataFromMasterNode,
} = require('./childNode/childUtils.js')(s,config,lang,app,io)
s.connectedToMasterNode = false;
const childIO = createWebSocketClient('ws://'+config.childNodes.host,{
onMessage: onDataFromMasterNode
})
2021-11-25 17:20:50 +00:00
function sendDataToMasterNode(data){
x.socketKey = config.childNodes.key;
childIO.send(JSON.stringify(data));
}
2021-11-25 17:20:50 +00:00
s.cx = sendDataToMasterNode;
s.tx = function(x,y){
sendDataToMasterNode({f:'s.tx',data:x,to:y})
}
s.userLog = function(x,y){
sendDataToMasterNode({f:'s.userLog',mon:x,data:y})
}
2018-10-18 04:45:15 +00:00
s.queuedSqlCallbacks = {}
s.sqlQuery = function(query,values,onMoveOn){
var callbackId = s.gid()
if(!values){values=[]}
if(typeof values === 'function'){
var onMoveOn = values;
var values = [];
}
if(typeof onMoveOn === 'function')s.queuedSqlCallbacks[callbackId] = onMoveOn;
2021-11-25 17:20:50 +00:00
sendDataToMasterNode({f:'sql',query:query,values:values,callbackId:callbackId});
2018-10-18 04:42:32 +00:00
}
s.knexQuery = function(options,onMoveOn){
var callbackId = s.gid()
if(typeof onMoveOn === 'function')s.queuedSqlCallbacks[callbackId] = onMoveOn;
2021-11-25 17:20:50 +00:00
sendDataToMasterNode({f:'knex',options:options,callbackId:callbackId});
}
2021-11-25 17:20:50 +00:00
childIO.on('connect', function(){
initiateConnectionToMasterNode()
2018-10-18 04:45:15 +00:00
})
2021-11-25 17:20:50 +00:00
childIO.on('disconnect',function(){
onDisconnectFromMasterNode()
2018-10-18 04:45:15 +00:00
})
2018-09-28 05:37:08 +00:00
}
}