<commit_before>// Copyright 2014 <NAME> <<EMAIL>>.
// All rights reserved.
// Use of this source code is governed by a Apache
// license that can be found in the LICENSE file.

var BaseObject = require('base_object');
var BufferedChannel = require('rtc-bufferedchannel');
var Peer = require('./peer')
var Settings = require('./settings')
var _ = require('underscore')
var log = require('./log');


class Swarm extends BaseObject {
  initialize() {
    this.peers = []
    this.satisfyCandidate = undefined
    this.chunksSent = 0
    this.chokedClients = 0
    this.avgSegmentSize = 0
  }

  size() {
    return this.peers.length
  }

  addPeer(id, dataChannel) {
    log.info("join: " + id)
    var bufferedChannel = BufferedChannel(dataChannel, {calcCharSize: false})
    var peer = new Peer({ident: id, dataChannel: bufferedChannel, swarm: this})
    this.peers.push(peer)
    this.trigger('swarm:sizeupdate', {swarmSize: this.size()})
  }

  removePeer(id) {
    var peer = this.findPeer(id)
    this.peers = _.without(this.peers, peer)
    log.info("quit: " + id + "(remains: " + this.size() + ")")
    this.trigger('swarm:sizeupdate', {swarmSize: this.size()})
  }

  findPeer(id) {
    return _.find(this.peers, function (peer) {
      return !!(peer.ident === id)
    }, this)
  }

  get partners() {
    var activePeers = _.filter(this.peers, function (p) { return p.active })
    var orderedPeers = _.sortBy(activePeers, function (p) { return p.score }).reverse()
    if (this.peers.length > Settings.maxPartners) {
      return orderedPeers.slice(0, Settings.maxPartners)
    } else {
      return orderedPeers
    }
  }

  sendTo(recipients, command, resource, content='') {
    /* recipients: all, partners or peer ident
    command: interested, contain, request, satisfy */
    log.debug("sending _" + command + "_ to " + recipients)
    if (recipients === 'partners') {
      _.each(this.partners, function(peer) { peer.send(command, resource, content) }, this)
    } else if (recipients === 'all') {
      _.each(this.peers, function(peer) { peer.send(command, resource, content) }, this)
    } else {
      var peer = this.findPeer(recipients)
      peer.send(command, resource, content);
    }
  }

  sendInterested(resource, callbackSuccess, callbackFail) {
    this.externalCallbackFail = callbackFail
    this.externalCallbackSuccess = callbackSuccess
    this.interestedFailID = setTimeout(this.callbackFail.bind(this), this.getTimeoutFor('interested'))
    this.currentResource = resource
    this.sendTo('partners', 'interested', resource)
  }

  chokeReceived(resource) {
    if (this.currentResource === resource) {
      this.chokedClients += 1
      if (this.chokedClients === this.partners.length) {
        log.warn("all partners choked, getting from cdn")
        this.callbackFail()
      }
    }
  }

  addSatisfyCandidate(peerId, resource) {
    if (this.satisfyCandidate || this.currentResource !== resource) {
      log.debug("already have satisfyCandidate or resources mismatch (" +
          this.satisfyCandidate + " || " + this.currentResource + "!=" + resource + ")")
      return
    }
    if (this.interestedFailID) {
      this.clearInterestedFailInterval()
      this.requestFailID = setTimeout(this.callbackFail.bind(this), this.getTimeoutFor('request'))
    }
    this.satisfyCandidate = peerId
    this.sendRequest(peerId, resource)
  }

  getTimeoutFor(command) {
    var segmentSize = this.avgSegmentSize * 1000
    if (command === 'interested') {
      log.debug("timeout for interested: " + (segmentSize/4))
      return (segmentSize / 4)
    } else if (command === 'request') {
      /* this timeout is longer due to the sending of the entire chunk */
      log.debug("timeout for request: " + (segmentSize/2))
      return (segmentSize / 2)
    }
  }

  sendRequest(peerId, resource) {
    this.sendTo(peerId, 'request', resource)
  }

