Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / teshsuite / s4u / host-on-off / host-on-off.cpp
1 /* Copyright (c) 2010-2021. 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 "simgrid/s4u.hpp"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
10
11 static void worker()
12 {
13   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("jupi");
14
15   while (true) {
16     std::unique_ptr<std::string> payload;
17     try {
18       payload = mailbox->get_unique<std::string>();
19     } catch (const simgrid::HostFailureException&) {
20       XBT_DEBUG("The host has been turned off, this was expected");
21       return;
22     }
23
24     if (*payload == "finalize") {
25       break;
26     }
27     simgrid::s4u::this_actor::execute(5E7);
28
29     XBT_INFO("Task \"%s\" done", payload->c_str());
30   }
31   XBT_INFO("I'm done. See you!");
32 }
33
34 static void master()
35 {
36   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("jupi");
37   simgrid::s4u::Host* jupiter    = simgrid::s4u::Host::by_name("Jupiter");
38
39   auto* payload = new std::string("task on");
40
41   XBT_INFO("Sending \"task on\"");
42   mailbox->put_async(payload, 1E6)->wait_for(1);
43
44   simgrid::s4u::this_actor::sleep_for(1);
45   jupiter->turn_off();
46
47   XBT_INFO("Sending \"task off\"");
48   payload = new std::string("task off");
49   try {
50     mailbox->put_async(payload, 1E6)->wait_for(1);
51   } catch (const simgrid::TimeoutException&) {
52     delete payload;
53   }
54
55   jupiter->turn_on();
56
57   std::vector<simgrid::s4u::ActorPtr> jupi_actors = jupiter->get_all_actors();
58   for (const auto& actor : jupi_actors)
59     actor->kill();
60
61   XBT_INFO("Sending \"task on without actor\"");
62   payload = new std::string("task on without actor");
63   try {
64     mailbox->put_async(payload, 1E6)->wait_for(1);
65   } catch (const simgrid::TimeoutException&) {
66     delete payload;
67   }
68
69   simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Jupiter"), worker);
70
71   XBT_INFO("Sending \"task on with actor\"");
72   payload = new std::string("task on with actor");
73   try {
74     mailbox->put_async(payload, 1E6)->wait_for(1);
75   } catch (const simgrid::TimeoutException&) {
76     delete payload;
77   }
78
79   XBT_INFO("Sending \"finalize\"");
80   payload = new std::string("finalize");
81   try {
82     mailbox->put_async(payload, 0)->wait_for(1);
83   } catch (const simgrid::TimeoutException&) {
84     delete payload;
85   }
86
87   XBT_INFO("Goodbye now!");
88 }
89
90 int main(int argc, char* argv[])
91 {
92   simgrid::s4u::Engine e(&argc, argv);
93   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
94
95   e.load_platform(argv[1]);
96
97   simgrid::s4u::Actor::create("master", simgrid::s4u::Host::by_name("Tremblay"), master);
98   simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Jupiter"), worker);
99
100   e.run();
101
102   XBT_INFO("Simulation time %g", e.get_clock());
103
104   return 0;
105 }