WRENCH  1.10
Cyberinfrastructure Simulation Workbench
Overview Installation Getting Started WRENCH 101 WRENCH 102
S4U_Daemon.h
1 
10 #ifndef WRENCH_SIM4U_DAEMON_H
11 #define WRENCH_SIM4U_DAEMON_H
12 
13 #include <string>
14 
15 #include <simgrid/s4u.hpp>
16 #include <iostream>
17 
18 //#define ACTOR_TRACKING_OUTPUT yes
19 
20 
21 namespace wrench {
22 
23  /***********************/
25  /***********************/
26 
27  class Simulation;
28 
32  class S4U_Daemon {
33 
34  class LifeSaver {
35  public:
36  explicit LifeSaver(std::shared_ptr<S4U_Daemon> &reference) : reference(reference) {}
37 
38  std::shared_ptr<S4U_Daemon> reference;
39  };
40 
41 
42 
43  public:
45  std::string process_name;
47  std::string initial_mailbox_name;
49  std::string mailbox_name;
51  std::string hostname;
52 
53 
54 
55  S4U_Daemon(std::string hostname, std::string process_name_prefix, std::string mailbox_prefix);
56 
57  // Daemon without a mailbox (not needed?)
58 // S4U_Daemon(std::string hostname, std::string process_name_prefix);
59 
60  virtual ~S4U_Daemon();
61 
62  void startDaemon(bool _daemonized, bool _auto_restart);
63 
64  void createLifeSaver(std::shared_ptr<S4U_Daemon> reference);
65 
66  virtual void cleanup(bool has_returned_from_main, int return_value);
67 
72  virtual int main() = 0;
73 
74  bool hasReturnedFromMain();
75  int getReturnValue();
76  bool isDaemonized();
77  bool isSetToAutoRestart();
78  void setupOnExitFunction();
79 
80  std::pair<bool, int> join();
81 
82  void suspendActor();
83 
84  void resumeActor();
85 
86 
87  std::string getName();
88 
90  enum State {
92  UP,
97  };
98 
100 
102  LifeSaver *life_saver = nullptr;
103 
106  protected:
107 
110 
111  friend class S4U_DaemonActor;
112  void runMainMethod();
113 
114  void killActor();
115 
116  void acquireDaemonLock();
117 
118  void releaseDaemonLock();
119 
121  unsigned int num_starts = 0;
122 
123  private:
124 
125 
126  // Lock used typically to prevent kill() from killing the actor
127  // while it's in the middle of doing something critical
128  simgrid::s4u::MutexPtr daemon_lock;
129 
130  simgrid::s4u::ActorPtr s4u_actor;
131 
132  bool has_returned_from_main = false; // Set to true after main returns
133  int return_value = 0; // Set to the value returned by main
134  bool daemonized; // Set to true if daemon is daemonized
135  bool auto_restart; // Set to true if daemon is supposed to auto-restart
136 
137 
138 #ifdef ACTOR_TRACKING_OUTPUT
139  std::string process_name_prefix;
140 #endif
141 
142  };
143 
144  /***********************/
146  /***********************/
147 };
148 
149 
150 #endif //WRENCH_SIM4U_DAEMONWITHMAILBOX_H
wrench::S4U_Daemon::createLifeSaver
void createLifeSaver(std::shared_ptr< S4U_Daemon > reference)
Create a life saver for the daemon.
Definition: S4U_Daemon.cpp:340
wrench::S4U_Daemon::startDaemon
void startDaemon(bool _daemonized, bool _auto_restart)
Start the daemon.
Definition: S4U_Daemon.cpp:140
wrench::S4U_Daemon::hasReturnedFromMain
bool hasReturnedFromMain()
Returns true if the daemon has returned from main() (i.e., not brutally killed)
Definition: S4U_Daemon.cpp:316
wrench::S4U_Daemon::join
std::pair< bool, int > join()
Join (i.e., wait for) the daemon.
Definition: S4U_Daemon.cpp:301
wrench::S4U_Daemon::suspendActor
void suspendActor()
Suspend the daemon/actor.
Definition: S4U_Daemon.cpp:280
wrench::S4U_Daemon::DOWN
@ DOWN
DOWN state: the daemon has been shutdown and/or has terminated.
Definition: S4U_Daemon.h:94
wrench::S4U_Daemon::~S4U_Daemon
virtual ~S4U_Daemon()
Definition: S4U_Daemon.cpp:83
wrench::S4U_Daemon::cleanup
virtual void cleanup(bool has_returned_from_main, int return_value)
Cleanup function called when the daemon terminates (for whatever reason). The default behavior is to ...
Definition: S4U_Daemon.cpp:113
wrench::S4U_Daemon::main
virtual int main()=0
The daemon's main method, to be overridden.
wrench::S4U_Daemon::setupOnExitFunction
void setupOnExitFunction()
Sets up the on_exit functionf for the actor.
Definition: S4U_Daemon.cpp:206
wrench::S4U_Daemon::initial_mailbox_name
std::string initial_mailbox_name
The initial name of the daemon's mailbox.
Definition: S4U_Daemon.h:47
wrench::S4U_Daemon::isDaemonized
bool isDaemonized()
Return the daemonized status of the daemon.
Definition: S4U_Daemon.cpp:240
wrench::S4U_Daemon::SUSPENDED
@ SUSPENDED
SUSPENDED state: the daemon has been suspended (and hopefully will be resumed0.
Definition: S4U_Daemon.h:96
wrench::S4U_Daemon::process_name
std::string process_name
The name of the daemon.
Definition: S4U_Daemon.h:45
wrench::S4U_Daemon::num_starts
unsigned int num_starts
The number of time that this daemon has started (i.e., 1 + number of restarts)
Definition: S4U_Daemon.h:121
wrench
Definition: Alarm.cpp:20
wrench::S4U_Daemon::life_saver
LifeSaver * life_saver
The daemon's life saver.
Definition: S4U_Daemon.h:102
wrench::S4U_Daemon::state
State state
The service's state.
Definition: S4U_Daemon.h:109
wrench::S4U_Daemon::acquireDaemonLock
void acquireDaemonLock()
Lock the daemon's lock.
Definition: S4U_Daemon.cpp:350
wrench::S4U_Daemon::releaseDaemonLock
void releaseDaemonLock()
Unlock the daemon's lock.
Definition: S4U_Daemon.cpp:357
wrench::S4U_Daemon
A generic "running daemon" abstraction that serves as a basis for all simulated processes.
Definition: S4U_Daemon.h:32
wrench::S4U_Daemon::isSetToAutoRestart
bool isSetToAutoRestart()
Return the auto-restart status of the daemon.
Definition: S4U_Daemon.cpp:232
wrench::S4U_Daemon::hostname
std::string hostname
The name of the host on which the daemon is running.
Definition: S4U_Daemon.h:51
wrench::Simulation
A class that provides basic simulation methods. Once the simulation object has been explicitly or imp...
Definition: Simulation.h:46
wrench::S4U_Daemon::getState
S4U_Daemon::State getState()
Get the daemon's state.
Definition: S4U_Daemon.cpp:127
wrench::S4U_Daemon::resumeActor
void resumeActor()
Resume the daemon/actor.
Definition: S4U_Daemon.cpp:289
wrench::S4U_Daemon::killActor
void killActor()
Kill the daemon/actor (does nothing if already dead)
Definition: S4U_Daemon.cpp:264
wrench::S4U_DaemonActor
The S4U actor that's the foundation for the S4U_Daemon abstraction.
Definition: S4U_DaemonActor.h:36
wrench::S4U_Daemon::getReturnValue
int getReturnValue()
Returns the value returned by main() (if the daemon has returned from main)
Definition: S4U_Daemon.cpp:324
wrench::S4U_Daemon::runMainMethod
void runMainMethod()
Method that run's the user-defined main method (that's called by the S4U actor class)
Definition: S4U_Daemon.cpp:247
wrench::S4U_Daemon::State
State
Daemon states.
Definition: S4U_Daemon.h:90
wrench::S4U_Daemon::getName
std::string getName()
Retrieve the process name.
Definition: S4U_Daemon.cpp:332
wrench::S4U_Daemon::S4U_Daemon
S4U_Daemon(std::string hostname, std::string process_name_prefix, std::string mailbox_prefix)
Constructor (daemon with a mailbox)
Definition: S4U_Daemon.cpp:38
wrench::S4U_Daemon::mailbox_name
std::string mailbox_name
The current name of the daemon's mailbox.
Definition: S4U_Daemon.h:49
wrench::S4U_Daemon::simulation
Simulation * simulation
a pointer to the simulation object
Definition: S4U_Daemon.h:105
wrench::S4U_Daemon::UP
@ UP
UP state: the daemon has been started and is still running.
Definition: S4U_Daemon.h:92