  resourceReceived(peer, resource, chunk) {
    if (this.satisfyCandidate === peer && this.currentResource === resource) {
      this.externalCallbackSuccess(chunk, "p2p")
      this.updatePartnerScore(Settings.points)
      this.rebootRoundVars()
      this.clearRequestFailInterval()
    }<commit_msg>swarm: remove logs<commit_after>// Copyright 2014 <NAME> <<EMAIL>>.
// All rights reserved.
// Use of this source code is governed by a Apache
// license that can be found in the LICENSE file.

var BaseObject = require('base_object');
var BufferedChannel = require('rtc-bufferedchannel');
var Peer = require('./peer')
var Settings = require('./settings')
var _ = require('underscore')
var log = require('./log');


class Swarm extends BaseObject {
  initialize() {
    this.peers = []
    this.satisfyCandidate = undefined
    this.chunksSent = 0
    this.chokedClients = 0
    this.avgSegmentSize = 0
  }

  size() {
    return this.peers.length
  }

  addPeer(id, dataChannel) {
    log.info("join: " + id)
    var bufferedChannel = BufferedChannel(dataChannel, {calcCharSize: false})
    var peer = new Peer({ident: id, dataChannel: bufferedChannel, swarm: this})
    this.peers.push(peer)
    this.trigger('swarm:sizeupdate', {swarmSize: this.size()})
  }

  removePeer(id) {
    var peer = this.findPeer(id)
    this.peers = _.without(this.peers, peer)
    log.info("quit: " + id + "(remains: " + this.size() + ")")
    this.trigger('swarm:sizeupdate', {swarmSize: this.size()})
  }

  findPeer(id) {
    return _.find(this.peers, function (peer) {
      return !!(peer.ident === id)
    }, this)
  }

  get partners() {
    var activePeers = _.filter(this.peers, function (p) { return p.active })
    var orderedPeers = _.sortBy(activePeers, function (p) { return p.score }).reverse()
    if (this.peers.length > Settings.maxPartners) {
      return orderedPeers.slice(0, Settings.maxPartners)
    } else {
      return orderedPeers
    }
  }

  sendTo(recipients, command, resource, content='') {
    /* recipients: all, partners or peer ident
    command: interested, contain, request, satisfy */
    log.debug("sending _" + command + "_ to " + recipients)
    if (recipients === 'partners') {
      _.each(this.partners, function(peer) { peer.send(command, resource, content) }, this)
    } else if (recipients === 'all') {
      _.each(this.peers, function(peer) { peer.send(command, resource, content) }, this)
    } else {
      var peer = this.findPeer(recipients)
      peer.send(command, resource, content);
    }
  }

  sendInterested(resource, callbackSuccess, callbackFail) {
    this.externalCallbackFail = callbackFail
    this.externalCallbackSuccess = callbackSuccess
    this.interestedFailID = setTimeout(this.callbackFail.bind(this), this.getTimeoutFor('interested'))
    this.currentResource = resource
    this.sendTo('partners', 'interested', resource)
  }

  chokeReceived(resource) {
    if (this.currentResource === resource) {
      this.chokedClients += 1
      if (this.chokedClients === this.partners.length) {
        log.warn("all partners choked, getting from cdn")
        this.callbackFail()
      }
    }
  }

  addSatisfyCandidate(peerId, resource) {
    if (this.satisfyCandidate || this.currentResource !== resource) {
      log.debug("already have satisfyCandidate or resources mismatch (" +
          this.satisfyCandidate + " || " + this.currentResource + "!=" + resource + ")")
      return
    }
    if (this.interestedFailID) {
      this.clearInterestedFailInterval()
      this.requestFailID = setTimeout(this.callbackFail.bind(this), this.getTimeoutFor('request'))
    }
    this.satisfyCandidate = peerId
    this.sendRequest(peerId, resource)
  }

  getTimeoutFor(command) {
    var segmentSize = this.avgSegmentSize * 1000
    if (command === 'interested') {
      return (segmentSize / 4)
    } else if (command === 'request') {
      return (segmentSize / 2)
    }
  }

  sendRequest(peerId, resource) {
    this.sendTo(peerId, 'request', resource)
  }

  resourceReceived(peer, resource, chunk) {
    if (this.satisfyCandidate === peer && this.currentResource === resource) {
      this.externalCallbackSuccess(chunk, "p2p")
      this.updatePartnerScore(Settings.points)
      this.rebootRoundVars()
      this.clearRequestFailInterval()
    }
{'fraction_non_alphanumeric': 0.13677403611517813, 'fraction_numerical': 0.004148365056124939, 'mean_word_length': 3.3624268227780734, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2701081', 'n_tokens_mistral': 2583, 'n_tokens_neox': 2489, 'n_words': 562}