Executor.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Nicola Miskowiec
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25
26#pragma once
27
28#include <chrono>
29#include <filesystem>
30#include <future>
31#include <map>
32#include <memory>
33
37#include "ApplicationStarter.h"
38#include "StopStrategy.h"
39
40namespace ScenarioManager
41{
42 class StatusManager;
43}
44
46{
47 class StopStrategy;
48 using StopStrategyPtr = std::shared_ptr<StopStrategy>;
49
50 // Only forward declared: StatusManager.h includes this header and ApplicationLifecycle.h
51 // needs StopStrategy.h, so including it here would close an include cycle.
53 using ApplicationLifecyclePtr = std::shared_ptr<ApplicationLifecycle>;
54
55 /**
56 * @class Executor
57 * @ingroup exec
58 * @brief Starts, stops and restarts applications and scenarios.
59 * Can also be used to request the status of an application. The proper system-specific stopstrategy and starter have to be set in the constructor.
60 */
62 {
63
64 private:
65 StopStrategyPtr defaultStopStrategy;
66 ApplicationStarterPtr defaultStartStrategy;
67 std::map<Data_Structure::ScenarioPtr, StopStrategyPtr> stopStrategy;
68 std::map<Data_Structure::ScenarioPtr, ApplicationStarterPtr> starter;
69 StatusManager statusManager;
70
71 /// How long an application may take to dump its property definitions.
72 static constexpr std::chrono::milliseconds PropertyGenerationTimeout{30000};
73
74 /**
75 * Runs the application binary once with `-p -f xml -o <xmlFilePath>` so that it writes its
76 * property definitions, and waits for it to finish.
77 * @param application the application to run
78 * @param xmlFilePath where the application should write its property definitions
79 */
80 static void generatePropertiesXml(const Data_Structure::ApplicationPtr& application,
81 const std::filesystem::path& xmlFilePath);
82
83 /// Returns the lifecycle owning `application`, creating it on first use.
85
86 /// Requests `intent` for every application of `scenario`.
87 void requestForScenario(const Data_Structure::ScenarioPtr& scenario,
89 const std::string& commandLineParameters,
90 bool printOnly);
91
92 std::mutex lifecyclesMutex;
93 std::map<Data_Structure::ApplicationInstance*, ApplicationLifecyclePtr> lifecycles;
94
95 public:
96 /**
97 * Constructor that sets StopStrategy and ApplicationStarter.
98 * @param strategy Strategy used to stop applications.
99 * @param starter Starter used to start applications and request their status.
100 */
102
103 /**
104 * Requests that an application be started.
105 *
106 * All of these methods record an intent and return immediately; the work happens on a
107 * worker thread owned by the application's ApplicationLifecycle. A request supersedes any
108 * intent that has not been carried out yet, so the last thing the user asked for is what
109 * happens. Nothing is ever refused.
110 *
111 * @param application application to be started.
112 */
114 bool printOnly = false,
115 const std::string& commandLineParameters = "");
116
117 /**
118 * Requests that an application be stopped.
119 * @param application application to be stopped
120 */
122
123 /**
124 * Requests that an application be restarted. An application that is not running is started.
125 * @param application application to be restarted
126 */
128 bool printOnly = false);
129
130 /**
131 * Blocks until the application has no operation requested or running any more.
132 *
133 * The GUI never needs this - it watches the status instead - but a caller that exits right
134 * afterwards, such as the command line interface, has to see the operation through.
135 */
137
138 /// Blocks until no application of the scenario has an operation requested or running.
139 void waitForScenario(const Data_Structure::ScenarioPtr& scenario);
140
141 /**
142 * Returns the status of an application. Uses the ApplicationStarter to request the status, therefore the proper
143 * system-specific starter has to be set, even to use this method.
144 * @param application application whose status is returned
145 * @return status of the application
146 */
148
149 /**
150 * Starts a scenario. Iterates over the applications in the scenario and starts them.
151 * @param scenario scenario to be started
152 * @return {@code true} if all applications were successfully started
153 */
155 bool printOnly = false,
156 const std::string& commandLineParameters = "");
157
158 /**
159 * Stops a scenario. Iterates over the applications in the scenario and stops them.
160 * @param scenario scenario to be stopped
161 * @return {@code true} if all applications were successfully stopped
162 */
164
165 /**
166 * Restarts a scenario. Iterates over the applications in the scenario and restarts them.
167 * @param scenario scenario to be restarted
168 * @return {@code true} if all applications were successfully restarted
169 */
170 void restartScenario(Data_Structure::ScenarioPtr scenario, bool printOnly = false);
171
172 /**
173 * Generates an XML file of the given application and saves it in the specified path.
174 * If there already is an XML file at that location it only reloads the file if the executable is more recently changed
175 * than the Xml file
176 * @param application Application whose XML file is to be generate
177 * @param path location to save the XML, must be a folder
178 * @param reload forces to reload the cached XML
179 * @param set if true the newly Cached Xml gets loaded into the Application (Warning: If you load an ApplicationInstance the cfg parameter have to be reloaded)
180 */
182 std::string path,
183 bool reload = false,
184 bool set = true);
185
186 /**
187 * Sets the strategy this Executor uses to stop applications. Needs to be system specific.
188 * Use the Factory-Classes to get the proper strategy for your system.
189 * @param strategy strategy to be set
190 * @see StopStrategyFactory
191 */
194
195 /**
196 * Sets the appStarter this Executor uses to start applications and request statuses. Needs to be system specific.
197 * Use the Factory-Classes to get the proper appStarter for your system.
198 * @param appStarter appStarter to be set
199 * @see StarterFactory
200 * @see StarterFactoryLinux
201 */
204
208 };
209
210 using ExecutorPtr = std::shared_ptr<Executor>;
211} // namespace ScenarioManager::Exec
Owns every start and stop of one application - nothing else may launch or signal it.
StopStrategyPtr getStopStrategy(Data_Structure::ScenarioPtr scenario)
Definition Executor.cpp:306
void restartScenario(Data_Structure::ScenarioPtr scenario, bool printOnly=false)
Restarts a scenario.
Definition Executor.cpp:163
void setDefaultStarter(ApplicationStarterPtr appStarter)
Sets the appStarter this Executor uses to start applications and request statuses.
Definition Executor.cpp:278
static void loadAndSetCachedProperties(Data_Structure::ApplicationPtr application, std::string path, bool reload=false, bool set=true)
Generates an XML file of the given application and saves it in the specified path.
Definition Executor.cpp:218
std::string getApplicationStatus(Data_Structure::ApplicationInstancePtr application)
Returns the status of an application.
Definition Executor.cpp:116
void waitForScenario(const Data_Structure::ScenarioPtr &scenario)
Blocks until no application of the scenario has an operation requested or running.
Definition Executor.cpp:107
void restartApplication(Data_Structure::ApplicationInstancePtr application, bool printOnly=false)
Requests that an application be restarted.
Definition Executor.cpp:89
void startScenario(Data_Structure::ScenarioPtr scenario, bool printOnly=false, const std::string &commandLineParameters="")
Starts a scenario.
Definition Executor.cpp:149
Executor(StopStrategyPtr strategy, ApplicationStarterPtr starter)
Constructor that sets StopStrategy and ApplicationStarter.
Definition Executor.cpp:47
void stopScenario(Data_Structure::ScenarioPtr scenario)
Stops a scenario.
Definition Executor.cpp:157
void setDefaultStopStrategy(StopStrategyPtr strategy)
Sets the strategy this Executor uses to stop applications.
Definition Executor.cpp:272
void startApplication(Data_Structure::ApplicationInstancePtr application, bool printOnly=false, const std::string &commandLineParameters="")
Requests that an application be started.
Definition Executor.cpp:67
void stopApplication(Data_Structure::ApplicationInstancePtr application)
Requests that an application be stopped.
Definition Executor.cpp:81
StopStrategyPtr getDefaultStopStrategy()
Definition Executor.cpp:316
void setStarter(ApplicationStarterPtr appStarter, Data_Structure::ScenarioPtr scenario)
Definition Executor.cpp:290
ApplicationStarterPtr getStarter(Data_Structure::ScenarioPtr scenario)
Definition Executor.cpp:296
void setStopStrategy(StopStrategyPtr strategy, Data_Structure::ScenarioPtr scenario)
Definition Executor.cpp:284
void waitForApplication(const Data_Structure::ApplicationInstancePtr &application)
Blocks until the application has no operation requested or running any more.
Definition Executor.cpp:101
Interface for classes that define how an application get stopped.
std::shared_ptr< Scenario > ScenarioPtr
Definition Scenario.h:35
Intent
What the ScenarioManager is currently trying to do with an application.
std::shared_ptr< Application > ApplicationPtr
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
std::shared_ptr< Executor > ExecutorPtr
Definition Executor.h:210
std::shared_ptr< StopStrategy > StopStrategyPtr
Definition Executor.h:48
std::shared_ptr< ApplicationStarter > ApplicationStarterPtr
std::shared_ptr< ApplicationLifecycle > ApplicationLifecyclePtr