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 
39 using namespace ScenarioManager;
40 using namespace armarx;
41 
42 const std::string Data_Structure::ApplicationStatus::Stopped = "Stopped";
43 const std::string Data_Structure::ApplicationStatus::Running = "Running";
44 const std::string Data_Structure::ApplicationStatus::Waiting = "Waiting";
45 const std::string Data_Structure::ApplicationStatus::Mixed = "Mixed";
46 const std::string Data_Structure::ApplicationStatus::Unknown = "Unknown";
47 const std::string Data_Structure::ApplicationStatus::Inactive = "Inactive";
48 const std::string Data_Structure::ApplicationStatus::Missing = "Missing";
49 
50 std::string
52 {
53  return configDomain;
54 }
55 
56 void
58 {
60  configDomain = value;
61 }
62 
63 bool
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),
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),
104 {
105 }
106 
109 {
110  return scenario;
111 }
112 
113 std::string
115 {
116  return this->instanceName;
117 }
118 
119 void
121 {
122  this->instanceName = newName;
123 }
124 
125 std::string
127 {
128  return this->configPath;
129 }
130 
131 void
133 {
134  this->configPath = configPath;
135 }
136 
137 void
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 
164 std::string
166 {
167  return this->status;
168 }
169 
170 bool
172 {
173  bool result = status != this->status;
174  if (result)
175  {
176  this->status = status;
177  }
178  return result;
179 }
180 
181 int
183 {
184  return this->pid;
185 }
186 
187 void
188 Data_Structure::ApplicationInstance::setPid(int pid) // if PID < -1 Error?
189 {
190  this->pid = pid;
191 }
192 
193 void
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 
203 void
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 
213 void
215 {
217  parser.saveCfg(shared_from_this());
218 }
219 
220 void
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 
240 void
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 
250 void
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 
260 void
262 {
263  if (std::filesystem::exists(this->configPath))
264  {
265  std::filesystem::remove(this->configPath);
266  }
267 }
268 
269 void
271 {
272  if (!std::filesystem::exists(configPath))
273  {
274  ARMARX_WARNING_S << "Cannot find ApplicationInstance Config at:" << configPath;
275  }
276  else
277  {
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 
286 bool
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 
297 bool
299 {
300  return statusWriteBlock;
301 }
302 
303 void
305 {
306  statusWriteBlock = blocked;
307 }
308 
309 std::string
311 {
312  return nodeName;
313 }
314 
315 std::string
317 {
318  return nodeName.empty() && scenario ? scenario->getNodeName() : nodeName;
319 }
320 
321 void
323 {
324  this->nodeName = nodeName;
325 }
326 
327 bool
329 {
330  return enabled;
331 }
332 
333 void
335 {
336  this->enabled = enabled;
337 }
ScenarioManager::Data_Structure::ScenarioPtr
std::shared_ptr< Scenario > ScenarioPtr
Definition: Scenario.h:35
ScenarioManager::Data_Structure::ApplicationInstance::getPid
int getPid()
Definition: ApplicationInstance.cpp:182
ScenarioManager::Data_Structure::ApplicationInstance::modifyProperty
void modifyProperty(std::string name, std::string value)
Changes the value of the specified property.
Definition: ApplicationInstance.cpp:194
ScenarioManager::Data_Structure::ApplicationInstance
Class containing data about the instance of an application. Provides methods to get and set the data....
Definition: ApplicationInstance.h:63
ScenarioManager::Data_Structure::ApplicationInstance::getNodeName
std::string getNodeName() const
Definition: ApplicationInstance.cpp:310
PropertyDefinitionContainerFormatter.h
ScenarioManager::Parser::IceParser
Definition: iceparser.h:39
ScenarioManager::Data_Structure::ApplicationStatus
Definition: ApplicationInstance.h:45
ScenarioManager::Data_Structure::ApplicationStatus::Stopped
static const std::string Stopped
Definition: ApplicationInstance.h:47
ScenarioManager::Data_Structure::ApplicationInstance::getEffectiveNodeName
std::string getEffectiveNodeName() const
Definition: ApplicationInstance.cpp:316
ScenarioManager::Data_Structure::ApplicationInstance::setStatus
bool setStatus(const std::string &status)
Sets the status of this ApplicationInstance.
Definition: ApplicationInstance.cpp:171
PropertyDefinitionFormatter.h
ScenarioManager::Data_Structure::ApplicationStatus::Missing
static const std::string Missing
Definition: ApplicationInstance.h:53
ScenarioManager::Data_Structure::ApplicationStatus::Running
static const std::string Running
Definition: ApplicationInstance.h:48
ScenarioManager::Data_Structure::ApplicationInstance::setConfigDomain
void setConfigDomain(const std::string &value)
Definition: ApplicationInstance.cpp:57
ScenarioManager::Data_Structure::ApplicationInstance::getScenario
ScenarioPtr getScenario()
Definition: ApplicationInstance.cpp:108
ScenarioManager::Data_Structure::Application
Class containing data about an application Provides methods to get and set the date contained in the ...
Definition: Application.h:46
ScenarioManager::Data_Structure::ApplicationInstance::setInstanceName
void setInstanceName(std::string newName)
Sets the name of this ApplicationInstance.
Definition: ApplicationInstance.cpp:120
ScenarioManager::Data_Structure::ApplicationInstance::getConfigDomain
std::string getConfigDomain() const
Definition: ApplicationInstance.cpp:51
armarx::navigation::algorithms::save
bool save(const Costmap &costmap, const std::filesystem::path &directory)
Definition: persistence.cpp:105
ScenarioManager::Data_Structure::ApplicationInstance::getStatus
std::string getStatus()
Returns the status.
Definition: ApplicationInstance.cpp:165
armarx::status
status
Definition: FiniteStateMachine.h:244
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
ScenarioManager::Data_Structure::ApplicationInstance::deleteConfig
void deleteConfig()
Definition: ApplicationInstance.cpp:261
ScenarioManager::Data_Structure::ApplicationInstance::ApplicationInstance
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.
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
ScenarioManager::Data_Structure::ApplicationInstance::resetConfigPath
void resetConfigPath()
resetConfigPath.
Definition: ApplicationInstance.cpp:138
PropertyDefinitionConfigFormatter.h
ScenarioManager::Data_Structure::ApplicationStatus::Waiting
static const std::string Waiting
Definition: ApplicationInstance.h:49
ScenarioManager::Data_Structure::ApplicationInstance::getConfigPath
std::string getConfigPath()
Definition: ApplicationInstance.cpp:126
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
ScenarioManager::Data_Structure::ApplicationInstance::setPid
void setPid(int pid)
Sets the pid of this ApplicationInstance.
Definition: ApplicationInstance.cpp:188
ScenarioManager::Data_Structure::ApplicationInstance::isConfigWritable
bool isConfigWritable()
Definition: ApplicationInstance.cpp:287
ScenarioManager::Data_Structure::ApplicationStatus::Inactive
static const std::string Inactive
Definition: ApplicationInstance.h:52
ScenarioManager::Data_Structure::ApplicationInstance::copyFrom
void copyFrom(ApplicationInstance &other)
copyFrom Copy the other instance configuration to this one.
Definition: ApplicationInstance.cpp:251
ScenarioManager::Data_Structure::ApplicationStatus::Mixed
static const std::string Mixed
Definition: ApplicationInstance.h:50
ScenarioManager::Data_Structure::ApplicationStatus::Unknown
static const std::string Unknown
Definition: ApplicationInstance.h:51
ScenarioManager::Data_Structure::ApplicationInstance::setStatusWriteBlock
void setStatusWriteBlock(bool blocked)
Definition: ApplicationInstance.cpp:304
armarx::Application
Baseclass for all ArmarX applications.
Definition: Application.h:193
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:213
ScenarioManager::Data_Structure::ApplicationInstance::addProperty
void addProperty(std::string name, std::string value)
Adds a new property with the specified name and value to this ApplicationInstance.
Definition: ApplicationInstance.cpp:204
ApplicationInstance.h
ScenarioManager::Data_Structure::ApplicationInstance::save
void save()
Saves the IceProperties to the configPath.
Definition: ApplicationInstance.cpp:214
ExpressionException.h
ScenarioManager::Data_Structure::ApplicationInstance::getInstanceName
std::string getInstanceName()
Definition: ApplicationInstance.cpp:114
IceStorm::parser
Parser * parser
Definition: Parser.cpp:34
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ScenarioManager
Definition: Application.cpp:180
ScenarioManager::Data_Structure::ApplicationInstance::setEnabled
void setEnabled(bool enabled)
Definition: ApplicationInstance.cpp:334
ScenarioManager::Data_Structure::ApplicationInstance::load
void load(bool firstLoad=false)
Loades the IceProperties from the configPath Firstload determins if properties that equal their defau...
Definition: ApplicationInstance.cpp:270
ScenarioManager::Data_Structure::ApplicationInstance::getEnabled
bool getEnabled()
Definition: ApplicationInstance.cpp:328
ScenarioManager::Data_Structure::ApplicationInstance::getStatusWriteBlock
bool getStatusWriteBlock()
Definition: ApplicationInstance.cpp:298
ScenarioManager::Data_Structure::ApplicationInstance::makeLocal
void makeLocal()
Make a local copy of this instance if it is linked and nothing otherwise.
Definition: ApplicationInstance.cpp:221
ScenarioManager::Data_Structure::ApplicationInstance::linkFrom
void linkFrom(ApplicationInstance &other)
linkFrom Link this instance configuration from the other instance.
Definition: ApplicationInstance.cpp:241
Logging.h
ScenarioManager::Data_Structure::ApplicationInstance::isReadOnly
bool isReadOnly() const
Definition: ApplicationInstance.cpp:64
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::server::ltm::mongodb::util::load
void load(const mongocxx::database &db, armem::wm::Memory &m)
Definition: operations.cpp:48
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
ScenarioManager::Data_Structure::ApplicationInstance::setNodeName
void setNodeName(std::string nodeName)
Definition: ApplicationInstance.cpp:322
ScenarioManager::Data_Structure::ApplicationInstance::setConfigPath
void setConfigPath(std::string configPath)
Sets the config path of this ApplicationInstance.
Definition: ApplicationInstance.cpp:132