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,
94  DOWN,
96  SUSPENDED,
97  };
98 
99  S4U_Daemon::State getState();
100 
102  LifeSaver *life_saver = nullptr;
103 
105  Simulation *simulation;
106  protected:
107 
109  State state;
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
Definition: Alarm.cpp:20