Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
09900f75e277da1cb9785a360610cd1d8532b73f
[simgrid.git] / examples / c / app-bittorrent / tracker.c
1 /* Copyright (c) 2012-2020. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "tracker.h"
8
9 #include "simgrid/actor.h"
10 #include "simgrid/comm.h"
11 #include "simgrid/engine.h"
12 #include "xbt/log.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(bittorrent_tracker, "Messages specific for the tracker");
15
16 void tracker_answer_free(void* data)
17 {
18   tracker_answer_t a = (tracker_answer_t)data;
19   xbt_dynar_free(&a->peers);
20   free(a);
21 }
22
23 static int is_in_list(const_xbt_dynar_t peers, int id)
24 {
25   return xbt_dynar_member(peers, &id);
26 }
27
28 void tracker(int argc, char* argv[])
29 {
30   // Checking arguments
31   xbt_assert(argc == 2, "Wrong number of arguments for the tracker.");
32   // Retrieving end time
33   double deadline = xbt_str_parse_double(argv[1], "Invalid deadline: %s");
34   xbt_assert(deadline > 0, "Wrong deadline supplied");
35
36   // Building peers array
37   xbt_dynar_t peers_list = xbt_dynar_new(sizeof(int), NULL);
38
39   sg_mailbox_t mailbox = sg_mailbox_by_name(TRACKER_MAILBOX);
40
41   XBT_INFO("Tracker launched.");
42   sg_comm_t comm_received = NULL;
43   void* received          = NULL;
44
45   while (simgrid_get_clock() < deadline) {
46     if (comm_received == NULL)
47       comm_received = sg_mailbox_get_async(mailbox, &received);
48
49     if (sg_comm_test(comm_received)) {
50       // Retrieve the data sent by the peer.
51       xbt_assert(received != NULL);
52       tracker_query_t tq = (tracker_query_t)received;
53
54       // Add the peer to our peer list.
55       if (!is_in_list(peers_list, tq->peer_id))
56         xbt_dynar_push_as(peers_list, int, tq->peer_id);
57
58       // Sending peers to the requesting peer
59       tracker_answer_t ta = tracker_answer_new(TRACKER_QUERY_INTERVAL);
60       int next_peer;
61       int peers_length = (int)xbt_dynar_length(peers_list);
62       for (int i = 0; i < MAXIMUM_PEERS && i < peers_length; i++) {
63         do {
64           next_peer = xbt_dynar_get_as(peers_list, rand() % peers_length, int);
65         } while (is_in_list(ta->peers, next_peer));
66         xbt_dynar_push_as(ta->peers, int, next_peer);
67       }
68       // sending the task back to the peer.
69       sg_comm_t answer = sg_mailbox_put_init(tq->return_mailbox, ta, TRACKER_COMM_SIZE);
70       sg_comm_detach(answer, tracker_answer_free);
71
72       xbt_free(tq);
73       comm_received = NULL;
74       received      = NULL;
75     } else {
76       sg_actor_sleep_for(1);
77     }
78   }
79   // Free the remaining communication if any
80   if (comm_received)
81     sg_comm_unref(comm_received);
82   // Free the peers list
83   xbt_dynar_free(&peers_list);
84
85   XBT_INFO("Tracker is leaving");
86 }
87
88 tracker_query_t tracker_query_new(int peer_id, sg_mailbox_t return_mailbox)
89 {
90   tracker_query_t tq = xbt_new(s_tracker_query_t, 1);
91   tq->peer_id        = peer_id;
92   tq->return_mailbox = return_mailbox;
93   return tq;
94 }
95
96 tracker_answer_t tracker_answer_new(int interval)
97 {
98   tracker_answer_t ta = xbt_new(s_tracker_answer_t, 1);
99   ta->interval        = interval;
100   ta->peers           = xbt_dynar_new(sizeof(int), NULL);
101
102   return ta;
103 }