<commit_before>var lpstream = require('length-prefixed-stream')
var duplexify = require('duplexify')
var ids = require('numeric-id-map')
var crypto = require('crypto')
var events = require('events')
var bitfield = require('bitfield')
var util = require('util')
var messages = require('./messages')

var MAX_BITFIELD = 5 * 1024 * 1024
var MAX_MESSAGE = 5 * 1024 * 1024

var ENCODINGS = [
  messages.Join,
  messages.Leave,
  messages.Have,
  messages.Choke,
  messages.Unchoke,
  messages.Request,
  messages.Response
]

module.exports = Protocol

function Channel (stream, link) {
  events.EventEmitter.call(this)

  this.stream = stream
  this.left = false
  this.link = link
  this.local = 0
  this.remote = 0
  this.remoteJoined = false

  this.remoteChoking = true
  this.remoteBitfield = bitfield(1, {grow: MAX_BITFIELD})
  this.amChoking = true
  this.amRequesting = bitfield(1, {grow: Infinity})
  this.inflight = 0
}

util.inherits(Channel, events.EventEmitter)

Channel.prototype.leave = function (err) {
  if (this.left) return
  if (err) this.emit('warn', err)
  this.stream._push(1, {channel: this.local})
  this.onleave()
}

Channel.prototype.bitfield = function (bitfield) {
  this.stream._push(2, {
    channel: this.local,
    bitfield: toBuffer(bitfield)
  })
}

Channel.prototype.have = function (blocks, bitfield) {
  if (typeof blocks === 'number') blocks = [blocks]
  this.stream._push(2, {
    channel: this.local,
    blocks: blocks,
    bitfield: toBuffer(bitfield)
  })
}

Channel.prototype.choke = function () {
  this.stream._push(3, {channel: this.local})
}

Channel.prototype.unchoke = function () {
  this.stream._push(4, {channel: this.local})
}

Channel.prototype.request = function (block) {
  this.amRequesting.set(block, true)
  this.inflight++
  this.stream.inflight++
  this.stream._push(5, {channel: this.local, block: block})
}

Channel.prototype.response = function (block, data, proof) {
  this.stream._push(6, {
    channel: this.local,
    block: block,
    data: data,
    proof: proof
  })<commit_msg>fix amChoking<commit_after>var lpstream = require('length-prefixed-stream')
var duplexify = require('duplexify')
var ids = require('numeric-id-map')
var crypto = require('crypto')
var events = require('events')
var bitfield = require('bitfield')
var util = require('util')
var messages = require('./messages')

var MAX_BITFIELD = 5 * 1024 * 1024
var MAX_MESSAGE = 5 * 1024 * 1024

var ENCODINGS = [
  messages.Join,
  messages.Leave,
  messages.Have,
  messages.Choke,
  messages.Unchoke,
  messages.Request,
  messages.Response
]

module.exports = Protocol

function Channel (stream, link) {
  events.EventEmitter.call(this)

  this.stream = stream
  this.left = false
  this.link = link
  this.local = 0
  this.remote = 0
  this.remoteJoined = false

  this.remoteChoking = true
  this.remoteBitfield = bitfield(1, {grow: MAX_BITFIELD})
  this.amChoking = true
  this.amRequesting = bitfield(1, {grow: Infinity})
  this.inflight = 0
}

util.inherits(Channel, events.EventEmitter)

Channel.prototype.leave = function (err) {
  if (this.left) return
  if (err) this.emit('warn', err)
  this.stream._push(1, {channel: this.local})
  this.onleave()
}

Channel.prototype.bitfield = function (bitfield) {
  this.stream._push(2, {
    channel: this.local,
    bitfield: toBuffer(bitfield)
  })
}

Channel.prototype.have = function (blocks, bitfield) {
  if (typeof blocks === 'number') blocks = [blocks]
  this.stream._push(2, {
    channel: this.local,
    blocks: blocks,
    bitfield: toBuffer(bitfield)
  })
}

Channel.prototype.choke = function () {
  this.amChoking = true
  this.stream._push(3, {channel: this.local})
}

Channel.prototype.unchoke = function () {
  this.amChoking = false
  this.stream._push(4, {channel: this.local})
}

Channel.prototype.request = function (block) {
  this.amRequesting.set(block, true)
  this.inflight++
  this.stream.inflight++
  this.stream._push(5, {channel: this.local, block: block})
}

Channel.prototype.response = function (block, data, proof) {
  this.stream._push(6, {
    channel: this.local,
    block: block,
    data: data,
    proof: proof
  })
{'fraction_non_alphanumeric': 0.1405457618932625, 'fraction_numerical': 0.014489253803429124, 'mean_word_length': 4.002415458937198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4690297', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1399, 'n_words': 311}