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,
103  DOWN,
105  SUSPENDED,
106  };
107 
108  S4U_Daemon::State getState();
109 
111  LifeSaver *life_saver = nullptr;
112 
114  void acquireDaemonLock();
115 
117  void releaseDaemonLock();
118 
119  Simulation *getSimulation();
120 
121  void setSimulation(Simulation *simulation);
122 
123  protected:
125  Simulation *simulation;
126 
127 
129  State state;
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
Definition: Action.cpp:28