lib/pool.js
function Pool(options) {
  /* jshint maxcomplexity: 10 */
  /* jshint maxstatements: 20 */

  var self = this;

  options = options || {};
  this.keepalive = false;

  this._connectedPeers = {};
  this._addrs = [];

  this.listenAddr = options.listenAddr !== false;
  this.dnsSeed = options.dnsSeed !== false;
  this.maxSize = options.maxSize || Pool.MaxConnectedPeers;
  this.messages = options.messages;
  this.network = Networks.get(options.network) || Networks.defaultNetwork;
  this.relay = options.relay === false ? false : true;

  if (options.addrs) {
    for(var i = 0; i < options.addrs.length; i++) {
      this._addAddr(options.addrs[i]);
    }
  }

  if (this.listenAddr) {
    this.on('peeraddr', function peerAddrEvent(peer, message) {
      var addrs = message.addresses;
      var length = addrs.length;
      for (var i = 0; i < length; i++) {
        var addr = addrs[i];
        var future = new Date().getTime() + (10 * 60 * 1000);
        if (addr.time.getTime() <= 100000000000 || addr.time.getTime() > future) {
          // In case of an invalid time, assume "5 days ago"
          var past = new Date(new Date().getTime() - 5 * 24 * 60 * 60 * 1000);
          addr.time = past;
        }
        this._addAddr(addr);
      }
    });
  }

  this.on('seed', function seedEvent(ips) {
    ips.forEach(function(ip) {
      self._addAddr({
        ip: {
          v4: ip
        }
      });
    });
    if (self.keepalive) {
      self._fillConnections();
    }
  });

  this.on('peerdisconnect', function peerDisconnectEvent(peer, addr) {
    self._deprioritizeAddr(addr);
    self._removeConnectedPeer(addr);
    if (self.keepalive) {
      self._fillConnections();
    }
  });

  return this;

}
function Pool(options) {
  /* jshint maxcomplexity: 10 */
  /* jshint maxstatements: 20 */

  var self = this;

  options = options || {};
  this.keepalive = false;

  this._connectedPeers = {};
  this._addrs = [];

  this.listenAddr = options.listenAddr !== false;
  this.dnsSeed = options.dnsSeed !== false;
  this.maxSize = options.maxSize || Pool.MaxConnectedPeers;
  this.messages = options.messages;
  this.network = Networks.get(options.network) || Networks.defaultNetwork;
  this.relay = options.relay === false ? false : true;

  if (options.addrs) {
    for(var i = 0; i < options.addrs.length; i++) {
      this._addAddr(options.addrs[i]);
    }
  }

  if (this.listenAddr) {
    this.on('peeraddr', function peerAddrEvent(peer, message) {
      var addrs = message.addresses;
      var length = addrs.length;
      for (var i = 0; i < length; i++) {
        var addr = addrs[i];
        var future = new Date().getTime() + (10 * 60 * 1000);
        if (addr.time.getTime() <= 100000000000 || addr.time.getTime() > future) {
          // In case of an invalid time, assume "5 days ago"
          var past = new Date(new Date().getTime() - 5 * 24 * 60 * 60 * 1000);
          addr.time = past;
        }
        this._addAddr(addr);
      }
    });
  }

  this.on('seed', function seedEvent(ips) {
    ips.forEach(function(ip) {
      self._addAddr({
        ip: {
          v4: ip
        }
      });
    });
    if (self.keepalive) {
      self._fillConnections();
    }
  });

  this.on('peerdisconnect', function peerDisconnectEvent(peer, addr) {
    self._deprioritizeAddr(addr);
    self._removeConnectedPeer(addr);
    if (self.keepalive) {
      self._fillConnections();
    }
  });

  return this;

}
function Pool ( options ) { /* jshint maxcomplexity: 10 */ /* jshint maxstatements: 20 */ var self = this ; options = options || { } ; this . keepalive = false ; this . _connectedPeers = { } ; this . _addrs = [ ] ; this . listenAddr = options . listenAddr !== false ; this . dnsSeed = options . dnsSeed !== false ; this . maxSize = options . maxSize || Pool . MaxConnectedPeers ; this . messages = options . messages ; this . network = Networks . get ( options . network ) || Networks . defaultNetwork ; this . relay = options . relay === false ? false : true ; if ( options . addrs ) { for ( var i = 0 ; i < options . addrs . length ; i ++ ) { this . _addAddr ( options . addrs [ i ] ) ; } } if ( this . listenAddr ) { this . on ( 'peeraddr' , function peerAddrEvent ( peer , message ) { var addrs = message . addresses ; var length = addrs . length ; for ( var i = 0 ; i < length ; i ++ ) { var addr = addrs [ i ] ; var future = new Date ( ) . getTime ( ) + ( 10 * 60 * 1000 ) ; if ( addr . time . getTime ( ) <= 100000000000 || addr . time . getTime ( ) > future ) { // In case of an invalid time, assume "5 days ago" var past = new Date ( new Date ( ) . getTime ( ) - 5 * 24 * 60 * 60 * 1000 ) ; addr . time = past ; } this . _addAddr ( addr ) ; } } ) ; } this . on ( 'seed' , function seedEvent ( ips ) { ips . forEach ( function ( ip ) { self . _addAddr ( { ip : { v4 : ip } } ) ; } ) ; if ( self . keepalive ) { self . _fillConnections ( ) ; } } ) ; this . on ( 'peerdisconnect' , function peerDisconnectEvent ( peer , addr ) { self . _deprioritizeAddr ( addr ) ; self . _removeConnectedPeer ( addr ) ; if ( self . keepalive ) { self . _fillConnections ( ) ; } } ) ; return this ; }
A pool is a collection of Peers. A pool will discover peers from DNS seeds, and
collect information about new peers in the network. When a peer disconnects the pool
will connect to others that are available to maintain a max number of
ongoing peer connections. Peer events are relayed to the pool.

@example
```javascript

var pool = new Pool({network: Networks.livenet});
pool.on('peerinv', function(peer, message) {
// do something with the inventory announcement
});
pool.connect();
```

@param {Object=} options
@param {Network=} options.network - The network configuration
@param {Boolean=} options.listenAddr - Prevent new peers being added from addr messages
@param {Boolean=} options.dnsSeed - Prevent seeding with DNS discovered known peers
@param {Boolean=} options.relay - Prevent inventory announcements until a filter is loaded
@param {Number=} options.maxSize - The max number of peers
@returns {Pool}
@constructor
A pool is a collection of Peers . A pool will discover peers from DNS seeds and collect information about new peers in the network . When a peer disconnects the pool will connect to others that are available to maintain a max number of ongoing peer connections . Peer events are relayed to the pool .