lib/ServerEvents.js
/**
 * Couples events between the given socket.io socket and the client.
 * @param {MasterProxyServer} server The MasterProxyServer instance.
 * @param {Object} socket A socket.io socket.
 * @param {winston.Logger} log A winston logger for logging purposes.
 * @return {undefined}
 */
Couples events between the given socket.io socket and the client.
Couples events between the given socket . io socket and the client .
function addSlaveServerEvents(server, socket, log) {
  const respond = {
    with: {
      error: (sid, e) => {
        log.warn(`Sending exception message from slave ${sid} to master @${socket.handshake.address}`);
        socket.emit('message', {
          from: sid,
          title: 'SlaveIOException',
          sent: Date.now(),
          error: { name: `RemoteSlaveError: ${e.name}`, message: e.message, stack: e.stack },
        });
      },
      responseError: (req, message) => {
        socket.emit('message', {
          title: 'SlaveIOResponse',
          sent: Date.now(),
          request: req,
          error: {
            name: 'RequestAborted',
            message,
          },
          data: undefined,
        });
      },
      success: (res) => {
        socket.emit('message', res.raw);
      },
    },
  };
  socket.on('disconnect', () => {
    delete remoteToLocalSlaveIdMap[socket.id];
    delete queuedSlaveTasks[socket.id];
    master.kill.group(socket.id);
  });
  socket.on('message', m => {
    const args = isInitMessageGetArgsOrFalse(m);
    if (args) {
      remoteToLocalSlaveIdMap[socket.id] = { [args['dist-io-slave-id']]: slaveIds++ };
      queuedSlaveTasks[socket.id] = [];
      if (master.slaveCount < server.maxConcurrentSlaves) {
        initSlave(server, socket, log, args['dist-io-slave-id'], m.path, m.options);
      } else {
        log.verbose(
          `Queuing slave with remote id ${args['dist-io-slave-id']} ` +
          `(current slave count is ${master.slaveCount}, max is ${server.maxConcurrentSlaves})`
        );
        queuedSlaves.push(() => {
          initSlave(server, socket, log, args['dist-io-slave-id'], m.path, m.options);
        });
      }
    } else { 
      const serverId = remoteToLocalSlaveIdMap[socket.id]
        ? remoteToLocalSlaveIdMap[socket.id][m.for]
        : -1;
      const slave = master.slave(serverId);
      if (!slave) {
        if (!Slave.isMasterMessage(m)) {
          log.error(`Client @${socket.handshake.address} send an invalid/malformed request, disconnecting.`);
          respond.with.error(m.for, new ReferenceError('Invalid request'));
        } else if (typeof remoteToLocalSlaveIdMap[socket.id][m.for] !== 'number') {
          respond.with.error(m.for, new ReferenceError(`No slave with id ${m.for} exists!`));
        } else {
          if (queuedSlaveTasks[socket.id]) {
            queuedSlaveTasks[socket.id].push(() => {
              executeTask(log, m, socket, serverId, respond);
            });
          } else {
            respond.with.responseError('Slave was killed or closed during request.');
          }
        }
      } else {
        executeTask(log, m, socket, serverId, respond);
      }
    }
  });
}
function addSlaveServerEvents ( server , socket , log ) { const respond = { with : { error : ( sid , e ) => { log . warn ( ` ${ sid } ${ socket . handshake . address } ` ) ; socket . emit ( 'message' , { from : sid , title : 'SlaveIOException' , sent : Date . now ( ) , error : { name : ` ${ e . name } ` , message : e . message , stack : e . stack } , } ) ; } , responseError : ( req , message ) => { socket . emit ( 'message' , { title : 'SlaveIOResponse' , sent : Date . now ( ) , request : req , error : { name : 'RequestAborted' , message , } , data : undefined , } ) ; } , success : ( res ) => { socket . emit ( 'message' , res . raw ) ; } , } , } ; socket . on ( 'disconnect' , ( ) => { delete remoteToLocalSlaveIdMap [ socket . id ] ; delete queuedSlaveTasks [ socket . id ] ; master . kill . group ( socket . id ) ; } ) ; socket . on ( 'message' , m => { const args = isInitMessageGetArgsOrFalse ( m ) ; if ( args ) { remoteToLocalSlaveIdMap [ socket . id ] = { [ args [ 'dist-io-slave-id' ] ] : slaveIds ++ } ; queuedSlaveTasks [ socket . id ] = [ ] ; if ( master . slaveCount < server . maxConcurrentSlaves ) { initSlave ( server , socket , log , args [ 'dist-io-slave-id' ] , m . path , m . options ) ; } else { log . verbose ( ` ${ args [ 'dist-io-slave-id' ] } ` + ` ${ master . slaveCount } ${ server . maxConcurrentSlaves } ` ) ; queuedSlaves . push ( ( ) => { initSlave ( server , socket , log , args [ 'dist-io-slave-id' ] , m . path , m . options ) ; } ) ; } } else { const serverId = remoteToLocalSlaveIdMap [ socket . id ] ? remoteToLocalSlaveIdMap [ socket . id ] [ m . for ] : - 1 ; const slave = master . slave ( serverId ) ; if ( ! slave ) { if ( ! Slave . isMasterMessage ( m ) ) { log . error ( ` ${ socket . handshake . address } ` ) ; respond . with . error ( m . for , new ReferenceError ( 'Invalid request' ) ) ; } else if ( typeof remoteToLocalSlaveIdMap [ socket . id ] [ m . for ] !== 'number' ) { respond . with . error ( m . for , new ReferenceError ( ` ${ m . for } ` ) ) ; } else { if ( queuedSlaveTasks [ socket . id ] ) { queuedSlaveTasks [ socket . id ] . push ( ( ) => { executeTask ( log , m , socket , serverId , respond ) ; } ) ; } else { respond . with . responseError ( 'Slave was killed or closed during request.' ) ; } } } else { executeTask ( log , m , socket , serverId , respond ) ; } } } ) ; }
Couples events between the given socket.io socket and the client.
Couples events between the given socket . io socket and the client .
/**
       * Send an error back to the client.
       * @param {Number} sid The slave id this error is in response to.
       * @param {String|Error} e The error to send.
       * @return {undefined}
       */ /**
       * Respond to a request successfully.
       * @param {Response} res The response from the slave.
       * @return {undefined}
       */ // Handle disconnect... // Kill any slaves not closed by the remote master... // Handle messages from the client... // Proxy command to master on this machine...