Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add bittorrent example
authorSamuel Lepetit <samuel.lepetit@inria.fr>
Tue, 3 Jul 2012 16:25:38 +0000 (18:25 +0200)
committerSamuel Lepetit <samuel.lepetit@inria.fr>
Tue, 3 Jul 2012 16:25:38 +0000 (18:25 +0200)
16 files changed:
buildtools/Cmake/AddTests.cmake
buildtools/Cmake/MakeExe.cmake
examples/msg/bittorrent/CMakeLists.txt [new file with mode: 0644]
examples/msg/bittorrent/bittorrent.c [new file with mode: 0644]
examples/msg/bittorrent/bittorrent.h [new file with mode: 0644]
examples/msg/bittorrent/bittorrent.tesh [new file with mode: 0644]
examples/msg/bittorrent/bittorrent.xml [new file with mode: 0644]
examples/msg/bittorrent/connection.c [new file with mode: 0644]
examples/msg/bittorrent/connection.h [new file with mode: 0644]
examples/msg/bittorrent/generate.py [new file with mode: 0755]
examples/msg/bittorrent/messages.c [new file with mode: 0644]
examples/msg/bittorrent/messages.h [new file with mode: 0644]
examples/msg/bittorrent/peer.c [new file with mode: 0644]
examples/msg/bittorrent/peer.h [new file with mode: 0644]
examples/msg/bittorrent/tracker.c [new file with mode: 0644]
examples/msg/bittorrent/tracker.h [new file with mode: 0644]

index c24a91d..7613bf6 100644 (file)
@@ -212,6 +212,9 @@ if(NOT enable_memcheck)
   ADD_TEST(msg-chord-no-crosstraffic-thread-parallel ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:thread --cfg contexts/nthreads:4 --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/chord --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/chord chord.tesh)
   ADD_TEST(msg-chord-thread                     ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:thread --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/chord --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/chord chord_crosstraffic.tesh)
   ADD_TEST(msg-chord-thread-parallel            ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:thread --cfg contexts/nthreads:4 --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/chord --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/chord chord_crosstraffic.tesh)
+    ADD_TEST(msg-bittorrent-thread                      ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:thread --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/bittorrent --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/bittorrent bittorrent.tesh)
+    ADD_TEST(msg-bittorrent-thread-parallel                      ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/nthreads:4 --cfg contexts/factory:thread --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/bittorrent --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/bittorrent bittorrent.tesh)
+
   if(CONTEXT_UCONTEXT)
     ADD_TEST(msg-migration-ucontext             ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:ucontext --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/msg --cd ${CMAKE_BINARY_DIR}/examples/msg ${CMAKE_HOME_DIRECTORY}/examples/msg/migration/migration.tesh)
     ADD_TEST(msg-ptask-ucontext                 ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:ucontext --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/msg --cd ${CMAKE_BINARY_DIR}/examples/msg ${CMAKE_HOME_DIRECTORY}/examples/msg/parallel_task/parallel_task.tesh)
@@ -224,6 +227,9 @@ if(NOT enable_memcheck)
     ADD_TEST(msg-chord-no-crosstraffic-ucontext-parallel ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:ucontext --cfg contexts/nthreads:4 --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/chord --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/chord chord.tesh)
     ADD_TEST(msg-chord-ucontext                 ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:ucontext --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/chord --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/chord chord_crosstraffic.tesh)
     ADD_TEST(msg-chord-ucontext-parallel        ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:ucontext --cfg contexts/nthreads:4 --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/chord --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/chord chord_crosstraffic.tesh)
+    ADD_TEST(msg-bittorrent-ucontext                      ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:ucontext --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/bittorrent --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/bittorrent bittorrent.tesh)
+    ADD_TEST(msg-bittorrent-ucontext-parallel                      ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/nthreads:4 --cfg contexts/factory:ucontext --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/bittorrent --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/bittorrent bittorrent.tesh)
+
   endif(CONTEXT_UCONTEXT)
   if(HAVE_RAWCTX)
     ADD_TEST(msg-migration-raw                  ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:raw --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/msg --cd ${CMAKE_BINARY_DIR}/examples/msg ${CMAKE_HOME_DIRECTORY}/examples/msg/migration/migration.tesh)
@@ -237,6 +243,9 @@ if(NOT enable_memcheck)
     ADD_TEST(msg-chord-no-crosstraffic-raw-parallel ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:raw --cfg contexts/nthreads:4 --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/chord --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/chord chord.tesh)
     ADD_TEST(msg-chord-raw                      ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:raw --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/chord --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/chord chord_crosstraffic.tesh)
     ADD_TEST(msg-chord-raw-parallel             ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:raw --cfg contexts/nthreads:4 --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/chord --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/chord chord_crosstraffic.tesh)
+    ADD_TEST(msg-bittorrent-raw                      ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:raw --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/bittorrent --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/bittorrent bittorrent.tesh)
+    ADD_TEST(msg-bittorrent-raw-parallel                      ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/nthreads:4 --cfg contexts/factory:raw --setenv bindir=${CMAKE_BINARY_DIR}/examples/msg/bittorrent --cd ${CMAKE_HOME_DIRECTORY}/examples/msg/bittorrent bittorrent.tesh)
+    
   endif(HAVE_RAWCTX)
 
   IF(${ARCH_32_BITS})
