aria2/src/Peer.h

106 lines
2.9 KiB
C
Raw Normal View History

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
class Peer {
public:
int entryId;
string ipaddr;
int port;
bool amChocking;
bool amInterested;
bool peerChoking;
bool peerInterested;
int tryCount;
int error;
int cuid;
private:
char peerId[PEER_ID_LENGTH];
BitfieldMan* bitfield;
long long int peerUpload;
long long int peerDownload;
int pieceLength;
long long int totalLength;
public:
Peer(string ipaddr, int port, int pieceLength, long long int totalLength):
entryId(0), ipaddr(ipaddr), port(port),
amChocking(true), amInterested(false),
peerChoking(true), peerInterested(false),
tryCount(0), error(0), cuid(0),
bitfield(NULL),
2006-03-21 14:12:51 +00:00
peerUpload(0), peerDownload(0),
pieceLength(pieceLength), totalLength(totalLength) {
this->bitfield = new BitfieldMan(pieceLength, totalLength);
}
~Peer() {
if(bitfield != NULL) {
delete bitfield;
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(); }
/**
* operation = 1: set index-th bit 1
* operation = 0: set index-th bit 0
*/
void updateBitfield(int index, int operation);
void addPeerUpload(int size) { peerUpload += size; }
void setPeerUpload(long long int size) { peerUpload = size; }
long long int getPeerUpload() const { return peerUpload; }
void addPeerDownload(int size) { peerDownload += size; }
void setPeerDownload(long long int size) { peerDownload = size; }
long long int getPeerDownload() const { return peerDownload; }
bool shouldChoke() const;
bool hasPiece(int index) const;
bool isSeeder() const;
2006-03-21 14:12:51 +00:00
static Peer* nullPeer;
};
#endif // _D_PEER_H_