Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'surf_precision' into 'master'
[simgrid.git] / teshsuite / s4u / host-on-off-recv / host-on-off-recv.cpp
1 /* Copyright (c) 2010-2020. 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   std::string* 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   const std::string* payload     = nullptr;
39   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("comm");
40
41   XBT_INFO("Worker receiving");
42   try {
43     payload = static_cast<std::string*>(mailbox->get());
44   } catch (const simgrid::HostFailureException&) {
45     XBT_DEBUG("The host has been turned off, this was expected");
46     delete payload;
47     return;
48   }
49
50   XBT_ERROR("Worker should be off already.");
51 }
52
53 int main(int argc, char* argv[])
54 {
55
56   simgrid::s4u::Engine e(&argc, argv);
57   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s small_platform.xml\n", argv[0], argv[0]);
58
59   e.load_platform(argv[1]);
60
61   simgrid::s4u::Actor::create("master", simgrid::s4u::Host::by_name("Tremblay"), master);
62   simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Jupiter"), worker);
63
64   e.run();
65
66   XBT_INFO("Simulation time %g", e.get_clock());
67
68   return 0;
69 }