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
void createLifeSaver(std::shared_ptr< S4U_Daemon > reference)
Create a life saver for the daemon.
Definition: S4U_Daemon.cpp:340
bool hasReturnedFromMain()
Returns true if the daemon has returned from main() (i.e., not brutally killed)
Definition: S4U_Daemon.cpp:316
std::pair< bool, int > join()
Join (i.e., wait for) the daemon.
Definition: S4U_Daemon.cpp:301
void suspendActor()
Suspend the daemon/actor.
Definition: S4U_Daemon.cpp:280
void startDaemon(bool daemonized, bool auto_restart)
Start the daemon.
Definition: S4U_Daemon.cpp:140
@ DOWN
DOWN state: the daemon has been shutdown and/or has terminated.
Definition: S4U_Daemon.h:94
virtual ~S4U_Daemon()
Definition: S4U_Daemon.cpp:83
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
virtual int main()=0
The daemon's main method, to be overridden.
void setupOnExitFunction()
Sets up the on_exit functionf for the actor.
Definition: S4U_Daemon.cpp:206
std::string initial_mailbox_name
The initial name of the daemon's mailbox.
Definition: S4U_Daemon.h:47
bool isDaemonized()
Return the daemonized status of the daemon.
Definition: S4U_Daemon.cpp:240
@ SUSPENDED
SUSPENDED state: the daemon has been suspended (and hopefully will be resumed0.
Definition: S4U_Daemon.h:96
std::string process_name
The name of the daemon.
Definition: S4U_Daemon.h:45
unsigned int num_starts
The number of time that this daemon has started (i.e., 1 + number of restarts)
Definition: S4U_Daemon.h:121
Definition: Alarm.cpp:20
LifeSaver * life_saver
The daemon's life saver.
Definition: S4U_Daemon.h:102
State state
The service's state.
Definition: S4U_Daemon.h:109
void acquireDaemonLock()
Lock the daemon's lock.
Definition: S4U_Daemon.cpp:350
void releaseDaemonLock()
Unlock the daemon's lock.
Definition: S4U_Daemon.cpp:357
A generic "running daemon" abstraction that serves as a basis for all simulated processes.
Definition: S4U_Daemon.h:32
bool isSetToAutoRestart()
Return the auto-restart status of the daemon.
Definition: S4U_Daemon.cpp:232
std::string hostname
The name of the host on which the daemon is running.
Definition: S4U_Daemon.h:51
A class that provides basic simulation methods. Once the simulation object has been explicitly or imp...
Definition: Simulation.h:46
S4U_Daemon::State getState()
Get the daemon's state.
Definition: S4U_Daemon.cpp:127
void resumeActor()
Resume the daemon/actor.
Definition: S4U_Daemon.cpp:289
void killActor()
Kill the daemon/actor (does nothing if already dead)
Definition: S4U_Daemon.cpp:264
The S4U actor that's the foundation for the S4U_Daemon abstraction.
Definition: S4U_DaemonActor.h:36
int getReturnValue()
Returns the value returned by main() (if the daemon has returned from main)
Definition: S4U_Daemon.cpp:324
void runMainMethod()
Method that run's the user-defined main method (that's called by the S4U actor class)
Definition: S4U_Daemon.cpp:247
State
Daemon states.
Definition: S4U_Daemon.h:90
std::string getName()
Retrieve the process name.
Definition: S4U_Daemon.cpp:332
S4U_Daemon(std::string hostname, std::string process_name_prefix, std::string mailbox_prefix)
Constructor (daemon with a mailbox)
Definition: S4U_Daemon.cpp:38
std::string mailbox_name
The current name of the daemon's mailbox.
Definition: S4U_Daemon.h:49
Simulation * simulation
a pointer to the simulation object
Definition: S4U_Daemon.h:105
@ UP
UP state: the daemon has been started and is still running.
Definition: S4U_Daemon.h:92