lib/Router.js
function(monitorJSON, callback) {

      // Build a key for this probe from the probeClass and initParams
      var t = this,
          probeKey = t.buildProbeKey(monitorJSON),
          probeClass = monitorJSON.probeClass,
          initParams = monitorJSON.initParams,
          probeImpl = null;

      var whenDone = function(error) {

        // Wait one tick before firing the callback.  This simulates a remote
        // connection, making the client callback order consistent, regardless
        // of a local or remote connection.
        setTimeout(function() {

          // Dont connect the probe on error
          if (error) {
            if (probeImpl) {
              delete t.runningProbesByKey[probeKey];
              delete t.runningProbesById[probeImpl.id];
              try {
                // This may fail depending on how many resources were created
                // by the probe before failure.  Ignore errors.
                probeImpl.release();
              } catch (e){}
            }
            log.error('connectInternal', {error: error, probeKey: probeKey});
            return callback(error);
          }

          // Probes are released based on reference count
          probeImpl.refCount++;
          log.info('connectInternal', {probeKey: probeKey, probeId: probeImpl.id});
          callback(null, probeImpl);
        }, 0);
      };

      // Get the probe instance
      probeImpl = t.runningProbesByKey[probeKey];
      if (!probeImpl) {

        // Instantiate the probe
        var ProbeClass = Probe.classes[probeClass];
        if (!ProbeClass) {
          return whenDone({msg:'Probe not available: ' + probeClass});
        }
        var initOptions = {asyncInit: false, callback: whenDone};
        try {
          // Deep copy the init params, because Backbone mutates them.  This
          // is bad if the init params came in from defaults of another object,
          // because those defaults will get mutated.
          var paramCopy = Monitor.deepCopy(initParams);

          // Instantiate a new probe
          probeImpl = new ProbeClass(paramCopy, initOptions);
          probeImpl.set({id: Monitor.generateUniqueId()});
          probeImpl.refCount = 0;
          probeImpl.probeKey = probeKey;
          t.runningProbesByKey[probeKey] = probeImpl;
          t.runningProbesById[probeImpl.id] = probeImpl;
        } catch (e) {
          var error = {msg: 'Error instantiating probe ' + probeClass, error: e.message};
          log.error('connect', error);
          return whenDone(error);
        }

        // Return early if the probe constructor transferred responsibility
        // for calling the callback.
        if (initOptions.asyncInit) {
          return;
        }
      }

      // The probe impl is found, and instantiated if necessary
      whenDone();
    }
function(monitorJSON, callback) {

      // Build a key for this probe from the probeClass and initParams
      var t = this,
          probeKey = t.buildProbeKey(monitorJSON),
          probeClass = monitorJSON.probeClass,
          initParams = monitorJSON.initParams,
          probeImpl = null;

      var whenDone = function(error) {

        // Wait one tick before firing the callback.  This simulates a remote
        // connection, making the client callback order consistent, regardless
        // of a local or remote connection.
        setTimeout(function() {

          // Dont connect the probe on error
          if (error) {
            if (probeImpl) {
              delete t.runningProbesByKey[probeKey];
              delete t.runningProbesById[probeImpl.id];
              try {
                // This may fail depending on how many resources were created
                // by the probe before failure.  Ignore errors.
                probeImpl.release();
              } catch (e){}
            }
            log.error('connectInternal', {error: error, probeKey: probeKey});
            return callback(error);
          }

          // Probes are released based on reference count
          probeImpl.refCount++;
          log.info('connectInternal', {probeKey: probeKey, probeId: probeImpl.id});
          callback(null, probeImpl);
        }, 0);
      };

      // Get the probe instance
      probeImpl = t.runningProbesByKey[probeKey];
      if (!probeImpl) {

        // Instantiate the probe
        var ProbeClass = Probe.classes[probeClass];
        if (!ProbeClass) {
          return whenDone({msg:'Probe not available: ' + probeClass});
        }
        var initOptions = {asyncInit: false, callback: whenDone};
        try {
          // Deep copy the init params, because Backbone mutates them.  This
          // is bad if the init params came in from defaults of another object,
          // because those defaults will get mutated.
          var paramCopy = Monitor.deepCopy(initParams);

          // Instantiate a new probe
          probeImpl = new ProbeClass(paramCopy, initOptions);
          probeImpl.set({id: Monitor.generateUniqueId()});
          probeImpl.refCount = 0;
          probeImpl.probeKey = probeKey;
          t.runningProbesByKey[probeKey] = probeImpl;
          t.runningProbesById[probeImpl.id] = probeImpl;
        } catch (e) {
          var error = {msg: 'Error instantiating probe ' + probeClass, error: e.message};
          log.error('connect', error);
          return whenDone(error);
        }

        // Return early if the probe constructor transferred responsibility
        // for calling the callback.
        if (initOptions.asyncInit) {
          return;
        }
      }

      // The probe impl is found, and instantiated if necessary
      whenDone();
    }
