From 04e829f2dc9c85be2ea9b594437a988557c94c00 Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Wed, 10 Feb 2016 21:44:46 +0100 Subject: [PATCH 1/1] Example cleaning (msg-bittorent) - replace deployment by something closer to what the provided generator produces - Stop using the ugly platform.xml file - revalidate tesh - reindent to 120 character width - more uniform comments --- examples/msg/bittorrent/bittorrent.c | 11 +- examples/msg/bittorrent/bittorrent.h | 43 +-- examples/msg/bittorrent/bittorrent.tesh | 34 +- examples/msg/bittorrent/bittorrent.xml | 51 ++- examples/msg/bittorrent/connection.h | 24 +- examples/msg/bittorrent/generate.py | 16 +- examples/msg/bittorrent/messages.c | 53 +-- examples/msg/bittorrent/messages.h | 67 ++-- examples/msg/bittorrent/peer.c | 461 ++++++++---------------- examples/msg/bittorrent/peer.h | 22 +- 10 files changed, 267 insertions(+), 515 deletions(-) diff --git a/examples/msg/bittorrent/bittorrent.c b/examples/msg/bittorrent/bittorrent.c index c7157b6500..4fc3fb19e1 100644 --- a/examples/msg/bittorrent/bittorrent.c +++ b/examples/msg/bittorrent/bittorrent.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2012-2015. The SimGrid Team. +/* Copyright (c) 2012-2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -10,9 +10,7 @@ #include #include -/** - * Bittorrent example launcher - */ +/** Bittorrent example launcher */ int main(int argc, char *argv[]) { xbt_dynar_t host_list; @@ -22,10 +20,7 @@ int main(int argc, char *argv[]) MSG_init(&argc, argv); /* Check the arguments */ - if (argc < 3) { - printf("Usage: %s platform_file deployment_file \n", argv[0]); - return -1; - } + xbt_assert (argc > 2, "Usage: %s platform_file deployment_file", argv[0]); const char *platform_file = argv[1]; const char *deployment_file = argv[2]; diff --git a/examples/msg/bittorrent/bittorrent.h b/examples/msg/bittorrent/bittorrent.h index 01a80eee72..b45d688a4c 100644 --- a/examples/msg/bittorrent/bittorrent.h +++ b/examples/msg/bittorrent/bittorrent.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2012-2014. The SimGrid Team. +/* Copyright (c) 2012-2014, 2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -7,51 +7,22 @@ #ifndef BITTORRENT_BITTORRENT_H_ #define BITTORRENT_BITTORRENT_H_ -/** - * Size of mailboxes - */ #define MAILBOX_SIZE 40 -/** - * Mailbox used to communicate with the tracker. - */ #define TRACKER_MAILBOX "tracker_mailbox" -/** - * Max number of pairs sent by the tracker to clients - */ +/** Max number of pairs sent by the tracker to clients */ #define MAXIMUM_PAIRS 50 -/** - * Interval of time where the peer should send a request to the tracker - */ +/** Interval of time where the peer should send a request to the tracker */ #define TRACKER_QUERY_INTERVAL 1000 -/** - * Communication size for a task to the tracker - */ +/** Communication size for a task to the tracker */ #define TRACKER_COMM_SIZE 0.01 -/** - * Timeout for the get peers data - */ #define GET_PEERS_TIMEOUT 10000 -/** - * Timeout for "standard" messages. - */ #define TIMEOUT_MESSAGE 10 -/** - * Timeout for tracker receive. - */ #define TRACKER_RECEIVE_TIMEOUT 10 -/** - * Number of peers that can be unchocked at a given time - */ +/** Number of peers that can be unchocked at a given time */ #define MAX_UNCHOKED_PEERS 4 - -/** - * Interval between each update of the choked peers - */ +/** Interval between each update of the choked peers */ #define UPDATE_CHOKED_INTERVAL 30 - -/** - * Number of pieces the peer asks for simultaneously - */ +/** Number of pieces the peer asks for simultaneously */ #define MAX_PIECES 1 #endif /* BITTORRENT_BITTORRENT_H_ */ diff --git a/examples/msg/bittorrent/bittorrent.tesh b/examples/msg/bittorrent/bittorrent.tesh index 651b01dca8..fbe1fc223f 100755 --- a/examples/msg/bittorrent/bittorrent.tesh +++ b/examples/msg/bittorrent/bittorrent.tesh @@ -4,20 +4,20 @@ p Testing the Bittorrent implementation with MSG ! timeout 10 ! output sort 19 -$ $SG_TEST_EXENV ${bindir:=.}/bittorrent ${srcdir:=.}/../../platforms/platform.xml ${srcdir:=.}/bittorrent.xml "--log=root.fmt:[%12.6r]%e(%i:%P@%h)%e%m%n" -> [ 0.000000] (1:tracker@Jacquelin) Tracker launched. -> [ 0.000000] (2:peer@Boivin) Hi, I'm joining the network with id 2 -> [ 0.000000] (3:peer@Jean_Yves) Hi, I'm joining the network with id 3 -> [ 0.000000] (4:peer@TeX) Hi, I'm joining the network with id 4 -> [ 0.000000] (5:peer@Geoff) Hi, I'm joining the network with id 5 -> [ 0.000000] (6:peer@Disney) Hi, I'm joining the network with id 6 -> [ 0.000000] (7:peer@iRMX) Hi, I'm joining the network with id 7 -> [ 0.000000] (8:peer@McGee) Hi, I'm joining the network with id 8 -> [ 3000.000000] (1:tracker@Jacquelin) Tracker is leaving -> [ 5000.005340] (5:peer@Geoff) Here is my current status: 1111111111 -> [ 5000.043658] (7:peer@iRMX) Here is my current status: 1111111111 -> [ 5000.048881] (2:peer@Boivin) Here is my current status: 1111111111 -> [ 5000.812587] (4:peer@TeX) Here is my current status: 1111111111 -> [ 5000.823538] (6:peer@Disney) Here is my current status: 1111111111 -> [ 5000.888352] (8:peer@McGee) Here is my current status: 1111111111 -> [ 5000.925088] (3:peer@Jean_Yves) Here is my current status: 1111111111 +$ $SG_TEST_EXENV ${bindir:=.}/bittorrent ${srcdir:=.}/../../platforms/cluster.xml ${srcdir:=.}/bittorrent.xml "--log=root.fmt:[%12.6r]%e(%i:%P@%h)%e%m%n" +> [ 0.000000] (1:tracker@node-0.acme.org) Tracker launched. +> [ 0.000000] (2:peer@node-1.acme.org) Hi, I'm joining the network with id 2 +> [ 0.000000] (3:peer@node-2.acme.org) Hi, I'm joining the network with id 3 +> [ 0.000000] (4:peer@node-3.acme.org) Hi, I'm joining the network with id 4 +> [ 0.000000] (5:peer@node-4.acme.org) Hi, I'm joining the network with id 5 +> [ 0.000000] (6:peer@node-5.acme.org) Hi, I'm joining the network with id 6 +> [ 0.000000] (7:peer@node-6.acme.org) Hi, I'm joining the network with id 7 +> [ 0.000000] (8:peer@node-7.acme.org) Hi, I'm joining the network with id 8 +> [ 3000.000000] (1:tracker@node-0.acme.org) Tracker is leaving +> [ 5000.007806] (2:peer@node-1.acme.org) Here is my current status: 1111111111 +> [ 5000.007806] (3:peer@node-2.acme.org) Here is my current status: 1111111111 +> [ 5000.007806] (4:peer@node-3.acme.org) Here is my current status: 1111111111 +> [ 5000.007806] (5:peer@node-4.acme.org) Here is my current status: 1111111111 +> [ 5000.007806] (6:peer@node-5.acme.org) Here is my current status: 1111111111 +> [ 5000.007806] (7:peer@node-6.acme.org) Here is my current status: 1111111111 +> [ 5000.007806] (8:peer@node-7.acme.org) Here is my current status: 1111111111 diff --git a/examples/msg/bittorrent/bittorrent.xml b/examples/msg/bittorrent/bittorrent.xml index 4dd8db5c1a..3836bde110 100644 --- a/examples/msg/bittorrent/bittorrent.xml +++ b/examples/msg/bittorrent/bittorrent.xml @@ -2,39 +2,38 @@ - - + + - - - - + + + + - - - + + + - - - + + + - - - - + + + + - - - + + + - - - + + + - - - + + + - diff --git a/examples/msg/bittorrent/connection.h b/examples/msg/bittorrent/connection.h index 418b638c2a..057e3d8cbe 100644 --- a/examples/msg/bittorrent/connection.h +++ b/examples/msg/bittorrent/connection.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2012-2014. The SimGrid Team. +/* Copyright (c) 2012-2014, 2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -6,9 +6,8 @@ #ifndef BITTORRENT_CONNECTION_H_ #define BITTORRENT_CONNECTION_H_ -/** - * Contains the connection data of a peer. - */ + +/** Contains the connection data of a peer. */ typedef struct s_connection { int id; //Peer id char *bitfield; //Fields @@ -22,19 +21,16 @@ typedef struct s_connection { int choked_upload:1; //Indicates if the peer is choked for the current peer int choked_download:1; //Indicates if the peer has choked the current peer } s_connection_t, *connection_t; -/** - * Build a new connection object from the peer id. - * @param id id of the peer + +/** @brief Build a new connection object from the peer id. + * @param id id of the peer */ connection_t connection_new(int id); -/** - * Add a new value to the peer speed average - * @param connection connection data - * @param speed speed to add to the speed average +/** @brief Add a new value to the peer speed average + * @param connection connection data + * @param speed speed to add to the speed average */ void connection_add_speed_value(connection_t connection, double speed); -/** - * Frees a connection object - */ +/** Frees a connection object */ void connection_free(void *data); #endif /* BITTORRENT_CONNECTION_H_ */ diff --git a/examples/msg/bittorrent/generate.py b/examples/msg/bittorrent/generate.py index 7f0d6d455f..564a5fa541 100755 --- a/examples/msg/bittorrent/generate.py +++ b/examples/msg/bittorrent/generate.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright (c) 2012, 2014. The SimGrid Team. +# Copyright (c) 2012, 2014, 2016. The SimGrid Team. # All rights reserved. # This program is free software; you can redistribute it and/or modify it @@ -26,9 +26,10 @@ max_id = 2 ** nb_bits - 1 all_ids = [42] sys.stdout.write("\n" -"\n" -"\n" -" \n" % end_date) +"\n" +"\n" +" \n" +" \n \n" % end_date) for i in range(1, nb_nodes): @@ -37,10 +38,11 @@ for i in range(1, nb_nodes): my_id = random.randint(0, max_id) ok = not my_id in all_ids start_date = i * 10 - line = " " % (i, my_id, end_date) + line = " \n" % i + line += " \n \n" % (my_id, end_date) if random.randint(0,100) < seed_percentage: - line += "" - line += "\n"; + line += " \n" + line += " \n"; sys.stdout.write(line) all_ids.append(my_id) sys.stdout.write("") diff --git a/examples/msg/bittorrent/messages.c b/examples/msg/bittorrent/messages.c index bfe1ea4a20..a0cfe0782f 100644 --- a/examples/msg/bittorrent/messages.c +++ b/examples/msg/bittorrent/messages.c @@ -12,16 +12,15 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, #define BITS_TO_BYTES(x) ((x / 8) + (x % 8) ? 1 : 0) -/** - * Build a new empty message +/** @brief Build a new empty message * @param type type of the message - * @param issuer_host_name hostname of the issuer, for debuging purposes + * @param issuer_host_name hostname of the issuer, for debugging purposes * @param mailbox mailbox where the peer should answer * @param peer_id id of the issuer * @param size message size in bytes */ -msg_task_t task_message_new(e_message_type type, const char *issuer_host_name, - const char *mailbox, int peer_id, int size) +msg_task_t task_message_new(e_message_type type, const char *issuer_host_name, const char *mailbox, int peer_id, + int size) { message_t message = xbt_new(s_message_t, 1); message->issuer_host_name = issuer_host_name; @@ -33,57 +32,41 @@ msg_task_t task_message_new(e_message_type type, const char *issuer_host_name, return task; } -/** - * Builds a message containing an index. - */ -msg_task_t task_message_index_new(e_message_type type, - const char *issuer_host_name, - const char *mailbox, int peer_id, +/** Builds a message containing an index. */ +msg_task_t task_message_index_new(e_message_type type, const char *issuer_host_name, const char *mailbox, int peer_id, int index, int varsize) { - msg_task_t task = task_message_new(type, issuer_host_name, mailbox, peer_id, - task_message_size(type) + varsize); + msg_task_t task = task_message_new(type, issuer_host_name, mailbox, peer_id, task_message_size(type) + varsize); message_t message = MSG_task_get_data(task); message->index = index; return task; } -msg_task_t task_message_bitfield_new(const char *issuer_host_name, - const char *mailbox, int peer_id, - char *bitfield, int bitfield_size) +msg_task_t task_message_bitfield_new(const char *issuer_host_name, const char *mailbox, int peer_id, char *bitfield, + int bitfield_size) { - msg_task_t task = - task_message_new(MESSAGE_BITFIELD, issuer_host_name, mailbox, peer_id, - task_message_size(MESSAGE_BITFIELD) + - /* Size of bitfield in bytes */ - BITS_TO_BYTES(bitfield_size)); + msg_task_t task = task_message_new(MESSAGE_BITFIELD, issuer_host_name, mailbox, peer_id, + task_message_size(MESSAGE_BITFIELD) + BITS_TO_BYTES(bitfield_size)); message_t message = MSG_task_get_data(task); message->bitfield = bitfield; return task; } -msg_task_t task_message_request_new(const char *issuer_host_name, - const char *mailbox, int peer_id, - int index, int block_index, - int block_length) +msg_task_t task_message_request_new(const char *issuer_host_name, const char *mailbox, int peer_id, int index, + int block_index, int block_length) { - msg_task_t task = - task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox, - peer_id, index, 0); + msg_task_t task = task_message_index_new(MESSAGE_REQUEST, issuer_host_name, mailbox, peer_id, index, 0); message_t message = MSG_task_get_data(task); message->block_index = block_index; message->block_length = block_length; return task; } -msg_task_t task_message_piece_new(const char *issuer_host_name, - const char *mailbox, int peer_id, int index, - int block_index, - int block_length, int block_size) +msg_task_t task_message_piece_new(const char *issuer_host_name, const char *mailbox, int peer_id, int index, + int block_index, int block_length, int block_size) { - msg_task_t task = - task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id, - index, block_length * block_size); + msg_task_t task = task_message_index_new(MESSAGE_PIECE, issuer_host_name, mailbox, peer_id, index, + block_length * block_size); message_t message = MSG_task_get_data(task); message->block_index = block_index; message->block_length = block_length; diff --git a/examples/msg/bittorrent/messages.h b/examples/msg/bittorrent/messages.h index 5e266aacdb..98f140a163 100644 --- a/examples/msg/bittorrent/messages.h +++ b/examples/msg/bittorrent/messages.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2012-2015. The SimGrid Team. +/* Copyright (c) 2012-2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -8,8 +8,7 @@ #define BITTORRENT_MESSAGES_H_ #include -/** - * Message sizes +/** Message sizes * Sizes based on report by A. Legout et al, Understanding BitTorrent: An Experimental Perspective * http://hal.inria.fr/inria-00000156/en */ @@ -24,9 +23,7 @@ #define MESSAGE_PIECE_SIZE 13 #define MESSAGE_CANCEL_SIZE 17 -/** - * Types of messages exchanged between two peers. - */ +/** Types of messages exchanged between two peers. */ typedef enum { MESSAGE_HANDSHAKE, MESSAGE_CHOKE, @@ -40,9 +37,7 @@ typedef enum { MESSAGE_CANCEL } e_message_type; -/** - * Message data - */ +/** Message data */ typedef struct s_message { e_message_type type; const char *mailbox; @@ -53,44 +48,24 @@ typedef struct s_message { int block_index; int block_length; } s_message_t, *message_t; -/** - * Builds a new value-less message - */ -msg_task_t task_message_new(e_message_type type, - const char *issuer_host_name, - const char *mailbox, int peer_id, int size); -/** - * Builds a new "have/piece" message - */ -msg_task_t task_message_index_new(e_message_type type, - const char *issuer_host_name, - const char *mailbox, int peer_id, - int index, int varsize); -/** - * Builds a new bitfield message - */ -msg_task_t task_message_bitfield_new(const char *issuer_host_name, - const char *mailbox, int peer_id, - char *bitfield, int bitfield_size); -/** - * Builds a new "request" message - */ -msg_task_t task_message_request_new(const char *issuer_host_name, - const char *mailbox, int peer_id, - int index, int block_index, - int block_length); -/** - * Build a new "piece" message - */ -msg_task_t task_message_piece_new(const char *issuer_host_name, - const char *mailbox, int peer_id, int index, - int block_index, - int block_length, int block_size); -/** - * Free a message task - */ +/** Builds a new value-less message */ +msg_task_t task_message_new(e_message_type type, const char *issuer_host_name, const char *mailbox, int peer_id, + int size); +/** Builds a new "have/piece" message */ +msg_task_t task_message_index_new(e_message_type type, const char *issuer_host_name, const char *mailbox, int peer_id, + int index, int varsize); +/** Builds a new bitfield message */ +msg_task_t task_message_bitfield_new(const char *issuer_host_name, const char *mailbox, int peer_id, char *bitfield, + int bitfield_size); +/** Builds a new "request" message */ +msg_task_t task_message_request_new(const char *issuer_host_name, const char *mailbox, int peer_id, int index, + int block_index, int block_length); +/** Build a new "piece" message */ +msg_task_t task_message_piece_new(const char *issuer_host_name, const char *mailbox, int peer_id, int index, + int block_index, int block_length, int block_size); +/** Free a message task */ void task_message_free(void *); - int task_message_size(e_message_type type); + #endif /* BITTORRENT_MESSAGES_H_ */ diff --git a/examples/msg/bittorrent/peer.c b/examples/msg/bittorrent/peer.c index fcd1a0ffde..372194bbb7 100644 --- a/examples/msg/bittorrent/peer.c +++ b/examples/msg/bittorrent/peer.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2012-2015. The SimGrid Team. +/* Copyright (c) 2012-2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -26,20 +26,14 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peers, "Messages specific for the peers"); #define BLOCK_SIZE 16384 #define ENABLE_END_GAME_MODE 1 -/** - * Number of blocks asked by each request - */ +/** Number of blocks asked by each request */ #define BLOCKS_REQUESTED 2 - static const unsigned long int FILE_SIZE = FILE_PIECES * PIECES_BLOCKS * BLOCK_SIZE; - #define SLEEP_DURATION 1 - /** - * Peer main function - */ +/** Peer main function */ int peer(int argc, char *argv[]) { s_peer_t peer; @@ -82,10 +76,9 @@ int peer(int argc, char *argv[]) return 0; } -/** - * Peer main loop when it is leeching. - * @param peer peer data - * @param deadline time at which the peer has to leave +/** @brief Peer main loop when it is leeching. + * @param peer peer data + * @param deadline time at which the peer has to leave */ void leech_loop(peer_t peer, double deadline) { @@ -125,10 +118,9 @@ void leech_loop(peer_t peer, double deadline) } -/** - * Peer main loop when it is seeding - * @param peer peer data - * @param deadline time when the peer will leave +/** @brief Peer main loop when it is seeding + * @param peer peer data + * @param deadline time when the peer will leave */ void seed_loop(peer_t peer, double deadline) { @@ -159,26 +151,23 @@ void seed_loop(peer_t peer, double deadline) } } -/** - * Retrieves the peer list from the tracker - * @param peer current peer data +/** @brief Retrieves the peer list from the tracker + * @param peer current peer data */ int get_peers_data(peer_t peer) { int success = 0, send_success = 0; double timeout = MSG_get_clock() + GET_PEERS_TIMEOUT; //Build the task to send to the tracker - tracker_task_data_t data = - tracker_task_data_new(MSG_host_get_name(MSG_host_self()), - peer->mailbox_tracker, peer->id, 0, 0, FILE_SIZE); + tracker_task_data_t data = tracker_task_data_new(MSG_host_get_name(MSG_host_self()), peer->mailbox_tracker, + peer->id, 0, 0, FILE_SIZE); //Build the task to send. msg_task_t task_send = MSG_task_create(NULL, 0, TRACKER_COMM_SIZE, data); msg_task_t task_received = NULL; msg_comm_t comm_received; while (!send_success && MSG_get_clock() < timeout) { XBT_DEBUG("Sending a peer request to the tracker."); - msg_error_t status = MSG_task_send_with_timeout(task_send, TRACKER_MAILBOX, - GET_PEERS_TIMEOUT); + msg_error_t status = MSG_task_send_with_timeout(task_send, TRACKER_MAILBOX, GET_PEERS_TIMEOUT); if (status == MSG_OK) { send_success = 1; } @@ -193,8 +182,7 @@ int get_peers_data(peer_t peer) //Add the peers the tracker gave us to our peer list. xbt_dynar_foreach(data->peers, i, peer_id) { if (peer_id != peer->id) - xbt_dict_set_ext(peer->peers, (char *) &peer_id, sizeof(int), - connection_new(peer_id), NULL); + xbt_dict_set_ext(peer->peers, (char *) &peer_id, sizeof(int), connection_new(peer_id), NULL); } success = 1; //free the communication and the task @@ -208,11 +196,10 @@ int get_peers_data(peer_t peer) return success; } -/** - * Initialize the peer data. - * @param peer peer data - * @param id id of the peer to take in the network - * @param seed indicates if the peer is a seed. +/** @brief Initialize the peer data. + * @param peer peer data + * @param id id of the peer to take in the network + * @param seed indicates if the peer is a seed. */ void peer_init(peer_t peer, int id, int seed) { @@ -227,12 +214,10 @@ void peer_init(peer_t peer, int id, int seed) peer->bitfield_blocks = xbt_new(char, (FILE_PIECES) * (PIECES_BLOCKS) + 1); if (seed) { memset(peer->bitfield, '1', sizeof(char) * (FILE_PIECES + 1)); - memset(peer->bitfield_blocks, '1', - sizeof(char) * FILE_PIECES * (PIECES_BLOCKS)); + memset(peer->bitfield_blocks, '1', sizeof(char) * FILE_PIECES * (PIECES_BLOCKS)); } else { memset(peer->bitfield, '0', sizeof(char) * (FILE_PIECES + 1)); - memset(peer->bitfield_blocks, '0', - sizeof(char) * FILE_PIECES * (PIECES_BLOCKS)); + memset(peer->bitfield_blocks, '0', sizeof(char) * FILE_PIECES * (PIECES_BLOCKS)); } peer->bitfield[FILE_PIECES] = '\0'; @@ -242,17 +227,13 @@ void peer_init(peer_t peer, int id, int seed) peer->current_pieces = xbt_dynar_new(sizeof(int), NULL); - peer->stream = - (RngStream)MSG_host_get_property_value(MSG_host_self(), "stream"); + peer->stream = (RngStream)MSG_host_get_property_value(MSG_host_self(), "stream"); peer->comm_received = NULL; peer->round = 0; - } -/** - * Destroys a poor peer object. - */ +/** Destroys a poor peer object. */ void peer_free(peer_t peer) { char *key; @@ -269,9 +250,8 @@ void peer_free(peer_t peer) xbt_free(peer->bitfield_blocks); } -/** - * Returns if a peer has finished downloading the file - * @param bitfield peer bitfield +/** @brief Returns if a peer has finished downloading the file + * @param bitfield peer bitfield */ int has_finished(char *bitfield) { @@ -291,20 +271,16 @@ int nb_interested_peers(peer_t peer) return nb; } - void update_active_peers_set(peer_t peer, connection_t remote_peer) { - if (remote_peer->interested && !remote_peer->choked_upload) { //add in the active peers set - xbt_dict_set_ext(peer->active_peers, (char *) &remote_peer->id, - sizeof(int), remote_peer, NULL); + xbt_dict_set_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int), remote_peer, NULL); } else { //remove xbt_ex_t e; TRY { - xbt_dict_remove_ext(peer->active_peers, (char *) &remote_peer->id, - sizeof(int)); + xbt_dict_remove_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int)); } CATCH(e) { xbt_ex_free(e); @@ -312,9 +288,7 @@ void update_active_peers_set(peer_t peer, connection_t remote_peer) } } - -/** - * Handle a received message sent by another peer +/** @brief Handle a received message sent by another peer * @param peer Peer data * @param task task received. */ @@ -322,59 +296,46 @@ void handle_message(peer_t peer, msg_task_t task) { message_t message = MSG_task_get_data(task); connection_t remote_peer; - remote_peer = - xbt_dict_get_or_null_ext(peer->peers, (char *) &message->peer_id, - sizeof(int)); + remote_peer = xbt_dict_get_or_null_ext(peer->peers, (char *) &message->peer_id, sizeof(int)); switch (message->type) { - case MESSAGE_HANDSHAKE: - XBT_DEBUG("Received a HANDSHAKE from %s (%s)", message->mailbox, - message->issuer_host_name); + XBT_DEBUG("Received a HANDSHAKE from %s (%s)", message->mailbox, message->issuer_host_name); //Check if the peer is in our connection list. if (!remote_peer) { - xbt_dict_set_ext(peer->peers, (char *) &message->peer_id, sizeof(int), - connection_new(message->peer_id), NULL); + xbt_dict_set_ext(peer->peers, (char *) &message->peer_id, sizeof(int), connection_new(message->peer_id), NULL); send_handshake(peer, message->mailbox); } //Send our bitfield to the peer send_bitfield(peer, message->mailbox); break; case MESSAGE_BITFIELD: - XBT_DEBUG("Recieved a BITFIELD message from %s (%s)", message->mailbox, - message->issuer_host_name); + XBT_DEBUG("Recieved a BITFIELD message from %s (%s)", message->mailbox, message->issuer_host_name); //Update the pieces list update_pieces_count_from_bitfield(peer, message->bitfield); //Store the bitfield remote_peer->bitfield = xbt_strdup(message->bitfield); - xbt_assert(!remote_peer->am_interested, - "Should not be interested at first"); + xbt_assert(!remote_peer->am_interested, "Should not be interested at first"); if (is_interested(peer, remote_peer)) { remote_peer->am_interested = 1; send_interested(peer, message->mailbox); } break; case MESSAGE_INTERESTED: - XBT_DEBUG("Recieved an INTERESTED message from %s (%s)", message->mailbox, - message->issuer_host_name); - xbt_assert((remote_peer != NULL), - "A non-in-our-list peer has sent us a message. WTH ?"); + XBT_DEBUG("Recieved an INTERESTED message from %s (%s)", message->mailbox, message->issuer_host_name); + xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?"); //Update the interested state of the peer. remote_peer->interested = 1; update_active_peers_set(peer, remote_peer); break; case MESSAGE_NOTINTERESTED: - XBT_DEBUG("Received a NOTINTERESTED message from %s (%s)", - message->mailbox, message->issuer_host_name); - xbt_assert((remote_peer != NULL), - "A non-in-our-list peer has sent us a message. WTH ?"); + XBT_DEBUG("Received a NOTINTERESTED message from %s (%s)", message->mailbox, message->issuer_host_name); + xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?"); remote_peer->interested = 0; update_active_peers_set(peer, remote_peer); break; case MESSAGE_UNCHOKE: - xbt_assert((remote_peer != NULL), - "A non-in-our-list peer has sent us a message. WTH ?"); - XBT_DEBUG("Received a UNCHOKE message from %s (%s)", message->mailbox, - message->issuer_host_name); + xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?"); + XBT_DEBUG("Received a UNCHOKE message from %s (%s)", message->mailbox, message->issuer_host_name); xbt_assert(remote_peer->choked_download, "WTF !!!"); remote_peer->choked_download = 0; //Send requests to the peer, since it has unchoked us @@ -382,21 +343,17 @@ void handle_message(peer_t peer, msg_task_t task) request_new_piece_to_peer(peer, remote_peer); break; case MESSAGE_CHOKE: - xbt_assert((remote_peer != NULL), - "A non-in-our-list peer has sent us a message. WTH ?"); - XBT_DEBUG("Received a CHOKE message from %s (%s)", message->mailbox, - message->issuer_host_name); + xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?"); + XBT_DEBUG("Received a CHOKE message from %s (%s)", message->mailbox, message->issuer_host_name); xbt_assert(!remote_peer->choked_download, "WTF !!!"); remote_peer->choked_download = 1; remove_current_piece(peer, remote_peer, remote_peer->current_piece); break; case MESSAGE_HAVE: - XBT_DEBUG("Received a HAVE message from %s (%s) of piece %d", - message->mailbox, message->issuer_host_name, message->index); + XBT_DEBUG("Received a HAVE message from %s (%s) of piece %d", message->mailbox, message->issuer_host_name, + message->index); xbt_assert(remote_peer->bitfield, "bitfield not received"); - xbt_assert((message->index >= 0 - && message->index < FILE_PIECES), - "Wrong HAVE message received"); + xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong HAVE message received"); remote_peer->bitfield[message->index] = '1'; peer->pieces_count[message->index]++; //If the piece is in our pieces, we tell the peer that we are interested. @@ -410,36 +367,30 @@ void handle_message(peer_t peer, msg_task_t task) case MESSAGE_REQUEST: xbt_assert(remote_peer->interested, "WTF !!!"); - xbt_assert((message->index >= 0 - && message->index < FILE_PIECES), "Wrong request received"); + xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong request received"); if (!remote_peer->choked_upload) { - XBT_DEBUG("Received a REQUEST from %s (%s) for %d (%d,%d)", - message->mailbox, message->issuer_host_name, message->index, - message->block_index, - message->block_index + message->block_length); + XBT_DEBUG("Received a REQUEST from %s (%s) for %d (%d,%d)", message->mailbox, message->issuer_host_name, + message->index, message->block_index, message->block_index + message->block_length); if (peer->bitfield[message->index] == '1') { - send_piece(peer, message->mailbox, message->index, - message->block_index, message->block_length); + send_piece(peer, message->mailbox, message->index, message->block_index, message->block_length); } } else { - XBT_DEBUG("Received a REQUEST from %s (%s) for %d but he is choked.", - message->mailbox, message->issuer_host_name, message->peer_id); + XBT_DEBUG("Received a REQUEST from %s (%s) for %d but he is choked.", message->mailbox, + message->issuer_host_name, message->peer_id); } break; case MESSAGE_PIECE: - XBT_DEBUG("Received piece %d (%d,%d) from %s (%s)", message->index, - message->block_index, - message->block_index + message->block_length, - message->mailbox, message->issuer_host_name); + XBT_DEBUG("Received piece %d (%d,%d) from %s (%s)", message->index, message->block_index, + message->block_index + message->block_length, message->mailbox, message->issuer_host_name); xbt_assert(!remote_peer->choked_download, "WTF !!!"); - xbt_assert(remote_peer->am_interested || ENABLE_END_GAME_MODE, "Can't received a piece if I'm not interested wihtout end-game mode! piece (%d) bitfield(%s) remote bitfield(%s)", message->index, peer->bitfield, remote_peer->bitfield); + xbt_assert(remote_peer->am_interested || ENABLE_END_GAME_MODE, + "Can't received a piece if I'm not interested wihtout end-game mode!" + "piece (%d) bitfield(%s) remote bitfield(%s)", message->index, peer->bitfield, remote_peer->bitfield); xbt_assert(remote_peer->choked_download != 1, "Can't received a piece if I'm choked !"); - xbt_assert((message->index >= 0 - && message->index < FILE_PIECES), "Wrong piece received"); + xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong piece received"); //TODO: Execute à computation. if (peer->bitfield[message->index] == '0') { - update_bitfield_blocks(peer, message->index, message->block_index, - message->block_length); + update_bitfield_blocks(peer, message->index, message->block_index, message->block_length); if (piece_complete(peer, message->index)) { //Removing the piece from our piece list remove_current_piece(peer, remote_peer, message->index); @@ -461,24 +412,19 @@ void handle_message(peer_t peer, msg_task_t task) } break; case MESSAGE_CANCEL: - XBT_DEBUG("The received CANCEL from %s (%s)", - message->mailbox, message->issuer_host_name); + XBT_DEBUG("The received CANCEL from %s (%s)", message->mailbox, message->issuer_host_name); break; } //Update the peer speed. if (remote_peer) { - connection_add_speed_value(remote_peer, - 1.0 / (MSG_get_clock() - - peer->begin_receive_time)); + connection_add_speed_value(remote_peer, 1.0 / (MSG_get_clock() - peer->begin_receive_time)); } peer->begin_receive_time = MSG_get_clock(); task_message_free(task); } -/** - * Selects the appropriate piece to download and requests it to the remote_peer - */ +/** Selects the appropriate piece to download and requests it to the remote_peer */ void request_new_piece_to_peer(peer_t peer, connection_t remote_peer) { int piece = select_piece_to_download(peer, remote_peer); @@ -488,11 +434,8 @@ void request_new_piece_to_peer(peer_t peer, connection_t remote_peer) } } -/** - * remove current_piece from the list of currently downloaded pieces. - */ -void remove_current_piece(peer_t peer, connection_t remote_peer, - int current_piece) +/** remove current_piece from the list of currently downloaded pieces. */ +void remove_current_piece(peer_t peer, connection_t remote_peer, int current_piece) { int piece_index = -1, piece; unsigned int i; @@ -507,10 +450,9 @@ void remove_current_piece(peer_t peer, connection_t remote_peer, remote_peer->current_piece = -1; } -/** - * Updates the list of who has a piece from a bitfield - * @param peer peer we want to update the list - * @param bitfield bitfield +/** @brief Updates the list of who has a piece from a bitfield + * @param peer peer we want to update the list + * @param bitfield bitfield */ void update_pieces_count_from_bitfield(peer_t peer, char *bitfield) { @@ -522,15 +464,11 @@ void update_pieces_count_from_bitfield(peer_t peer, char *bitfield) } } - - -/** - * Return the piece to be downloaded +/** @brief Return the piece to be downloaded * There are two cases (as described in "Bittorrent Architecture Protocol", Ryan Toole : * If a piece is partially downloaded, this piece will be selected prioritarily * If the peer has strictly less than 4 pieces, he chooses a piece at random. - * If the peer has more than pieces, he downloads the pieces that are the less - * replicated (rarest policy). + * If the peer has more than pieces, he downloads the pieces that are the less replicated (rarest policy). * If all pieces have been downloaded or requested, we select a random requested piece (endgame mode). * @param peer: local peer * @param remote_peer: information about the connection @@ -546,8 +484,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer) return piece; // end game mode - if (xbt_dynar_length(peer->current_pieces) >= (FILE_PIECES - peer->pieces) - && is_interested(peer, remote_peer)) { + if (xbt_dynar_length(peer->current_pieces) >= (FILE_PIECES - peer->pieces) && is_interested(peer, remote_peer)) { if(!ENABLE_END_GAME_MODE) return -1; int i; @@ -561,8 +498,7 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer) } xbt_assert(nb_interesting_pieces != 0, "WTF !!!"); // get a random interesting piece - random_piece_index = - RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1); + random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1); for (i = 0; i < FILE_PIECES; i++) { if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1') { if (random_piece_index == current_index) { @@ -582,18 +518,15 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer) int random_piece_index, current_index = 0; // compute the number of interesting pieces for (i = 0; i < FILE_PIECES; i++) { - if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' - && !in_current_pieces(peer, i)) { + if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' && !in_current_pieces(peer, i)) { nb_interesting_pieces++; } } xbt_assert(nb_interesting_pieces != 0, "WTF !!!"); // get a random interesting piece - random_piece_index = - RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1); + random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1); for (i = 0; i < FILE_PIECES; i++) { - if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' - && !in_current_pieces(peer, i)) { + if (peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' && !in_current_pieces(peer, i)) { if (random_piece_index == current_index) { piece = i; break; @@ -610,25 +543,23 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer) int random_rarest_index, current_index = 0; // compute the smallest number of copies of available pieces for (i = 0; i < FILE_PIECES; i++) { - if (peer->pieces_count[i] < min && peer->bitfield[i] == '0' - && remote_peer->bitfield[i] == '1' && !in_current_pieces(peer, i)) + if (peer->pieces_count[i] < min && peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' + && !in_current_pieces(peer, i)) min = peer->pieces_count[i]; } - xbt_assert(min != SHRT_MAX - || !is_interested_and_free(peer, remote_peer), "WTF !!!"); + xbt_assert(min != SHRT_MAX || !is_interested_and_free(peer, remote_peer), "WTF !!!"); // compute the number of rarest pieces for (i = 0; i < FILE_PIECES; i++) { - if (peer->pieces_count[i] == min && peer->bitfield[i] == '0' - && remote_peer->bitfield[i] == '1' && !in_current_pieces(peer, i)) + if (peer->pieces_count[i] == min && peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' + && !in_current_pieces(peer, i)) nb_min_pieces++; } - xbt_assert(nb_min_pieces != 0 - || !is_interested_and_free(peer, remote_peer), "WTF !!!"); + xbt_assert(nb_min_pieces != 0 || !is_interested_and_free(peer, remote_peer), "WTF !!!"); // get a random rarest piece random_rarest_index = RngStream_RandInt(peer->stream, 0, nb_min_pieces - 1); for (i = 0; i < FILE_PIECES; i++) { - if (peer->pieces_count[i] == min && peer->bitfield[i] == '0' - && remote_peer->bitfield[i] == '1' && !in_current_pieces(peer, i)) { + if (peer->pieces_count[i] == min && peer->bitfield[i] == '0' && remote_peer->bitfield[i] == '1' + && !in_current_pieces(peer, i)) { if (random_rarest_index == current_index) { piece = i; break; @@ -636,26 +567,19 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer) current_index++; } } - xbt_assert(piece != -1 - || !is_interested_and_free(peer, remote_peer), "WTF !!!"); + xbt_assert(piece != -1 || !is_interested_and_free(peer, remote_peer), "WTF !!!"); return piece; } } - -/** - * Update the list of current choked and unchoked peers, using the - * choke algorithm - * @param peer the current peer +/** @brief Update the list of current choked and unchoked peers, using the choke algorithm + * @param peer the current peer */ void update_choked_peers(peer_t peer) { if (nb_interested_peers(peer) == 0) return; - // if(xbt_dict_size(peer->active_peers) > 0) - // return; - XBT_DEBUG("(%d) update_choked peers %d active peers", peer->id, - xbt_dict_size(peer->active_peers)); + XBT_DEBUG("(%d) update_choked peers %d active peers", peer->id, xbt_dict_size(peer->active_peers)); //update the current round peer->round = (peer->round + 1) % 3; char *key, *key_choked=NULL; @@ -670,17 +594,13 @@ void update_choked_peers(peer_t peer) } xbt_dict_cursor_free(&cursor); - /** - * If we are currently seeding, we unchoke the peer which has - * been unchoke the least time. - */ + /**If we are currently seeding, we unchoke the peer which has been unchoked the last time.*/ if (peer->pieces == FILE_PIECES) { connection_t connection; double unchoke_time = MSG_get_clock() + 1; xbt_dict_foreach(peer->peers, cursor, key, connection) { - if (connection->last_unchoke < unchoke_time && connection->interested - && connection->choked_upload) { + if (connection->last_unchoke < unchoke_time && connection->interested && connection->choked_upload) { unchoke_time = connection->last_unchoke; peer_choosed = connection; } @@ -691,8 +611,7 @@ void update_choked_peers(peer_t peer) int j = 0; do { //We choose a random peer to unchoke. - int id_chosen = RngStream_RandInt(peer->stream, 0, - xbt_dict_length(peer->peers) - 1); + int id_chosen = RngStream_RandInt(peer->stream, 0, xbt_dict_length(peer->peers) - 1); int i = 0; connection_t connection; xbt_dict_foreach(peer->peers, cursor, key, connection) { @@ -713,48 +632,31 @@ void update_choked_peers(peer_t peer) connection_t connection; double fastest_speed = 0.0; xbt_dict_foreach(peer->peers, cursor, key, connection) { - if (connection->peer_speed > fastest_speed - && connection->choked_upload && connection->interested) { + if (connection->peer_speed > fastest_speed && connection->choked_upload && connection->interested) { peer_choosed = connection; fastest_speed = connection->peer_speed; } } } - } - if (peer_choosed != NULL) - XBT_DEBUG - ("(%d) update_choked peers unchoked (%d) ; int (%d) ; choked (%d) ", - peer->id, peer_choosed->id, peer_choosed->interested, - peer_choosed->choked_upload); - - // if (xbt_dict_size(peer->peers) > 0) - // xbt_assert((xbt_dict_size(peer->active_peers) != 0), - // "No more active peers !"); - - // if (peer_choked != NULL && peer_choked->choked_upload != 0) - // peer_choked = NULL; - // if (peer_choosed != NULL && peer_choosed->choked_upload == 0) - // peer_choosed = NULL; + if (peer_choosed != NULL) + XBT_DEBUG("(%d) update_choked peers unchoked (%d) ; int (%d) ; choked (%d) ", + peer->id, peer_choosed->id, peer_choosed->interested, peer_choosed->choked_upload); if (peer_choked != peer_choosed) { if (peer_choked != NULL) { - xbt_assert((!peer_choked->choked_upload), - "Tries to choked a choked peer"); + xbt_assert((!peer_choked->choked_upload), "Tries to choked a choked peer"); peer_choked->choked_upload = 1; xbt_assert((*((int *) key_choked) == peer_choked->id), "WTF !!!"); update_active_peers_set(peer, peer_choked); - // xbt_dict_remove_ext(peer->active_peers, key_choked, sizeof(int)); XBT_DEBUG("(%d) Sending a CHOKE to %d", peer->id, peer_choked->id); send_choked(peer, peer_choked->mailbox); } if (peer_choosed != NULL) { - xbt_assert((peer_choosed->choked_upload), - "Tries to unchoked an unchoked peer"); + xbt_assert((peer_choosed->choked_upload), "Tries to unchoked an unchoked peer"); peer_choosed->choked_upload = 0; - xbt_dict_set_ext(peer->active_peers, (char *) &peer_choosed->id, - sizeof(int), peer_choosed, NULL); + xbt_dict_set_ext(peer->active_peers, (char *) &peer_choosed->id, sizeof(int), peer_choosed, NULL); peer_choosed->last_unchoke = MSG_get_clock(); XBT_DEBUG("(%d) Sending a UNCHOKE to %d", peer->id, peer_choosed->id); update_active_peers_set(peer, peer_choosed); @@ -763,10 +665,8 @@ void update_choked_peers(peer_t peer) } } -/** - * Updates our "interested" state about peers: send "not interested" to peers - * that don't have any more pieces we want. - * @param peer our peer data +/** @brief Update "interested" state of peers: send "not interested" to peers that don't have any more pieces we want. + * @param peer our peer data */ void update_interested_after_receive(peer_t peer) { @@ -794,22 +694,17 @@ void update_interested_after_receive(peer_t peer) } } -void update_bitfield_blocks(peer_t peer, int index, int block_index, - int block_length) +void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_length) { int i; 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((block_index >= 0 && block_index <= PIECES_BLOCKS), "Wrong block : %d.", block_index); for (i = block_index; i < (block_index + block_length); i++) { peer->bitfield_blocks[index * PIECES_BLOCKS + i] = '1'; } } -/** - * Returns if a peer has completed the download of a piece - */ +/** Returns if a peer has completed the download of a piece */ int piece_complete(peer_t peer, int index) { int i; @@ -821,10 +716,7 @@ int piece_complete(peer_t peer, int index) return 1; } -/** - * Returns the first block that a peer doesn't have in a piece. - * If the peer has all blocks of the piece, returns -1. - */ +/** 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_block(peer_t peer, int piece) { int i; @@ -836,9 +728,7 @@ int get_first_block(peer_t peer, int piece) return -1; } -/** - * Indicates if the remote peer has a piece not stored by the local peer - */ +/** Indicates if the remote peer has a piece not stored by the local peer */ int is_interested(peer_t peer, connection_t remote_peer) { xbt_assert(remote_peer->bitfield, "Bitfield not received"); @@ -851,34 +741,26 @@ int is_interested(peer_t peer, connection_t remote_peer) return 0; } -/** - * Indicates if the remote peer has a piece not stored by the local peer nor requested by the local peer - */ +/** 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(peer_t peer, connection_t remote_peer) { xbt_assert(remote_peer->bitfield, "Bitfield not received"); int i; for (i = 0; i < FILE_PIECES; i++) { - if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' - && !in_current_pieces(peer, i)) { + if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' && !in_current_pieces(peer, i)) { return 1; } } return 0; } - -/** - * Returns a piece that is partially downloaded and stored by the remote peer if any - * -1 otherwise. - */ +/** Returns a piece that is partially downloaded and stored by the remote peer if any -1 otherwise. */ int partially_downloaded_piece(peer_t peer, connection_t remote_peer) { xbt_assert(remote_peer->bitfield, "Bitfield not received"); int i; for (i = 0; i < FILE_PIECES; i++) { - if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' - && !in_current_pieces(peer, i)) { + if (remote_peer->bitfield[i] == '1' && peer->bitfield[i] == '0' && !in_current_pieces(peer, i)) { if (get_first_block(peer, i) > 0) return i; } @@ -886,11 +768,9 @@ int partially_downloaded_piece(peer_t peer, connection_t remote_peer) return -1; } - -/** - * Send request messages to a peer that have unchoked us - * @param peer peer - * @param remote_peer peer data to the peer we want to send the request +/** @brief Send request messages to a peer that have unchoked us + * @param peer peer + * @param remote_peer peer data to the peer we want to send the request */ void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece) { @@ -906,10 +786,7 @@ void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece) } } - -/** - * Indicates if a piece is currently being downloaded by the peer. - */ +/** Indicates if a piece is currently being downloaded by the peer. */ int in_current_pieces(peer_t peer, int piece) { unsigned i; @@ -922,9 +799,6 @@ int in_current_pieces(peer_t peer, int piece) return 0; } - - - /*********************************************************** * * Low level message functions @@ -933,39 +807,32 @@ int in_current_pieces(peer_t peer, int piece) -/** - * Send a "interested" message to a peer - * @param peer peer data - * @param mailbox destination mailbox +/** @brief Send a "interested" message to a peer + * @param peer peer data + * @param mailbox destination mailbox */ void send_interested(peer_t peer, const char *mailbox) { - msg_task_t task = - task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox, - peer->id, task_message_size(MESSAGE_INTERESTED)); + msg_task_t task = task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox, peer->id, + task_message_size(MESSAGE_INTERESTED)); MSG_task_dsend(task, mailbox, task_message_free); XBT_DEBUG("Sending INTERESTED to %s", mailbox); - } -/** - * Send a "not interested" message to a peer - * @param peer peer data - * @param mailbox destination mailbox +/** @brief Send a "not interested" message to a peer + * @param peer peer data + * @param mailbox destination mailbox */ void send_notinterested(peer_t peer, const char *mailbox) { - msg_task_t task = - task_message_new(MESSAGE_NOTINTERESTED, peer->hostname, peer->mailbox, - peer->id, task_message_size(MESSAGE_NOTINTERESTED)); + msg_task_t task = task_message_new(MESSAGE_NOTINTERESTED, peer->hostname, peer->mailbox, peer->id, + task_message_size(MESSAGE_NOTINTERESTED)); MSG_task_dsend(task, mailbox, task_message_free); XBT_DEBUG("Sending NOTINTERESTED to %s", mailbox); - } -/** - * Send a handshake message to all the peers the peer has. - * @param peer peer data +/** @brief Send a handshake message to all the peers the peer has. + * @param peer peer data */ void send_handshake_all(peer_t peer) { @@ -973,55 +840,44 @@ void send_handshake_all(peer_t peer) xbt_dict_cursor_t cursor = NULL; char *key; xbt_dict_foreach(peer->peers, cursor, key, remote_peer) { - msg_task_t task = - task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox, - peer->id, task_message_size(MESSAGE_HANDSHAKE)); + msg_task_t task = task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox, peer->id, + task_message_size(MESSAGE_HANDSHAKE)); MSG_task_dsend(task, remote_peer->mailbox, task_message_free); XBT_DEBUG("Sending a HANDSHAKE to %s", remote_peer->mailbox); } } -/** - * Send a "handshake" message to an user - * @param peer peer data - * @param mailbox mailbox where to we send the message +/** @brief Send a "handshake" message to an user + * @param peer peer data + * @param mailbox mailbox where to we send the message */ void send_handshake(peer_t peer, const char *mailbox) { - msg_task_t task = - task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox, - peer->id, task_message_size(MESSAGE_HANDSHAKE)); + msg_task_t task = task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox, peer->id, + task_message_size(MESSAGE_HANDSHAKE)); MSG_task_dsend(task, mailbox, task_message_free); XBT_DEBUG("Sending a HANDSHAKE to %s", mailbox); } -/** - * Send a "choked" message to a peer. - */ +/** Send a "choked" message to a peer. */ void send_choked(peer_t peer, const char *mailbox) { XBT_DEBUG("Sending a CHOKE to %s", mailbox); - msg_task_t task = - task_message_new(MESSAGE_CHOKE, peer->hostname, peer->mailbox, - peer->id, task_message_size(MESSAGE_CHOKE)); + msg_task_t task = task_message_new(MESSAGE_CHOKE, peer->hostname, peer->mailbox, peer->id, + task_message_size(MESSAGE_CHOKE)); MSG_task_dsend(task, mailbox, task_message_free); } -/** - * Send a "unchoked" message to a peer - */ +/** Send a "unchoked" message to a peer */ void send_unchoked(peer_t peer, const char *mailbox) { XBT_DEBUG("Sending a UNCHOKE to %s", mailbox); - msg_task_t task = - task_message_new(MESSAGE_UNCHOKE, peer->hostname, peer->mailbox, - peer->id, task_message_size(MESSAGE_UNCHOKE)); + msg_task_t task = task_message_new(MESSAGE_UNCHOKE, peer->hostname, peer->mailbox, peer->id, + task_message_size(MESSAGE_UNCHOKE)); MSG_task_dsend(task, mailbox, task_message_free); } -/** - * Send a "HAVE" message to all peers we are connected to - */ +/** Send a "HAVE" message to all peers we are connected to */ void send_have(peer_t peer, int piece) { XBT_DEBUG("Sending HAVE message to all my peers"); @@ -1029,54 +885,37 @@ void send_have(peer_t peer, int piece) xbt_dict_cursor_t cursor = NULL; char *key; xbt_dict_foreach(peer->peers, cursor, key, remote_peer) { - msg_task_t task = - task_message_index_new(MESSAGE_HAVE, peer->hostname, peer->mailbox, - peer->id, piece, - task_message_size(MESSAGE_HAVE)); + msg_task_t task = task_message_index_new(MESSAGE_HAVE, peer->hostname, peer->mailbox, peer->id, piece, + task_message_size(MESSAGE_HAVE)); MSG_task_dsend(task, remote_peer->mailbox, task_message_free); } } -/** - * Send a bitfield message to all the peers the peer has. - * @param peer peer data +/** @brief Send a bitfield message to all the peers the peer has. + * @param peer peer data */ void send_bitfield(peer_t peer, const char *mailbox) { XBT_DEBUG("Sending a BITFIELD to %s", mailbox); - msg_task_t task = - task_message_bitfield_new(peer->hostname, peer->mailbox, peer->id, - peer->bitfield, FILE_PIECES); + msg_task_t task = task_message_bitfield_new(peer->hostname, peer->mailbox, peer->id, peer->bitfield, FILE_PIECES); MSG_task_dsend(task, mailbox, task_message_free); } -/** - * Send a "request" message to a pair, containing a request for a piece - */ -void send_request(peer_t peer, const char *mailbox, int piece, - int block_index, int block_length) +/** Send a "request" message to a pair, containing a request for a piece */ +void send_request(peer_t peer, const char *mailbox, int piece, int block_index, int block_length) { - XBT_DEBUG("Sending a REQUEST to %s for piece %d (%d,%d)", mailbox, piece, - block_index, block_length); - msg_task_t task = - task_message_request_new(peer->hostname, peer->mailbox, peer->id, piece, - block_index, block_length); + XBT_DEBUG("Sending a REQUEST to %s for piece %d (%d,%d)", mailbox, piece, block_index, block_length); + msg_task_t task = task_message_request_new(peer->hostname, peer->mailbox, peer->id, piece, block_index, block_length); MSG_task_dsend(task, mailbox, task_message_free); } -/** - * Send a "piece" message to a pair, containing a piece of the file - */ -void send_piece(peer_t peer, const char *mailbox, int piece, - int block_index, int block_length) +/** Send a "piece" message to a pair, containing a piece of the file */ +void send_piece(peer_t peer, const char *mailbox, int piece, int block_index, int block_length) { - XBT_DEBUG("Sending the PIECE %d (%d,%d) to %s", piece, block_index, - block_length, mailbox); + XBT_DEBUG("Sending the PIECE %d (%d,%d) to %s", piece, block_index, block_length, mailbox); xbt_assert(piece >= 0, "Tried to send a piece that doesn't exist."); - xbt_assert((peer->bitfield[piece] == '1'), - "Tried to send a piece that we doesn't have."); - msg_task_t task = - task_message_piece_new(peer->hostname, peer->mailbox, peer->id, piece, - block_index, block_length, BLOCK_SIZE); + xbt_assert((peer->bitfield[piece] == '1'), "Tried to send a piece that we doesn't have."); + msg_task_t task = task_message_piece_new(peer->hostname, peer->mailbox, peer->id, piece, block_index, block_length, + BLOCK_SIZE); MSG_task_dsend(task, mailbox, task_message_free); } diff --git a/examples/msg/bittorrent/peer.h b/examples/msg/bittorrent/peer.h index ce96099c7f..009de84f2c 100644 --- a/examples/msg/bittorrent/peer.h +++ b/examples/msg/bittorrent/peer.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2012-2015. The SimGrid Team. +/* Copyright (c) 2012-2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -13,9 +13,7 @@ #include "connection.h" #include "bittorrent.h" -/** - * Peer data - */ +/** Peer data */ typedef struct s_peer { int id; //peer id @@ -45,9 +43,7 @@ typedef struct s_peer { } s_peer_t, *peer_t; -/** - * Peer main function - */ +/** Peer main function */ int peer(int argc, char *argv[]); int get_peers_data(peer_t peer); @@ -66,8 +62,7 @@ void update_choked_peers(peer_t peer); void update_interested_after_receive(peer_t peer); -void update_bitfield_blocks(peer_t peer, int index, int block_index, - int block_length); +void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_length); int piece_complete(peer_t peer, int index); int get_first_block(peer_t peer, int piece); @@ -80,8 +75,7 @@ int partially_downloaded_piece(peer_t peer, connection_t remote_peer); void request_new_piece_to_peer(peer_t peer, connection_t remote_peer); void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece); -void remove_current_piece(peer_t peer, connection_t remote_peer, - int current_piece); +void remove_current_piece(peer_t peer, connection_t remote_peer, int current_piece); void update_active_peers_set(peer_t peer, connection_t remote_peer); int select_piece_to_download(peer_t peer, connection_t remote_peer); @@ -97,9 +91,7 @@ void send_choked(peer_t peer, const char *mailbox); void send_unchoked(peer_t peer, const char *mailbox); void send_have(peer_t peer, int piece); -void send_request(peer_t peer, const char *mailbox, int piece, - int block_index, int block_length); -void send_piece(peer_t peer, const char *mailbox, int piece, - int block_index, int block_length); +void send_request(peer_t peer, const char *mailbox, int piece, int block_index, int block_length); +void send_piece(peer_t peer, const char *mailbox, int piece, int block_index, int block_length); #endif /* BITTORRENT_PEER_H */ -- 2.20.1