WRENCH  1.11
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:
44 
45  static std::unordered_map<aid_t , simgrid::s4u::Mailbox*> map_actor_to_recv_mailbox;
46 
48  std::string process_name;
49 // /** @brief The initial name of the daemon's mailbox */
50 // std::string initial_mailbox_name;
51 // /** @brief The current name of the daemon's mailbox */
52 // std::string mailbox_name;
53 
55  simgrid::s4u::Mailbox *mailbox;
57  simgrid::s4u::Mailbox *recv_mailbox;
58 
60  std::string hostname;
61 
62  static simgrid::s4u::Mailbox *getRunningActorRecvMailbox();
63 
64  S4U_Daemon(std::string hostname, std::string process_name_prefix);
65 
66  // Daemon without a mailbox (not needed?)
67 // S4U_Daemon(std::string hostname, std::string process_name_prefix);
68 
69  virtual ~S4U_Daemon();
70 
71  void startDaemon(bool _daemonized, bool _auto_restart);
72 
73  void createLifeSaver(std::shared_ptr<S4U_Daemon> reference);
74 
75  virtual void cleanup(bool has_returned_from_main, int return_value);
76 
81  virtual int main() = 0;
82 
83  bool hasReturnedFromMain();
84  int getReturnValue();
85  bool isDaemonized();
86  bool isSetToAutoRestart();
87  void setupOnExitFunction();
88 
89  std::pair<bool, int> join();
90 
91  void suspendActor();
92 
93  void resumeActor();
94 
95 
96  std::string getName();
97 
99  enum State {
101  UP,
106  };
107 
109 
111  LifeSaver *life_saver = nullptr;
112 
114  void acquireDaemonLock();
115 
117  void releaseDaemonLock();
118 
120 
122 
123  protected:
126 
127 
130 
131  friend class S4U_DaemonActor;
132  void runMainMethod();
133 
134  bool killActor();
135 
136 
137 
139  unsigned int num_starts = 0;
140 
141  private:
142 
143 
144  // Lock used typically to prevent kill() from killing the actor
145  // while it's in the middle of doing something critical
146  simgrid::s4u::MutexPtr daemon_lock;
147 
148  simgrid::s4u::ActorPtr s4u_actor;
149 
150  bool has_returned_from_main = false; // Set to true after main returns
151  int return_value = 0; // Set to the value returned by main
152  bool daemonized; // Set to true if daemon is daemonized
153  bool auto_restart; // Set to true if daemon is supposed to auto-restart
154 
155 
156 #ifdef ACTOR_TRACKING_OUTPUT
157  std::string process_name_prefix;
158 #endif
159 
160  };
161 
162  /***********************/
164  /***********************/
165 };
166 
167 
168 #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:344
wrench::S4U_Daemon::startDaemon
void startDaemon(bool _daemonized, bool _auto_restart)
Start the daemon.
Definition: S4U_Daemon.cpp:131
wrench::S4U_Daemon::hasReturnedFromMain
bool hasReturnedFromMain()
Returns true if the daemon has returned from main() (i.e., not brutally killed)
Definition: S4U_Daemon.cpp:320
wrench::S4U_Daemon::killActor
bool killActor()
Kill the daemon/actor (does nothing if already dead)
Definition: S4U_Daemon.cpp:263
wrench::S4U_Daemon::join
std::pair< bool, int > join()
Join (i.e., wait for) the daemon.
Definition: S4U_Daemon.cpp:305
wrench::S4U_Daemon::suspendActor
void suspendActor()
Suspend the daemon/actor.
Definition: S4U_Daemon.cpp:284
wrench::S4U_Daemon::DOWN
@ DOWN
DOWN state: the daemon has been shutdown and/or has terminated.
Definition: S4U_Daemon.h:103
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:104
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 function for the actor.
Definition: S4U_Daemon.cpp:198
wrench::S4U_Daemon::isDaemonized
bool isDaemonized()
Return the daemonized status of the daemon.
Definition: S4U_Daemon.cpp:232
wrench::S4U_Daemon::recv_mailbox
simgrid::s4u::Mailbox * recv_mailbox
The daemon's receive mailbox (to send to another daemon so that that daemon can reply)
Definition: S4U_Daemon.h:57
wrench::S4U_Daemon::SUSPENDED
@ SUSPENDED
SUSPENDED state: the daemon has been suspended (and hopefully will be resumed0.
Definition: S4U_Daemon.h:105
wrench::S4U_Daemon::process_name
std::string process_name
The name of the daemon.
Definition: S4U_Daemon.h:48
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:139
wrench
Definition: Action.cpp:28
wrench::S4U_Daemon::life_saver
LifeSaver * life_saver
The daemon's life saver.
Definition: S4U_Daemon.h:111
wrench::S4U_Daemon::state
State state
The service's state.
Definition: S4U_Daemon.h:129
wrench::S4U_Daemon::S4U_Daemon
S4U_Daemon(std::string hostname, std::string process_name_prefix)
Constructor (daemon with a mailbox)
Definition: S4U_Daemon.cpp:39
wrench::S4U_Daemon::acquireDaemonLock
void acquireDaemonLock()
Method to acquire the daemon's lock.
Definition: S4U_Daemon.cpp:354
wrench::S4U_Daemon::getSimulation
Simulation * getSimulation()
Get the service's simulation.
Definition: S4U_Daemon.cpp:369
wrench::S4U_Daemon::releaseDaemonLock
void releaseDaemonLock()
Method to release the daemon's lock.
Definition: S4U_Daemon.cpp:361
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::getRunningActorRecvMailbox
static simgrid::s4u::Mailbox * getRunningActorRecvMailbox()
Return the running actor's recv mailbox.
Definition: S4U_Daemon.cpp:385
wrench::S4U_Daemon::isSetToAutoRestart
bool isSetToAutoRestart()
Return the auto-restart status of the daemon.
Definition: S4U_Daemon.cpp:224
wrench::S4U_Daemon::hostname
std::string hostname
The name of the host on which the daemon is running.
Definition: S4U_Daemon.h:60
wrench::Simulation
A class that provides basic simulation methods. Once the simulation object has been explicitly or imp...
Definition: Simulation.h:48
wrench::S4U_Daemon::getState
S4U_Daemon::State getState()
Get the daemon's state.
Definition: S4U_Daemon.cpp:118
wrench::S4U_Daemon::resumeActor
void resumeActor()
Resume the daemon/actor.
Definition: S4U_Daemon.cpp:293
wrench::S4U_Daemon::setSimulation
void setSimulation(Simulation *simulation)
Set the service's simulation.
Definition: S4U_Daemon.cpp:377
wrench::S4U_Daemon::mailbox
simgrid::s4u::Mailbox * mailbox
The daemon's mailbox.
Definition: S4U_Daemon.h:55
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:328
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:239
wrench::S4U_Daemon::State
State
Daemon states.
Definition: S4U_Daemon.h:99
wrench::S4U_Daemon::getName
std::string getName()
Retrieve the process name.
Definition: S4U_Daemon.cpp:336
wrench::S4U_Daemon::simulation
Simulation * simulation
a pointer to the simulation object
Definition: S4U_Daemon.h:125
wrench::S4U_Daemon::UP
@ UP
UP state: the daemon has been started and is still running.
Definition: S4U_Daemon.h:101