index f3f0a2b..246b93e 100644 (file)
@@ -54,6 +54,7 @@ add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/msg/priority)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/msg/masterslave)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/msg/icomms)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/msg/chord)
+add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/msg/bittorrent)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/msg/token_ring)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/msg/pmm)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/msg/start_kill_time)
diff --git a/examples/msg/bittorrent/CMakeLists.txt b/examples/msg/bittorrent/CMakeLists.txt
new file mode 100644 (file)
index 0000000..2e2ceac
--- /dev/null
@@ -0,0 +1,9 @@
+cmake_minimum_required(VERSION 2.6)
+
+set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}")
+
+add_executable(bittorrent "bittorrent.c" "messages.c" "peer.c" 
+"tracker.c" "connection.c")
+set(CMAKE_C_FLAGS "-O3 -ggdb")
+### Add definitions for compile
+target_link_libraries(bittorrent simgrid )
diff --git a/examples/msg/bittorrent/bittorrent.c b/examples/msg/bittorrent/bittorrent.c
new file mode 100644 (file)
index 0000000..ba52ea4
--- /dev/null
@@ -0,0 +1,37 @@
+/* Copyright (c) 2012. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+#include "bittorrent.h"
+#include "peer.h"
+#include "tracker.h"
+#include <msg/msg.h>
+/**
+ * Bittorrent example launcher
+ */
+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;
+  }
+
+  const char *platform_file = argv[1];
+  const char *deployment_file = argv[2];
+
+  MSG_create_environment(platform_file);
+
+  MSG_function_register("tracker",tracker);
+  MSG_function_register("peer",peer);
+
+  MSG_launch_application(deployment_file);
+
+  msg_error_t res = MSG_main();
+
+  MSG_clean();
+
+  return 0;
+}
diff --git a/examples/msg/bittorrent/bittorrent.h b/examples/msg/bittorrent/bittorrent.h
new file mode 100644 (file)
index 0000000..7a81709
--- /dev/null
@@ -0,0 +1,70 @@
+/* Copyright (c) 2012. 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_BITTORRENT_H_
+#define BITTORRENT_BITTORRENT_H_
+//TODO: Let users change this
+/*
+ * File transfered data
+ */
+static int FILE_SIZE = 5120;
+static int FILE_PIECE_SIZE = 512;
+static int FILE_PIECES = 10;
+
+static int PIECES_BLOCKS = 5;
+static int BLOCKS_REQUESTED = 2;
+
+static int PIECE_COMM_SIZE = 1;
+/**
+ * Information message size
+ */
+#define MESSAGE_SIZE 1
+
+
+#define MAILBOX_SIZE 40 /* Size of mailboxes */
+/**
+ * Mailbox used to communicate with the tracker.
+ */
+#define TRACKER_MAILBOX "tracker_mailbox"
+/**
+ * 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
+ */
+#define TRACKER_QUERY_INTERVAL 1000
+/**
+ * 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
+ */
+#define MAX_UNCHOKED_PEERS 4
+
+/**
+ * Interval between each update of the choked peers
+ */
+#define UPDATE_CHOKED_INTERVAL 30
+
+/**
+ * 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
new file mode 100644 (file)
index 0000000..01b31a7
--- /dev/null
@@ -0,0 +1,24 @@
+#! ./tesh
+
+p Testing the Bittorrent implementation with MSG
+
+! output sort
+$ $SG_TEST_EXENV ${bindir:=.}/bittorrent ${srcdir:=.}/../msg_platform.xml ${srcdir:=.}/bittorrent.xml "--log=root.fmt:[%10.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.012427] (7:peer@iRMX) Here is my current status: 1111111111
+> [5000.048881] (2:peer@Boivin) Here is my current status: 1111111111
+> [5000.827216] (8:peer@McGee) Here is my current status: 1111111111
+> [5000.832407] (3:peer@Jean_Yves) Here is my current status: 1111111111
+> [5000.897053] (4:peer@TeX) Here is my current status: 1111111111
+> [5000.897053] (6:peer@Disney) Here is my current status: 1111111111
+
+
diff --git a/examples/msg/bittorrent/bittorrent.xml b/examples/msg/bittorrent/bittorrent.xml
new file mode 100644 (file)
index 0000000..58627ee
--- /dev/null
@@ -0,0 +1,40 @@
+<?xml version='1.0'?>
+<!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
+<platform version="3">
+
+  <process host="Jacquelin" function="tracker">
+    <argument value="3000" />                  
+  </process>
+
+  <process host="Boivin" function="peer">
+    <argument value="00000002"/>        <!-- my id -->
+    <argument value="5000" />                  <!-- end time -->       
+    <argument value="1" />                     <!-- indicates if the peer is a seed at the begining of the simulation -->      
+  </process>
+  <process host="Jean_Yves" function="peer">
+    <argument value="00000003"/>        <!-- my id -->
+    <argument value="5000" />                  <!-- end time -->       
+  </process>
+  <process host="TeX" function="peer">
+    <argument value="00000004"/>        <!-- my id -->
+    <argument value="5000" />                  <!-- end time -->       
+  </process>
+  <process host="Geoff" function="peer">
+    <argument value="00000005"/>        <!-- my id -->
+    <argument value="5000" />                  <!-- end time -->       
+    <argument value="1" />                     <!-- indicates if the peer is a seed at the begining of the simulation -->      
+  </process>
+  <process host="Disney" function="peer">
+    <argument value="00000006"/>        <!-- my id -->
+    <argument value="5000" />                  <!-- end time -->       
+  </process>
+  <process host="iRMX" function="peer">
+    <argument value="00000007"/>        <!-- my id -->
+    <argument value="5000" />                  <!-- end time -->       
+  </process>
+  <process host="McGee" function="peer">
+    <argument value="00000008"/>        <!-- my id -->
+    <argument value="5000" />                  <!-- end time -->       
+  </process>
+
+</platform>
diff --git a/examples/msg/bittorrent/connection.c b/examples/msg/bittorrent/connection.c
new file mode 100644 (file)
index 0000000..43ead7c
--- /dev/null
@@ -0,0 +1,35 @@
+  /* Copyright (c) 2012. 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 "connection.h"
+#include "bittorrent.h"
+#include <xbt/sysdep.h>
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg_peers);
+
+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 = NULL;
+       connection->interested = 0;
+       connection->am_interested = 0;
+       connection->choked_upload = 1;
+       connection->choked_download = 1;
+       connection->peer_speed = 0;
+       connection->last_unchoke = 0;
+
+       return connection;
+}
+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) {
+       connection_t co = (connection_t)data;
+       xbt_free(co->bitfield);
+       xbt_free(co->mailbox);
+       xbt_free(co);
+
+}
diff --git a/examples/msg/bittorrent/connection.h b/examples/msg/bittorrent/connection.h
new file mode 100644 (file)
index 0000000..7647ef6
--- /dev/null
@@ -0,0 +1,38 @@
+/* Copyright (c) 2012. 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_CONNECTION_H_
+#define BITTORRENT_CONNECTION_H_
+/**
+ * Contains the connection data of a peer.
+ */
+typedef struct s_connection {
+       int id; //Peer id
+       char *bitfield; //Fields
+       char *mailbox;
+       int messages_count;
+       double peer_speed;
+       double last_unchoke;
+       int am_interested:1; //Indicates if we are interested in something the peer has
+       int interested:1; //Indicates if the peer is interested in one of our pieces
+       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
+ */
+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
+ */
+void connection_add_speed_value(connection_t connection, double speed);
+/**
+ * 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
new file mode 100755 (executable)
index 0000000..1aed434
--- /dev/null
@@ -0,0 +1,41 @@
+#!/usr/bin/python
+
+# This script generates a specific deployment file for the Bittorrent example.
+# It assumes that the platform will be a cluster.
+# Usage: python generate.py nb_nodes nb_bits end_date percentage
+# Example: python generate.py 10000 5000
+
+import sys, random
+
+if len(sys.argv) != 4:
+       print("Usage: python generate.py nb_nodes end_date seed_percentage > deployment_file.xml")
+       sys.exit(1)
+
+nb_nodes = int(sys.argv[1])
+end_date = int(sys.argv[2])
+seed_percentage = int(sys.argv[3]);
+
+nb_bits = 24
+max_id = 2 ** nb_bits - 1
+all_ids = [42]
+
+sys.stdout.write("<?xml version='1.0'?>\n"
+"<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">\n"
+"<platform version=\"3\">\n"
+"  <process host=\"c-0.me\" function=\"tracker\"><argument value=\"%d\"/></process>\n" % end_date)
+
+for i in range(1, nb_nodes):
+
+  ok = False
+  while not ok:
+    my_id = random.randint(0, max_id)
+    ok = not my_id in all_ids
+  start_date = i * 10
+  line = "  <process host=\"c-%d.me\" function=\"peer\"><argument value=\"%d\" /><argument value=\"%d\" />" % (i, my_id, end_date)
+  if random.randint(0,100) < seed_percentage:
+    line += "<argument value=\"1\" />"
+  line += "</process>\n";
+  sys.stdout.write(line)
+  all_ids.append(my_id)
+sys.stdout.write("</platform>")
+
diff --git a/examples/msg/bittorrent/messages.c b/examples/msg/bittorrent/messages.c
new file mode 100644 (file)
index 0000000..a2e5e31
--- /dev/null
@@ -0,0 +1,58 @@
+  /* Copyright (c) 2012. 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"
+/**
+ * Build a new empty message
+ * @param type type of the message
+ * @param issuer_host_name hostname of the issuer, for debuging purposes
+ * @param mailbox mailbox where the peer should answer
+ * @param peer_id id of the issuer
+ */
+msg_task_t task_message_new(e_message_type type, const char *issuer_host_name, const char *mailbox, int peer_id) {
+  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,MESSAGE_SIZE,message);
+  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) {
+  msg_task_t task = task_message_new(type,issuer_host_name, mailbox, peer_id);
+  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) {
+  msg_task_t task = task_message_new(MESSAGE_BITFIELD,issuer_host_name, mailbox, peer_id);
+  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);
+  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 stalled, int block_index, int block_length) {
+  msg_task_t task = task_message_index_new(MESSAGE_PIECE,issuer_host_name, mailbox, peer_id, index);
+  message_t message = MSG_task_get_data(task);
+  message->stalled = stalled;
+  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);
+}
diff --git a/examples/msg/bittorrent/messages.h b/examples/msg/bittorrent/messages.h
new file mode 100644 (file)
index 0000000..342df0f
--- /dev/null
@@ -0,0 +1,64 @@
+/* Copyright (c) 2012. 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_MESSAGES_H_
+#define BITTORRENT_MESSAGES_H_
+#include <msg/msg.h>
+/**
+ * Types of messages exchanged between two peers.
+ */
+typedef enum {
+       MESSAGE_HANDSHAKE,
+       MESSAGE_CHOKE,
+       MESSAGE_UNCHOKE,
+       MESSAGE_INTERESTED,
+       MESSAGE_NOTINTERESTED,
+       MESSAGE_HAVE,
+       MESSAGE_BITFIELD,
+       MESSAGE_REQUEST,
+       MESSAGE_PIECE
+} e_message_type;
+
+/**
+ * Message data
+ */
+typedef struct s_message {
+       e_message_type type;
+       const char *mailbox;
+       const char *issuer_host_name;
+       int peer_id;
+       char *bitfield;
+       int index;
+       int block_index;
+       int block_length;
+       int stalled:1;
+} s_message_t, *message_t;
+/**
+ * Builds a new value-less message
+ */
+XBT_INLINE msg_task_t task_message_new(e_message_type type, const char *issuer_host_name, const char *mailbox, int peer_id);
+/**
+ * Builds a new "have/piece" message
+ */
+XBT_INLINE msg_task_t task_message_index_new(e_message_type type, const char *issuer_host_name, const char *mailbox, int peer_id, int index);
+/**
+ * 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);
+/**
+ * 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 stalled, int block_index, int block_length);
+/**
+ * Free a message task
+ */
+void task_message_free(void*);
+#endif /* BITTORRENT_MESSAGES_H_ */
diff --git a/examples/msg/bittorrent/peer.c b/examples/msg/bittorrent/peer.c
new file mode 100644 (file)
index 0000000..5dc1078
--- /dev/null
@@ -0,0 +1,766 @@
+  /* Copyright (c) 2012. 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 "peer.h"
+#include "tracker.h"
+#include "connection.h"
+#include "messages.h"
+#include <msg/msg.h>
+#include <xbt/RngStream.h>
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peers,
+                             "Messages specific for the peers");
+
+
+
+/**
+ * Peer main function
+ */
+int peer(int argc, char *argv[]) {
+       s_peer_t peer;
+       //Check arguments
+       xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments");
+       //Build peer object
+       if (argc == 4) {
+               peer_init(&peer, atoi(argv[1]),1);
+       }
+       else {
+               peer_init(&peer, atoi(argv[1]),0);
+       }
+       //Retrieve deadline
+       double deadline = atof(argv[2]);
+       xbt_assert(deadline > 0, "Wrong deadline supplied");
+       XBT_INFO("Hi, I'm joining the network with id %d", peer.id);
+       //Getting peer data from the tracker.
+       if (get_peers_data(&peer)) {
+               XBT_DEBUG("Got %d peers from the tracker", xbt_dict_length(peer.peers));
+               XBT_DEBUG("Here is my current status: %s",peer.bitfield);
+               peer.begin_receive_time = MSG_get_clock();
+               if (has_finished(peer.bitfield)) {
+                       peer.pieces = FILE_PIECES;
+                       send_handshake_all(&peer);
+                       seed_loop(&peer,deadline);
+               }
+               else {
+                       leech_loop(&peer,deadline);
+                       seed_loop(&peer,deadline);
+               }
+       }
+       else {
+               XBT_INFO("Couldn't contact the tracker.");
+       }
+
+       XBT_INFO("Here is my current status: %s", peer.bitfield);
+       if (peer.comm_received) {
+               MSG_comm_destroy(peer.comm_received);
+       }
+
+       peer_free(&peer);
+       return 0;
+}
+/**
+ * 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) {
+       double next_choked_update = MSG_get_clock() + UPDATE_CHOKED_INTERVAL;
+       XBT_DEBUG("Start downloading.");
+       /*
+        * Send a "handshake" message to all the peers it got
+        * (since it couldn't have gotten more than 50 peers)
+        */
+       send_handshake_all(peer);
+       //Wait for at least one "bitfield" message.
+       wait_for_pieces(peer,deadline);
+       XBT_DEBUG("Starting main leech loop");
+       while (MSG_get_clock() < deadline && peer->pieces < FILE_PIECES) {
+               if (peer->comm_received == NULL) {
+                       peer->task_received = NULL;
+                       peer->comm_received = MSG_task_irecv(&peer->task_received,peer->mailbox);
+               }
+               if (MSG_comm_test(peer->comm_received)) {
+                       msg_error_t status = MSG_comm_get_status(peer->comm_received);
+                       MSG_comm_destroy(peer->comm_received);
+                       peer->comm_received = NULL;
+                       if (status == MSG_OK) {
+                               handle_message(peer,peer->task_received);
+                       }
+               }
+               else {
+                       if (peer->current_piece != -1) {
+                               send_interested_to_peers(peer);
+                       }
+                       else {
+                               //If the current interested pieces is < MAX
+                               if (peer->pieces_requested < MAX_PIECES) {
+                                       update_current_piece(peer);
+                               }
+                       }
+                       //We don't execute the choke algorithm if we don't already have a piece
+                       if (MSG_get_clock() >= next_choked_update && peer->pieces > 0) {
+                               update_choked_peers(peer);
+                               next_choked_update += UPDATE_CHOKED_INTERVAL;
+                       }
+                       else {
+                               MSG_process_sleep(1);
+                       }
+               }
+       }
+}
+/**
+ * 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) {
+       double next_choked_update = MSG_get_clock() + UPDATE_CHOKED_INTERVAL;
+       XBT_DEBUG("Start seeding.");
+       //start the main seed loop
+       while (MSG_get_clock() < deadline) {
+               if (peer->comm_received == NULL) {
+                       peer->task_received = NULL;
+                       peer->comm_received = MSG_task_irecv(&peer->task_received,peer->mailbox);
+               }
+               if (MSG_comm_test(peer->comm_received)) {
+                       msg_error_t status = MSG_comm_get_status(peer->comm_received);
+                       MSG_comm_destroy(peer->comm_received);
+                       peer->comm_received = NULL;
+                       if (status == MSG_OK) {
+                               handle_message(peer,peer->task_received);
+                       }
+               }
+               else {
+                       if (MSG_get_clock() >= next_choked_update) {
+                               update_choked_peers(peer);
+                               //TODO: Change the choked peer algorithm when seeding.
+                               next_choked_update += UPDATE_CHOKED_INTERVAL;
+                       }
+                       else {
+                               MSG_process_sleep(1);
+                       }
+               }
+       }
+}
+/**
+ * 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);
+       //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);
+               if (status == MSG_OK) {
+                       send_success = 1;
+               }
+       }
+       while (!success && MSG_get_clock() < timeout) {
+               comm_received = MSG_task_irecv(&task_received,peer->mailbox_tracker);
+               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);
+                       int 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);
+                       }
+                       success = 1;
+                       //free the communication and the task
+                       MSG_comm_destroy(comm_received);
+                       tracker_task_data_free(data);
+                       MSG_task_destroy(task_received);
+                       comm_received = NULL;
+               }
+       }
+
+       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.
+ */
+void peer_init(peer_t peer, int id, int seed) {
+       peer->id = id;
+       sprintf(peer->mailbox,"%d",id);
+       sprintf(peer->mailbox_tracker,"tracker_%d",id);
+       peer->peers = xbt_dict_new();
+       peer->active_peers = xbt_dict_new();
+       peer->hostname = MSG_host_get_name(MSG_host_self());
+
+       peer->bitfield = xbt_new(char,FILE_PIECES + 1);
+       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));
+       }
+       else {
+               memset(peer->bitfield,'0',sizeof(char) * (FILE_PIECES + 1));
+        memset(peer->bitfield_blocks,'0',sizeof(char) * FILE_PIECES * (PIECES_BLOCKS));
+       }
+
+       peer->bitfield[FILE_PIECES] = '\0';
+    peer->pieces = 0;
+
+       peer->pieces_count = xbt_new0(short,FILE_PIECES);
+       peer->pieces_requested = 0;
+
+       peer->current_pieces = xbt_dynar_new(sizeof(int),NULL);
+       peer->current_piece = -1;
+
+       peer->stream = RngStream_CreateStream("");
+       peer->comm_received = NULL;
+
+       peer->round = 0;
+}
+/**
+ * Destroys a poor peer object.
+ */
+void peer_free(peer_t peer) {
+       char *key;
+       connection_t connection;
+       xbt_dict_cursor_t cursor;
+       xbt_dict_foreach(peer->peers,cursor,key,connection) {
+               connection_free(connection);
+       }
+       xbt_dict_free(&peer->peers);
+       xbt_dict_free(&peer->active_peers);
+       xbt_dynar_free(&peer->current_pieces);
+       xbt_free(peer->pieces_count);
+       xbt_free(peer->bitfield);
+       xbt_free(peer->bitfield_blocks);
+
+       RngStream_DeleteStream(&peer->stream);
+}
+/**
+ * Returns if a peer has finished downloading the file
+ * @param bitfield peer bitfield
+ */
+int has_finished(char *bitfield) {
+       return ((memchr(bitfield,'0',sizeof(char) * FILE_PIECES) == NULL) ? 1 : 0);
+}
+/**
+ * Handle a received message sent by another peer
+ * @param peer Peer data
+ * @param task task received.
+ */
+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));
+       switch (message->type) {
+
+               case MESSAGE_HANDSHAKE:
+                       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);
+                               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);
+                       //Update the pieces list
+                       update_pieces_count_from_bitfield(peer,message->bitfield);
+                       //Store the bitfield
+                       remote_peer->bitfield = xbt_strdup(message->bitfield);
+                       //Update the current piece
+                       if (peer->current_piece == -1 && peer->pieces < FILE_PIECES) {
+                               update_current_piece(peer);
+                       }
+               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 ?");
+                       //Update the interested state of the peer.
+                       remote_peer->interested = 1;
+               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 ?");
+                       remote_peer->interested = 0;
+               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);
+                       remote_peer->choked_download = 0;
+                       xbt_dict_set_ext(peer->active_peers,(char*)&message->peer_id,sizeof(int),remote_peer,NULL);
+                       //Send requests to the peer, since it has unchoked us
+                       send_requests_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);
+                       remote_peer->choked_download = 1;
+                       xbt_ex_t e;
+                       TRY {
+                               xbt_dict_remove_ext(peer->active_peers,(char*)&message->peer_id,sizeof(int));
+                       }
+                       CATCH(e) {
+                               xbt_ex_free(e);
+                       }
+               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_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong HAVE message received");
+                       if (remote_peer->bitfield == NULL)
+                               return;
+                       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.
+                       if (!remote_peer->am_interested && in_current_pieces(peer, message->index)) {
+                               remote_peer->am_interested = 1;
+                               send_interested(peer,remote_peer->mailbox);
+                       }
+                       break;
+               case MESSAGE_REQUEST:
+                       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->peer_id, message->block_index,message->block_index + message->block_length);
+                               if (peer->bitfield[message->index] == '1') {
+                                       send_piece(peer, message->mailbox, message->index, 0, 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);
+                       }
+               break;
+               case MESSAGE_PIECE:
+                       xbt_assert((message->index >= 0 && message->index < FILE_PIECES), "Wrong piece received");
+                       //TODO: Execute Ã  computation.
+                       if (message->stalled) {
+                               XBT_DEBUG("The received piece %d from %s (%s) is STALLED",message->index, message->mailbox, message->issuer_host_name);
+                       }
+                       else {
+                         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);
+                               if (peer->bitfield[message->index] == '0') {
+                                   update_bitfield_blocks(peer,message->index,message->block_index,message->block_length);
+                                   if (piece_complete(peer,message->index)) {
+                      peer->pieces_requested--;
+                      //Removing the piece from our piece list
+                      int piece_index = -1, i, piece;
+                      xbt_dynar_foreach(peer->current_pieces, i, piece) {
+                          if (piece == message->index) {
+                              piece_index = i;
+                              break;
+                          }
+                      }
+                      xbt_assert(piece_index != -1, "Received an incorrect piece");
+                      xbt_dynar_remove_at(peer->current_pieces,piece_index,NULL);
+                      //Setting the fact that we have the piece
+                      peer->bitfield[message->index] = '1';
+                      peer->pieces++;
+                      XBT_DEBUG("My status is now %s",peer->bitfield);
+                      //Sending the information to all the peers we are connected to
+                      send_have(peer,message->index);
+                      //sending UNINTERSTED to peers that doesn't have what we want.
+                      update_interested_after_receive(peer);
+                                 }
+                               }
+                               else {
+                                       XBT_DEBUG("However, we already have it");
+                               }
+                       }
+               break;
+       }
+       //Update the peer speed.
+       if (remote_peer) {
+         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);
+}
+
+/**
+ * Wait for the node to receive interesting bitfield messages (ie: non empty)
+ * to be received
+ * @param deadline peer deadline
+ * @param peer peer data
+ */
+void wait_for_pieces(peer_t peer, double deadline) {
+       int finished = 0;
+       while (MSG_get_clock() < deadline && !finished) {
+               if (peer->comm_received == NULL) {
+                       peer->task_received = NULL;
+                       peer->comm_received = MSG_task_irecv(&peer->task_received,peer->mailbox);
+               }
+               msg_error_t status = MSG_comm_wait(peer->comm_received,TIMEOUT_MESSAGE);
+               //free the comm already, we don't need it anymore
+               MSG_comm_destroy(peer->comm_received);
+               peer->comm_received = NULL;
+               if (status == MSG_OK) {
+                       message_t message = MSG_task_get_data(peer->task_received);
+                       handle_message(peer,peer->task_received);
+                       if (peer->current_piece != -1) {
+                               finished = 1;
+                       }
+               }
+       }
+}
+/**
+ * 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) {
+       int i;
+       for (i = 0; i < FILE_PIECES; i++) {
+               if (bitfield[i] == '1') {
+                       peer->pieces_count[i]++;
+               }
+       }
+}
+/**
+ * Update the piece the peer is currently interested in.
+ * There is two cases (as described in "Bittorrent Architecture Protocol", Ryan Toole :
+ * If the peer has less than 3 pieces, he chooses a piece at random.
+ * If the peer has more than pieces, he downloads the pieces that are the less
+ * replicated
+ */
+void update_current_piece(peer_t peer) {
+       if (xbt_dynar_length(peer->current_pieces) >= (FILE_PIECES - peer->pieces)) {
+               return;
+       }
+       if (1 || peer->pieces < 3) {
+               int i = 0       ;
+               do {
+                       peer->current_piece = RngStream_RandInt(peer->stream,0,FILE_PIECES - 1);;
+                       i++;
+               } while (!(peer->bitfield[peer->current_piece] == '0' && !in_current_pieces(peer,peer->current_piece)));
+       }
+       else {
+               //Trivial min algorithm.
+               int i, min_id = -1;
+               short min = -1;
+               for (i = 0; i < FILE_PIECES; i++) {
+                       if (peer->bitfield[i] == '0') {
+                               min = peer->pieces_count[i];
+                               min_id = i;
+                               break;
+                       }
+               }
+               xbt_assert((min > -1), "Couldn't find a minimum");
+               for (i = 1; i < FILE_PIECES; i++) {
+                       if (peer->pieces_count[i] < min && peer->bitfield[i] == '0') {
+                               min = peer->pieces_count[i];
+                               min_id = i;
+                       }
+               }
+               peer->current_piece = min_id;
+       }
+       xbt_dynar_push_as(peer->current_pieces, int, peer->current_piece);
+       XBT_DEBUG("New interested piece: %d",peer->current_piece);
+       xbt_assert((peer->current_piece >= 0 && peer->current_piece < FILE_PIECES, "Peer want to retrieve a piece that doesn't exist."));
+}
+/**
+ * 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) {
+       //update the current round
+       peer->round = (peer->round + 1) % 3;
+       int i;
+       char *key;
+    connection_t peer_choosed = 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 = xbt_dict_cursor_get_key(cursor);
+               connection_t peer_choked = xbt_dict_cursor_get_data(cursor);
+               if (peer_choked) {
+                       send_choked(peer,peer_choked->mailbox);
+                       peer_choked->choked_upload = 1;
+               }
+               xbt_dict_remove_ext(peer->active_peers,key,sizeof(int));
+       }
+       xbt_dict_cursor_free(&cursor);
+
+       /**
+        * If we are currently seeding, we unchoke the peer which has
+        * been unchoke the least 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) {
+          unchoke_time = connection->last_unchoke;
+          peer_choosed = connection;
+        }
+      }
+       }
+       else {
+      //Random optimistic unchoking
+      if (peer->round == 0) {
+          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 i = 0;
+              connection_t connection;
+              xbt_dict_foreach(peer->peers,cursor,key,connection) {
+                  if (i == id_chosen) {
+                      peer_choosed = connection;
+                      break;
+                  }
+                  i++;
+              }
+              xbt_dict_cursor_free(&cursor);
+              if (peer_choosed->interested == 0) {
+                  peer_choosed = NULL;
+              }
+              j++;
+          } while (peer_choosed == NULL && j < MAXIMUM_PAIRS);
+      }
+      else {
+        //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 && connection->interested) {
+            peer_choosed = connection;
+            fastest_speed = connection->peer_speed;
+          }
+        }
+      }
+
+       }
+    if (peer_choosed != NULL) {
+        peer_choosed->choked_upload = 0;
+        xbt_dict_set_ext(peer->active_peers,(char*)&peer_choosed->id,sizeof(int),peer_choosed,NULL);
+        peer_choosed->last_unchoke = MSG_get_clock();
+        send_unchoked(peer,peer_choosed->mailbox);
+    }
+
+}
+/**
+ * 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
+ */
+void update_interested_after_receive(peer_t peer) {
+       char *key;
+       xbt_dict_cursor_t cursor;
+       connection_t connection;
+       int interested, cpt, piece;
+       xbt_dict_foreach(peer->peers, cursor, key, connection) {
+               interested = 0;
+               if (connection->am_interested) {
+                   //Check if the peer still has a piece we want.
+                       xbt_dynar_foreach(peer->current_pieces, cpt, piece) {
+                               xbt_assert((piece >= 0), "Wrong piece.");
+                               if (connection->bitfield && connection->bitfield[piece] == '1') {
+                                       interested = 1;
+                                       break;
+                               }
+                       }
+                       if (!interested) {
+                               connection->am_interested = 0;
+                               send_notinterested(peer,connection->mailbox);
+                       }
+               }
+       }
+}
+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);
+  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
+ */
+int piece_complete(peer_t peer, int index) {
+  int i;
+  for (i = 0; i < PIECES_BLOCKS; i++) {
+    if (peer->bitfield_blocks[index * PIECES_BLOCKS + i] == '0') {
+      return 0;
+    }
+  }
+  return 1;
+}
+/**
+ * Returns the first block that a peer doesn't have in a piece
+ */
+int get_first_block(peer_t peer, int piece) {
+  int i;
+  for (i = 0; i < PIECES_BLOCKS; i++) {
+    if (peer->bitfield_blocks[piece * PIECES_BLOCKS + i] == '0') {
+      return i;
+    }
+  }
+  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
+ */
+void send_requests_to_peer(peer_t peer, connection_t remote_peer) {
+       int i, piece, block_index, block_length;
+       xbt_dynar_foreach(peer->current_pieces, i, piece) {
+               if (remote_peer->bitfield && remote_peer->bitfield[piece] == '1') {
+                 block_index = get_first_block(peer,piece);
+                 if (block_index != -1) {
+            block_length = PIECES_BLOCKS - block_index;
+            block_length = min(BLOCKS_REQUESTED,block_length);
+            send_request(peer, remote_peer->mailbox, piece, block_index, block_length);
+            break;
+                 }
+               }
+       }
+}
+/**
+ * Find the peers that have the current interested piece and send them
+ * the "interested" message
+ */
+void send_interested_to_peers(peer_t peer) {
+       char *key;
+       xbt_dict_cursor_t cursor=NULL;
+       connection_t connection;
+       xbt_assert((peer->current_piece != -1), "Tried to send a interested message wheras the current_piece is -1");
+       xbt_dict_foreach(peer->peers, cursor, key, connection) {
+               if (connection->bitfield && connection->bitfield[peer->current_piece] == '1') {
+                       connection->am_interested = 1;
+                       msg_task_t task = task_message_new(MESSAGE_INTERESTED, peer->hostname, peer->mailbox, peer->id);
+                       MSG_task_dsend(task,connection->mailbox,task_message_free);
+                       XBT_DEBUG("Send INTERESTED to %s",connection->mailbox);
+               }
+       }
+       peer->current_piece = -1;
+       peer->pieces_requested++;
+}
+/**
+ * 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);
+       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
+ */
+void send_notinterested(peer_t peer, const char *mailbox) {
+       msg_task_t task = task_message_new(MESSAGE_NOTINTERESTED, peer->hostname, peer->mailbox, peer->id);
+       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
+ */
+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) {
+               msg_task_t task = task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,peer->id);
+               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
+ */
+void send_handshake(peer_t peer, const char *mailbox) {
+       msg_task_t task = task_message_new(MESSAGE_HANDSHAKE, peer->hostname, peer->mailbox,peer->id);
+       MSG_task_dsend(task,mailbox,task_message_free);
+       XBT_DEBUG("Sending a HANDSHAKE to %s",mailbox);
+}
+/**
+ * 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);
+       MSG_task_dsend(task,mailbox,task_message_free);
+}
+/**
+ * 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);
+       MSG_task_dsend(task,mailbox,task_message_free);
+}
+/**
+ * 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");
+       connection_t remote_peer;
+       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);
+               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
+ */
+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);
+       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) {
+       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 stalled, 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->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, stalled, block_index, block_length);
+       MSG_task_dsend(task,mailbox,task_message_free);
+}
+
+int in_current_pieces(peer_t peer, int piece) {
+       int is_in = 0, i, peer_piece;
+       xbt_dynar_foreach(peer->current_pieces,i, peer_piece) {
+               if (peer_piece == piece) {
+                       is_in = 1;
+                       break;
+               }
+       }
+       return is_in;
+}
diff --git a/examples/msg/bittorrent/peer.h b/examples/msg/bittorrent/peer.h
new file mode 100644 (file)
index 0000000..a5755d2
--- /dev/null
@@ -0,0 +1,94 @@
+/* Copyright (c) 2012. 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_PEER_H
+#define BITTORRENT_PEER_H
+#include <msg/msg.h>
+#include <xbt/dict.h>
+#include <xbt/dynar.h>
+#include <xbt/RngStream.h>
+#include "connection.h"
+#include "bittorrent.h"
+
+/**
+ * Peer data
+ */
+typedef struct s_peer {
+       int id; //peer id
+
+       int pieces; //number of pieces the peer has.
+       char *bitfield; //list of pieces the peer has.
+       char *bitfield_blocks; //list of blocks the peer has.
+       short *pieces_count; //number of peers that have each piece.
+
+       xbt_dynar_t current_pieces; //current pieces the peer is downloading
+       int current_piece; //current pieces
+       int pieces_requested; //number of pieces the peer has requested
+
+       xbt_dict_t peers; //peers list
+       xbt_dict_t active_peers; //active peers list
+
+       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
+
+       int round; //current round for the chocking algortihm.
+
+       RngStream stream; //RngStream for
+
+       double begin_receive_time; //time when the receiving communication has begun, useful for calculating host speed.
+} s_peer_t, *peer_t;
+
+/**
+ * Peer main function
+ */
+int peer(int argc, char *argv[]);
+
+int get_peers_data(peer_t peer);
+void leech_loop(peer_t peer, double deadline);
+void seed_loop(peer_t peer, double deadline);
+
+void peer_init(peer_t, int id, int seed);
+void peer_free(peer_t peer);
+
+int has_finished(char *bitfield);
+
+void handle_message(peer_t peer, msg_task_t task);
+
+void wait_for_pieces( peer_t peer, double deadline);
+
+void update_pieces_count_from_bitfield(peer_t peer, char *bitfield);
+void update_current_piece(peer_t peer);
+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);
+int piece_complete(peer_t peer, int index);
+int get_first_block(peer_t peer, int piece);
+
+
+void send_requests_to_peer(peer_t peer, connection_t remote_peer);
+
+void send_interested_to_peers(peer_t peer);
+void send_handshake_all(peer_t peer);
+
+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_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 stalled, int block_index, int block_length);
+
+int in_current_pieces(peer_t peer, int piece);
+#endif /* BITTORRENT_PEER_H */
diff --git a/examples/msg/bittorrent/tracker.c b/examples/msg/bittorrent/tracker.c
new file mode 100644 (file)
index 0000000..55b3693
--- /dev/null
@@ -0,0 +1,137 @@
+/* Copyright (c) 2012. 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 "tracker.h"
+#include <msg/msg.h>
+#include <xbt/RngStream.h>
+
+static void task_free(void *data);
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(msg_tracker,
+                             "Messages specific for the tracker");
+/**
+ * Tracker main function
+ * @param argc number of arguments
+ * @param argv arguments
+ */
+int tracker(int argc, char *argv[]) {
+       int i, key_elm;
+       char *key;
+       void *data_p;
+
+       RngStream stream = RngStream_CreateStream("tracker");
+
+       //Checking arguments
+       xbt_assert(argc == 2, "Wrong number of arguments for the tracker.");
+       //Retrieving end time
+       double deadline = atof(argv[1]);
+       xbt_assert(deadline > 0, "Wrong deadline supplied");
+       //Building peers array
+       xbt_dynar_t peers_list = xbt_dynar_new(sizeof(int), NULL);
+
+       XBT_INFO("Tracker launched.");
+
+       msg_comm_t comm_received = NULL;
+       msg_task_t task_received = NULL;
+
+       while (MSG_get_clock() < deadline) {
+               if (comm_received == NULL) {
+                       comm_received = MSG_task_irecv(&task_received,TRACKER_MAILBOX);
+               }
+               if (MSG_comm_test(comm_received)) {
+                       //Check for correct status
+                       if (MSG_comm_get_status(comm_received) == MSG_OK) {
+                               //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.
+                               if (!is_in_list(peers_list,data->peer_id)) {
+                                       xbt_dynar_push_as(peers_list, int, data->peer_id);
+                               }
+                               //Sending peers to the peer
+                               int nb_peers = 0, next_peer;
+                               int peers_length = xbt_dynar_length(peers_list);
+                               for (i = 0; i < MAXIMUM_PAIRS && i < peers_length; i++) {
+                                       do {
+                                               next_peer = xbt_dynar_get_as(peers_list,RngStream_RandInt(stream, 0, peers_length - 1),int);
+                                       } while (is_in_list(data->peers, next_peer));
+                                       xbt_dynar_push_as(data->peers,int,next_peer);
+                               }
+                               //setting the interval
+                               data->interval = TRACKER_QUERY_INTERVAL;
+                               //sending the task back to the peer.
+                               MSG_task_dsend(task_received,data->mailbox, task_free);
+                               //destroy the communication.
+                       }
+                       MSG_comm_destroy(comm_received);
+                       comm_received = NULL;
+                       task_received = NULL;
+               }
+               else {
+                       MSG_process_sleep(1);
+               }
+       }
+       //Free the remaining communication if any
+       if (comm_received) {
+               MSG_comm_destroy(comm_received);
+       }
+       //Free the peers list
+       xbt_dynar_free(&peers_list);
+       //Free the RngStream object.
+       RngStream_DeleteStream(&stream);
+
+       XBT_INFO("Tracker is leaving");
+
+       return 0;
+}
+/**
+ * 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 task = xbt_new(s_tracker_task_data_t, 1);
+
+       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->peers = xbt_dynar_new(sizeof(int),NULL);
+
+       return task;
+}
+/**
+ * Free a tracker task that has not succefully been sent.
+ * @param data Task to free
+ */
+static void task_free(void *data) {
+       tracker_task_data_t task_data = MSG_task_get_data(data);
+       tracker_task_data_free(task_data);
+       MSG_task_destroy(data);
+}
+/**
+ * Free the data structure of a tracker task.
+ * @param task data to free
+ */
+void tracker_task_data_free(tracker_task_data_t task) {
+       xbt_dynar_free(&task->peers);
+       xbt_free(task);
+}
+/**
+ * Returns if the given id is in the peers lsit
+ * @param peers dynar containing the peers
+ * @param id identifier of the peer to test
+ */
+int is_in_list(xbt_dynar_t peers, int id) {
+       int i, elm;
+       xbt_dynar_foreach(peers, i, elm ) {
+               if (elm == id) {
+                       return 1;
+               }
+       }
+       return 0;
+}
diff --git a/examples/msg/bittorrent/tracker.h b/examples/msg/bittorrent/tracker.h
new file mode 100644 (file)
index 0000000..5bdb375
--- /dev/null
@@ -0,0 +1,43 @@
+/* Copyright (c) 2012. 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 <xbt/dynar.h>
+#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, *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 */