From: Frederic Suter Date: Sun, 6 Aug 2017 19:39:58 +0000 (+0200) Subject: mv MSG version of bittorrent to teshsuite X-Git-Tag: v3_17~241 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/fad318e69b9130ad51b5bcff09362b1f162f5270?hp=-c mv MSG version of bittorrent to teshsuite --- fad318e69b9130ad51b5bcff09362b1f162f5270 diff --git a/examples/msg/CMakeLists.txt b/examples/msg/CMakeLists.txt index ca06ab37ba..fc994e39e4 100644 --- a/examples/msg/CMakeLists.txt +++ b/examples/msg/CMakeLists.txt @@ -30,13 +30,6 @@ if(SIMGRID_HAVE_NS3) set_target_properties(network-ns3 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/network-ns3) endif() -add_executable (bittorrent app-bittorrent/bittorrent.c app-bittorrent/messages.c app-bittorrent/peer.c app-bittorrent/tracker.c app-bittorrent/connection.c) -target_link_libraries(bittorrent simgrid) -set_target_properties(bittorrent PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/app-bittorrent) -foreach (file bittorrent connection messages peer tracker) - set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/${file}.c ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/${file}.h) -endforeach() - add_executable (chainsend app-chainsend/chainsend.c app-chainsend/iterator.c app-chainsend/common.c app-chainsend/messages.c app-chainsend/broadcaster.c app-chainsend/peer.c) target_link_libraries(chainsend simgrid) set_target_properties(chainsend PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/app-chainsend) @@ -51,21 +44,19 @@ foreach (file answer dht-kademlia node routing_table task) set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/dht-kademlia/${file}.c ${CMAKE_CURRENT_SOURCE_DIR}/dht-kademlia/${file}.h) endforeach() -foreach (file app-bittorrent app-chainsend app-masterworker app-pingpong async-wait +foreach (file app-chainsend app-masterworker app-pingpong async-wait async-waitall async-waitany dht-kademlia dht-pastry io-remote platform-properties process-yield task-priority) set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/${file}/${file}_d.xml) endforeach() set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/README.doc PARENT_SCOPE) -set(bin_files ${bin_files} ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/generate.py - ${CMAKE_CURRENT_SOURCE_DIR}/dht-kademlia/generate.py +set(bin_files ${bin_files} ${CMAKE_CURRENT_SOURCE_DIR}/dht-kademlia/generate.py ${CMAKE_CURRENT_SOURCE_DIR}/dht-pastry/generate.py PARENT_SCOPE) set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/app-chainsend/chainsend.c ${CMAKE_CURRENT_SOURCE_DIR}/dht-kademlia/common.h ${CMAKE_CURRENT_SOURCE_DIR}/network-ns3/network-ns3.c PARENT_SCOPE) -set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/app-bittorrent.tesh - ${CMAKE_CURRENT_SOURCE_DIR}/app-chainsend/app-chainsend.tesh +set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/app-chainsend/app-chainsend.tesh ${CMAKE_CURRENT_SOURCE_DIR}/dht-kademlia/dht-kademlia.tesh ${CMAKE_CURRENT_SOURCE_DIR}/app-masterworker/app-masterworker-multicore.tesh ${CMAKE_CURRENT_SOURCE_DIR}/app-masterworker/app-masterworker-vivaldi.tesh @@ -89,7 +80,7 @@ set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/app-masterworker/a ${CMAKE_CURRENT_SOURCE_DIR}/process-startkilltime/start_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/process-startkilltime/start_kill_d.xml PARENT_SCOPE) -foreach(x app-bittorrent app-chainsend app-masterworker app-pingpong app-token-ring +foreach(x app-chainsend app-masterworker app-pingpong app-token-ring async-wait async-waitall async-waitany cloud-capping cloud-masterworker cloud-migration cloud-simple cloud-two-tasks dht-pastry dht-kademlia platform-failures io-file io-remote io-storage task-priority process-create process-daemon process-join process-kill process-migration process-startkilltime process-suspend process-yield @@ -105,7 +96,6 @@ foreach (x categories route-user-variables link-user-variables masterworker plat ADD_TESH(msg-trace-${x} --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/trace-${x} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/trace-${x} trace-${x}.tesh) endforeach() -ADD_TESH_FACTORIES(msg-app-bittorrent-parallel "thread;ucontext;raw" --cfg contexts/nthreads:4 ${CONTEXTS_SYNCHRO} --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/app-bittorrent --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/app-bittorrent app-bittorrent.tesh) ADD_TESH_FACTORIES(msg-dht-kademlia-parallel "thread;ucontext;raw" --cfg contexts/nthreads:4 ${CONTEXTS_SYNCHRO} --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/dht-kademlia --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/dht-kademlia dht-kademlia.tesh) ADD_TESH_FACTORIES(msg-energy-pstate-ptask "thread;ucontext;raw;boost" --cfg host/model:ptask_L07 --log xbt_cfg.threshold:critical --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/msg --cd ${CMAKE_BINARY_DIR}/examples/msg ${CMAKE_HOME_DIRECTORY}/examples/msg/energy-pstate/energy-pstate.tesh) ADD_TESH_FACTORIES(msg-energy-consumption-ptask "thread;ucontext;raw;boost" --cfg host/model:ptask_L07 --log xbt_cfg.threshold:critical --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/msg --cd ${CMAKE_BINARY_DIR}/examples/msg ${CMAKE_HOME_DIRECTORY}/examples/msg/energy-consumption/energy-consumption.tesh) diff --git a/examples/msg/app-bittorrent/messages.c b/examples/msg/app-bittorrent/messages.c deleted file mode 100644 index c3536926c8..0000000000 --- a/examples/msg/app-bittorrent/messages.c +++ /dev/null @@ -1,118 +0,0 @@ -/* Copyright (c) 2012-2014. 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 "messages.h" -#include "bittorrent.h" - -XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, "Messages specific for the message factory"); - -#define BITS_TO_BYTES(x) (((x) / 8 + (x) % 8) ? 1 : 0) - -/** @brief Build a new empty message - * @param type type of the message - * @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) -{ - message_t message = xbt_new(s_message_t, 1); - message->issuer_host_name = issuer_host_name; - message->peer_id = peer_id; - message->mailbox = mailbox; - message->type = type; - msg_task_t task = MSG_task_create(NULL, 0, size, message); - XBT_DEBUG("type: %d size: %d", (int) type, size); - 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, - int index, int 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, - unsigned int bitfield, int 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 = 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 = 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; - return task; -} - -void task_message_free(void *task) -{ - message_t message = MSG_task_get_data(task); - xbt_free(message); - MSG_task_destroy(task); -} - -int task_message_size(e_message_type type) -{ - int size = 0; - switch (type) { - case MESSAGE_HANDSHAKE: - size = MESSAGE_HANDSHAKE_SIZE; - break; - case MESSAGE_CHOKE: - size = MESSAGE_CHOKE_SIZE; - break; - case MESSAGE_UNCHOKE: - size = MESSAGE_UNCHOKE_SIZE; - break; - case MESSAGE_INTERESTED: - case MESSAGE_NOTINTERESTED: - size = MESSAGE_INTERESTED_SIZE; - break; - case MESSAGE_HAVE: - size = MESSAGE_HAVE_SIZE; - break; - case MESSAGE_BITFIELD: - size = MESSAGE_BITFIELD_SIZE; - break; - case MESSAGE_REQUEST: - size = MESSAGE_REQUEST_SIZE; - break; - case MESSAGE_PIECE: - size = MESSAGE_PIECE_SIZE; - break; - case MESSAGE_CANCEL: - size = MESSAGE_CANCEL_SIZE; - break; - default: - THROW_IMPOSSIBLE; - } - return size; -} diff --git a/examples/msg/app-bittorrent/tracker.h b/examples/msg/app-bittorrent/tracker.h deleted file mode 100644 index 40e7d9da65..0000000000 --- a/examples/msg/app-bittorrent/tracker.h +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright (c) 2012-2014. 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. */ - -#ifndef BITTORRENT_TRACKER_H_ -#define BITTORRENT_TRACKER_H_ -#include -#include "bittorrent.h" -/** - * Tracker main function - */ -int tracker(int argc, char *argv[]); -/** - * Task types exchanged between a node and the tracker - */ -typedef enum { - TRACKER_TASK_QUERY, - TRACKER_TASK_ANSWER -} e_tracker_task_type_t; -/** - * Tasks exchanged between a tracker and peers. - */ -typedef struct s_tracker_task_data { - e_tracker_task_type_t type; //type of the task - const char *mailbox; //mailbox where the tracker should answer - const char *issuer_host_name; //hostname, for debug purposes - //Query data - int peer_id; //peer id - int uploaded; //how much the peer has already uploaded - int downloaded; //how much the peer has downloaded - int left; //how much the peer has left - //Answer data - int interval; //how often the peer should contact the tracker (unused for now) - xbt_dynar_t peers; //the peer list the peer has asked for. -} s_tracker_task_data_t; -typedef s_tracker_task_data_t *tracker_task_data_t; - -tracker_task_data_t tracker_task_data_new(const char *issuer_host_name, const char *mailbox, int peer_id, - int uploaded, int downloaded, int left); -void tracker_task_data_free(tracker_task_data_t task); - -int is_in_list(xbt_dynar_t peers, int id); -#endif /* BITTORRENT_TRACKER_H */ diff --git a/examples/s4u/app-bittorrent/s4u_peer.cpp b/examples/s4u/app-bittorrent/s4u_peer.cpp index ba68a23e65..77e04c4655 100644 --- a/examples/s4u/app-bittorrent/s4u_peer.cpp +++ b/examples/s4u/app-bittorrent/s4u_peer.cpp @@ -348,21 +348,18 @@ void Peer::handleMessage() } break; case MESSAGE_INTERESTED: - xbt_assert((remote_peer != nullptr), - "The impossible did happened: A non-in-our-list peer has sent us a message."); + xbt_assert((remote_peer != nullptr), "The impossible did happened: A not-in-our-list peer sent us a message."); // Update the interested state of the peer. remote_peer->interested = true; updateActivePeersSet(remote_peer); break; case MESSAGE_NOTINTERESTED: - xbt_assert((remote_peer != nullptr), - "The impossible did happened: A non-in-our-list peer has sent us a message."); + xbt_assert((remote_peer != nullptr), "The impossible did happened: A not-in-our-list peer sent us a message."); remote_peer->interested = false; updateActivePeersSet(remote_peer); break; case MESSAGE_UNCHOKE: - xbt_assert((remote_peer != nullptr), - "The impossible did happened: A non-in-our-list peer has sent us a message."); + xbt_assert((remote_peer != nullptr), "The impossible did happened: A not-in-our-list peer sent us a message."); xbt_assert(remote_peer->choked_download); remote_peer->choked_download = false; // Send requests to the peer, since it has unchoked us @@ -370,8 +367,7 @@ void Peer::handleMessage() requestNewPieceTo(remote_peer); break; case MESSAGE_CHOKE: - xbt_assert((remote_peer != nullptr), - "The impossible did happened: A non-in-our-list peer has sent us a message."); + xbt_assert((remote_peer != nullptr), "The impossible did happened: A not-in-our-list peer sent us a message."); xbt_assert(not remote_peer->choked_download); remote_peer->choked_download = true; if (remote_peer->current_piece != -1) diff --git a/teshsuite/msg/CMakeLists.txt b/teshsuite/msg/CMakeLists.txt index 8203287fac..be9c437bdb 100644 --- a/teshsuite/msg/CMakeLists.txt +++ b/teshsuite/msg/CMakeLists.txt @@ -19,8 +19,17 @@ foreach(x task_destroy_cancel task_listen_from) set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.cpp) endforeach() +add_executable (bittorrent app-bittorrent/bittorrent.c app-bittorrent/messages.c app-bittorrent/peer.c app-bittorrent/tracker.c app-bittorrent/connection.c) +target_link_libraries(bittorrent simgrid) +set_target_properties(bittorrent PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/app-bittorrent) +foreach (file bittorrent connection messages peer tracker) + set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/${file}.c ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/${file}.h) +endforeach() + + set(teshsuite_src ${teshsuite_src} PARENT_SCOPE) -set(tesh_files ${tesh_files} PARENT_SCOPE) +set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/app-bittorrent.tesh PARENT_SCOPE) +set(bin_files ${bin_files} ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/generate.py PARENT_SCOPE) set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/actions-comm.txt ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/actions-comm_split_p0.txt ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/actions-comm_split_p1.txt @@ -28,7 +37,8 @@ set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/acti set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/actions-comm_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/actions-comm_split_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/actions-storage/actions-storage_d.xml - ${CMAKE_CURRENT_SOURCE_DIR}/trace_integration/test-hbp1.0-hbp1.0-hbp1.0.xml + ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/app-bittorrent_d.xml + ${CMAKE_CURRENT_SOURCE_DIR}/trace_integration/test-hbp1.0-hbp1.0-hbp1.0.xml ${CMAKE_CURRENT_SOURCE_DIR}/trace_integration/test-hbp1.0-hbp3.0-hbp4.0.xml ${CMAKE_CURRENT_SOURCE_DIR}/trace_integration/test-hbp1.5-hbp1.5.xml ${CMAKE_CURRENT_SOURCE_DIR}/trace_integration/test-hbp1-c0s0-c0s1.xml @@ -45,7 +55,7 @@ foreach(x get_sender host_on_off host_on_off_processes host_on_off_recv task_des ADD_TESH_FACTORIES(tesh-msg-${x} "thread;boost;ucontext;raw" --setenv srcdir=${CMAKE_HOME_DIRECTORY}/teshsuite/msg/${x} --cd ${CMAKE_BINARY_DIR}/teshsuite/msg/${x} ${CMAKE_HOME_DIRECTORY}/teshsuite/msg/${x}/${x}.tesh) endforeach() -foreach(x actions-comm actions-storage) +foreach(x actions-comm actions-storage app-bittorrent) ADD_TESH_FACTORIES(tesh-msg-${x} "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/msg/${x} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/msg/${x} ${x}.tesh) endforeach() # One context factory is enough for these ones @@ -53,3 +63,5 @@ endforeach() foreach(x cloud-sharing) ADD_TESH(tesh-msg-${x} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/teshsuite/msg/${x} --cd ${CMAKE_BINARY_DIR}/teshsuite/msg/${x} ${CMAKE_HOME_DIRECTORY}/teshsuite/msg/${x}/${x}.tesh) endforeach() + +ADD_TESH_FACTORIES(tesh-app-bittorrent-parallel "thread;ucontext;raw" --cfg contexts/nthreads:4 ${CONTEXTS_SYNCHRO} --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/msg/app-bittorrent --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/msg/app-bittorrent app-bittorrent.tesh) diff --git a/examples/msg/app-bittorrent/app-bittorrent.tesh b/teshsuite/msg/app-bittorrent/app-bittorrent.tesh similarity index 88% rename from examples/msg/app-bittorrent/app-bittorrent.tesh rename to teshsuite/msg/app-bittorrent/app-bittorrent.tesh index dbb9cbccdb..262fce3ae9 100644 --- a/examples/msg/app-bittorrent/app-bittorrent.tesh +++ b/teshsuite/msg/app-bittorrent/app-bittorrent.tesh @@ -4,7 +4,7 @@ p Testing the Bittorrent implementation with MSG ! timeout 10 ! output sort 19 -$ $SG_TEST_EXENV ${bindir:=.}/bittorrent ${srcdir:=.}/cluster.xml ${srcdir:=.}/../msg/app-bittorrent/app-bittorrent_d.xml "--log=root.fmt:[%12.6r]%e(%i:%P@%h)%e%m%n" +$ ${bindir:=.}/bittorrent ${srcdir:=.}/cluster.xml app-bittorrent_d.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 diff --git a/examples/msg/app-bittorrent/app-bittorrent_d.xml b/teshsuite/msg/app-bittorrent/app-bittorrent_d.xml similarity index 100% rename from examples/msg/app-bittorrent/app-bittorrent_d.xml rename to teshsuite/msg/app-bittorrent/app-bittorrent_d.xml diff --git a/examples/msg/app-bittorrent/bittorrent.c b/teshsuite/msg/app-bittorrent/bittorrent.c similarity index 85% rename from examples/msg/app-bittorrent/bittorrent.c rename to teshsuite/msg/app-bittorrent/bittorrent.c index c38162ad2d..93b8e0e183 100644 --- a/examples/msg/app-bittorrent/bittorrent.c +++ b/teshsuite/msg/app-bittorrent/bittorrent.c @@ -11,7 +11,7 @@ #include /** Bittorrent example launcher */ -int main(int argc, char *argv[]) +int main(int argc, char* argv[]) { msg_host_t host; unsigned i; @@ -19,12 +19,12 @@ int main(int argc, char *argv[]) MSG_init(&argc, argv); /* Check the arguments */ - xbt_assert (argc > 2, "Usage: %s platform_file deployment_file", argv[0]); + xbt_assert(argc > 2, "Usage: %s platform_file deployment_file", argv[0]); MSG_create_environment(argv[1]); xbt_dynar_t host_list = MSG_hosts_as_dynar(); - xbt_dynar_foreach(host_list, i, host) { + xbt_dynar_foreach (host_list, i, host) { char descr[512]; snprintf(descr, sizeof descr, "RngSream<%s>", MSG_host_get_name(host)); RngStream stream = RngStream_CreateStream(descr); @@ -38,7 +38,7 @@ int main(int argc, char *argv[]) MSG_main(); - xbt_dynar_foreach(host_list, i, host) { + xbt_dynar_foreach (host_list, i, host) { RngStream stream = (RngStream)MSG_host_get_data(host); RngStream_DeleteStream(&stream); MSG_host_set_data(host, NULL); diff --git a/examples/msg/app-bittorrent/bittorrent.h b/teshsuite/msg/app-bittorrent/bittorrent.h similarity index 94% rename from examples/msg/app-bittorrent/bittorrent.h rename to teshsuite/msg/app-bittorrent/bittorrent.h index 62d4172e9e..98fcbabf34 100644 --- a/examples/msg/app-bittorrent/bittorrent.h +++ b/teshsuite/msg/app-bittorrent/bittorrent.h @@ -25,4 +25,4 @@ /** Number of pieces the peer asks for simultaneously */ #define MAX_PIECES 1 -#endif /* BITTORRENT_BITTORRENT_H_ */ +#endif /* BITTORRENT_BITTORRENT_H_ */ diff --git a/examples/msg/app-bittorrent/connection.c b/teshsuite/msg/app-bittorrent/connection.c similarity index 63% rename from examples/msg/app-bittorrent/connection.c rename to teshsuite/msg/app-bittorrent/connection.c index 43140e6fb8..3a53ed9cf6 100644 --- a/examples/msg/app-bittorrent/connection.c +++ b/teshsuite/msg/app-bittorrent/connection.c @@ -13,16 +13,16 @@ connection_t connection_new(int id) { connection_t connection = xbt_new(s_connection_t, 1); - connection->id = id; - connection->mailbox = bprintf("%d", id); - connection->bitfield = 0; - connection->current_piece = -1; - connection->interested = 0; - connection->am_interested = 0; - connection->choked_upload = 1; + connection->id = id; + connection->mailbox = bprintf("%d", id); + connection->bitfield = 0; + connection->current_piece = -1; + connection->interested = 0; + connection->am_interested = 0; + connection->choked_upload = 1; connection->choked_download = 1; - connection->peer_speed = 0; - connection->last_unchoke = 0; + connection->peer_speed = 0; + connection->last_unchoke = 0; return connection; } @@ -32,13 +32,14 @@ void connection_add_speed_value(connection_t connection, double speed) connection->peer_speed = connection->peer_speed * 0.6 + speed * 0.4; } -void connection_free(void *data) +void connection_free(void* data) { - connection_t co = (connection_t) data; + connection_t co = (connection_t)data; xbt_free(co->mailbox); xbt_free(co); } -int connection_has_piece(connection_t connection, unsigned int piece){ - return (connection->bitfield & 1U<bitfield & 1U << piece); } diff --git a/examples/msg/app-bittorrent/connection.h b/teshsuite/msg/app-bittorrent/connection.h similarity index 62% rename from examples/msg/app-bittorrent/connection.h rename to teshsuite/msg/app-bittorrent/connection.h index cd3362014f..2b64528b7e 100644 --- a/examples/msg/app-bittorrent/connection.h +++ b/teshsuite/msg/app-bittorrent/connection.h @@ -9,20 +9,20 @@ /** Contains the connection data of a peer. */ typedef struct s_connection { - int id; //Peer id - unsigned int bitfield; //Fields - char *mailbox; + int id; // Peer id + unsigned int bitfield; // Fields + char* mailbox; int messages_count; double peer_speed; double last_unchoke; int current_piece; - unsigned int am_interested:1; //Indicates if we are interested in something the peer has - unsigned int interested:1; //Indicates if the peer is interested in one of our pieces - unsigned int choked_upload:1; //Indicates if the peer is choked for the current peer - unsigned int choked_download:1; //Indicates if the peer has choked the current peer + unsigned int am_interested : 1; // Indicates if we are interested in something the peer has + unsigned int interested : 1; // Indicates if the peer is interested in one of our pieces + unsigned int choked_upload : 1; // Indicates if the peer is choked for the current peer + unsigned int choked_download : 1; // Indicates if the peer has choked the current peer } s_connection_t; -typedef s_connection_t *connection_t; +typedef s_connection_t* connection_t; /** @brief Build a new connection object from the peer id. * @param id id of the peer @@ -34,6 +34,6 @@ connection_t connection_new(int id); */ void connection_add_speed_value(connection_t connection, double speed); /** Frees a connection object */ -void connection_free(void *data); +void connection_free(void* data); int connection_has_piece(connection_t connection, unsigned int piece); -#endif /* BITTORRENT_CONNECTION_H_ */ +#endif /* BITTORRENT_CONNECTION_H_ */ diff --git a/examples/msg/app-bittorrent/generate.py b/teshsuite/msg/app-bittorrent/generate.py similarity index 100% rename from examples/msg/app-bittorrent/generate.py rename to teshsuite/msg/app-bittorrent/generate.py diff --git a/teshsuite/msg/app-bittorrent/messages.c b/teshsuite/msg/app-bittorrent/messages.c new file mode 100644 index 0000000000..671c289215 --- /dev/null +++ b/teshsuite/msg/app-bittorrent/messages.c @@ -0,0 +1,118 @@ +/* Copyright (c) 2012-2014. 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 "messages.h" +#include "bittorrent.h" + +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_messages, "Messages specific for the message factory"); + +#define BITS_TO_BYTES(x) (((x) / 8 + (x) % 8) ? 1 : 0) + +/** @brief Build a new empty message + * @param type type of the message + * @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) +{ + message_t message = xbt_new(s_message_t, 1); + message->issuer_host_name = issuer_host_name; + message->peer_id = peer_id; + message->mailbox = mailbox; + message->type = type; + msg_task_t task = MSG_task_create(NULL, 0, size, message); + XBT_DEBUG("type: %d size: %d", (int)type, size); + 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, + int index, int 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, + unsigned int bitfield, int 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 = 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 = + 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; + return task; +} + +void task_message_free(void* task) +{ + message_t message = MSG_task_get_data(task); + xbt_free(message); + MSG_task_destroy(task); +} + +int task_message_size(e_message_type type) +{ + int size = 0; + switch (type) { + case MESSAGE_HANDSHAKE: + size = MESSAGE_HANDSHAKE_SIZE; + break; + case MESSAGE_CHOKE: + size = MESSAGE_CHOKE_SIZE; + break; + case MESSAGE_UNCHOKE: + size = MESSAGE_UNCHOKE_SIZE; + break; + case MESSAGE_INTERESTED: + case MESSAGE_NOTINTERESTED: + size = MESSAGE_INTERESTED_SIZE; + break; + case MESSAGE_HAVE: + size = MESSAGE_HAVE_SIZE; + break; + case MESSAGE_BITFIELD: + size = MESSAGE_BITFIELD_SIZE; + break; + case MESSAGE_REQUEST: + size = MESSAGE_REQUEST_SIZE; + break; + case MESSAGE_PIECE: + size = MESSAGE_PIECE_SIZE; + break; + case MESSAGE_CANCEL: + size = MESSAGE_CANCEL_SIZE; + break; + default: + THROW_IMPOSSIBLE; + } + return size; +} diff --git a/examples/msg/app-bittorrent/messages.h b/teshsuite/msg/app-bittorrent/messages.h similarity index 74% rename from examples/msg/app-bittorrent/messages.h rename to teshsuite/msg/app-bittorrent/messages.h index 66129d984b..a86649b411 100644 --- a/examples/msg/app-bittorrent/messages.h +++ b/teshsuite/msg/app-bittorrent/messages.h @@ -40,33 +40,33 @@ typedef enum { /** Message data */ typedef struct s_message { e_message_type type; - const char *mailbox; - const char *issuer_host_name; + const char* mailbox; + const char* issuer_host_name; int peer_id; unsigned int bitfield; int index; int block_index; int block_length; } s_message_t; -typedef s_message_t *message_t; +typedef 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, +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, +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, +msg_task_t task_message_bitfield_new(const char* issuer_host_name, const char* mailbox, int peer_id, unsigned int 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, +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, +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 *); +void task_message_free(void*); int task_message_size(e_message_type type); -#endif /* BITTORRENT_MESSAGES_H_ */ +#endif /* BITTORRENT_MESSAGES_H_ */ diff --git a/examples/msg/app-bittorrent/peer.c b/teshsuite/msg/app-bittorrent/peer.c similarity index 64% rename from examples/msg/app-bittorrent/peer.c rename to teshsuite/msg/app-bittorrent/peer.c index 23b78ef9e5..69d75df59b 100644 --- a/examples/msg/app-bittorrent/peer.c +++ b/teshsuite/msg/app-bittorrent/peer.c @@ -4,12 +4,12 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "peer.h" -#include "tracker.h" #include "connection.h" #include "messages.h" +#include "tracker.h" +#include #include #include -#include XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peers, "Messages specific for the peers"); @@ -19,7 +19,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peers, "Messages specific for the peers"); */ #define FILE_PIECES 10UL #define PIECES_BLOCKS 5UL -#define BLOCK_SIZE 16384 +#define BLOCK_SIZE 16384 static const unsigned long int FILE_SIZE = FILE_PIECES * PIECES_BLOCKS * BLOCK_SIZE; /** Number of blocks asked by each request */ @@ -28,48 +28,52 @@ static const unsigned long int FILE_SIZE = FILE_PIECES * PIECES_BLOCKS * BLOCK_S #define ENABLE_END_GAME_MODE 1 #define SLEEP_DURATION 1 -int count_pieces(unsigned int bitfield){ - int count=0; +int count_pieces(unsigned int bitfield) +{ + int count = 0; unsigned int n = bitfield; - while (n){ + while (n) { count += n & 1U; - n >>= 1U ; + n >>= 1U; } return count; } -int peer_has_not_piece(peer_t peer, unsigned int piece){ - return !(peer->bitfield & 1U<bitfield & 1U << piece); } /** Check that a piece is not currently being download by the peer. */ -int peer_is_not_downloading_piece(peer_t peer, unsigned int piece){ - return !(peer->current_pieces & 1U<current_pieces & 1U << piece); } -void get_status(char **status, unsigned int bitfield){ - for(int i=FILE_PIECES-1; i>=0; i--) +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'; } /** Peer main function */ -int peer(int argc, char *argv[]) +int peer(int argc, char* argv[]) { - //Check arguments + // Check arguments xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments"); - //Build peer object - peer_t peer = peer_init(xbt_str_parse_int(argv[1],"Invalid ID: %s"), argc==4 ? 1:0); + // Build peer object + peer_t peer = peer_init(xbt_str_parse_int(argv[1], "Invalid ID: %s"), argc == 4 ? 1 : 0); - //Retrieve deadline - double deadline = xbt_str_parse_double(argv[2],"Invalid deadline: %s"); + // Retrieve deadline + double deadline = xbt_str_parse_double(argv[2], "Invalid deadline: %s"); xbt_assert(deadline > 0, "Wrong deadline supplied"); - char *status = xbt_malloc0(FILE_PIECES+1); + char* status = xbt_malloc0(FILE_PIECES + 1); get_status(&status, peer->bitfield); XBT_INFO("Hi, I'm joining the network with id %d", peer->id); - //Getting peer data from the tracker. + // Getting peer data from the tracker. if (get_peers_data(peer)) { XBT_DEBUG("Got %d peers from the tracker. Current status is: %s", xbt_dict_length(peer->peers), status); peer->begin_receive_time = MSG_get_clock(); @@ -121,7 +125,7 @@ void leech_loop(peer_t peer, double deadline) handle_message(peer, peer->task_received); } } else { - //We don't execute the choke algorithm if we don't already have a piece + // We don't execute the choke algorithm if we don't already have a piece if (MSG_get_clock() >= next_choked_update && count_pieces(peer->bitfield) > 0) { update_choked_peers(peer); next_choked_update += UPDATE_CHOKED_INTERVAL; @@ -142,7 +146,7 @@ void seed_loop(peer_t peer, double deadline) { double next_choked_update = MSG_get_clock() + UPDATE_CHOKED_INTERVAL; XBT_DEBUG("Start seeding."); - //start the main seed loop + // start the main seed loop while (MSG_get_clock() < deadline) { if (peer->comm_received == NULL) { peer->task_received = NULL; @@ -158,7 +162,7 @@ void seed_loop(peer_t peer, double deadline) } else { if (MSG_get_clock() >= next_choked_update) { update_choked_peers(peer); - //TODO: Change the choked peer algorithm when seeding. + // TODO: Change the choked peer algorithm when seeding. next_choked_update += UPDATE_CHOKED_INTERVAL; } else { MSG_process_sleep(SLEEP_DURATION); @@ -172,12 +176,12 @@ void seed_loop(peer_t peer, double deadline) */ int get_peers_data(peer_t peer) { - int success = 0; + int 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); + // 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); msg_task_t task_send = MSG_task_create(NULL, 0, TRACKER_COMM_SIZE, data); while ((success == 0) && MSG_get_clock() < timeout) { XBT_DEBUG("Sending a peer request to the tracker."); @@ -187,22 +191,22 @@ int get_peers_data(peer_t peer) } } - success = 0; + success = 0; msg_task_t task_received = NULL; while ((success == 0) && MSG_get_clock() < timeout) { msg_comm_t comm_received = MSG_task_irecv(&task_received, peer->mailbox_tracker); - msg_error_t status = MSG_comm_wait(comm_received, GET_PEERS_TIMEOUT); + msg_error_t status = MSG_comm_wait(comm_received, GET_PEERS_TIMEOUT); if (status == MSG_OK) { tracker_task_data_t data = MSG_task_get_data(task_received); unsigned i; int peer_id; - //Add the peers the tracker gave us to our peer list. - xbt_dynar_foreach(data->peers, i, peer_id) { + // 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 + // free the communication and the task MSG_comm_destroy(comm_received); tracker_task_data_free(data); MSG_task_destroy(task_received); @@ -219,20 +223,20 @@ int get_peers_data(peer_t peer) */ peer_t peer_init(int id, int seed) { - peer_t peer = xbt_new(s_peer_t,1); - peer->id = id; + peer_t peer = xbt_new(s_peer_t, 1); + peer->id = id; peer->hostname = MSG_host_get_name(MSG_host_self()); - snprintf(peer->mailbox,MAILBOX_SIZE-1, "%d", id); - snprintf(peer->mailbox_tracker,MAILBOX_SIZE-1, "tracker_%d", id); + snprintf(peer->mailbox, MAILBOX_SIZE - 1, "%d", id); + snprintf(peer->mailbox_tracker, MAILBOX_SIZE - 1, "tracker_%d", id); peer->peers = xbt_dict_new_homogeneous(NULL); peer->active_peers = xbt_dict_new_homogeneous(NULL); if (seed) { - peer->bitfield = (1U<bitfield_blocks = (1ULL<<(FILE_PIECES * PIECES_BLOCKS))-1ULL ; + peer->bitfield = (1U << FILE_PIECES) - 1U; + peer->bitfield_blocks = (1ULL << (FILE_PIECES * PIECES_BLOCKS)) - 1ULL; } else { - peer->bitfield = 0; + peer->bitfield = 0; peer->bitfield_blocks = 0; } @@ -251,10 +255,10 @@ peer_t peer_init(int id, int seed) /** Destroys a poor peer object. */ void peer_free(peer_t peer) { - char *key; + char* key; connection_t connection; xbt_dict_cursor_t cursor; - xbt_dict_foreach(peer->peers, cursor, key, connection) { + xbt_dict_foreach (peer->peers, cursor, key, connection) { connection_free(connection); } xbt_dict_free(&peer->peers); @@ -268,16 +272,16 @@ void peer_free(peer_t peer) */ int has_finished(unsigned int bitfield) { - return bitfield == (1U<peers, cursor, key, connection) { + xbt_dict_foreach (peer->peers, cursor, key, connection) { if (connection->interested) nb++; } @@ -287,10 +291,10 @@ int nb_interested_peers(peer_t peer) void update_active_peers_set(peer_t peer, connection_t remote_peer) { if ((remote_peer->interested != 0) && (remote_peer->choked_upload == 0)) { - //add in the active peers set - xbt_dict_set_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int), remote_peer, NULL); - } else if (xbt_dict_get_or_null_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int))) { - xbt_dict_remove_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int)); + // add in the active peers set + xbt_dict_set_ext(peer->active_peers, (char*)&remote_peer->id, sizeof(int), remote_peer, NULL); + } else if (xbt_dict_get_or_null_ext(peer->active_peers, (char*)&remote_peer->id, sizeof(int))) { + xbt_dict_remove_ext(peer->active_peers, (char*)&remote_peer->id, sizeof(int)); } } @@ -300,128 +304,129 @@ void update_active_peers_set(peer_t peer, connection_t remote_peer) */ void handle_message(peer_t peer, msg_task_t task) { - const char* type_names[10] = - {"HANDSHAKE","CHOKE","UNCHOKE","INTERESTED","NOTINTERESTED","HAVE","BITFIELD","REQUEST", "PIECE", "CANCEL" }; + const char* type_names[10] = {"HANDSHAKE", "CHOKE", "UNCHOKE", "INTERESTED", "NOTINTERESTED", + "HAVE", "BITFIELD", "REQUEST", "PIECE", "CANCEL"}; message_t message = MSG_task_get_data(task); XBT_DEBUG("Received a %s message from %s (%s)", type_names[message->type], message->mailbox, - message->issuer_host_name); + message->issuer_host_name); 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: - //Check if the peer is in our connection list. - if (remote_peer == 0) { - 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: - //Update the pieces list - update_pieces_count_from_bitfield(peer, message->bitfield); - //Store the bitfield - remote_peer->bitfield = message->bitfield; - 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_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_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_assert(remote_peer->choked_download); - remote_peer->choked_download = 0; - //Send requests to the peer, since it has unchoked us - if (remote_peer->am_interested) - 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_assert(!remote_peer->choked_download); - remote_peer->choked_download = 1; - if (remote_peer->current_piece != -1) - remove_current_piece(peer, remote_peer, remote_peer->current_piece); - break; - case MESSAGE_HAVE: - XBT_DEBUG("\t for piece %d", message->index); - xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong HAVE message received"); - remote_peer->bitfield = remote_peer->bitfield | (1U<index); - peer->pieces_count[message->index]++; - //If the piece is in our pieces, we tell the peer that we are interested. - if ((remote_peer->am_interested == 0) && peer_has_not_piece(peer,message->index)) { - remote_peer->am_interested = 1; - send_interested(peer, message->mailbox); - if (remote_peer->choked_download == 0) + case MESSAGE_HANDSHAKE: + // Check if the peer is in our connection list. + if (remote_peer == 0) { + 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: + // Update the pieces list + update_pieces_count_from_bitfield(peer, message->bitfield); + // Store the bitfield + remote_peer->bitfield = message->bitfield; + 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_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_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_assert(remote_peer->choked_download); + remote_peer->choked_download = 0; + // Send requests to the peer, since it has unchoked us + if (remote_peer->am_interested) request_new_piece_to_peer(peer, remote_peer); - } - break; - case MESSAGE_REQUEST: - xbt_assert(remote_peer->interested); - xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong request received"); - if (remote_peer->choked_upload == 0) { - XBT_DEBUG("\t for piece %d (%d,%d)", message->index, message->block_index, - message->block_index + message->block_length); - if (!peer_has_not_piece(peer, message->index)) { - send_piece(peer, message->mailbox, message->index, message->block_index, message->block_length); + break; + case MESSAGE_CHOKE: + xbt_assert((remote_peer != NULL), "A non-in-our-list peer has sent us a message. WTH ?"); + xbt_assert(!remote_peer->choked_download); + remote_peer->choked_download = 1; + if (remote_peer->current_piece != -1) + remove_current_piece(peer, remote_peer, remote_peer->current_piece); + break; + case MESSAGE_HAVE: + XBT_DEBUG("\t for piece %d", message->index); + xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong HAVE message received"); + remote_peer->bitfield = remote_peer->bitfield | (1U << message->index); + peer->pieces_count[message->index]++; + // If the piece is in our pieces, we tell the peer that we are interested. + if ((remote_peer->am_interested == 0) && peer_has_not_piece(peer, message->index)) { + remote_peer->am_interested = 1; + send_interested(peer, message->mailbox); + if (remote_peer->choked_download == 0) + request_new_piece_to_peer(peer, remote_peer); } - } else { - XBT_DEBUG("\t for piece %d but he is choked.", message->peer_id); - } - break; - case MESSAGE_PIECE: - XBT_DEBUG(" \t for piece %d (%d,%d)", message->index, message->block_index, - message->block_index + message->block_length); - xbt_assert(!remote_peer->choked_download); - 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(%u) remote bitfield(%u)", 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"); - //TODO: Execute à computation. - if (peer_has_not_piece(peer,message->index)) { - 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); - //Setting the fact that we have the piece - peer->bitfield = peer->bitfield | (1U<index); - char* status = xbt_malloc0(FILE_PIECES+1); - 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 - send_have(peer, message->index); - //sending UNINTERESTED to peers that do not have what we want. - update_interested_after_receive(peer); - } else { // piece not completed - send_request_to_peer(peer, remote_peer, message->index); // ask for the next block + break; + case MESSAGE_REQUEST: + xbt_assert(remote_peer->interested); + xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong request received"); + if (remote_peer->choked_upload == 0) { + XBT_DEBUG("\t for piece %d (%d,%d)", message->index, message->block_index, + message->block_index + message->block_length); + if (!peer_has_not_piece(peer, message->index)) { + send_piece(peer, message->mailbox, message->index, message->block_index, message->block_length); + } + } else { + XBT_DEBUG("\t for piece %d but he is choked.", message->peer_id); } - } else { - XBT_DEBUG("However, we already have it"); - xbt_assert(ENABLE_END_GAME_MODE, "Should not happen because we don't use end game mode !"); - request_new_piece_to_peer(peer, remote_peer); - } - break; - case MESSAGE_CANCEL: - break; - default: - THROW_IMPOSSIBLE; + break; + case MESSAGE_PIECE: + XBT_DEBUG(" \t for piece %d (%d,%d)", message->index, message->block_index, + message->block_index + message->block_length); + xbt_assert(!remote_peer->choked_download); + 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(%u) remote bitfield(%u)", + 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"); + // TODO: Execute à computation. + if (peer_has_not_piece(peer, message->index)) { + 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); + // Setting the fact that we have the piece + peer->bitfield = peer->bitfield | (1U << message->index); + char* status = xbt_malloc0(FILE_PIECES + 1); + 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 + send_have(peer, message->index); + // sending UNINTERESTED to peers that do not have what we want. + update_interested_after_receive(peer); + } else { // piece not completed + send_request_to_peer(peer, remote_peer, message->index); // ask for the next block + } + } else { + XBT_DEBUG("However, we already have it"); + xbt_assert(ENABLE_END_GAME_MODE, "Should not happen because we don't use end game mode !"); + request_new_piece_to_peer(peer, remote_peer); + } + break; + case MESSAGE_CANCEL: + break; + default: + THROW_IMPOSSIBLE; } - //Update the peer speed. + // Update the peer speed. if (remote_peer) { connection_add_speed_value(remote_peer, 1.0 / (MSG_get_clock() - peer->begin_receive_time)); } @@ -435,7 +440,7 @@ void request_new_piece_to_peer(peer_t peer, connection_t remote_peer) { int piece = select_piece_to_download(peer, remote_peer); if (piece != -1) { - peer->current_pieces|= (1U << (unsigned int) piece); + peer->current_pieces |= (1U << (unsigned int)piece); send_request_to_peer(peer, remote_peer, piece); } } @@ -481,21 +486,21 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer) if (count_pieces(peer->current_pieces) >= (FILE_PIECES - count_pieces(peer->bitfield)) && (is_interested(peer, remote_peer) != 0)) { #if ENABLE_END_GAME_MODE == 0 - return -1; + return -1; #endif int nb_interesting_pieces = 0; // compute the number of interesting pieces for (int i = 0; i < FILE_PIECES; i++) { - if (peer_has_not_piece(peer,i) && connection_has_piece(remote_peer,i)) { + if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i)) { nb_interesting_pieces++; } } xbt_assert(nb_interesting_pieces != 0); // get a random interesting piece int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1); - int current_index = 0; + int current_index = 0; for (int i = 0; i < FILE_PIECES; i++) { - if (peer_has_not_piece(peer,i) && connection_has_piece(remote_peer,i)) { + if (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i)) { if (random_piece_index == current_index) { piece = i; break; @@ -511,16 +516,18 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer) int nb_interesting_pieces = 0; // compute the number of interesting pieces for (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 (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) && + peer_is_not_downloading_piece(peer, i)) { nb_interesting_pieces++; } } xbt_assert(nb_interesting_pieces != 0); // get a random interesting piece int random_piece_index = RngStream_RandInt(peer->stream, 0, nb_interesting_pieces - 1); - int current_index = 0; + int current_index = 0; for (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 (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) { piece = i; break; @@ -530,28 +537,28 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer) } xbt_assert(piece != -1); return piece; - } else { // Rarest first policy - short min = SHRT_MAX; + } else { // Rarest first policy + short min = SHRT_MAX; 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++) { - if (peer->pieces_count[i] < min && peer_has_not_piece(peer, i) && connection_has_piece(remote_peer,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)); + 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++) { - if (peer->pieces_count[i] == min && peer_has_not_piece(peer, i) && connection_has_piece(remote_peer,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++; } - xbt_assert(nb_min_pieces != 0 || (is_interested_and_free(peer, remote_peer)==0)); + xbt_assert(nb_min_pieces != 0 || (is_interested_and_free(peer, remote_peer) == 0)); // get a random rarest piece int random_rarest_index = RngStream_RandInt(peer->stream, 0, nb_min_pieces - 1); for (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) && + 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) { piece = i; @@ -573,17 +580,17 @@ void update_choked_peers(peer_t peer) if (nb_interested_peers(peer) == 0) return; XBT_DEBUG("(%d) update_choked peers %u active peers", peer->id, xbt_dict_size(peer->active_peers)); - //update the current round + // update the current round peer->round = (peer->round + 1) % 3; - char *key; - char *key_choked=NULL; + char* key; + char* key_choked = NULL; connection_t peer_choosed = NULL; - connection_t peer_choked = NULL; - //remove a peer from the list + connection_t peer_choked = NULL; + // remove a peer from the list xbt_dict_cursor_t cursor = NULL; xbt_dict_cursor_first(peer->active_peers, &cursor); if (xbt_dict_length(peer->active_peers) > 0) { - key_choked = xbt_dict_cursor_get_key(cursor); + key_choked = xbt_dict_cursor_get_key(cursor); peer_choked = xbt_dict_cursor_get_data(cursor); } xbt_dict_cursor_free(&cursor); @@ -593,23 +600,23 @@ void update_choked_peers(peer_t peer) 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 != 0) && (connection->choked_upload != 0)) { + xbt_dict_foreach (peer->peers, cursor, key, connection) { + if (connection->last_unchoke < unchoke_time && (connection->interested != 0) && + (connection->choked_upload != 0)) { unchoke_time = connection->last_unchoke; peer_choosed = connection; } } } else { - //Random optimistic unchoking + // Random optimistic unchoking if (peer->round == 0) { int j = 0; do { - //We choose a random peer to unchoke. + // We choose a random peer to unchoke. int id_chosen = RngStream_RandInt(peer->stream, 0, xbt_dict_length(peer->peers) - 1); - int i = 0; + int i = 0; connection_t connection; - xbt_dict_foreach(peer->peers, cursor, key, connection) { + xbt_dict_foreach (peer->peers, cursor, key, connection) { if (i == id_chosen) { peer_choosed = connection; break; @@ -626,13 +633,13 @@ void update_choked_peers(peer_t peer) j++; } while (peer_choosed == NULL && j < MAXIMUM_PEERS); } else { - //Use the "fastest download" policy. + // Use the "fastest download" policy. 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 != 0) && (connection->interested != 0)) { - peer_choosed = connection; + xbt_dict_foreach (peer->peers, cursor, key, connection) { + if (connection->peer_speed > fastest_speed && (connection->choked_upload != 0) && + (connection->interested != 0)) { + peer_choosed = connection; fastest_speed = connection->peer_speed; } } @@ -640,14 +647,14 @@ void update_choked_peers(peer_t peer) } 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); + 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"); peer_choked->choked_upload = 1; - xbt_assert((*((int *) key_choked) == peer_choked->id)); + xbt_assert((*((int*)key_choked) == peer_choked->id)); update_active_peers_set(peer, peer_choked); XBT_DEBUG("(%d) Sending a CHOKE to %d", peer->id, peer_choked->id); send_choked(peer, peer_choked->mailbox); @@ -655,7 +662,7 @@ void update_choked_peers(peer_t peer) if (peer_choosed != NULL) { 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); @@ -669,20 +676,20 @@ void update_choked_peers(peer_t peer) */ void update_interested_after_receive(peer_t peer) { - char *key; + char* key; xbt_dict_cursor_t cursor; connection_t connection; - xbt_dict_foreach(peer->peers, cursor, key, connection) { + xbt_dict_foreach (peer->peers, cursor, key, connection) { if (connection->am_interested != 0) { int interested = 0; - //Check if the peer still has a piece we want. + // Check if the peer still has a piece we want. for (int i = 0; i < FILE_PIECES; i++) { - if (peer_has_not_piece(peer, i) && connection_has_piece(connection,i)) { + if (peer_has_not_piece(peer, i) && connection_has_piece(connection, i)) { interested = 1; break; } } - if (!interested) { //no more piece to download from connection + if (!interested) { // no more piece to download from connection connection->am_interested = 0; send_notinterested(peer, connection->mailbox); } @@ -695,7 +702,7 @@ void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_l xbt_assert((index >= 0 && index <= FILE_PIECES), "Wrong piece."); xbt_assert((block_index >= 0 && 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)); + peer->bitfield_blocks |= (1ULL << (unsigned int)(index * PIECES_BLOCKS + i)); } } @@ -703,7 +710,7 @@ void update_bitfield_blocks(peer_t peer, int index, int block_index, int block_l int piece_complete(peer_t peer, int index) { for (int i = 0; i < PIECES_BLOCKS; i++) { - if (!(peer->bitfield_blocks & 1ULL<<(index * PIECES_BLOCKS + i))) { + if (!(peer->bitfield_blocks & 1ULL << (index * PIECES_BLOCKS + i))) { return 0; } } @@ -714,7 +721,7 @@ int piece_complete(peer_t peer, int index) int get_first_block(peer_t peer, int piece) { for (int i = 0; i < PIECES_BLOCKS; i++) { - if (!(peer->bitfield_blocks & 1ULL<<(piece * PIECES_BLOCKS + i))) { + if (!(peer->bitfield_blocks & 1ULL << (piece * PIECES_BLOCKS + i))) { return i; } } @@ -724,14 +731,14 @@ int get_first_block(peer_t peer, int piece) /** Indicates if the remote peer has a piece not stored by the local peer */ int is_interested(peer_t peer, connection_t remote_peer) { - return remote_peer->bitfield & (peer->bitfield^((1<bitfield & (peer->bitfield ^ ((1 << FILE_PIECES) - 1)); } /** 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) { for (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 (peer_has_not_piece(peer, i) && connection_has_piece(remote_peer, i) && peer_is_not_downloading_piece(peer, i)) { return 1; } } @@ -756,7 +763,7 @@ int partially_downloaded_piece(peer_t peer, connection_t remote_peer) void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece) { remote_peer->current_piece = piece; - xbt_assert(connection_has_piece(remote_peer,piece)); + xbt_assert(connection_has_piece(remote_peer, piece)); int block_index = get_first_block(peer, piece); if (block_index != -1) { int block_length = MIN(BLOCKS_REQUESTED, PIECES_BLOCKS - block_index); @@ -774,7 +781,7 @@ void send_request_to_peer(peer_t peer, connection_t remote_peer, int piece) * @param peer peer data * @param mailbox destination mailbox */ -void send_interested(peer_t peer, const char *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)); @@ -786,7 +793,7 @@ void send_interested(peer_t peer, const char *mailbox) * @param peer peer data * @param mailbox destination mailbox */ -void send_notinterested(peer_t peer, const char *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)); @@ -801,8 +808,8 @@ void send_handshake_all(peer_t peer) { connection_t remote_peer; xbt_dict_cursor_t cursor = NULL; - char *key; - xbt_dict_foreach(peer->peers, cursor, key, remote_peer) { + 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_dsend(task, remote_peer->mailbox, task_message_free); @@ -814,7 +821,7 @@ void send_handshake_all(peer_t peer) * @param peer peer data * @param mailbox mailbox where to we send the message */ -void send_handshake(peer_t peer, const char *mailbox) +void send_handshake(peer_t peer, const char* mailbox) { XBT_DEBUG("Sending a HANDSHAKE to %s", mailbox); msg_task_t task = task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox, peer->id, @@ -823,20 +830,20 @@ void send_handshake(peer_t peer, const char *mailbox) } /** Send a "choked" message to a peer. */ -void send_choked(peer_t peer, const char *mailbox) +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 */ -void send_unchoked(peer_t peer, const char *mailbox) +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); } @@ -846,8 +853,8 @@ void send_have(peer_t peer, int piece) XBT_DEBUG("Sending HAVE message to all my peers"); connection_t remote_peer; xbt_dict_cursor_t cursor = NULL; - char *key; - xbt_dict_foreach(peer->peers, cursor, key, remote_peer) { + 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_dsend(task, remote_peer->mailbox, task_message_free); @@ -857,7 +864,7 @@ void send_have(peer_t peer, int piece) /** @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) +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); @@ -865,7 +872,7 @@ void send_bitfield(peer_t peer, const char *mailbox) } /** 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) +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); @@ -873,12 +880,12 @@ void send_request(peer_t peer, const char *mailbox, int piece, int block_index, } /** 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) +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_assert(piece >= 0, "Tried to send a piece that doesn't exist."); - xbt_assert(!peer_has_not_piece(peer,piece), "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_has_not_piece(peer, piece), "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/app-bittorrent/peer.h b/teshsuite/msg/app-bittorrent/peer.h similarity index 57% rename from examples/msg/app-bittorrent/peer.h rename to teshsuite/msg/app-bittorrent/peer.h index d08f1f2738..a3ac6bf11d 100644 --- a/examples/msg/app-bittorrent/peer.h +++ b/teshsuite/msg/app-bittorrent/peer.h @@ -6,43 +6,43 @@ #ifndef BITTORRENT_PEER_H #define BITTORRENT_PEER_H +#include "bittorrent.h" +#include "connection.h" #include +#include #include #include -#include -#include "connection.h" -#include "bittorrent.h" /** Peer data */ typedef struct s_peer { - int id; //peer id + int id; // peer id - unsigned int bitfield; //list of pieces the peer has. - unsigned long long bitfield_blocks; //list of blocks the peer has. - short *pieces_count; //number of peers that have each piece. + unsigned int bitfield; // list of pieces the peer has. + unsigned long long bitfield_blocks; // list of blocks the peer has. + short* pieces_count; // number of peers that have each piece. - unsigned int current_pieces; //current pieces the peer is downloading + unsigned int current_pieces; // current pieces the peer is downloading - xbt_dict_t peers; //peers list - xbt_dict_t active_peers; //active peers list - int round; //current round for the chocking algorithm. + xbt_dict_t peers; // peers list + xbt_dict_t active_peers; // active peers list + int round; // current round for the chocking algorithm. - char mailbox[MAILBOX_SIZE]; //peer mailbox. - char mailbox_tracker[MAILBOX_SIZE]; //pair mailbox while communicating with the tracker. - const char *hostname; //peer hostname + char mailbox[MAILBOX_SIZE]; // peer mailbox. + char mailbox_tracker[MAILBOX_SIZE]; // pair mailbox while communicating with the tracker. + const char* hostname; // peer hostname - msg_task_t task_received; //current task being received - msg_comm_t comm_received; //current comm + msg_task_t task_received; // current task being received + msg_comm_t comm_received; // current comm - RngStream stream; //RngStream for + RngStream stream; // RngStream for - double begin_receive_time; //time when the receiving communication has begun, useful for calculating host speed. + double begin_receive_time; // time when the receiving communication has begun, useful for calculating host speed. } s_peer_t; -typedef s_peer_t *peer_t; +typedef s_peer_t* peer_t; /** Peer main function */ -int peer(int argc, char *argv[]); -void get_status(char **status, unsigned int bitfield); +int peer(int argc, char* argv[]); +void get_status(char** status, unsigned int bitfield); int get_peers_data(peer_t peer); void leech_loop(peer_t peer, double deadline); @@ -83,16 +83,16 @@ int select_piece_to_download(peer_t peer, connection_t remote_peer); void send_handshake_all(peer_t peer); -void send_interested(peer_t peer, const char *mailbox); +void send_interested(peer_t peer, const char* mailbox); -void send_notinterested(peer_t peer, const char *mailbox); -void send_handshake(peer_t peer, const char *mailbox); -void send_bitfield(peer_t peer, const char *mailbox); -void send_choked(peer_t peer, const char *mailbox); -void send_unchoked(peer_t peer, const char *mailbox); +void send_notinterested(peer_t peer, const char* mailbox); +void send_handshake(peer_t peer, const char* mailbox); +void send_bitfield(peer_t peer, const char* mailbox); +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 */ +#endif /* BITTORRENT_PEER_H */ diff --git a/examples/msg/app-bittorrent/tracker.c b/teshsuite/msg/app-bittorrent/tracker.c similarity index 73% rename from examples/msg/app-bittorrent/tracker.c rename to teshsuite/msg/app-bittorrent/tracker.c index 255ed261b9..d4025c7be2 100644 --- a/examples/msg/app-bittorrent/tracker.c +++ b/teshsuite/msg/app-bittorrent/tracker.c @@ -8,7 +8,7 @@ #include #include -static void task_free(void *data); +static void task_free(void* data); XBT_LOG_NEW_DEFAULT_CATEGORY(msg_tracker, "Messages specific for the tracker"); /** @@ -16,16 +16,16 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_tracker, "Messages specific for the tracker"); * @param argc number of arguments * @param argv arguments */ -int tracker(int argc, char *argv[]) +int tracker(int argc, char* argv[]) { - //Checking arguments + // Checking arguments xbt_assert(argc == 2, "Wrong number of arguments for the tracker."); - //Retrieving end time - double deadline = xbt_str_parse_double(argv[1],"Invalid deadline: %s"); + // Retrieving end time + double deadline = xbt_str_parse_double(argv[1], "Invalid deadline: %s"); xbt_assert(deadline > 0, "Wrong deadline supplied"); - RngStream stream = (RngStream) MSG_host_get_data(MSG_host_self()); - //Building peers array + RngStream stream = (RngStream)MSG_host_get_data(MSG_host_self()); + // Building peers array xbt_dynar_t peers_list = xbt_dynar_new(sizeof(int), NULL); XBT_INFO("Tracker launched."); @@ -38,15 +38,15 @@ int tracker(int argc, char *argv[]) comm_received = MSG_task_irecv(&task_received, TRACKER_MAILBOX); } if (MSG_comm_test(comm_received)) { - //Check for correct status + // Check for correct status if (MSG_comm_get_status(comm_received) == MSG_OK) { - //Retrieve the data sent by the peer. + // Retrieve the data sent by the peer. tracker_task_data_t data = MSG_task_get_data(task_received); - //Add the peer to our peer list. + // Add the peer to our peer list. if (is_in_list(peers_list, data->peer_id) == 0) { xbt_dynar_push_as(peers_list, int, data->peer_id); } - //Sending peers to the peer + // Sending peers to the peer int next_peer; int peers_length = xbt_dynar_length(peers_list); for (int i = 0; i < MAXIMUM_PEERS && i < peers_length; i++) { @@ -55,11 +55,11 @@ int tracker(int argc, char *argv[]) } while (is_in_list(data->peers, next_peer)); xbt_dynar_push_as(data->peers, int, next_peer); } - //setting the interval + // setting the interval data->interval = TRACKER_QUERY_INTERVAL; - //sending the task back to the peer. + // sending the task back to the peer. MSG_task_dsend(task_received, data->mailbox, task_free); - //destroy the communication. + // destroy the communication. } MSG_comm_destroy(comm_received); comm_received = NULL; @@ -68,11 +68,11 @@ int tracker(int argc, char *argv[]) MSG_process_sleep(1); } } - //Free the remaining communication if any + // Free the remaining communication if any if (comm_received) { MSG_comm_destroy(comm_received); } - //Free the peers list + // Free the peers list xbt_dynar_free(&peers_list); XBT_INFO("Tracker is leaving"); @@ -84,18 +84,18 @@ int tracker(int argc, char *argv[]) * Build a new task for the tracker. * @param issuer_host_name Hostname of the issuer. For debugging purposes */ -tracker_task_data_t tracker_task_data_new(const char *issuer_host_name, const char *mailbox, int peer_id, - int uploaded, int downloaded, int left) +tracker_task_data_t tracker_task_data_new(const char* issuer_host_name, const char* mailbox, int peer_id, int uploaded, + int downloaded, int left) { tracker_task_data_t task = xbt_new(s_tracker_task_data_t, 1); - task->type = TRACKER_TASK_QUERY; + task->type = TRACKER_TASK_QUERY; task->issuer_host_name = issuer_host_name; - task->mailbox = mailbox; - task->peer_id = peer_id; - task->uploaded = uploaded; - task->downloaded = downloaded; - task->left = left; + task->mailbox = mailbox; + task->peer_id = peer_id; + task->uploaded = uploaded; + task->downloaded = downloaded; + task->left = left; task->peers = xbt_dynar_new(sizeof(int), NULL); @@ -106,7 +106,7 @@ tracker_task_data_t tracker_task_data_new(const char *issuer_host_name, const ch * Free a tracker task that has not successfully been sent. * @param data Task to free */ -static void task_free(void *data) +static void task_free(void* data) { tracker_task_data_t task_data = MSG_task_get_data(data); tracker_task_data_free(task_data); diff --git a/teshsuite/msg/app-bittorrent/tracker.h b/teshsuite/msg/app-bittorrent/tracker.h new file mode 100644 index 0000000000..f00e71ff76 --- /dev/null +++ b/teshsuite/msg/app-bittorrent/tracker.h @@ -0,0 +1,42 @@ +/* Copyright (c) 2012-2014. 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. */ + +#ifndef BITTORRENT_TRACKER_H_ +#define BITTORRENT_TRACKER_H_ +#include "bittorrent.h" +#include +/** + * Tracker main function + */ +int tracker(int argc, char* argv[]); +/** + * Task types exchanged between a node and the tracker + */ +typedef enum { TRACKER_TASK_QUERY, TRACKER_TASK_ANSWER } e_tracker_task_type_t; +/** + * Tasks exchanged between a tracker and peers. + */ +typedef struct s_tracker_task_data { + e_tracker_task_type_t type; // type of the task + const char* mailbox; // mailbox where the tracker should answer + const char* issuer_host_name; // hostname, for debug purposes + // Query data + int peer_id; // peer id + int uploaded; // how much the peer has already uploaded + int downloaded; // how much the peer has downloaded + int left; // how much the peer has left + // Answer data + int interval; // how often the peer should contact the tracker (unused for now) + xbt_dynar_t peers; // the peer list the peer has asked for. +} s_tracker_task_data_t; +typedef s_tracker_task_data_t* tracker_task_data_t; + +tracker_task_data_t tracker_task_data_new(const char* issuer_host_name, const char* mailbox, int peer_id, int uploaded, + int downloaded, int left); +void tracker_task_data_free(tracker_task_data_t task); + +int is_in_list(xbt_dynar_t peers, int id); +#endif /* BITTORRENT_TRACKER_H */