Skip to content

Commit

Permalink
write up/down data bytes into user/peer record
Browse files Browse the repository at this point in the history
  • Loading branch information
taobataoma committed Apr 18, 2017
1 parent ddf35ff commit f32cacb
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions modules/announce/server/controllers/announces.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const PEERSTATE_LEECHER = 'leecher';

const PEER_COMPACT_SIZE = 6;
const ANNOUNCE_INTERVAL = 60;
const ANNOUNCE_GHOST = 2;

const PARAMS_INTEGER = [
'port', 'uploaded', 'downloaded', 'left', 'compact', 'numwant'
Expand Down Expand Up @@ -244,12 +245,15 @@ exports.announce = function (req, res) {
} else {
req.torrent = t;

//find my peers
/*
find myself peers
if the peer is ghost, deleted it
*/
if (req.torrent._peers.length > 0) {
req.torrent._peers.forEach(function (p) {
if (p.user.str === req.passkeyuser._id.str) {
var diff = moment(Date.now()).diff(moment(p.last_announce_at), 'seconds');
if (diff > ANNOUNCE_INTERVAL * 2) { //ghost peer, delete
if (diff > ANNOUNCE_INTERVAL * ANNOUNCE_GHOST) {
console.log('---------------DELETE_GHOST---------------');

req.torrent._peers.pull(p);
Expand Down Expand Up @@ -361,15 +365,17 @@ exports.announce = function (req, res) {
console.log('---------------WRITE_UP_DOWN_DATA----------------');

var udr = getUDRatio();
var u = Math.round(query.uploaded * udr.ur);
var d = Math.round(query.downloaded * udr.dr);
var curru = query.uploaded - req.currentPeer.peer_uploaded;
var currd = query.downloaded - req.currentPeer.peer_downloaded;
var u = Math.round(curru * udr.ur);
var d = Math.round(currd * udr.dr);

if (event(query.event) !== EVENT_STOPPED) {
if (req.currentPeer === undefined) {
createCurrentPeer();
}
req.currentPeer.peer_uploaded += query.uploaded;
req.currentPeer.peer_downloaded += query.downloaded;
req.currentPeer.peer_uploaded = query.uploaded;
req.currentPeer.peer_downloaded = query.downloaded;
req.currentPeer.last_announce_at = Date.now();
}

Expand Down Expand Up @@ -424,10 +430,6 @@ exports.announce = function (req, res) {
var len = writePeers(peerBuffer, want, req.torrent._peers);
peerBuffer = peerBuffer.slice(0, len);

//req.torrent.torrent_seeds = 77;
//req.torrent.torrent_leechers = 88;
//req.torrent.torrent_finished = 99;

var resp = 'd8:intervali' + ANNOUNCE_INTERVAL + 'e8:completei' + req.torrent.torrent_seeds + 'e10:incompletei' + req.torrent.torrent_leechers + 'e10:downloadedi' + req.torrent.torrent_finished + 'e5:peers' + len + ':';
console.log(resp);

Expand Down Expand Up @@ -592,7 +594,7 @@ exports.announce = function (req, res) {
udr.ur = 3;
udr.dr = 1;
break;
default: //U1D1
default: /* U1D1 */
udr.ur = 1;
udr.dr = 1;
}
Expand Down Expand Up @@ -662,13 +664,32 @@ exports.announce = function (req, res) {
}
}

/**
* binaryToHex
* @param str
*/
function binaryToHex(str) {
if (typeof str !== 'string') {
str = String(str);
}
return Buffer.from(str, 'binary').toString('hex');
}

/**
* hexToBinary
* @param str
*/
function hexToBinary(str) {
if (typeof str !== 'string') {
str = String(str);
}
return Buffer.from(str, 'hex').toString('binary');
}

/**
* querystringParse
* @param q
*/
function querystringParse(q) {
return querystring.parse(q, null, null, {decodeURIComponent: unescape});
}
Expand Down

0 comments on commit f32cacb

Please sign in to comment.