-/* Copyright (c) 2012-2020. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2012-2022. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include "bittorrent-peer.h"
#include "tracker.h"
#include <simgrid/forward.h>
+#include <xbt/ex.h>
#include <limits.h>
#include <stdio.h> /* snprintf */
#define SLEEP_DURATION 1
#define BITS_TO_BYTES(x) (((x) / 8 + (x) % 8) ? 1 : 0)
+const char* const message_type_names[10] = {"HANDSHAKE", "CHOKE", "UNCHOKE", "INTERESTED", "NOTINTERESTED",
+ "HAVE", "BITFIELD", "REQUEST", "PIECE", "CANCEL"};
+
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
}
/** Peer main function */
-void peer(int argc, char* argv[])
+void peer_run(int argc, char* argv[])
{
// Check arguments
xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments");
// Build peer object
- peer_t peer = peer_init((int)xbt_str_parse_int(argv[1], "Invalid ID: %s"), argc == 4 ? 1 : 0);
+ peer_t peer = peer_init((int)xbt_str_parse_int(argv[1], "Invalid ID"), argc == 4 ? 1 : 0);
// Retrieve deadline
- peer->deadline = xbt_str_parse_double(argv[2], "Invalid deadline: %s");
+ peer->deadline = xbt_str_parse_double(argv[2], "Invalid deadline");
xbt_assert(peer->deadline > 0, "Wrong deadline supplied");
char* status = xbt_malloc0(FILE_PIECES + 1);
- get_status(&status, peer->bitfield);
+ get_status(status, peer->bitfield);
XBT_INFO("Hi, I'm joining the network with id %d", peer->id);
XBT_INFO("Couldn't contact the tracker.");
}
- get_status(&status, peer->bitfield);
+ get_status(status, peer->bitfield);
XBT_INFO("Here is my current status: %s", status);
if (peer->comm_received) {
sg_comm_unref(peer->comm_received);
void send_message(const_peer_t peer, sg_mailbox_t mailbox, e_message_type type, uint64_t size)
{
- const char* type_names[6] = {"HANDSHAKE", "CHOKE", "UNCHOKE", "INTERESTED", "NOTINTERESTED", "CANCEL"};
- XBT_DEBUG("Sending %s to %s", type_names[type], sg_mailbox_get_name(mailbox));
+ XBT_DEBUG("Sending %s to %s", message_type_names[type], sg_mailbox_get_name(mailbox));
message_t message = message_other_new(type, peer->id, peer->mailbox, peer->bitfield);
sg_comm_t comm = sg_mailbox_put_init(mailbox, message, size);
sg_comm_detach(comm, NULL);
}
}
-void get_status(char** status, unsigned int bitfield)
+void get_status(char* status, unsigned int bitfield)
{
for (int i = FILE_PIECES - 1; i >= 0; i--)
- (*status)[i] = (bitfield & (1U << i)) ? '1' : '0';
- (*status)[FILE_PIECES] = '\0';
+ status[i] = (bitfield & (1U << i)) ? '1' : '0';
+ status[FILE_PIECES] = '\0';
}
int has_finished(unsigned int bitfield)
/** Indicates if the remote peer has a piece not stored by the local peer nor requested by the local peer */
int is_interested_and_free(const_peer_t peer, const_connection_t remote_peer)
{
- for (int i = 0; i < FILE_PIECES; i++)
+ for (unsigned int i = 0; i < FILE_PIECES; i++)
if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) && peer_is_not_downloading_piece(peer, i))
return 1;
return 0;
/** @brief Updates the list of who has a piece from a bitfield */
void update_pieces_count_from_bitfield(const_peer_t peer, unsigned int bitfield)
{
- for (int i = 0; i < FILE_PIECES; i++)
+ for (unsigned int i = 0; i < FILE_PIECES; i++)
if (bitfield & (1U << i))
peer->pieces_count[i]++;
}
-int count_pieces(unsigned int bitfield)
+unsigned int count_pieces(unsigned int bitfield)
{
- int count = 0;
+ unsigned int count = 0;
unsigned int n = bitfield;
while (n) {
count += n & 1U;
/** @brief Handle a received message sent by another peer */
void handle_message(peer_t peer, message_t message)
{
- const char* type_names[10] = {"HANDSHAKE", "CHOKE", "UNCHOKE", "INTERESTED", "NOTINTERESTED",
- "HAVE", "BITFIELD", "REQUEST", "PIECE", "CANCEL"};
- XBT_DEBUG("Received a %s message from %s", type_names[message->type], sg_mailbox_get_name(message->return_mailbox));
+ XBT_DEBUG("Received a %s message from %s", message_type_names[message->type],
+ sg_mailbox_get_name(message->return_mailbox));
connection_t remote_peer = xbt_dict_get_or_null_ext(peer->connected_peers, (char*)&message->peer_id, sizeof(int));
xbt_assert(remote_peer != NULL || message->type == MESSAGE_HANDSHAKE,
break;
case MESSAGE_HAVE:
XBT_DEBUG("\t for piece %d", message->piece);
- xbt_assert((message->piece >= 0 && message->piece < FILE_PIECES), "Wrong HAVE message received");
+ xbt_assert((message->piece >= 0 && (unsigned)message->piece < FILE_PIECES), "Wrong HAVE message received");
remote_peer->bitfield = remote_peer->bitfield | (1U << message->piece);
peer->pieces_count[message->piece]++;
// If the piece is in our pieces, we tell the peer that we are interested.
break;
case MESSAGE_REQUEST:
xbt_assert(remote_peer->interested);
- xbt_assert((message->piece >= 0 && message->piece < FILE_PIECES), "Wrong request received");
+ xbt_assert((message->piece >= 0 && (unsigned)message->piece < FILE_PIECES), "Wrong request received");
if (remote_peer->choked_upload == 0) {
XBT_DEBUG("\t for piece %d (%d,%d)", message->piece, message->block_index,
message->block_index + message->block_length);
message->block_index + message->block_length);
xbt_assert(!remote_peer->choked_download);
xbt_assert(remote_peer->choked_download != 1, "Can't received a piece if I'm choked !");
- xbt_assert((message->piece >= 0 && message->piece < FILE_PIECES), "Wrong piece received");
- // TODO: Execute à computation.
+ xbt_assert((message->piece >= 0 && (unsigned)message->piece < FILE_PIECES), "Wrong piece received");
+ // TODO: Execute a computation.
if (peer_has_not_piece(peer, message->piece)) {
update_bitfield_blocks(peer, message->piece, message->block_index, message->block_length);
if (piece_complete(peer, message->piece)) {
// Setting the fact that we have the piece
peer->bitfield = peer->bitfield | (1U << message->piece);
char* status = xbt_malloc0(FILE_PIECES + 1);
- get_status(&status, peer->bitfield);
+ get_status(status, peer->bitfield);
XBT_DEBUG("My status is now %s", status);
xbt_free(status);
// Sending the information to all the peers we are connected to
(is_interested(peer, remote_peer) != 0)) {
int nb_interesting_pieces = 0;
// compute the number of interesting pieces
- for (int i = 0; i < FILE_PIECES; i++) {
+ for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i)) {
nb_interesting_pieces++;
}
// get a random interesting piece
int random_piece_index = rand() % nb_interesting_pieces;
int current_index = 0;
- for (int i = 0; i < FILE_PIECES; i++) {
+ for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i)) {
if (random_piece_index == current_index) {
piece = i;
if (count_pieces(peer->bitfield) < 4 && (is_interested_and_free(peer, remote_peer) != 0)) {
int nb_interesting_pieces = 0;
// compute the number of interesting pieces
- for (int i = 0; i < FILE_PIECES; i++) {
+ for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
peer_is_not_downloading_piece(peer, i)) {
nb_interesting_pieces++;
// get a random interesting piece
int random_piece_index = rand() % nb_interesting_pieces;
int current_index = 0;
- for (int i = 0; i < FILE_PIECES; i++) {
+ for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
peer_is_not_downloading_piece(peer, i)) {
if (random_piece_index == current_index) {
int nb_min_pieces = 0;
int current_index = 0;
// compute the smallest number of copies of available pieces
- for (int i = 0; i < FILE_PIECES; i++) {
+ for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (peer->pieces_count[i] < min && peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
peer_is_not_downloading_piece(peer, i))
min = peer->pieces_count[i];
}
xbt_assert(min != SHRT_MAX || (is_interested_and_free(peer, remote_peer) == 0));
// compute the number of rarest pieces
- for (int i = 0; i < FILE_PIECES; i++) {
+ for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (peer->pieces_count[i] == min && peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
peer_is_not_downloading_piece(peer, i))
nb_min_pieces++;
if (nb_min_pieces > 0) {
random_rarest_index = rand() % nb_min_pieces;
}
- for (int i = 0; i < FILE_PIECES; i++) {
+ for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (peer->pieces_count[i] == min && peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) &&
peer_is_not_downloading_piece(peer, i)) {
if (random_rarest_index == current_index) {
if (connection->am_interested != 0) {
int interested = 0;
// Check if the peer still has a piece we want.
- for (int i = 0; i < FILE_PIECES; i++) {
+ for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (peer_has_not_piece(peer, i) && connection_has_piece(connection, i)) {
interested = 1;
break;
void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_length)
{
- xbt_assert((index >= 0 && index <= FILE_PIECES), "Wrong piece.");
- xbt_assert((block_index >= 0 && block_index <= PIECES_BLOCKS), "Wrong block : %d.", block_index);
+ xbt_assert((index >= 0 && (unsigned)index <= FILE_PIECES), "Wrong piece.");
+ xbt_assert((block_index >= 0 && (unsigned)block_index <= PIECES_BLOCKS), "Wrong block : %d.", block_index);
for (int i = block_index; i < (block_index + block_length); i++) {
peer->bitfield_blocks |= (1ULL << (unsigned int)(index * PIECES_BLOCKS + i));
}
/** Returns if a peer has completed the download of a piece */
int piece_complete(const_peer_t peer, int index)
{
- for (int i = 0; i < PIECES_BLOCKS; i++) {
+ for (unsigned int i = 0; i < PIECES_BLOCKS; i++) {
if (!(peer->bitfield_blocks & 1ULL << (index * PIECES_BLOCKS + i))) {
return 0;
}
/** Returns the first block that a peer doesn't have in a piece. If the peer has all blocks of the piece, returns -1. */
int get_first_missing_block_from(const_peer_t peer, int piece)
{
- for (int i = 0; i < PIECES_BLOCKS; i++) {
+ for (unsigned int i = 0; i < PIECES_BLOCKS; i++) {
if (!(peer->bitfield_blocks & 1ULL << (piece * PIECES_BLOCKS + i))) {
return i;
}
/** Returns a piece that is partially downloaded and stored by the remote peer if any -1 otherwise. */
int partially_downloaded_piece(const_peer_t peer, const_connection_t remote_peer)
{
- for (int i = 0; i < FILE_PIECES; i++) {
+ for (unsigned int i = 0; i < FILE_PIECES; i++) {
if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) && peer_is_not_downloading_piece(peer, i) &&
get_first_missing_block_from(peer, i) > 0)
return i;