Service.h
1 
11 #ifndef WRENCH_SERVICE_H
12 #define WRENCH_SERVICE_H
13 
14 
15 #include <string>
16 #include <map>
17 
18 #include <wrench/simgrid_S4U_util/S4U_Daemon.h>
19 
20 namespace wrench {
21 
26  class Service : public S4U_Daemon {
27 
28  public:
29 
30 
31 
32  /***********************/
34  /***********************/
35 
36  void start(std::shared_ptr<Service> this_service, bool daemonize, bool auto_restart);
37  virtual void stop();
38  void suspend();
39  void resume();
40 
41  std::string getHostname();
42 
43  bool isUp();
44 
45  std::string getPropertyValueAsString(std::string);
46  double getPropertyValueAsDouble(std::string);
47  unsigned long getPropertyValueAsUnsignedLong(std::string);
48  bool getPropertyValueAsBoolean(std::string);
49 
50  void assertServiceIsUp();
51 
52 
53  double getNetworkTimeoutValue();
54  void setNetworkTimeoutValue(double value);
55 
56  /***********************/
58  /***********************/
59 
60  /***********************/
62  /***********************/
63 
64  double getMessagePayloadValue(std::string);
65 
66  void setStateToDown();
67 
69  static void clearTrackedServices();
70  static void cleanupTrackedServices();
71 
72  /***********************/
74  /***********************/
75 
76  protected:
77 
78  /***********************/
80  /***********************/
81 
86  static void assertServiceIsUp(std::shared_ptr<Service> s) { s->assertServiceIsUp(); };
87 
88  friend class Simulation;
89 
90  ~Service();
91 
92  Service(std::string hostname, std::string process_name_prefix, std::string mailbox_name_prefix);
93 
94  // Property stuff
95  void setProperty(std::string, std::string);
96 
97  void setProperties(std::map<std::string, std::string> default_property_values,
98  std::map<std::string, std::string> overriden_property_values);
99 
100  // MessagePayload stuff
101  void setMessagePayload(std::string, double);
102 
103  void setMessagePayloads(std::map<std::string, double> default_messagepayload_values,
104  std::map<std::string, double> overriden_messagepayload_values);
105 
106 
107  void serviceSanityCheck();
108 
110  std::map<std::string, std::string> property_list;
111 
113  std::map<std::string, double> messagepayload_list;
114 
116  std::string name;
117 
121  double network_timeout = 30.0;
122 
128  template <class T>
129  std::shared_ptr<T> getSharedPtr() {
130  if (Service::service_shared_ptr_map.find(this) == Service::service_shared_ptr_map.end()) {
131  throw std::runtime_error("Service::getSharedPtr(): master shared_ptr to service not found! This should happen only "
132  "if the service has not been started, in which case this method shouldn't have been called");
133  }
134  auto shared_ptr = std::dynamic_pointer_cast<T>(Service::service_shared_ptr_map[this]);
135  if (not shared_ptr) {
136  throw std::runtime_error("Service::getSharedPtr(): Invalid provided template");
137  }
138  return shared_ptr;
139  }
140 
147  template <class T>
148  static std::shared_ptr<T> getServiceByName(std::string name) {
149  for (auto const &s : Service::service_shared_ptr_map) {
150  if (s.first->getName() == name) {
151  return std::dynamic_pointer_cast<T>(s.second);
152  }
153  }
154  return nullptr;
155  }
156 
157 
158  private:
159 
160  static std::unordered_map<Service *, std::shared_ptr<Service>> service_shared_ptr_map;
161 
162  bool shutting_down = false;
163 
164  static unsigned long num_terminated_services;
165 
166 
167  /***********************/
169  /***********************/
170 
171  };
172 };
173 
174 
175 #endif //WRENCH_SERVICE_H
void setMessagePayload(std::string, double)
Set a message payload of the Service.
Definition: Service.cpp:100
void suspend()
Suspend the service.
Definition: Service.cpp:312
static void increaseNumCompletedServicesCount()
Increase the completed service count.
Definition: Service.cpp:31
void setProperties(std::map< std::string, std::string > default_property_values, std::map< std::string, std::string > overriden_property_values)
Set default and user-defined properties.
Definition: Service.cpp:366
double getMessagePayloadValue(std::string)
Get a message payload of the Service as a double.
Definition: Service.cpp:187
static void clearTrackedServices()
Forget all tracked services.
Definition: Service.cpp:38
void setProperty(std::string, std::string)
Set a property of the Service.
Definition: Service.cpp:85
void assertServiceIsUp()
Throws an exception if the service is not up.
Definition: Service.cpp:428
static void assertServiceIsUp(std::shared_ptr< Service > s)
Assert for the service being up.
Definition: Service.h:86
std::map< std::string, std::string > property_list
The service&#39;s property list.
Definition: Service.h:110
static void cleanupTrackedServices()
Go through the tracked services and remove all entries with a refcount of 1!
Definition: Service.cpp:45
A service that can be added to the simulation and that can be used by a WMS when executing a workflow...
Definition: Service.h:26
std::string getHostname()
Get the name of the host on which the service is / will be running.
Definition: Service.cpp:342
void serviceSanityCheck()
Check whether the service is properly configured and running.
Definition: Service.cpp:403
Service(std::string hostname, std::string process_name_prefix, std::string mailbox_name_prefix)
Constructor.
Definition: Service.cpp:75
void setMessagePayloads(std::map< std::string, double > default_messagepayload_values, std::map< std::string, double > overriden_messagepayload_values)
Set default and user-defined message payloads.
Definition: Service.cpp:384
void resume()
Resume the service.
Definition: Service.cpp:328
std::string getPropertyValueAsString(std::string)
Get a property of the Service as a string.
Definition: Service.cpp:118
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:45
virtual void stop()
Synchronously stop the service (does nothing if the service is already stopped)
Definition: Service.cpp:264
std::string name
The service&#39;s name.
Definition: Service.h:116
void setNetworkTimeoutValue(double value)
Sets the service&#39;s network timeout value.
Definition: Service.cpp:420
void setStateToDown()
Set the state of the service to DOWN.
Definition: Service.cpp:357
static std::shared_ptr< T > getServiceByName(std::string name)
Method to retrieve the shared_ptr to a service based on the service&#39;s name (not efficient) ...
Definition: Service.h:148
unsigned long getPropertyValueAsUnsignedLong(std::string)
Get a property of the Service as an unsigned long.
Definition: Service.cpp:160
double getPropertyValueAsDouble(std::string)
Get a property of the Service as a double.
Definition: Service.cpp:134
double network_timeout
The time (in seconds) after which a service that doesn&#39;t send back a reply (control) message causes a...
Definition: Service.h:121
bool isUp()
Returns true if the service is UP, false otherwise.
Definition: Service.cpp:350
bool getPropertyValueAsBoolean(std::string)
Get a property of the Service as a boolean.
Definition: Service.cpp:204
double getNetworkTimeoutValue()
Returns the service&#39;s network timeout value.
Definition: Service.cpp:411
A generic "running daemon" abstraction that serves as a basis for all simulated processes.
Definition: S4U_Daemon.h:32
void start(std::shared_ptr< Service > this_service, bool daemonize, bool auto_restart)
Start the service.
Definition: Service.cpp:232
~Service()
Destructor.
Definition: Service.cpp:65
std::shared_ptr< T > getSharedPtr()
Method to retrieve the shared_ptr to a service.
Definition: Service.h:129
Definition: Alarm.cpp:19
std::map< std::string, double > messagepayload_list
The service&#39;s messagepayload list.
Definition: Service.h:113