Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0c146226a3d4d176c852dcbaaaa44e468fbd3af5
[simgrid.git] / examples / s4u / app-bittorrent / s4u-tracker.cpp
1 /* Copyright (c) 2012-2018. 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 "s4u-tracker.hpp"
8 #include <algorithm>
9 #include <xbt/RngStream.h>
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_bt_tracker, "Messages specific for the tracker");
12
13 Tracker::Tracker(std::vector<std::string> args)
14 {
15   // Checking arguments
16   xbt_assert(args.size() == 2, "Wrong number of arguments for the tracker.");
17   // Retrieving end time
18   try {
19     deadline = std::stod(args[1]);
20   } catch (std::invalid_argument& ia) {
21     throw std::invalid_argument(std::string("Invalid deadline:") + args[1].c_str());
22   }
23   xbt_assert(deadline > 0, "Wrong deadline supplied");
24
25   stream = simgrid::s4u::this_actor::get_host()->extension<HostBittorrent>()->getStream();
26
27   mailbox = simgrid::s4u::Mailbox::byName(TRACKER_MAILBOX);
28
29   XBT_INFO("Tracker launched.");
30 }
31
32 void Tracker::operator()()
33 {
34   simgrid::s4u::CommPtr comm = nullptr;
35   void* received             = nullptr;
36   while (simgrid::s4u::Engine::get_clock() < deadline) {
37     if (comm == nullptr)
38       comm = mailbox->get_async(&received);
39     if (comm->test()) {
40       // Retrieve the data sent by the peer.
41       xbt_assert(received != nullptr);
42       TrackerQuery* tq = static_cast<TrackerQuery*>(received);
43
44       // Add the peer to our peer list, if not already known.
45       if (known_peers.find(tq->getPeerId()) == known_peers.end()) {
46         known_peers.insert(tq->getPeerId());
47       }
48
49       // Sending back peers to the requesting peer
50       TrackerAnswer* ta = new TrackerAnswer(TRACKER_QUERY_INTERVAL);
51       std::set<int>::iterator next_peer;
52       int nb_known_peers = known_peers.size();
53       int max_tries      = std::min(MAXIMUM_PEERS, nb_known_peers);
54       int tried          = 0;
55       while (tried < max_tries) {
56         do {
57           next_peer = known_peers.begin();
58           std::advance(next_peer, RngStream_RandInt(stream, 0, nb_known_peers - 1));
59         } while (ta->getPeers()->find(*next_peer) != ta->getPeers()->end());
60         ta->addPeer(*next_peer);
61         tried++;
62       }
63       tq->getReturnMailbox()->put_init(ta, TRACKER_COMM_SIZE)->detach();
64
65       delete tq;
66       comm = nullptr;
67     } else {
68       simgrid::s4u::this_actor::sleep_for(1);
69     }
70   }
71   XBT_INFO("Tracker is leaving");
72 }