Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[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     simgrid::s4u::this_actor::sleep_for(200);
14     XBT_INFO("I stop");
15 }
16
17 static void dummy_daemon()
18 {
19   simgrid::s4u::Actor::self()->daemonize();
20   while (1) {
21     XBT_INFO("Hello from the infinite loop");
22     simgrid::s4u::this_actor::sleep_for(80.0);
23   }
24 }
25
26 static void autostart()
27 {
28   simgrid::s4u::Host* host = simgrid::s4u::Host::by_name("Fafard");
29
30   XBT_INFO("starting a dummy process on %s", host->get_cname());
31   simgrid::s4u::ActorPtr dummy_actor = simgrid::s4u::Actor::create("Dummy", host, dummy);
32   dummy_actor->on_exit([](bool) { XBT_INFO("On_exit callback set before autorestart"); });
33   dummy_actor->set_auto_restart(true);
34   dummy_actor->on_exit([](bool) { XBT_INFO("On_exit callback set after autorestart"); });
35
36   XBT_INFO("starting a daemon process on %s", host->get_cname());
37   simgrid::s4u::ActorPtr daemon_actor = simgrid::s4u::Actor::create("Daemon", host, dummy_daemon);
38   daemon_actor->on_exit([](bool) { XBT_INFO("On_exit callback set before autorestart"); });
39   daemon_actor->set_auto_restart(true);
40   daemon_actor->on_exit([](bool) { XBT_INFO("On_exit callback set after autorestart"); });
41
42   simgrid::s4u::this_actor::sleep_for(50);
43
44   XBT_INFO("powering off %s", host->get_cname());
45   host->turn_off();
46
47   simgrid::s4u::this_actor::sleep_for(10);
48
49   XBT_INFO("powering on %s", host->get_cname());
50   host->turn_on();
51   simgrid::s4u::this_actor::sleep_for(200);
52 }
53
54 int main(int argc, char* argv[])
55 {
56   simgrid::s4u::Engine e(&argc, argv);
57   e.load_platform(argv[1]);
58
59   simgrid::s4u::Actor::create("Autostart", simgrid::s4u::Host::by_name("Tremblay"), autostart);
60
61   e.run();
62   XBT_INFO("Simulation time %g", e.get_clock());
63
64   return 0;
65 }