lib/Connection.js
function(monitorJSON, callback) {
      callback = callback || function(){};
      var t = this,
          errorText = '',
          router = Monitor.getRouter(),
          gateway = t.get('gateway'),
          startTime = Date.now(),
          firewall = t.get('firewall'),
          logCtxt = _.extend({}, monitorJSON);

      // Don't allow inbound requests if this connection is firewalled
      if (firewall) {
        errorText = 'firewalled';
        log.error('probeConnect', errorText, logCtxt);
        return callback(errorText);
      }

      // Determine the connection to use (or internal)
      router.determineConnection(monitorJSON, gateway, function(err, connection) {
        if (err) {return callback(err);}
        if (connection && !gateway) {return callback('Not a gateway');}

        // Function to run upon connection (internal or external)
        var onConnect = function(error, probe) {

          if (error) {
            log.error(t.logId + 'probeConnect', error, logCtxt);
            return callback(error);
          }

          // Get probe info
          var probeId = probe.get('id');
          logCtxt.id = probeId;

          // Check for a duplicate proxy for this probeId.  This happens when
          // two connect requests are made before the first one completes.
          var monitorProxy = t.incomingMonitorsById[probeId];
          if (monitorProxy != null) {
            probe.refCount--;
            logCtxt.dupDetected = true;
            logCtxt.refCount = probe.refCount;
            log.info(t.logId + 'probeConnected', logCtxt);
            return callback(null, monitorProxy.probe.toJSON());
          }

          // Connect the montior proxy
          monitorProxy = new Monitor(monitorJSON);
          monitorProxy.set('probeId', probeId);
          t.incomingMonitorsById[probeId] = monitorProxy;
          monitorProxy.probe = probe;
          monitorProxy.probeChange = function(){
            try {
              t.emit('probe:change:' + probeId, probe.changedAttributes());
            }
            catch (e) {
              log.error('probeChange', e, probe, logCtxt);
            }
          };
          probe.connectTime = Date.now();
          var duration = probe.connectTime - startTime;
          logCtxt.duration = duration;
          logCtxt.refCount = probe.refCount;
          log.info(t.logId + 'probeConnected', logCtxt);
          stat.time(t.logId + 'probeConnected', duration);
          callback(null, probe.toJSON());
          probe.on('change', monitorProxy.probeChange);

          // Disconnect the probe on connection disconnect
          t.on('disconnect', function() {
            t.probeDisconnect({probeId:probeId});
          });
        };

        // Connect internally or externally
        if (connection) {
          router.connectExternal(monitorJSON, connection, onConnect);
        } else {
          router.connectInternal(monitorJSON, onConnect);
        }
      });
    }
function(monitorJSON, callback) {
      callback = callback || function(){};
      var t = this,
          errorText = '',
          router = Monitor.getRouter(),
          gateway = t.get('gateway'),
          startTime = Date.now(),
          firewall = t.get('firewall'),
          logCtxt = _.extend({}, monitorJSON);

      // Don't allow inbound requests if this connection is firewalled
      if (firewall) {
        errorText = 'firewalled';
        log.error('probeConnect', errorText, logCtxt);
        return callback(errorText);
      }

      // Determine the connection to use (or internal)
      router.determineConnection(monitorJSON, gateway, function(err, connection) {
        if (err) {return callback(err);}
        if (connection && !gateway) {return callback('Not a gateway');}

        // Function to run upon connection (internal or external)
        var onConnect = function(error, probe) {

          if (error) {
            log.error(t.logId + 'probeConnect', error, logCtxt);
            return callback(error);
          }

          // Get probe info
          var probeId = probe.get('id');
          logCtxt.id = probeId;

          // Check for a duplicate proxy for this probeId.  This happens when
          // two connect requests are made before the first one completes.
          var monitorProxy = t.incomingMonitorsById[probeId];
          if (monitorProxy != null) {
            probe.refCount--;
            logCtxt.dupDetected = true;
            logCtxt.refCount = probe.refCount;
            log.info(t.logId + 'probeConnected', logCtxt);
            return callback(null, monitorProxy.probe.toJSON());
          }

          // Connect the montior proxy
          monitorProxy = new Monitor(monitorJSON);
          monitorProxy.set('probeId', probeId);
          t.incomingMonitorsById[probeId] = monitorProxy;
          monitorProxy.probe = probe;
          monitorProxy.probeChange = function(){
            try {
              t.emit('probe:change:' + probeId, probe.changedAttributes());
            }
            catch (e) {
              log.error('probeChange', e, probe, logCtxt);
            }
          };
          probe.connectTime = Date.now();
          var duration = probe.connectTime - startTime;
          logCtxt.duration = duration;
          logCtxt.refCount = probe.refCount;
          log.info(t.logId + 'probeConnected', logCtxt);
          stat.time(t.logId + 'probeConnected', duration);
          callback(null, probe.toJSON());
          probe.on('change', monitorProxy.probeChange);

          // Disconnect the probe on connection disconnect
          t.on('disconnect', function() {
            t.probeDisconnect({probeId:probeId});
          });
        };

        // Connect internally or externally
        if (connection) {
          router.connectExternal(monitorJSON, connection, onConnect);
        } else {
          router.connectInternal(monitorJSON, onConnect);
        }
      });
    }
