Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use Mailbox::get_unique<>(), and save a few delete.
[simgrid.git] / teshsuite / s4u / ns3-simultaneous-send-rcv / ns3-simultaneous-send-rcv.cpp
1 /* Copyright (c) 2019-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* This test checks that ns-3 behave correctly when multiple flows finish   */
7 /* at the exact same time. Given the amount of simultaneous senders, it     */
8 /* also serves as a (small) crash test for ns-3.                            */
9
10 #include "simgrid/s4u.hpp"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u tests");
13
14 const int payload            = 1000;
15 const int nb_message_to_send = 5;
16 const double sleep_time      = 5;
17 const int nb_sender          = 100;
18
19 int nb_messages_sent = 0;
20
21 simgrid::s4u::Mailbox* box = simgrid::s4u::Mailbox::by_name("test");
22
23 static void test_send(){
24   for (int nb_message = 0; nb_message < nb_message_to_send; nb_message++) {
25     nb_messages_sent++;
26     XBT_VERB("start sending test #%i", nb_messages_sent);
27     box->put(new int(nb_messages_sent), payload);
28     XBT_VERB("done sending test #%i", nb_messages_sent);
29     simgrid::s4u::this_actor::sleep_until(sleep_time * (nb_message + 1));
30   }
31 }
32
33 static void test_receive(){
34   for (int nb_message = 0; nb_message < nb_message_to_send * nb_sender; nb_message++) {
35     XBT_VERB("waiting for messages");
36     auto ptr = box->get_unique<int>();
37     int id   = *ptr;
38     XBT_VERB("received messages #%i", id);
39   }
40   XBT_INFO("Done receiving from %d senders, each of them sending %d messages", nb_sender, nb_message_to_send);
41 }
42
43
44 int main(int argc, char *argv[])
45 {
46   simgrid::s4u::Engine e(&argc, argv);
47  
48   e.load_platform(argv[1]);
49
50   auto hosts = e.get_all_hosts();
51
52   simgrid::s4u::Actor::create("receiver", hosts[0], test_receive);
53   for (int i = 0; i < nb_sender; i++)
54     simgrid::s4u::Actor::create("sender_" + std::to_string(i), hosts[i % (e.get_host_count() - 1) + 1], test_send);
55
56   e.run();
57
58   return 0;
59 }