function ( monitorJSON , callback ) { // Build a key for this probe from the probeClass and initParams var t = this , probeKey = t . buildProbeKey ( monitorJSON ) , probeClass = monitorJSON . probeClass , initParams = monitorJSON . initParams , probeImpl = null ; var whenDone = function ( error ) { // Wait one tick before firing the callback.  This simulates a remote // connection, making the client callback order consistent, regardless // of a local or remote connection. setTimeout ( function ( ) { // Dont connect the probe on error if ( error ) { if ( probeImpl ) { delete t . runningProbesByKey [ probeKey ] ; delete t . runningProbesById [ probeImpl . id ] ; try { // This may fail depending on how many resources were created // by the probe before failure.  Ignore errors. probeImpl . release ( ) ; } catch ( e ) { } } log . error ( 'connectInternal' , { error : error , probeKey : probeKey } ) ; return callback ( error ) ; } // Probes are released based on reference count probeImpl . refCount ++ ; log . info ( 'connectInternal' , { probeKey : probeKey , probeId : probeImpl . id } ) ; callback ( null , probeImpl ) ; } , 0 ) ; } ; // Get the probe instance probeImpl = t . runningProbesByKey [ probeKey ] ; if ( ! probeImpl ) { // Instantiate the probe var ProbeClass = Probe . classes [ probeClass ] ; if ( ! ProbeClass ) { return whenDone ( { msg : 'Probe not available: ' + probeClass } ) ; } var initOptions = { asyncInit : false , callback : whenDone } ; try { // Deep copy the init params, because Backbone mutates them.  This // is bad if the init params came in from defaults of another object, // because those defaults will get mutated. var paramCopy = Monitor . deepCopy ( initParams ) ; // Instantiate a new probe probeImpl = new ProbeClass ( paramCopy , initOptions ) ; probeImpl . set ( { id : Monitor . generateUniqueId ( ) } ) ; probeImpl . refCount = 0 ; probeImpl . probeKey = probeKey ; t . runningProbesByKey [ probeKey ] = probeImpl ; t . runningProbesById [ probeImpl . id ] = probeImpl ; } catch ( e ) { var error = { msg : 'Error instantiating probe ' + probeClass , error : e . message } ; log . error ( 'connect' , error ) ; return whenDone ( error ) ; } // Return early if the probe constructor transferred responsibility // for calling the callback. if ( initOptions . asyncInit ) { return ; } } // The probe impl is found, and instantiated if necessary whenDone ( ) ; }
Connect to an internal probe implementation

This connects with a probe running in this process.  It will instantiate
the probe if it isn't currently running.

@method connectInternal
@protected
@param monitorJSON {Object} - The monitor toJSON data.  Containing:
@param monitorJSON.probeClass {String} - The probe class name to connect with (required)
@param monitorJSON.initParams {Object} - Probe initialization parameters.
@param callback {Function(error, probeImpl)} - Called when connected
Connect to an internal probe implementation