2021-11-25 07:59:13 +00:00
|
|
|
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'){
|
2021-11-25 07:59:13 +00:00
|
|
|
const {
|
|
|
|
initiateDataConnection,
|
|
|
|
initiateVideoTransferConnection,
|
|
|
|
onWebSocketData,
|
|
|
|
onDataConnectionDisconnect,
|
|
|
|
} = require('./childNode/utils.js')(s,config,lang,app,io)
|
2018-10-18 04:45:15 +00:00
|
|
|
s.childNodes = {};
|
2021-11-25 07:59:13 +00:00
|
|
|
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
|
|
|
});
|
2021-11-25 07:59:13 +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);
|
2019-09-24 18:35:37 +00:00
|
|
|
});
|
2021-11-25 07:59:13 +00:00
|
|
|
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{
|
2021-11-25 07:59:13 +00:00
|
|
|
childNodesConnectionIndex[y].sendJson(data)
|
2021-11-25 17:20:50 +00:00
|
|
|
// }
|
2018-10-24 00:37:51 +00:00
|
|
|
}
|
2018-10-18 04:45:15 +00:00
|
|
|
//child Node Websocket
|
2021-11-25 07:59:13 +00:00
|
|
|
childNodeWebsocket.on('connection', function (client, req) {
|
2018-10-18 04:45:15 +00:00
|
|
|
//functions for dispersing work to child servers;
|
2021-11-25 07:59:13 +00:00
|
|
|
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
|
|
|
}
|
2021-11-25 07:59:13 +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)
|
2021-11-25 07:59:13 +00:00
|
|
|
s.connectedToMasterNode = false;
|
|
|
|
const childIO = createWebSocketClient('ws://'+config.childNodes.host,{
|
|
|
|
onMessage: onDataFromMasterNode
|
|
|
|
})
|
2021-11-25 17:20:50 +00:00
|
|
|
function sendDataToMasterNode(data){
|
2021-11-25 07:59:13 +00:00
|
|
|
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 = [];
|
|
|
|
}
|
2021-11-25 07:59:13 +00:00
|
|
|
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
|
|
|
}
|
2020-07-11 21:44:37 +00:00
|
|
|
s.knexQuery = function(options,onMoveOn){
|
|
|
|
var callbackId = s.gid()
|
2021-11-25 07:59:13 +00:00
|
|
|
if(typeof onMoveOn === 'function')s.queuedSqlCallbacks[callbackId] = onMoveOn;
|
2021-11-25 17:20:50 +00:00
|
|
|
sendDataToMasterNode({f:'knex',options:options,callbackId:callbackId});
|
2020-07-11 21:44:37 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|