ApplicationInstance.cpp
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 Dennis Weigelt
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25
26#include "ApplicationInstance.h"
27
28#include <filesystem>
29
35
36#include "../parser/iceparser.h"
37
38
39using namespace ScenarioManager;
40using namespace armarx;
41
42const std::string Data_Structure::ApplicationStatus::Stopped = "Stopped";
43const std::string Data_Structure::ApplicationStatus::Running = "Running";
44const std::string Data_Structure::ApplicationStatus::Waiting = "Waiting";
45const std::string Data_Structure::ApplicationStatus::Mixed = "Mixed";
46const std::string Data_Structure::ApplicationStatus::Unknown = "Unknown";
47const std::string Data_Structure::ApplicationStatus::Inactive = "Inactive";
48const std::string Data_Structure::ApplicationStatus::Missing = "Missing";
49
50std::string
52{
53 return configDomain;
54}
55
56void
58{
59 ARMARX_CHECK_EXPRESSION(!value.empty());
60 configDomain = value;
61}
62
63bool
65{
66 return this->readOnly;
67}
68
70 std::string executablePath,
71 std::string instanceName,
72 std::string configPath,
73 std::string packageName,
74 ScenarioPtr scenario,
75 std::string node,
76 bool enabled) :
77 Application(executableName, executablePath, packageName),
78 instanceName(instanceName),
79 configPath(configPath),
80 status(ApplicationStatus::Unknown),
81 nodeName(node),
82 scenario(scenario),
83 pid(-1),
84 statusWriteBlock(false),
85 enabled(enabled)
86{
87}
88
90 std::string instanceName,
91 std::string configPath,
92 ScenarioPtr scenario,
93 std::string node,
94 bool enabled) :
95 Application(application),
96 instanceName(instanceName),
97 configPath(configPath),
98 status(ApplicationStatus::Unknown),
99 nodeName(node),
100 scenario(scenario),
101 pid(-1),
102 statusWriteBlock(false),
103 enabled(enabled)
104{
105}
106
112
113std::string
115{
116 return this->instanceName;
117}
118
119void
121{
122 this->instanceName = newName;
123}
124
125std::string
127{
128 return this->configPath;
129}
130
131void
133{
134 this->configPath = configPath;
135}
136
137void
139{
140 std::filesystem::path scenariosFolder(scenario->getPackage()->getScenarioPath());
141 std::filesystem::path scenarioFolder = scenariosFolder / scenario->getName();
142 std::filesystem::path scenarioCfgFolder = scenarioFolder / "config";
143
144 std::filesystem::path configPath = scenarioCfgFolder;
145
146 if (!this->instanceName.empty() && this->instanceName != R"("")" &&
147 this->instanceName != R"(" ")")
148 {
149 configPath = configPath / (this->getName() + "." + this->getInstanceName() + ".cfg");
150 }
151 else
152 {
153 configPath = configPath / (this->getName() + ".cfg");
154 ;
155 }
156
157 if (configPath != this->configPath)
158 {
159 std::filesystem::remove(std::filesystem::path(this->configPath));
160 }
161 this->configPath = configPath.string();
162}
163
164std::string
166{
167 return this->status;
168}
169
170bool
172{
173 bool result = status != this->status;
174 if (result)
175 {
176 this->status = status;
177 }
178 return result;
179}
180
181int
183{
184 return this->pid;
185}
186
187void
189{
190 this->pid = pid;
191}
192
193void
194Data_Structure::ApplicationInstance::modifyProperty(std::string name, std::string value)
195{
196 if (!properties->isPropertySet(name))
197 {
198 properties->defineOptionalProperty(name, std::string("::NOT_DEFINED::"), "Custom Property");
199 }
200 properties->getProperties()->setProperty(name, value);
201}
202
203void
204Data_Structure::ApplicationInstance::addProperty(std::string name, std::string value)
205{
206 if (!properties->isPropertySet(name))
207 {
208 properties->defineOptionalProperty(name, std::string("::NOT_DEFINED::"), "Custom Property");
209 }
210 properties->getProperties()->setProperty(name, value);
211}
212
213void
215{
216 Parser::IceParser parser;
217 parser.saveCfg(shared_from_this());
218}
219
220void
222{
223 if (!isReadOnly())
224 {
225 return;
226 }
227
228 if (!std::filesystem::is_symlink(configPath))
229 {
230 ARMARX_WARNING << "Make local called on non-linked application";
231 return;
232 }
233
234 std::filesystem::remove(configPath);
235 this->save();
236 // Reload to register status changes
237 this->load();
238}
239
240void
242{
243 deleteConfig();
244 load(true);
245 auto target_path = other.configPath.lexically_relative(configPath.parent_path());
246 std::filesystem::create_symlink(target_path, configPath);
247 load();
248}
249
250void
252{
253 deleteConfig();
254 load(true);
255 std::filesystem::copy_file(
256 other.configPath, configPath, std::filesystem::copy_options::overwrite_existing);
257 load();
258}
259
260void
262{
263 if (std::filesystem::exists(this->configPath))
264 {
265 std::filesystem::remove(this->configPath);
266 }
267}
268
269void
271{
272 if (!std::filesystem::exists(configPath))
273 {
274 ARMARX_WARNING_S << "Cannot find ApplicationInstance Config at:" << configPath;
275 }
276 else
277 {
278 Parser::IceParser parser;
279 setProperties(parser.mergeXmlAndCfg(shared_from_this(), firstLoad));
280 }
281
282 // If the config file is a symlink, put app into read-only mode
283 this->readOnly = std::filesystem::is_symlink(configPath);
284}
285
286bool
288{
289 std::ofstream ofs;
290
291 ofs.open(configPath.c_str(), std::ofstream::out | std::ofstream::app);
292 ARMARX_DEBUG << configPath << " is writeable: " << ofs.is_open();
293
294 return ofs.is_open();
295}
296
297bool
299{
300 return statusWriteBlock;
301}
302
303void
305{
306 statusWriteBlock = blocked;
307}
308
309std::string
311{
312 return nodeName;
313}
314
315std::string
317{
318 return nodeName.empty() && scenario ? scenario->getNodeName() : nodeName;
319}
320
321void
323{
324 this->nodeName = nodeName;
325}
326
327bool
332
333void
335{
336 this->enabled = enabled;
337}
ApplicationInstance(std::string executableName, std::string executablePath, std::string instanceName, std::string configPath, std::string packageName, ScenarioPtr scenarioName, std::string node, bool enabled)
Constructor that sets the base data of this ApplicationInstance.
void setInstanceName(std::string newName)
Sets the name of this ApplicationInstance.
void setPid(int pid)
Sets the pid of this ApplicationInstance.
void addProperty(std::string name, std::string value)
Adds a new property with the specified name and value to this ApplicationInstance.
void setConfigPath(std::string configPath)
Sets the config path of this ApplicationInstance.
void modifyProperty(std::string name, std::string value)
Changes the value of the specified property.
void makeLocal()
Make a local copy of this instance if it is linked and nothing otherwise.
void save()
Saves the IceProperties to the configPath.
void copyFrom(ApplicationInstance &other)
copyFrom Copy the other instance configuration to this one.
void linkFrom(ApplicationInstance &other)
linkFrom Link this instance configuration from the other instance.
void load(bool firstLoad=false)
Loades the IceProperties from the configPath Firstload determins if properties that equal their defau...
bool setStatus(const std::string &status)
Sets the status of this ApplicationInstance.
void setProperties(armarx::PropertyDefinitionsPtr properties)
Sets the properties of this application.
armarx::PropertyDefinitionsPtr properties
Definition Application.h:57
Application(std::string name, std::string executablePath, std::string packageName)
Constructor that sets the name and the path to the executable of the application.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
std::shared_ptr< Scenario > ScenarioPtr
Definition Scenario.h:35
This file offers overloads of toIce() and fromIce() functions for STL container types.