StatusManager.cpp
Go to the documentation of this file.
1#include "StatusManager.h"
2
3#include <filesystem>
4#include <fstream>
5
9
10using namespace ScenarioManager;
11using namespace Exec;
12using namespace armarx;
13
17
18int
20{
21 std::string cachePath = ArmarXDataPath::GetCachePath();
22 if (cachePath.empty())
23 {
24 return -1;
25 }
26
27 cachePath.append("/pids/");
28 auto dir = remove_trailing_separator(cachePath);
29
30 if (!std::filesystem::exists(dir) && !std::filesystem::create_directories(dir))
31 {
32 ARMARX_WARNING_S << "Unable to create Cache directory for the ScenarioManager plugin at "
33 << cachePath;
34 }
35
36
37 std::string filename = cachePath + app->getPackageName() + "." + app->getScenario()->getName() +
38 "." + app->getName() + (app->getInstanceName().empty() ? "" : ".") +
39 app->getInstanceName() + ".pids";
40
41 if (!std::filesystem::exists(filename))
42 {
43 return -1;
44 }
45
46 std::ifstream input(filename);
47
48 int pid;
49 input >> pid;
50
51 return pid;
52}
53
54void
56{
57 std::string cachePath = ArmarXDataPath::GetCachePath();
58 cachePath.append("/pids/");
59 auto dir = remove_trailing_separator(cachePath);
60
61 if (!std::filesystem::exists(dir) && !std::filesystem::create_directories(dir))
62 {
63 ARMARX_WARNING_S << "Unable to create Cache directory for the ScenarioManager plugin at "
64 << cachePath;
65 }
66
67 std::string filename = cachePath + app->getPackageName() + "." + app->getScenario()->getName() +
68 "." + app->getName() + (app->getInstanceName().empty() ? "" : ".") +
69 app->getInstanceName() + ".pids";
70
71 if (app->getPid() == -1)
72 {
73 std::filesystem::remove(std::filesystem::path(filename));
74 return;
75 }
76
77 std::ofstream output(filename);
78
79 output << app->getPid();
80 output.close();
81}
82
83void
85{
86 std::string cachePath = ArmarXDataPath::GetCachePath();
87 cachePath.append("/pids/");
88
89 std::filesystem::remove_all(std::filesystem::path(cachePath));
90}
void savePid(Data_Structure::ApplicationInstancePtr app)
int loadPid(Data_Structure::ApplicationInstancePtr app)
static std::string GetCachePath()
The base Cache directory of ArmarX.
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
fs::path remove_trailing_separator(fs::path p)
Definition filesystem.h:34