Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Wifi: Add wifi tesh sources
[simgrid.git] / teshsuite / surf / wifi_usage / 2STA-CT.cpp
1 /* Copyright (c) 2017-2018. 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/msg.h"
7 #include "simgrid/s4u.hpp"
8 #include "src/surf/network_cm02.hpp"
9 #include "xbt/log.h"
10 #include <exception>
11 #include <iostream>
12 #include <random>
13 #include <string>
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(simulator, "[wifi_usage] 2STA-1NODE");
16
17 void setup_simulation();
18 static void flowActor(std::vector<std::string> args);
19
20 /**
21  * Theory says:
22  *   - When two STA communicates on the same AP we have the following AP constraint:
23  *     1.05/r_STA1 * rho_STA1 + 1.05/r_STA2 * rho_2 <= 1
24  *   - Thus:
25  *      mu = 1 / [ 1/2 * 1.05/54Mbps + 1.05/54Mbps ] =  51428571
26  *      simulation_time = 1000*8 / [ mu / 2 ] = 0.0003111111s
27  *
28  */
29 int main(int argc, char** argv)
30 {
31
32   // Build engine
33   simgrid::s4u::Engine engine(&argc, argv);
34   engine.load_platform(argv[1]);
35   setup_simulation();
36   engine.run();
37   XBT_INFO("Simulation took %fs", simgrid::s4u::Engine::get_clock());
38
39   return (0);
40 }
41
42 void setup_simulation()
43 {
44
45   std::vector<std::string> args, noArgs;
46   args.push_back("STA2");
47   args.push_back("1000");
48   simgrid::s4u::Actor::create("STA1", simgrid::s4u::Host::by_name("STA1"), flowActor, args);
49   simgrid::s4u::Actor::create("STA2", simgrid::s4u::Host::by_name("STA2"), flowActor, noArgs);
50   simgrid::kernel::resource::NetworkWifiLink* l =
51       (simgrid::kernel::resource::NetworkWifiLink*)simgrid::s4u::Link::by_name("AP1")->get_impl();
52   l->set_host_rate(simgrid::s4u::Host::by_name("STA1"), 0);
53   l->set_host_rate(simgrid::s4u::Host::by_name("STA2"), 0);
54 }
55
56 static void flowActor(std::vector<std::string> args)
57 {
58   std::string selfName               = simgrid::s4u::this_actor::get_host()->get_name();
59   simgrid::s4u::Mailbox* selfMailbox = simgrid::s4u::Mailbox::by_name(simgrid::s4u::this_actor::get_host()->get_name());
60
61   if (args.size() > 0) { // We should send
62     simgrid::s4u::Mailbox* dstMailbox = simgrid::s4u::Mailbox::by_name(args.at(0));
63
64     int dataSize        = std::atoi(args.at(1).c_str());
65     double comStartTime = simgrid::s4u::Engine::get_clock();
66     dstMailbox->put(const_cast<char*>("message"), dataSize);
67     double comEndTime = simgrid::s4u::Engine::get_clock();
68     XBT_INFO("%s sent %d bytes to %s in %f seconds from %f to %f", selfName.c_str(), dataSize, args.at(0).c_str(),
69              comEndTime - comStartTime, comStartTime, comEndTime);
70   } else { // We should receive
71     selfMailbox->get();
72   }
73 }