ext/peer.js
function peer_send(packet, link, cbSend)
    {
      var router = mesh.index[to];
      if(!router) return cbSend('cannot peer to an unknown router: '+pipe.to);
      if(!router.x) return cbSend('cannot peer yet via this router: '+pipe.to);
      if(!link) return cbSend('requires link');

      // no packet means try to send our keys
      if(!packet)
      {
        Object.keys(mesh.keys).forEach(function(csid){
          if(link.csid && link.csid != csid) return; // if we know the csid, only send that key
          var json = {type:'peer',peer:link.hashname,c:router.x.cid()};
          var body = lob.encode(hashname.intermediates(mesh.keys), hashname.key(csid, mesh.keys));
          var attach = lob.encode({type:'link', csid:csid}, body);
          log.debug('sending peer key to',router.hashname,json,csid);
          router.x.send({json:json,body:attach});
        });
        return;
      }

      // if it's an encrypted channel packet, pass through direct to router
      if(packet.head.length == 0) return router.x.sending(packet);

      // otherwise we're always creating a new peer channel to carry the request
      var json = {type:'peer',peer:link.hashname,c:router.x.cid()};
      var body = lob.encode(packet);
      log.debug('sending peer handshake to',router.hashname,json,body);
      router.x.send({json:json,body:body});
      cbSend();
    }
function peer_send(packet, link, cbSend)
    {
      var router = mesh.index[to];
      if(!router) return cbSend('cannot peer to an unknown router: '+pipe.to);
      if(!router.x) return cbSend('cannot peer yet via this router: '+pipe.to);
      if(!link) return cbSend('requires link');

      // no packet means try to send our keys
      if(!packet)
      {
        Object.keys(mesh.keys).forEach(function(csid){
          if(link.csid && link.csid != csid) return; // if we know the csid, only send that key
          var json = {type:'peer',peer:link.hashname,c:router.x.cid()};
          var body = lob.encode(hashname.intermediates(mesh.keys), hashname.key(csid, mesh.keys));
          var attach = lob.encode({type:'link', csid:csid}, body);
          log.debug('sending peer key to',router.hashname,json,csid);
          router.x.send({json:json,body:attach});
        });
        return;
      }

      // if it's an encrypted channel packet, pass through direct to router
      if(packet.head.length == 0) return router.x.sending(packet);

      // otherwise we're always creating a new peer channel to carry the request
      var json = {type:'peer',peer:link.hashname,c:router.x.cid()};
      var body = lob.encode(packet);
      log.debug('sending peer handshake to',router.hashname,json,body);
      router.x.send({json:json,body:body});
      cbSend();
    }
function peer_send ( packet , link , cbSend ) { var router = mesh . index [ to ] ; if ( ! router ) return cbSend ( 'cannot peer to an unknown router: ' + pipe . to ) ; if ( ! router . x ) return cbSend ( 'cannot peer yet via this router: ' + pipe . to ) ; if ( ! link ) return cbSend ( 'requires link' ) ; // no packet means try to send our keys if ( ! packet ) { Object . keys ( mesh . keys ) . forEach ( function ( csid ) { if ( link . csid && link . csid != csid ) return ; // if we know the csid, only send that key var json = { type : 'peer' , peer : link . hashname , c : router . x . cid ( ) } ; var body = lob . encode ( hashname . intermediates ( mesh . keys ) , hashname . key ( csid , mesh . keys ) ) ; var attach = lob . encode ( { type : 'link' , csid : csid } , body ) ; log . debug ( 'sending peer key to' , router . hashname , json , csid ) ; router . x . send ( { json : json , body : attach } ) ; } ) ; return ; } // if it's an encrypted channel packet, pass through direct to router if ( packet . head . length == 0 ) return router . x . sending ( packet ) ; // otherwise we're always creating a new peer channel to carry the request var json = { type : 'peer' , peer : link . hashname , c : router . x . cid ( ) } ; var body = lob . encode ( packet ) ; log . debug ( 'sending peer handshake to' , router . hashname , json , body ) ; router . x . send ( { json : json , body : body } ) ; cbSend ( ) ; }
handle any peer delivery through the router
handle any peer delivery through the router