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 "../parser/iceparser.h"
35 
36 #include <filesystem>
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 
51 {
52  return configDomain;
53 }
54 
56 {
58  configDomain = value;
59 }
60 
62 {
63  return this->readOnly;
64 }
65 
66 Data_Structure::ApplicationInstance::ApplicationInstance(std::string executableName, std::string executablePath,
67  std::string instanceName, std::string configPath,
68  std::string packageName, ScenarioPtr scenario,
69  std::string node, bool enabled)
70  : Application(executableName, executablePath, packageName), instanceName(instanceName)
71  , configPath(configPath)
72  , status(ApplicationStatus::Unknown), nodeName(node), scenario(scenario), pid(-1), statusWriteBlock(false), enabled(enabled)
73 {
74 }
75 
76 
77 Data_Structure::ApplicationInstance::ApplicationInstance(Application application, std::string instanceName,
78  std::string configPath, ScenarioPtr scenario,
79  std::string node, bool enabled)
80  : Application(application),
81  instanceName(instanceName),
82  configPath(configPath),
83  status(ApplicationStatus::Unknown),
84  nodeName(node),
85  scenario(scenario),
86  pid(-1),
87  statusWriteBlock(false),
89 {
90 }
91 
93 {
94  return scenario;
95 }
96 
98 {
99  return this->instanceName;
100 }
101 
103 {
104  this->instanceName = newName;
105 }
106 
108 {
109  return this->configPath;
110 }
111 
113 {
114  this->configPath = configPath;
115 }
116 
118 {
119  std::filesystem::path scenariosFolder(scenario->getPackage()->getScenarioPath());
120  std::filesystem::path scenarioFolder = scenariosFolder / scenario->getName();
121  std::filesystem::path scenarioCfgFolder = scenarioFolder / "config";
122 
123  std::filesystem::path configPath = scenarioCfgFolder;
124 
125  if (!this->instanceName.empty() && this->instanceName != R"("")" && this->instanceName != R"(" ")")
126  {
127  configPath = configPath / (this->getName() + "." + this->getInstanceName() + ".cfg");
128  }
129  else
130  {
131  configPath = configPath / (this->getName() + ".cfg");;
132  }
133 
134  if (configPath != this->configPath)
135  {
136  std::filesystem::remove(std::filesystem::path(this->configPath));
137  }
138  this->configPath = configPath.string();
139 }
140 
142 {
143  return this->status;
144 }
145 
147 {
148  bool result = status != this->status;
149  if (result)
150  {
151  this->status = status;
152  }
153  return result;
154 }
155 
157 {
158  return this->pid;
159 }
160 
161 void Data_Structure::ApplicationInstance::setPid(int pid) // if PID < -1 Error?
162 {
163  this->pid = pid;
164 }
165 
167 {
168  if (!properties->isPropertySet(name))
169  {
170  properties->defineOptionalProperty(name, std::string("::NOT_DEFINED::"), "Custom Property");
171  }
172  properties->getProperties()->setProperty(name, value);
173 }
174 
175 void Data_Structure::ApplicationInstance::addProperty(std::string name, std::string value)
176 {
177  if (!properties->isPropertySet(name))
178  {
179  properties->defineOptionalProperty(name, std::string("::NOT_DEFINED::"), "Custom Property");
180  }
181  properties->getProperties()->setProperty(name, value);
182 }
183 
185 {
187  parser.saveCfg(shared_from_this());
188 }
189 
191 {
192  if (!isReadOnly())
193  {
194  return;
195  }
196 
197  if (!std::filesystem::is_symlink(configPath))
198  {
199  ARMARX_WARNING << "Make local called on non-linked application";
200  return;
201  }
202 
203  std::filesystem::remove(configPath);
204  this->save();
205  // Reload to register status changes
206  this->load();
207 }
208 
210 {
211  deleteConfig();
212  load(true);
213  auto target_path = other.configPath.lexically_relative(configPath.parent_path());
214  std::filesystem::create_symlink(target_path, configPath);
215  load();
216 }
217 
219 {
220  deleteConfig();
221  load(true);
222  std::filesystem::copy_file(other.configPath, configPath,
223  std::filesystem::copy_options::overwrite_existing);
224  load();
225 }
226 
228 {
229  if (std::filesystem::exists(this->configPath))
230  {
231  std::filesystem::remove(this->configPath);
232  }
233 }
234 
236 {
237  if (!std::filesystem::exists(configPath))
238  {
239  ARMARX_WARNING_S << "Cannot find ApplicationInstance Config at:" << configPath;
240  }
241  else
242  {
244  setProperties(parser.mergeXmlAndCfg(shared_from_this(), firstLoad));
245  }
246 
247  // If the config file is a symlink, put app into read-only mode
248  this->readOnly = std::filesystem::is_symlink(configPath);
249 }
250 
252 {
253  std::ofstream ofs;
254 
255  ofs.open(configPath.c_str(), std::ofstream::out | std::ofstream::app);
256  ARMARX_DEBUG << configPath << " is writeable: " << ofs.is_open();
257 
258  return ofs.is_open();
259 }
260 
262 {
263  return statusWriteBlock;
264 }
265 
267 {
268  statusWriteBlock = blocked;
269 }
270 
272 {
273  return nodeName;
274 }
275 
277 {
278  return nodeName.empty() && scenario ? scenario->getNodeName() : nodeName;
279 }
280 
282 {
283  this->nodeName = nodeName;
284 }
285 
287 {
288  return enabled;
289 }
290 
292 {
293  this->enabled = enabled;
294 }
ScenarioManager::Data_Structure::ScenarioPtr
std::shared_ptr< Scenario > ScenarioPtr
Definition: Scenario.h:36
ScenarioManager::Data_Structure::ApplicationInstance::getPid
int getPid()
Definition: ApplicationInstance.cpp:156
ScenarioManager::Data_Structure::ApplicationInstance::modifyProperty
void modifyProperty(std::string name, std::string value)
Changes the value of the specified property.
Definition: ApplicationInstance.cpp:166
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:271
PropertyDefinitionContainerFormatter.h
ScenarioManager::Parser::IceParser
Definition: iceparser.h:37
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:276
ScenarioManager::Data_Structure::ApplicationInstance::setStatus
bool setStatus(const std::string &status)
Sets the status of this ApplicationInstance.
Definition: ApplicationInstance.cpp:146
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:55
ScenarioManager::Data_Structure::ApplicationInstance::getScenario
ScenarioPtr getScenario()
Definition: ApplicationInstance.cpp:92
ScenarioManager::Data_Structure::Application
Class containing data about an application Provides methods to get and set the date contained in the ...
Definition: Application.h:45
ScenarioManager::Data_Structure::ApplicationInstance::setInstanceName
void setInstanceName(std::string newName)
Sets the name of this ApplicationInstance.
Definition: ApplicationInstance.cpp:102
ScenarioManager::Data_Structure::ApplicationInstance::getConfigDomain
std::string getConfigDomain() const
Definition: ApplicationInstance.cpp:50
armarx::navigation::algorithms::save
bool save(const Costmap &costmap, const std::filesystem::path &directory)
Definition: persistence.cpp:101
ScenarioManager::Data_Structure::ApplicationInstance::getStatus
std::string getStatus()
Returns the status.
Definition: ApplicationInstance.cpp:141
armarx::status
status
Definition: FiniteStateMachine.h:259
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
ScenarioManager::Data_Structure::ApplicationInstance::deleteConfig
void deleteConfig()
Definition: ApplicationInstance.cpp:227
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:177
ScenarioManager::Data_Structure::ApplicationInstance::resetConfigPath
void resetConfigPath()
resetConfigPath.
Definition: ApplicationInstance.cpp:117
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:107
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:161
ScenarioManager::Data_Structure::ApplicationInstance::isConfigWritable
bool isConfigWritable()
Definition: ApplicationInstance.cpp:251
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:218
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:266
armarx::Application
Baseclass for all ArmarX applications.
Definition: Application.h:193
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
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:175
ApplicationInstance.h
ScenarioManager::Data_Structure::ApplicationInstance::save
void save()
Saves the IceProperties to the configPath.
Definition: ApplicationInstance.cpp:184
ExpressionException.h
ScenarioManager::Data_Structure::ApplicationInstance::getInstanceName
std::string getInstanceName()
Definition: ApplicationInstance.cpp:97
IceStorm::parser
Parser * parser
Definition: Parser.cpp:33
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:166
ScenarioManager::Data_Structure::ApplicationInstance::setEnabled
void setEnabled(bool enabled)
Definition: ApplicationInstance.cpp:291
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:235
ScenarioManager::Data_Structure::ApplicationInstance::getEnabled
bool getEnabled()
Definition: ApplicationInstance.cpp:286
ScenarioManager::Data_Structure::ApplicationInstance::getStatusWriteBlock
bool getStatusWriteBlock()
Definition: ApplicationInstance.cpp:261
ScenarioManager::Data_Structure::ApplicationInstance::makeLocal
void makeLocal()
Make a local copy of this instance if it is linked and nothing otherwise.
Definition: ApplicationInstance.cpp:190
ScenarioManager::Data_Structure::ApplicationInstance::linkFrom
void linkFrom(ApplicationInstance &other)
linkFrom Link this instance configuration from the other instance.
Definition: ApplicationInstance.cpp:209
Logging.h
ScenarioManager::Data_Structure::ApplicationInstance::isReadOnly
bool isReadOnly() const
Definition: ApplicationInstance.cpp:61
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::server::ltm::mongodb::util::load
void load(const mongocxx::database &db, armem::wm::Memory &m)
Definition: operations.cpp:40
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
ScenarioManager::Data_Structure::ApplicationInstance::setNodeName
void setNodeName(std::string nodeName)
Definition: ApplicationInstance.cpp:281
ScenarioManager::Data_Structure::ApplicationInstance::setConfigPath
void setConfigPath(std::string configPath)
Sets the config path of this ApplicationInstance.
Definition: ApplicationInstance.cpp:112