1 /* Copyright (c) 2012-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "s4u-peer.hpp"
11 #include "s4u-tracker.hpp"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_bt_peer, "Messages specific for the peers");
16 * User parameters for transferred file data. For the test, the default values are :
17 * File size: 10 pieces * 5 blocks/piece * 16384 bytes/block = 819200 bytes
19 constexpr unsigned long FILE_PIECES = 10UL;
20 constexpr unsigned long PIECES_BLOCKS = 5UL;
21 constexpr int BLOCK_SIZE = 16384;
23 /** Number of blocks asked by each request */
24 constexpr unsigned long BLOCKS_REQUESTED = 2UL;
26 constexpr double SLEEP_DURATION = 1.0;
27 #define BITS_TO_BYTES(x) (((x) / 8 + (x) % 8) ? 1 : 0)
30 * Sizes based on report by A. Legout et al, Understanding BitTorrent: An Experimental Perspective
31 * http://hal.inria.fr/inria-00000156/en
33 constexpr unsigned message_size(MessageType type)
35 constexpr std::array<unsigned, 10> sizes{{/* HANDSHAKE */ 68,
39 /* NOTINTERESTED */ 5,
45 return sizes[static_cast<int>(type)];
48 constexpr const char* message_name(MessageType type)
50 constexpr std::array<const char*, 10> names{{"HANDSHAKE", "CHOKE", "UNCHOKE", "INTERESTED", "NOTINTERESTED", "HAVE",
51 "BITFIELD", "REQUEST", "PIECE", "CANCEL"}};
52 return names[static_cast<int>(type)];
55 Peer::Peer(std::vector<std::string> args)
58 xbt_assert(args.size() == 3 || args.size() == 4, "Wrong number of arguments");
60 id = std::stoi(args[1]);
61 mailbox_ = simgrid::s4u::Mailbox::by_name(std::to_string(id));
62 } catch (const std::invalid_argument&) {
63 throw std::invalid_argument("Invalid ID:" + args[1]);
68 deadline = std::stod(args[2]);
69 } catch (const std::invalid_argument&) {
70 throw std::invalid_argument("Invalid deadline:" + args[2]);
72 xbt_assert(deadline > 0, "Wrong deadline supplied");
74 if (args.size() == 4 && args[3] == "1") {
75 bitfield_ = (1U << FILE_PIECES) - 1U;
76 bitfield_blocks = (1ULL << (FILE_PIECES * PIECES_BLOCKS)) - 1ULL;
78 pieces_count.resize(FILE_PIECES);
80 XBT_INFO("Hi, I'm joining the network with id %d", id);
83 /** Peer main function */
84 void Peer::operator()()
86 // Getting peer data from the tracker.
87 if (getPeersFromTracker()) {
88 XBT_DEBUG("Got %zu peers from the tracker. Current status is: %s", connected_peers.size(), getStatus().c_str());
89 begin_receive_time = simgrid::s4u::Engine::get_clock();
90 mailbox_->set_receiver(simgrid::s4u::Actor::self());
92 sendHandshakeToAllPeers();
98 XBT_INFO("Couldn't contact the tracker.");
101 XBT_INFO("Here is my current status: %s", getStatus().c_str());
104 bool Peer::getPeersFromTracker()
106 simgrid::s4u::Mailbox* tracker_mailbox = simgrid::s4u::Mailbox::by_name(TRACKER_MAILBOX);
107 // Build the task to send to the tracker
108 auto* peer_request = new TrackerQuery(id, mailbox_);
110 XBT_DEBUG("Sending a peer request to the tracker.");
111 tracker_mailbox->put(peer_request, TRACKER_COMM_SIZE, GET_PEERS_TIMEOUT);
112 } catch (const simgrid::TimeoutException&) {
113 XBT_DEBUG("Timeout expired when requesting peers to tracker");
119 auto answer = mailbox_->get_unique<TrackerAnswer>(GET_PEERS_TIMEOUT);
120 // Add the peers the tracker gave us to our peer list.
121 for (auto const& peer_id : answer->getPeers())
123 connected_peers.emplace(peer_id, Connection(peer_id));
124 } catch (const simgrid::TimeoutException&) {
125 XBT_DEBUG("Timeout expired when requesting peers to tracker");
131 void Peer::sendHandshakeToAllPeers()
133 for (auto const& kv : connected_peers) {
134 const Connection& remote_peer = kv.second;
135 auto* handshake = new Message(MessageType::HANDSHAKE, id, mailbox_);
136 remote_peer.mailbox_->put_init(handshake, message_size(MessageType::HANDSHAKE))->detach();
137 XBT_DEBUG("Sending a HANDSHAKE to %d", remote_peer.id);
141 void Peer::sendMessage(simgrid::s4u::Mailbox* mailbox, MessageType type, uint64_t size)
143 XBT_DEBUG("Sending %s to %s", message_name(type), mailbox->get_cname());
144 mailbox->put_init(new Message(type, id, bitfield_, mailbox_), size)->detach();
147 void Peer::sendBitfield(simgrid::s4u::Mailbox* mailbox)
149 XBT_DEBUG("Sending a BITFIELD to %s", mailbox->get_cname());
151 ->put_init(new Message(MessageType::BITFIELD, id, bitfield_, mailbox_),
152 message_size(MessageType::BITFIELD) + BITS_TO_BYTES(FILE_PIECES))
156 void Peer::sendPiece(simgrid::s4u::Mailbox* mailbox, unsigned int piece, int block_index, int block_length)
158 xbt_assert(not hasNotPiece(piece), "Tried to send a unavailable piece.");
159 XBT_DEBUG("Sending the PIECE %u (%d,%d) to %s", piece, block_index, block_length, mailbox->get_cname());
160 mailbox->put_init(new Message(MessageType::PIECE, id, mailbox_, piece, block_index, block_length), BLOCK_SIZE)
164 void Peer::sendHaveToAllPeers(unsigned int piece)
166 XBT_DEBUG("Sending HAVE message to all my peers");
167 for (auto const& kv : connected_peers) {
168 const Connection& remote_peer = kv.second;
169 remote_peer.mailbox_->put_init(new Message(MessageType::HAVE, id, mailbox_, piece), message_size(MessageType::HAVE))
174 void Peer::sendRequestTo(Connection* remote_peer, unsigned int piece)
176 remote_peer->current_piece = piece;
177 xbt_assert(remote_peer->hasPiece(piece));
178 int block_index = getFirstMissingBlockFrom(piece);
179 if (block_index != -1) {
180 int block_length = static_cast<int>(std::min(BLOCKS_REQUESTED, PIECES_BLOCKS - block_index));
181 XBT_DEBUG("Sending a REQUEST to %s for piece %u (%d,%d)", remote_peer->mailbox_->get_cname(), piece, block_index,
183 remote_peer->mailbox_
184 ->put_init(new Message(MessageType::REQUEST, id, mailbox_, piece, block_index, block_length),
185 message_size(MessageType::REQUEST))
190 std::string Peer::getStatus() const
193 for (unsigned i = 0; i < FILE_PIECES; i++)
194 res += (bitfield_ & (1U << i)) ? '1' : '0';
198 bool Peer::hasFinished() const
200 return bitfield_ == (1U << FILE_PIECES) - 1U;
203 /** Indicates if the remote peer has a piece not stored by the local peer */
204 bool Peer::isInterestedBy(const Connection* remote_peer) const
206 return remote_peer->bitfield & (bitfield_ ^ ((1 << FILE_PIECES) - 1));
209 bool Peer::isInterestedByFree(const Connection* remote_peer) const
211 for (unsigned int i = 0; i < FILE_PIECES; i++)
212 if (hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i))
217 void Peer::updatePiecesCountFromBitfield(unsigned int bitfield)
219 for (unsigned int i = 0; i < FILE_PIECES; i++)
220 if (bitfield & (1U << i))
224 unsigned int Peer::countPieces(unsigned int bitfield) const
226 unsigned int count = 0U;
227 unsigned int n = bitfield;
235 int Peer::nbInterestedPeers() const
238 for (auto const& kv : connected_peers)
239 if (kv.second.interested)
246 double next_choked_update = simgrid::s4u::Engine::get_clock() + UPDATE_CHOKED_INTERVAL;
247 XBT_DEBUG("Start downloading.");
249 /* Send a "handshake" message to all the peers it got (since it couldn't have gotten more than 50 peers) */
250 sendHandshakeToAllPeers();
251 XBT_DEBUG("Starting main leech loop listening on mailbox: %s", mailbox_->get_cname());
253 while (simgrid::s4u::Engine::get_clock() < deadline && countPieces(bitfield_) < FILE_PIECES) {
254 if (comm_received == nullptr) {
255 comm_received = mailbox_->get_async<Message>(&message);
257 if (comm_received->test()) {
260 comm_received = nullptr;
262 // We don't execute the choke algorithm if we don't already have a piece
263 if (simgrid::s4u::Engine::get_clock() >= next_choked_update && countPieces(bitfield_) > 0) {
265 next_choked_update += UPDATE_CHOKED_INTERVAL;
267 simgrid::s4u::this_actor::sleep_for(SLEEP_DURATION);
272 XBT_DEBUG("%d becomes a seeder", id);
277 double next_choked_update = simgrid::s4u::Engine::get_clock() + UPDATE_CHOKED_INTERVAL;
278 XBT_DEBUG("Start seeding.");
279 // start the main seed loop
280 while (simgrid::s4u::Engine::get_clock() < deadline) {
281 if (comm_received == nullptr) {
282 comm_received = mailbox_->get_async<Message>(&message);
284 if (comm_received->test()) {
287 comm_received = nullptr;
289 if (simgrid::s4u::Engine::get_clock() >= next_choked_update) {
291 // TODO: Change the choked peer algorithm when seeding.
292 next_choked_update += UPDATE_CHOKED_INTERVAL;
294 simgrid::s4u::this_actor::sleep_for(SLEEP_DURATION);
300 void Peer::updateActivePeersSet(Connection* remote_peer)
302 if (remote_peer->interested && not remote_peer->choked_upload)
303 active_peers.insert(remote_peer);
305 active_peers.erase(remote_peer);
308 void Peer::handleMessage()
310 XBT_DEBUG("Received a %s message from %s", message_name(message->type), message->return_mailbox->get_cname());
312 auto known_peer = connected_peers.find(message->peer_id);
313 Connection* remote_peer = (known_peer == connected_peers.end()) ? nullptr : &known_peer->second;
314 xbt_assert(remote_peer != nullptr || message->type == MessageType::HANDSHAKE,
315 "The impossible did happened: A not-in-our-list peer sent us a message.");
317 switch (message->type) {
318 case MessageType::HANDSHAKE:
319 // Check if the peer is in our connection list.
320 if (remote_peer == nullptr) {
321 XBT_DEBUG("This peer %d was unknown, answer to its handshake", message->peer_id);
322 connected_peers.emplace(message->peer_id, Connection(message->peer_id));
323 sendMessage(message->return_mailbox, MessageType::HANDSHAKE, message_size(MessageType::HANDSHAKE));
325 // Send our bitfield to the peer
326 sendBitfield(message->return_mailbox);
328 case MessageType::BITFIELD:
329 // Update the pieces list
330 updatePiecesCountFromBitfield(message->bitfield);
331 // Store the bitfield
332 remote_peer->bitfield = message->bitfield;
333 xbt_assert(not remote_peer->am_interested, "Should not be interested at first");
334 if (isInterestedBy(remote_peer)) {
335 remote_peer->am_interested = true;
336 sendMessage(message->return_mailbox, MessageType::INTERESTED, message_size(MessageType::INTERESTED));
339 case MessageType::INTERESTED:
340 // Update the interested state of the peer.
341 remote_peer->interested = true;
342 updateActivePeersSet(remote_peer);
344 case MessageType::NOTINTERESTED:
345 remote_peer->interested = false;
346 updateActivePeersSet(remote_peer);
348 case MessageType::UNCHOKE:
349 xbt_assert(remote_peer->choked_download);
350 remote_peer->choked_download = false;
351 // Send requests to the peer, since it has unchoked us
352 if (remote_peer->am_interested)
353 requestNewPieceTo(remote_peer);
355 case MessageType::CHOKE:
356 xbt_assert(not remote_peer->choked_download);
357 remote_peer->choked_download = true;
358 if (remote_peer->current_piece != -1)
359 removeCurrentPiece(remote_peer, remote_peer->current_piece);
361 case MessageType::HAVE:
362 XBT_DEBUG("\t for piece %d", message->piece);
363 xbt_assert((message->piece >= 0 && static_cast<unsigned int>(message->piece) < FILE_PIECES),
364 "Wrong HAVE message received");
365 remote_peer->bitfield = remote_peer->bitfield | (1U << static_cast<unsigned int>(message->piece));
366 pieces_count[message->piece]++;
367 // If the piece is in our pieces, we tell the peer that we are interested.
368 if (not remote_peer->am_interested && hasNotPiece(message->piece)) {
369 remote_peer->am_interested = true;
370 sendMessage(message->return_mailbox, MessageType::INTERESTED, message_size(MessageType::INTERESTED));
371 if (not remote_peer->choked_download)
372 requestNewPieceTo(remote_peer);
375 case MessageType::REQUEST:
376 xbt_assert(remote_peer->interested);
377 xbt_assert((message->piece >= 0 && static_cast<unsigned int>(message->piece) < FILE_PIECES),
378 "Wrong HAVE message received");
379 if (not remote_peer->choked_upload) {
380 XBT_DEBUG("\t for piece %d (%d,%d)", message->piece, message->block_index,
381 message->block_index + message->block_length);
382 if (not hasNotPiece(message->piece)) {
383 sendPiece(message->return_mailbox, message->piece, message->block_index, message->block_length);
386 XBT_DEBUG("\t for piece %d but he is choked.", message->peer_id);
389 case MessageType::PIECE:
390 XBT_DEBUG(" \t for piece %d (%d,%d)", message->piece, message->block_index,
391 message->block_index + message->block_length);
392 xbt_assert(not remote_peer->choked_download);
393 xbt_assert(not remote_peer->choked_download, "Can't received a piece if I'm choked !");
394 xbt_assert((message->piece >= 0 && static_cast<unsigned int>(message->piece) < FILE_PIECES),
395 "Wrong piece received");
396 // TODO: Execute a computation.
397 if (hasNotPiece(static_cast<unsigned int>(message->piece))) {
398 updateBitfieldBlocks(message->piece, message->block_index, message->block_length);
399 if (hasCompletedPiece(static_cast<unsigned int>(message->piece))) {
400 // Removing the piece from our piece list
401 removeCurrentPiece(remote_peer, message->piece);
402 // Setting the fact that we have the piece
403 bitfield_ = bitfield_ | (1U << static_cast<unsigned int>(message->piece));
404 XBT_DEBUG("My status is now %s", getStatus().c_str());
405 // Sending the information to all the peers we are connected to
406 sendHaveToAllPeers(message->piece);
407 // sending UNINTERESTED to peers that do not have what we want.
408 updateInterestedAfterReceive();
409 } else { // piece not completed
410 sendRequestTo(remote_peer, message->piece); // ask for the next block
413 XBT_DEBUG("However, we already have it");
414 requestNewPieceTo(remote_peer);
417 case MessageType::CANCEL:
422 // Update the peer speed.
424 remote_peer->addSpeedValue(1.0 / (simgrid::s4u::Engine::get_clock() - begin_receive_time));
426 begin_receive_time = simgrid::s4u::Engine::get_clock();
429 /** Selects the appropriate piece to download and requests it to the remote_peer */
430 void Peer::requestNewPieceTo(Connection* remote_peer)
432 int piece = selectPieceToDownload(remote_peer);
434 current_pieces |= (1U << (unsigned int)piece);
435 sendRequestTo(remote_peer, piece);
439 void Peer::removeCurrentPiece(Connection* remote_peer, unsigned int current_piece)
441 current_pieces &= ~(1U << current_piece);
442 remote_peer->current_piece = -1;
445 /** @brief Return the piece to be downloaded
446 * There are two cases (as described in "Bittorrent Architecture Protocol", Ryan Toole :
447 * If a piece is partially downloaded, this piece will be selected prioritarily
448 * If the peer has strictly less than 4 pieces, he chooses a piece at random.
449 * If the peer has more than pieces, he downloads the pieces that are the less replicated (rarest policy).
450 * If all pieces have been downloaded or requested, we select a random requested piece (endgame mode).
451 * @param remote_peer: information about the connection
452 * @return the piece to download if possible. -1 otherwise
454 int Peer::selectPieceToDownload(const Connection* remote_peer)
456 int piece = partiallyDownloadedPiece(remote_peer);
457 // strict priority policy
462 if (countPieces(current_pieces) >= (FILE_PIECES - countPieces(bitfield_)) && isInterestedBy(remote_peer)) {
463 int nb_interesting_pieces = 0;
464 // compute the number of interesting pieces
465 for (unsigned int i = 0; i < FILE_PIECES; i++)
466 if (remotePeerHasMissingPiece(remote_peer, i))
467 nb_interesting_pieces++;
469 xbt_assert(nb_interesting_pieces != 0);
470 // get a random interesting piece
471 int random_piece_index = random.uniform_int(0, nb_interesting_pieces - 1);
472 int current_index = 0;
473 for (unsigned int i = 0; i < FILE_PIECES; i++) {
474 if (remotePeerHasMissingPiece(remote_peer, i)) {
475 if (random_piece_index == current_index) {
482 xbt_assert(piece != -1);
485 // Random first policy
486 if (countPieces(bitfield_) < 4 && isInterestedByFree(remote_peer)) {
487 int nb_interesting_pieces = 0;
488 // compute the number of interesting pieces
489 for (unsigned int i = 0; i < FILE_PIECES; i++)
490 if (remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i))
491 nb_interesting_pieces++;
492 xbt_assert(nb_interesting_pieces != 0);
493 // get a random interesting piece
494 int random_piece_index = random.uniform_int(0, nb_interesting_pieces - 1);
495 int current_index = 0;
496 for (unsigned int i = 0; i < FILE_PIECES; i++) {
497 if (remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i)) {
498 if (random_piece_index == current_index) {
505 xbt_assert(piece != -1);
507 } else { // Rarest first policy
508 short min = SHRT_MAX;
509 int nb_min_pieces = 0;
510 int current_index = 0;
511 // compute the smallest number of copies of available pieces
512 for (unsigned int i = 0; i < FILE_PIECES; i++) {
513 if (pieces_count[i] < min && remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i))
514 min = pieces_count[i];
517 xbt_assert(min != SHRT_MAX || not isInterestedByFree(remote_peer));
518 // compute the number of rarest pieces
519 for (unsigned int i = 0; i < FILE_PIECES; i++)
520 if (pieces_count[i] == min && remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i))
523 xbt_assert(nb_min_pieces != 0 || not isInterestedByFree(remote_peer));
524 // get a random rarest piece
525 int random_rarest_index = 0;
526 if (nb_min_pieces > 0) {
527 random_rarest_index = random.uniform_int(0, nb_min_pieces - 1);
529 for (unsigned int i = 0; i < FILE_PIECES; i++)
530 if (pieces_count[i] == min && remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i)) {
531 if (random_rarest_index == current_index) {
538 xbt_assert(piece != -1 || not isInterestedByFree(remote_peer));
543 void Peer::updateChokedPeers()
545 if (nbInterestedPeers() == 0)
547 XBT_DEBUG("(%d) update_choked peers %zu active peers", id, active_peers.size());
548 // update the current round
549 round_ = (round_ + 1) % 3;
550 Connection* chosen_peer = nullptr;
551 // select first active peer and remove it from the set
552 Connection* choked_peer;
553 if (active_peers.empty()) {
554 choked_peer = nullptr;
556 choked_peer = *active_peers.begin();
557 active_peers.erase(choked_peer);
560 /**If we are currently seeding, we unchoke the peer which has been unchoked the last time.*/
562 double unchoke_time = simgrid::s4u::Engine::get_clock() + 1;
563 for (auto& kv : connected_peers) {
564 Connection& remote_peer = kv.second;
565 if (remote_peer.last_unchoke < unchoke_time && remote_peer.interested && remote_peer.choked_upload) {
566 unchoke_time = remote_peer.last_unchoke;
567 chosen_peer = &remote_peer;
571 // Random optimistic unchoking
575 // We choose a random peer to unchoke.
576 auto chosen_peer_it = connected_peers.begin();
577 std::advance(chosen_peer_it, random.uniform_int(0, static_cast<int>(connected_peers.size() - 1)));
578 chosen_peer = &chosen_peer_it->second;
579 if (not chosen_peer->interested || not chosen_peer->choked_upload)
580 chosen_peer = nullptr;
582 XBT_DEBUG("Nothing to do, keep going");
584 } while (chosen_peer == nullptr && j < MAXIMUM_PEERS);
586 // Use the "fastest download" policy.
587 double fastest_speed = 0.0;
588 for (auto& kv : connected_peers) {
589 Connection& remote_peer = kv.second;
590 if (remote_peer.peer_speed > fastest_speed && remote_peer.choked_upload && remote_peer.interested) {
591 fastest_speed = remote_peer.peer_speed;
592 chosen_peer = &remote_peer;
598 if (chosen_peer != nullptr)
599 XBT_DEBUG("(%d) update_choked peers unchoked (%d) ; int (%d) ; choked (%d) ", id, chosen_peer->id,
600 chosen_peer->interested, chosen_peer->choked_upload);
602 if (choked_peer != chosen_peer) {
603 if (choked_peer != nullptr) {
604 xbt_assert(not choked_peer->choked_upload, "Tries to choked a choked peer");
605 choked_peer->choked_upload = true;
606 updateActivePeersSet(choked_peer);
607 XBT_DEBUG("(%d) Sending a CHOKE to %d", id, choked_peer->id);
608 sendMessage(choked_peer->mailbox_, MessageType::CHOKE, message_size(MessageType::CHOKE));
610 if (chosen_peer != nullptr) {
611 xbt_assert((chosen_peer->choked_upload), "Tries to unchoked an unchoked peer");
612 chosen_peer->choked_upload = false;
613 active_peers.insert(chosen_peer);
614 chosen_peer->last_unchoke = simgrid::s4u::Engine::get_clock();
615 XBT_DEBUG("(%d) Sending a UNCHOKE to %d", id, chosen_peer->id);
616 updateActivePeersSet(chosen_peer);
617 sendMessage(chosen_peer->mailbox_, MessageType::UNCHOKE, message_size(MessageType::UNCHOKE));
622 /** @brief Update "interested" state of peers: send "not interested" to peers that don't have any more pieces we want.*/
623 void Peer::updateInterestedAfterReceive()
625 for (auto& kv : connected_peers) {
626 Connection& remote_peer = kv.second;
627 if (remote_peer.am_interested) {
628 bool interested = false;
629 // Check if the peer still has a piece we want.
630 for (unsigned int i = 0; i < FILE_PIECES; i++)
631 if (remotePeerHasMissingPiece(&remote_peer, i)) {
636 if (not interested) { // no more piece to download from connection
637 remote_peer.am_interested = false;
638 sendMessage(remote_peer.mailbox_, MessageType::NOTINTERESTED, message_size(MessageType::NOTINTERESTED));
644 void Peer::updateBitfieldBlocks(int piece, int block_index, int block_length)
646 xbt_assert((piece >= 0 && static_cast<unsigned int>(piece) <= FILE_PIECES), "Wrong piece.");
647 xbt_assert((block_index >= 0 && static_cast<unsigned int>(block_index) <= PIECES_BLOCKS), "Wrong block : %d.",
649 for (int i = block_index; i < (block_index + block_length); i++)
650 bitfield_blocks |= (1ULL << static_cast<unsigned int>(piece * PIECES_BLOCKS + i));
653 bool Peer::hasCompletedPiece(unsigned int piece) const
655 for (unsigned int i = 0; i < PIECES_BLOCKS; i++)
656 if (not(bitfield_blocks & 1ULL << (piece * PIECES_BLOCKS + i)))
661 int Peer::getFirstMissingBlockFrom(int piece) const
663 for (unsigned int i = 0; i < PIECES_BLOCKS; i++)
664 if (not(bitfield_blocks & 1ULL << (piece * PIECES_BLOCKS + i)))
669 /** Returns a piece that is partially downloaded and stored by the remote peer if any -1 otherwise. */
670 int Peer::partiallyDownloadedPiece(const Connection* remote_peer) const
672 for (unsigned int i = 0; i < FILE_PIECES; i++)
673 if (remotePeerHasMissingPiece(remote_peer, i) && isNotDownloadingPiece(i) && getFirstMissingBlockFrom(i) > 0)