Skip to content

Commit

Permalink
Delegator Refactoring: More bugfixes.
Browse files Browse the repository at this point in the history
git-svn-id: svn://rakshasa.no/libtorrent/trunk/libtorrent@162 e378c898-3ddf-0310-93e7-cc216c733640
  • Loading branch information
rakshasa committed Nov 22, 2004
1 parent 406608c commit 3b7c6e8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/content/delegator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ Delegator::delegate(const BitField& bf, int affinity) {
bool_not(call_member(&DelegatorPiece::get_reservees_size))),
assign_ref(target, back_as_ptr()))));

if (target)
return target->create();

// else find a new chunk
target = new_chunk(bf);

if (target)
return target->create();

Expand All @@ -63,12 +69,6 @@ Delegator::delegate(const BitField& bf, int affinity) {

assign_ref(target, back_as_ptr()))));

if (target)
return target->create();

// else find a new chunk
target = new_chunk(bf);

if (target)
return target->create();

Expand Down
24 changes: 21 additions & 3 deletions src/peer/request_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ RequestList::downloading(const Piece& p) {
if (m_downloading)
throw internal_error("RequestList::downloading(...) bug, m_downloaing is already set");

ReserveeList::iterator itr = std::find_if(m_reservees.begin(), m_reservees.end(),
eq(ref(p), call_member(&DelegatorReservee::get_piece)));

ReserveeList::iterator itr =
std::find_if(m_reservees.begin(), m_reservees.end(),
eq(ref(p), call_member(&DelegatorReservee::get_piece)));

if (itr == m_reservees.end() || !m_delegator->downloading(**itr))
return false;

Expand Down Expand Up @@ -106,4 +107,21 @@ RequestList::cancel_range(ReserveeList::iterator end) {
}
}

unsigned int
RequestList::remove_invalid() {
unsigned int count = 0;
ReserveeList::iterator itr;

// Could be more efficient, but rarely do we find any.
while ((itr = std::find_if(m_reservees.begin(), m_reservees.end(),
bool_not(call_member(&DelegatorReservee::is_valid))))
!= m_reservees.end()) {
count++;
delete *itr;
m_reservees.erase(itr);
}

return count;
}

}
10 changes: 9 additions & 1 deletion src/peer/request_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ class RequestList {
bool is_wanted() { return m_reservees.front()->is_valid(); }

bool has_index(unsigned int i);
unsigned int remove_invalid();

unsigned int get_size() { return m_reservees.size(); }

const Piece& get_piece() { return m_piece; }
const Piece& get_queued_piece(unsigned int i) { return m_reservees[i]->get_piece(); }

Piece get_queued_piece(unsigned int i) {
// TODO: Make this unnessesary?
if (m_reservees[i]->is_valid())
return m_reservees[i]->get_piece();
else
return Piece();
}

void set_delegator(Delegator* d) { m_delegator = d; }
void set_bitfield(const BitField* b) { m_bitfield = b; }
Expand Down
29 changes: 16 additions & 13 deletions src/peer_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ void PeerConnection::read() {
m_down.length = piece.get_length();
m_down.state = READ_SKIP_PIECE;

remove_service(SERVICE_STALL);
caughtExceptions.push_back("Receiving piece we don't want from " + m_peer.dns());
}

Expand Down Expand Up @@ -234,7 +233,8 @@ void PeerConnection::read() {
m_down.pos = 0;

m_requests.skip();
remove_service(SERVICE_STALL);

goto evil_goto_read;
}

previous = m_down.pos;
Expand Down Expand Up @@ -264,32 +264,32 @@ void PeerConnection::read() {
goto evil_goto_read;

case READ_SKIP_PIECE:
// TODO: Temporary, kill as soon as possible.
if (in_service(SERVICE_STALL))
throw internal_error("READ_SKIP_PIECE state but peer is in SERVICE_STALL");
if (m_down.pos != 0)
throw internal_error("READ_SKIP_PIECE m_down.pos != 0");

previous = m_down.pos;
s = readBuf(m_down.buf,
std::min(m_down.length, BUFFER_SIZE),
m_down.pos);

m_throttle.down().add(m_down.pos - previous);
m_throttle.down().add(m_down.pos);

if (!s)
return;

m_down.length -= m_down.pos;
m_down.pos = 0;

if (m_down.length == 0) {
// Done with this piece.
m_down.state = IDLE;

remove_service(SERVICE_STALL);

if (m_requests.get_size())
insert_service(Timer::cache() + m_download->settings().stallTimeout, SERVICE_STALL);
}

goto evil_goto_read;
if (s)
goto evil_goto_read;
else
return;

default:
throw internal_error("peer_connectino::read() called on object in wrong state");
Expand Down Expand Up @@ -663,7 +663,6 @@ void PeerConnection::sendHave(int index) {
// skip it.
if (m_requests.is_downloading() &&
m_requests.get_piece().get_index() == index) {
throw internal_error("PeerConnection::sendHave(...) got RequestList.is_downloading() with this index");

if (m_down.state != READ_PIECE)
throw internal_error("PeerConnection::sendHave(...) got RequestList.is_downloading() but state is not READ_PIECE");
Expand All @@ -672,11 +671,15 @@ void PeerConnection::sendHave(int index) {
m_down.length = m_requests.get_piece().get_length() - m_down.pos;
m_down.pos = 0;

// TODO: Do some proper canceling here.
m_requests.skip();

m_down.data = Storage::Chunk();
}

if (m_requests.remove_invalid() &&
m_requests.get_size() == 0)
remove_service(SERVICE_STALL);

if (m_requests.has_index(index))
throw internal_error("PeerConnection::sendHave(...) found a request with the same index");

Expand Down

0 comments on commit 3b7c6e8

Please sign in to comment.