function ( monitorJSON , callback ) { callback = callback || function ( ) { } ; var t = this , errorText = '' , router = Monitor . getRouter ( ) , gateway = t . get ( 'gateway' ) , startTime = Date . now ( ) , firewall = t . get ( 'firewall' ) , logCtxt = _ . extend ( { } , monitorJSON ) ; // Don't allow inbound requests if this connection is firewalled if ( firewall ) { errorText = 'firewalled' ; log . error ( 'probeConnect' , errorText , logCtxt ) ; return callback ( errorText ) ; } // Determine the connection to use (or internal) router . determineConnection ( monitorJSON , gateway , function ( err , connection ) { if ( err ) { return callback ( err ) ; } if ( connection && ! gateway ) { return callback ( 'Not a gateway' ) ; } // Function to run upon connection (internal or external) var onConnect = function ( error , probe ) { if ( error ) { log . error ( t . logId + 'probeConnect' , error , logCtxt ) ; return callback ( error ) ; } // Get probe info var probeId = probe . get ( 'id' ) ; logCtxt . id = probeId ; // Check for a duplicate proxy for this probeId.  This happens when // two connect requests are made before the first one completes. var monitorProxy = t . incomingMonitorsById [ probeId ] ; if ( monitorProxy != null ) { probe . refCount -- ; logCtxt . dupDetected = true ; logCtxt . refCount = probe . refCount ; log . info ( t . logId + 'probeConnected' , logCtxt ) ; return callback ( null , monitorProxy . probe . toJSON ( ) ) ; } // Connect the montior proxy monitorProxy = new Monitor ( monitorJSON ) ; monitorProxy . set ( 'probeId' , probeId ) ; t . incomingMonitorsById [ probeId ] = monitorProxy ; monitorProxy . probe = probe ; monitorProxy . probeChange = function ( ) { try { t . emit ( 'probe:change:' + probeId , probe . changedAttributes ( ) ) ; } catch ( e ) { log . error ( 'probeChange' , e , probe , logCtxt ) ; } } ; probe . connectTime = Date . now ( ) ; var duration = probe . connectTime - startTime ; logCtxt . duration = duration ; logCtxt . refCount = probe . refCount ; log . info ( t . logId + 'probeConnected' , logCtxt ) ; stat . time ( t . logId + 'probeConnected' , duration ) ; callback ( null , probe . toJSON ( ) ) ; probe . on ( 'change' , monitorProxy . probeChange ) ; // Disconnect the probe on connection disconnect t . on ( 'disconnect' , function ( ) { t . probeDisconnect ( { probeId : probeId } ) ; } ) ; } ; // Connect internally or externally if ( connection ) { router . connectExternal ( monitorJSON , connection , onConnect ) ; } else { router . connectInternal ( monitorJSON , onConnect ) ; } } ) ; }
Process an inbound request to connect with a probe

This will fail if this connection was created as a firewall.

@method probeConnect
@protected
@param monitorJSON {Object} Probe connection parameters, including:
@param monitorJSON.probeClass {String} The probe class
@param monitorJSON.initParams {Object} Probe initialization parameters
@param monitorJSON.hostName {String} Connect with this host (if called as a gateway)
@param monitorJSON.appName {String} Connect with this app (if called as a gateway)
@param callback {Function(error, probeJSON)} Callback function
Process an inbound request to connect with a probe