Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
save some hidden calls to Engine::get_instance
[simgrid.git] / teshsuite / s4u / host-on-off-recv / host-on-off-recv.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 example");
10
11 static void master()
12 {
13   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("comm");
14   simgrid::s4u::Host* jupiter    = simgrid::s4u::Host::by_name("Jupiter");
15
16   XBT_INFO("Master starting");
17   simgrid::s4u::this_actor::sleep_for(0.5);
18
19   auto* payload              = new std::string("COMM");
20   simgrid::s4u::CommPtr comm = mailbox->put_async(payload, 1E8);
21
22   simgrid::s4u::this_actor::sleep_for(0.5);
23
24   XBT_INFO("Turning off the worker host");
25   jupiter->turn_off();
26
27   try {
28     comm->wait();
29   } catch (const simgrid::NetworkFailureException&) {
30     delete payload;
31   }
32
33   XBT_INFO("Master has finished");
34 }
35
36 static void worker()
37 {
38   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("comm");
39
40   XBT_INFO("Worker receiving");
41   try {
42     auto payload = mailbox->get_unique<std::string>();
43     XBT_DEBUG("Received message: %s", payload->c_str());
44   } catch (const simgrid::HostFailureException&) {
45     XBT_DEBUG("The host has been turned off, this was expected");
46     return;
47   }
48
49   XBT_ERROR("Worker should be off already.");
50 }
51
52 int main(int argc, char* argv[])
53 {
54   simgrid::s4u::Engine e(&argc, argv);
55   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s small_platform.xml\n", argv[0], argv[0]);
56
57   e.load_platform(argv[1]);
58
59   simgrid::s4u::Actor::create("master", e.host_by_name("Tremblay"), master);
60   simgrid::s4u::Actor::create("worker", e.host_by_name("Jupiter"), worker);
61
62   e.run();
63
64   XBT_INFO("Simulation time %g", e.get_clock());
65
66   return 0;
67 }