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  public:
43  std::string process_name;
45  std::string mailbox_name;
47  std::string hostname;
48 
49  S4U_Daemon(std::string hostname, std::string process_name_prefix, std::string mailbox_prefix);
50 
51  // Daemon without a mailbox (not needed?)
52 // S4U_Daemon(std::string hostname, std::string process_name_prefix);
53 
54  virtual ~S4U_Daemon();
55 
56  void startDaemon(bool daemonized, bool auto_restart);
57 
58  void createLifeSaver(std::shared_ptr<S4U_Daemon> reference);
59 
60  virtual void cleanup();
61 
66  virtual int main() = 0;
67 
68  void setCleanlyTerminated();
69  bool hasCleanlyTerminated();
70  bool isSetToAutoRestart();
71 
72  bool join();
73 
74  void suspend();
75 
76  void resume();
77 
78  std::string getName();
79 
81  LifeSaver *life_saver = nullptr;
82 
85  protected:
86 
87  friend class S4U_DaemonActor;
88  void runMainMethod();
89 
90  void killActor();
91 
92  void acquireDaemonLock();
93 
94  void releaseDaemonLock();
95 
96  unsigned int num_starts = 0;
97  private:
98  // Lock use typically to prevent kill() from killing the actor
99  // while it's in the middle of doing something critical
100  simgrid::s4u::MutexPtr daemon_lock;
101 
102  simgrid::s4u::ActorPtr s4u_actor;
103 
104  bool cleanly_terminated; // Set to true after main returns
105  bool auto_restart; // Set to true if daemon is supposed to auto-restart
106 
107 #ifdef ACTOR_TRACKING_OUTPUT
108  std::string process_name_prefix;
109 #endif
110 
111  };
112 
113  /***********************/
115  /***********************/
116 };
117 
118 
119 #endif //WRENCH_SIM4U_DAEMONWITHMAILBOX_H
void releaseDaemonLock()
Unlock the daemon&#39;s lock.
Definition: S4U_Daemon.cpp:349
void setCleanlyTerminated()
Set the terminated status of the daemon/actor.
Definition: S4U_Daemon.cpp:315
bool hasCleanlyTerminated()
Returned the terminated status of the daemon/actor.
Definition: S4U_Daemon.cpp:308
bool join()
Join (i.e., wait for) the daemon.
Definition: S4U_Daemon.cpp:289
void acquireDaemonLock()
Lock the daemon&#39;s lock.
Definition: S4U_Daemon.cpp:342
void killActor()
Kill the daemon/actor.
Definition: S4U_Daemon.cpp:232
void resume()
Resume the daemon/actor.
Definition: S4U_Daemon.cpp:272
std::string process_name
The name of the daemon.
Definition: S4U_Daemon.h:43
std::string getName()
Retrieve the process name.
Definition: S4U_Daemon.cpp:324
LifeSaver * life_saver
The daemon&#39;s life saver.
Definition: S4U_Daemon.h:81
std::string mailbox_name
The name of the daemon&#39;s mailbox.
Definition: S4U_Daemon.h:45
bool isSetToAutoRestart()
Return the auto-restart status of the daemon.
Definition: S4U_Daemon.cpp:204
virtual void cleanup()
Cleanup function called when the daemon terminates (for whatever reason)
Definition: S4U_Daemon.cpp:109
void suspend()
Suspend the daemon/actor.
Definition: S4U_Daemon.cpp:257
void runMainMethod()
Method that run&#39;s the user-defined main method (that&#39;s called by the S4U actor class) ...
Definition: S4U_Daemon.cpp:211
std::string hostname
The name of the host on which the daemon is running.
Definition: S4U_Daemon.h:47
virtual ~S4U_Daemon()
Constructor (daemon without a mailbox)
Definition: S4U_Daemon.cpp:99
A class that provides basic simulation methods.
Definition: Simulation.h:34
The S4U actor that&#39;s the foundation for the S4U_Daemon abstraction.
Definition: S4U_DaemonActor.h:35
S4U_Daemon(std::string hostname, std::string process_name_prefix, std::string mailbox_prefix)
Constructor (daemon with a mailbox)
Definition: S4U_Daemon.cpp:39
virtual int main()=0
The daemon&#39;s main method, to be overridden.
Simulation * simulation
a pointer to the simulation object
Definition: S4U_Daemon.h:84
A generic "running daemon" abstraction that serves as a basis for all simulated processes.
Definition: S4U_Daemon.h:32
void startDaemon(bool daemonized, bool auto_restart)
Start the daemon.
Definition: S4U_Daemon.cpp:145
void createLifeSaver(std::shared_ptr< S4U_Daemon > reference)
Create a life saver for the daemon.
Definition: S4U_Daemon.cpp:332
Definition: TerminalOutput.cpp:15