2006-03-21 14:12:51 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - a simple utility for downloading files faster
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#ifndef _D_PEER_H_
|
|
|
|
#define _D_PEER_H_
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "BitfieldMan.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#define PEER_ID_LENGTH 20
|
2006-05-24 16:08:00 +00:00
|
|
|
#define DEFAULT_LATENCY 1500
|
2006-03-21 14:12:51 +00:00
|
|
|
|
|
|
|
class Peer {
|
|
|
|
public:
|
|
|
|
int entryId;
|
|
|
|
string ipaddr;
|
|
|
|
int port;
|
2006-04-28 15:55:11 +00:00
|
|
|
bool amChoking;
|
2006-03-21 14:12:51 +00:00
|
|
|
bool amInterested;
|
|
|
|
bool peerChoking;
|
|
|
|
bool peerInterested;
|
|
|
|
int tryCount;
|
|
|
|
int error;
|
|
|
|
int cuid;
|
2006-04-28 15:55:11 +00:00
|
|
|
bool chokingRequired;
|
|
|
|
bool optUnchoking;
|
2006-03-21 14:12:51 +00:00
|
|
|
private:
|
|
|
|
char peerId[PEER_ID_LENGTH];
|
|
|
|
BitfieldMan* bitfield;
|
2006-05-18 17:08:29 +00:00
|
|
|
bool fastExtensionEnabled;
|
|
|
|
// allowed fast indexes that peer has sent by Allowed Fast message
|
|
|
|
Integers fastSet;
|
2006-03-21 14:12:51 +00:00
|
|
|
long long int peerUpload;
|
|
|
|
long long int peerDownload;
|
|
|
|
int pieceLength;
|
|
|
|
long long int totalLength;
|
2006-04-28 15:55:11 +00:00
|
|
|
int deltaUpload;
|
|
|
|
int deltaDownload;
|
2006-05-21 16:19:17 +00:00
|
|
|
int latency;
|
2006-03-21 14:12:51 +00:00
|
|
|
public:
|
|
|
|
Peer(string ipaddr, int port, int pieceLength, long long int totalLength):
|
|
|
|
entryId(0), ipaddr(ipaddr), port(port),
|
2006-04-28 15:55:11 +00:00
|
|
|
amChoking(true), amInterested(false),
|
2006-03-21 14:12:51 +00:00
|
|
|
peerChoking(true), peerInterested(false),
|
|
|
|
tryCount(0), error(0), cuid(0),
|
2006-04-28 15:55:11 +00:00
|
|
|
chokingRequired(true), optUnchoking(false),
|
2006-03-31 16:15:23 +00:00
|
|
|
bitfield(NULL),
|
2006-05-18 17:08:29 +00:00
|
|
|
fastExtensionEnabled(false),
|
2006-03-21 14:12:51 +00:00
|
|
|
peerUpload(0), peerDownload(0),
|
2006-04-28 15:55:11 +00:00
|
|
|
pieceLength(pieceLength), totalLength(totalLength),
|
2006-05-21 16:19:17 +00:00
|
|
|
deltaUpload(0), deltaDownload(0),
|
|
|
|
latency(DEFAULT_LATENCY) {
|
2006-03-21 14:12:51 +00:00
|
|
|
this->bitfield = new BitfieldMan(pieceLength, totalLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Peer() {
|
|
|
|
if(bitfield != NULL) {
|
2006-03-31 16:15:23 +00:00
|
|
|
delete bitfield;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-28 15:55:11 +00:00
|
|
|
void resetStatus();
|
|
|
|
|
|
|
|
void addDeltaUpload(int length) {
|
|
|
|
this->deltaUpload += length;
|
|
|
|
}
|
|
|
|
void resetDeltaUpload() { this->deltaUpload = 0; }
|
|
|
|
int getDeltaUpload() const { return this->deltaUpload; }
|
|
|
|
|
|
|
|
void addDeltaDownload(int length) {
|
|
|
|
this->deltaDownload += length;
|
|
|
|
}
|
|
|
|
void resetDeltaDownload() { this->deltaDownload = 0; }
|
|
|
|
int getDeltaDownload() const { return this->deltaDownload; }
|
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
void setPeerId(const char* peerId) {
|
|
|
|
memcpy(this->peerId, peerId, PEER_ID_LENGTH);
|
|
|
|
}
|
|
|
|
const char* getPeerId() const { return this->peerId; }
|
|
|
|
|
|
|
|
void setBitfield(const unsigned char* bitfield, int bitfieldLength) {
|
|
|
|
this->bitfield->setBitfield(bitfield, bitfieldLength);
|
|
|
|
}
|
|
|
|
const unsigned char* getBitfield() const { return bitfield->getBitfield(); }
|
|
|
|
int getBitfieldLength() const { return bitfield->getBitfieldLength(); }
|
2006-05-18 17:08:29 +00:00
|
|
|
void setAllBitfield();
|
2006-03-21 14:12:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* operation = 1: set index-th bit 1
|
|
|
|
* operation = 0: set index-th bit 0
|
|
|
|
*/
|
|
|
|
void updateBitfield(int index, int operation);
|
2006-05-18 17:08:29 +00:00
|
|
|
|
2006-04-28 15:55:11 +00:00
|
|
|
void addPeerUpload(int size) {
|
|
|
|
peerUpload += size;
|
|
|
|
addDeltaUpload(size);
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
void setPeerUpload(long long int size) { peerUpload = size; }
|
|
|
|
long long int getPeerUpload() const { return peerUpload; }
|
|
|
|
|
2006-04-28 15:55:11 +00:00
|
|
|
void addPeerDownload(int size) {
|
|
|
|
peerDownload += size;
|
|
|
|
addDeltaDownload(size);
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
void setPeerDownload(long long int size) { peerDownload = size; }
|
|
|
|
long long int getPeerDownload() const { return peerDownload; }
|
|
|
|
|
2006-05-18 17:08:29 +00:00
|
|
|
void setFastExtensionEnabled(bool enabled) {
|
|
|
|
fastExtensionEnabled = enabled;
|
|
|
|
}
|
|
|
|
bool isFastExtensionEnabled() const { return fastExtensionEnabled; }
|
|
|
|
|
|
|
|
void addFastSetIndex(int index);
|
2006-05-19 15:49:32 +00:00
|
|
|
const Integers& getFastSet() const { return fastSet; }
|
2006-05-18 17:08:29 +00:00
|
|
|
bool isInFastSet(int index) const;
|
|
|
|
int countFastSet() const { return fastSet.size(); }
|
|
|
|
|
2006-04-28 15:55:11 +00:00
|
|
|
bool shouldBeChoking() const;
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2006-03-23 10:47:25 +00:00
|
|
|
bool hasPiece(int index) const;
|
|
|
|
|
2006-03-31 13:58:22 +00:00
|
|
|
bool isSeeder() const;
|
|
|
|
|
2006-05-21 16:19:17 +00:00
|
|
|
void updateLatency(int latency);
|
|
|
|
int getLatency() const { return latency; }
|
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
static Peer* nullPeer;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _D_PEER_H_
|