src/sig-server/routes-ws/index.js
function join (options) {
  if (options.peerId.length !== 12) {
    return this.emit('we-ready', new Error('Unvalid peerId length, must be 48 bits, received: ' + options.peerId).toString())
  }

  if (peerTable[options.peerId]) {
    return this.emit('we-ready', new Error('peerId already exists').toString())
  }

  peerTable[options.peerId] = {
    socket: this,
    notify: typeof options.notify === 'boolean' && options.notify === true,
    fingers: options.fingers || {
      '0': {
        ideal: idealFinger(new Id(options.peerId), '0').toHex(),
        current: undefined
      }}
  }

  log('peer joined: ' + options.peerId)
  this.emit('we-ready')
  if (Object.keys(peerTable).length === 1) {
    return log('This was the first peer join, do nothing')
  }
  notify()
  if (peerTable[options.peerId].fingers['0'].current === undefined) {
    if (!peerTable[options.peerId].notify) {
      return
    }
    updateFinger(options.peerId, '0')
  }

  // notify if to other peers if this new Peer is a best finger for them
  function notify () {
    const newId = options.peerId
    const peerIds = Object.keys(peerTable)
    // check for all the peers
    // if same id skip
    // if notify === false skip
    // check the first finger that matches the criteria for ideal or next to ideal
    peerIds.forEach((peerId) => {
      if (newId === peerId) {
        return // skip ourselves
      }
      if (!peerTable[peerId].notify) {
        return // skip if it doesn't want to be notified of available peers
      }

      // if it had none, notify to get a successor
      if (peerTable[peerId].fingers['0'].current === undefined) {
        peerTable[peerId].fingers['0'].current = newId
        peerTable[peerId].socket.emit('we-update-finger', {
          row: '0',
          id: newId
        })
        return
      }

      const rows = Object.keys(peerTable[peerId].fingers)
      // find the first row that could use this finger
      rows.some((row) => {
        const finger = peerTable[peerId].fingers[row]
        const bestCandidate = fingerBestFit(
            new Id(peerId),
            new Id(finger.ideal),
            new Id(finger.current),
            new Id(newId))
        if (bestCandidate) {
          peerTable[peerId].fingers[row].current = newId
          peerTable[peerId].socket.emit('we-update-finger', {
            row: row,
            id: newId
          })
          return true
        }
      })
    })
  }
}
function join (options) {
  if (options.peerId.length !== 12) {
    return this.emit('we-ready', new Error('Unvalid peerId length, must be 48 bits, received: ' + options.peerId).toString())
  }

  if (peerTable[options.peerId]) {
    return this.emit('we-ready', new Error('peerId already exists').toString())
  }

  peerTable[options.peerId] = {
    socket: this,
    notify: typeof options.notify === 'boolean' && options.notify === true,
    fingers: options.fingers || {
      '0': {
        ideal: idealFinger(new Id(options.peerId), '0').toHex(),
        current: undefined
      }}
  }

  log('peer joined: ' + options.peerId)
  this.emit('we-ready')
  if (Object.keys(peerTable).length === 1) {
    return log('This was the first peer join, do nothing')
  }
  notify()
  if (peerTable[options.peerId].fingers['0'].current === undefined) {
    if (!peerTable[options.peerId].notify) {
      return
    }
    updateFinger(options.peerId, '0')
  }

  // notify if to other peers if this new Peer is a best finger for them
  function notify () {
    const newId = options.peerId
    const peerIds = Object.keys(peerTable)
    // check for all the peers
    // if same id skip
    // if notify === false skip
    // check the first finger that matches the criteria for ideal or next to ideal
    peerIds.forEach((peerId) => {
      if (newId === peerId) {
        return // skip ourselves
      }
      if (!peerTable[peerId].notify) {
        return // skip if it doesn't want to be notified of available peers
      }

      // if it had none, notify to get a successor
      if (peerTable[peerId].fingers['0'].current === undefined) {
        peerTable[peerId].fingers['0'].current = newId
        peerTable[peerId].socket.emit('we-update-finger', {
          row: '0',
          id: newId
        })
        return
      }

      const rows = Object.keys(peerTable[peerId].fingers)
      // find the first row that could use this finger
      rows.some((row) => {
        const finger = peerTable[peerId].fingers[row]
        const bestCandidate = fingerBestFit(
            new Id(peerId),
            new Id(finger.ideal),
            new Id(finger.current),
            new Id(newId))
        if (bestCandidate) {
          peerTable[peerId].fingers[row].current = newId
          peerTable[peerId].socket.emit('we-update-finger', {
            row: row,
            id: newId
          })
          return true
        }
      })
    })
  }
}
function join ( options ) { if ( options . peerId . length !== 12 ) { return this . emit ( 'we-ready' , new Error ( 'Unvalid peerId length, must be 48 bits, received: ' + options . peerId ) . toString ( ) ) } if ( peerTable [ options . peerId ] ) { return this . emit ( 'we-ready' , new Error ( 'peerId already exists' ) . toString ( ) ) } peerTable [ options . peerId ] = { socket : this , notify : typeof options . notify === 'boolean' && options . notify === true , fingers : options . fingers || { '0' : { ideal : idealFinger ( new Id ( options . peerId ) , '0' ) . toHex ( ) , current : undefined } } } log ( 'peer joined: ' + options . peerId ) this . emit ( 'we-ready' ) if ( Object . keys ( peerTable ) . length === 1 ) { return log ( 'This was the first peer join, do nothing' ) } notify ( ) if ( peerTable [ options . peerId ] . fingers [ '0' ] . current === undefined ) { if ( ! peerTable [ options . peerId ] . notify ) { return } updateFinger ( options . peerId , '0' ) } // notify if to other peers if this new Peer is a best finger for them function notify ( ) { const newId = options . peerId const peerIds = Object . keys ( peerTable ) // check for all the peers // if same id skip // if notify === false skip // check the first finger that matches the criteria for ideal or next to ideal peerIds . forEach ( ( peerId ) => { if ( newId === peerId ) { return // skip ourselves } if ( ! peerTable [ peerId ] . notify ) { return // skip if it doesn't want to be notified of available peers } // if it had none, notify to get a successor if ( peerTable [ peerId ] . fingers [ '0' ] . current === undefined ) { peerTable [ peerId ] . fingers [ '0' ] . current = newId peerTable [ peerId ] . socket . emit ( 'we-update-finger' , { row : '0' , id : newId } ) return } const rows = Object . keys ( peerTable [ peerId ] . fingers ) // find the first row that could use this finger rows . some ( ( row ) => { const finger = peerTable [ peerId ] . fingers [ row ] const bestCandidate = fingerBestFit ( new Id ( peerId ) , new Id ( finger . ideal ) , new Id ( finger . current ) , new Id ( newId ) ) if ( bestCandidate ) { peerTable [ peerId ] . fingers [ row ] . current = newId peerTable [ peerId ] . socket . emit ( 'we-update-finger' , { row : row , id : newId } ) return true } } ) } ) } }
join this signaling server network
join this signaling server network