Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix sonar issues in ns3 examples.
[simgrid.git] / examples / cpp / network-ns3 / s4u-network-ns3.cpp
1 /* Copyright (c) 2007-2022. 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 #include "simgrid/s4u.hpp"
7 #include <string>
8 #include <unordered_map>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11
12 double start_time;
13 std::unordered_map<int, std::string> workernames;
14 std::unordered_map<int, std::string> masternames;
15
16 static void master(std::vector<std::string> args)
17 {
18   xbt_assert(args.size() == 4, "Strange number of arguments expected 3 got %zu", args.size() - 1);
19
20   XBT_DEBUG("Master started");
21
22   /* data size */
23   double msg_size = std::stod(args[1]);
24   int id          = std::stoi(args[3]); // unique id to control statistics
25
26   /* worker name */
27   workernames[id] = args[2];
28
29   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(args[3]);
30
31   masternames[id] = simgrid::s4u::Host::current()->get_name();
32
33   auto* payload = new double(msg_size);
34
35   /* time measurement */
36   start_time = simgrid::s4u::Engine::get_clock();
37   mbox->put(payload, static_cast<uint64_t>(msg_size));
38
39   XBT_DEBUG("Finished");
40 }
41
42 static void worker(std::vector<std::string> args)
43 {
44   xbt_assert(args.size() == 2, "Strange number of arguments expected 1 got %zu", args.size() - 1);
45
46   int id                      = std::stoi(args[1]);
47   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(args[1]);
48
49   XBT_DEBUG("Worker started");
50
51   auto payload = mbox->get_unique<double>();
52
53   double elapsed_time = simgrid::s4u::Engine::get_clock() - start_time;
54
55   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, *payload, masternames.at(id).c_str(),
56            workernames.at(id).c_str());
57   XBT_DEBUG("FLOW[%d] : transferred in  %f seconds", id, elapsed_time);
58
59   XBT_DEBUG("Finished");
60 }
61
62 int main(int argc, char* argv[])
63 {
64   simgrid::s4u::Engine e(&argc, argv);
65   xbt_assert(argc > 2,
66              "Usage: %s platform_file deployment_file\n"
67              "\tExample: %s platform.xml deployment.xml\n",
68              argv[0], argv[0]);
69
70   e.load_platform(argv[1]);
71
72   e.register_function("master", master);
73   e.register_function("worker", worker);
74
75   e.load_deployment(argv[2]);
76
77   e.run();
78
79   return 0;
80 }