Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix again FG #11 (mixing daemonize and auto-restart).
[simgrid.git] / teshsuite / s4u / actor-autorestart / actor-autorestart.cpp
1 /* Copyright (c) 2017-2019. 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
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
9
10 static void dummy()
11 {
12   XBT_INFO("I start");
13   try {
14     simgrid::s4u::this_actor::sleep_for(200);
15     XBT_INFO("I stop");
16   } catch (simgrid::HostFailureException& e) {
17     XBT_DEBUG("The host has died ... as expected. This actor silently stops");
18   }
19 }
20
21 static void dummy_daemon()
22 {
23   simgrid::s4u::Actor::self()->daemonize();
24   while (1) {
25     XBT_INFO("Hello from the infinite loop");
26     simgrid::s4u::this_actor::sleep_for(80.0);
27   }
28 }
29
30 static void autostart()
31 {
32   simgrid::s4u::Host* host = simgrid::s4u::Host::by_name("Fafard");
33
34   XBT_INFO("starting a dummy process on %s", host->get_cname());
35   simgrid::s4u::ActorPtr dummy_actor = simgrid::s4u::Actor::create("Dummy", host, dummy);
36   dummy_actor->on_exit([](bool) { XBT_INFO("On_exit callback set before autorestart"); });
37   dummy_actor->set_auto_restart(true);
38   dummy_actor->on_exit([](bool) { XBT_INFO("On_exit callback set after autorestart"); });
39
40   XBT_INFO("starting a daemon process on %s", host->get_cname());
41   simgrid::s4u::ActorPtr daemon_actor = simgrid::s4u::Actor::create("Daemon", host, dummy_daemon);
42   daemon_actor->on_exit([](bool) { XBT_INFO("On_exit callback set before autorestart"); });
43   daemon_actor->set_auto_restart(true);
44   daemon_actor->on_exit([](bool) { XBT_INFO("On_exit callback set after autorestart"); });
45
46   simgrid::s4u::this_actor::sleep_for(50);
47
48   XBT_INFO("powering off %s", host->get_cname());
49   host->turn_off();
50
51   simgrid::s4u::this_actor::sleep_for(10);
52
53   XBT_INFO("powering on %s", host->get_cname());
54   host->turn_on();
55   simgrid::s4u::this_actor::sleep_for(200);
56 }
57
58 int main(int argc, char* argv[])
59 {
60   simgrid::s4u::Engine e(&argc, argv);
61   e.load_platform(argv[1]);
62
63   simgrid::s4u::Actor::create("Autostart", simgrid::s4u::Host::by_name("Tremblay"), autostart);
64
65   e.run();
66   XBT_INFO("Simulation time %g", e.get_clock());
67
68   return 0;
69 }