<commit_before>var events = require('events');		
var dht = require('bittorrent-dht');		
var tracker = require('bittorrent-tracker');		
		
var DEFAULT_PORT = 6881;		

module.exports = function(opts) {		
	var torrent;	
		
	var port = opts.port || DEFAULT_PORT;		
		
	var discovery = new events.EventEmitter();		
		
	discovery.dht = null;	
	discovery.tracker = null;
	discovery.niceSeeder = false;
		
	var onpeer = function(addr) {		
		discovery.emit('peer', addr);
	};		
	
	var onforcepeer = function(addr) {		
		discovery.emit('forcePeer', addr);
	};
		
	var createDHT = function(infoHash) {		
		if (opts.dht === false) return;		
		
		var table = dht();		
		
		table.on('peer', function(addr) {
			if (discovery.amSeeder) {
				onforcepeer(addr);
			} else {
				onpeer(addr);
			}
		});		
		table.on('ready', function() {		
			table.lookup(infoHash);		
		});		
		table.listen();		
		
		return table;		
	};		
		
	var createTracker = function(torrent) {		
		if (opts.trackers) {		
			torrent = Object.create(torrent);		
			var trackers = (opts.tracker !== false) && torrent.announce ? torrent.announce : [];		
			torrent.announce = trackers.concat(opts.trackers);		
		} else if (opts.tracker === false) {		
			return;		
		}		
		
		if (!torrent.announce || !torrent.announce.length) return;		
		
		var tr = new tracker.Client(new Buffer(opts.id), port, torrent);		
		
		tr.on('peer', function(addr) {
			if (discovery.amSeeder) {
				onforcepeer(addr);
			} else {
				onpeer(addr);
			}
		});		
		tr.on('error', function() { /* noop */ });		
		
		if (discovery.amSeeder) {
			tr.complete();
		} else {
			tr.start();
		}
		return tr;		
	};
		
	discovery.setTorrent = function(t) {		
		torrent = t;		
		
		if (discovery.tracker) {		
			// If we have tracker then it had probably been created before we got infoDictionary.		
			// So client do not know torrent length and can not report right information about uploads		
			discovery.tracker.torrentLength = torrent.length;		
		} else {		
			process.nextTick(function() {		
				if (!discovery.dht) discovery.dht = createDHT(torrent.infoHash);		
				if (!discovery.tracker) discovery.tracker = createTracker(torrent);		
			});		
		}		
	};

	discovery.updatePort = function(p) {
		if (port === p) return;
		port = p;
		if (discovery.tracker) discovery.tracker.stop();
		if (torrent) {
			if (discovery.dht) discovery.dht.announce(torrent.infoHash, port);
			discovery.tracker = createTracker(torrent);
		}
	};		
	
	discovery.complete = function () {
		discovery.amSeeder = true;
		if (discovery.tracker) {
			if (!discovery.niceSeeder) {
				discovery.niceSeeder = setInterval(function() {
					discovery.complete()
				},60000);
			}
			discovery.tracker.complete();
		}
	};
	
	discovery.stop = function() {
		if (discovery.niceSeeder) {
			clearInterval(discovery.niceSeeder);
			discovery.niceSeeder = false;
		}
		if (discovery.tracker) discovery.tracker.stop();		
		if (discovery.dht) discovery.dht.destroy();		
	};		
		
	discovery.restart = function() {
		if (!torrent) return;
		if (discovery.tracker) discovery.tracker.stop();
		if (discovery.dht) discovery.dht.destroy();
		discovery.tracker = createTracker(torrent);
		discovery.dht = createDHT(torrent.infoHash);
	};
		
	return discovery;		
};<commit_msg>Clean Up Peer Discovery<commit_after>var events = require('events');		
var dht = require('bittorrent-dht');		
var tracker = require('bittorrent-tracker');		
		
var DEFAULT_PORT = 6881;		

module.exports = function(opts) {		
	var torrent;	
		
	var port = opts.port || DEFAULT_PORT;		
		
	var discovery = new events.EventEmitter();		
		
	discovery.dht = null;	
	discovery.tracker = null;
		
	var onpeer = function(addr) {		
		discovery.emit('peer', addr);
	};		
	
	var onforcepeer = function(addr) {		
		discovery.emit('forcePeer', addr);
	};
		
	var createDHT = function(infoHash) {		
		if (opts.dht === false) return;		
		
		var table = dht();		
		
		table.on('peer', function(addr) {
			if (discovery.amSeeder) {
				onforcepeer(addr);
			} else {
				onpeer(addr);
			}
		});		
		table.on('ready', function() {		
			table.lookup(infoHash);		
		});		
		table.listen();		
		
		return table;		
	};		
		
	var createTracker = function(torrent) {		
		if (opts.trackers) {		
			torrent = Object.create(torrent);		
			var trackers = (opts.tracker !== false) && torrent.announce ? torrent.announce : [];		
			torrent.announce = trackers.concat(opts.trackers);		
		} else if (opts.tracker === false) {		
			return;		
		}		
		
		if (!torrent.announce || !torrent.announce.length) return;		
		
		var tr = new tracker.Client(new Buffer(opts.id), port, torrent);		
		
		tr.on('peer', function(addr) {
			if (discovery.amSeeder) {
				onforcepeer(addr);
			} else {
				onpeer(addr);
			}
		});		
		tr.on('error', function() { /* noop */ });		
		
		if (discovery.amSeeder) {
			tr.complete();
		} else {
			tr.start();
		}
		return tr;		
	};
		
	discovery.setTorrent = function(t) {		
		torrent = t;		
		
		if (discovery.tracker) {		
			// If we have tracker then it had probably been created before we got infoDictionary.		
			// So client do not know torrent length and can not report right information about uploads		
			discovery.tracker.torrentLength = torrent.length;		
		} else {		
			process.nextTick(function() {		
				if (!discovery.dht) discovery.dht = createDHT(torrent.infoHash);		
				if (!discovery.tracker) discovery.tracker = createTracker(torrent);		
			});		
		}		
	};

	discovery.updatePort = function(p) {
		if (port === p) return;
		port = p;
		if (discovery.tracker) discovery.tracker.stop();
		if (torrent) {
			if (discovery.dht) discovery.dht.announce(torrent.infoHash, port);
			discovery.tracker = createTracker(torrent);
		}
	};		
	
	discovery.complete = function () {
		discovery.amSeeder = true;
		if (discovery.tracker) {
			discovery.tracker.complete();
		}
	};
	
	discovery.stop = function() {
		if (discovery.tracker) discovery.tracker.stop();		
		if (discovery.dht) discovery.dht.destroy();		
	};		
		
	discovery.restart = function() {
		if (!torrent) return;
		if (discovery.tracker) discovery.tracker.stop();
		if (discovery.dht) discovery.dht.destroy();
		discovery.tracker = createTracker(torrent);
		discovery.dht = createDHT(torrent.infoHash);
	};
		
	return discovery;		
};
{'fraction_non_alphanumeric': 0.14465910905316942, 'fraction_numerical': 0.002075682580233115, 'mean_word_length': 3.0205391527599486, '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': '2058993', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2122, 'n_words': 430}