-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSpawner.hpp
More file actions
155 lines (126 loc) · 4.32 KB
/
Spawner.hpp
File metadata and controls
155 lines (126 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef SPAWNER_H
#define SPAWNER_H
#include <unistd.h>
#include <string>
#include <vector>
#include <base/Time.hpp>
#include "NameService.hpp"
#include "Deployment.hpp"
#include <boost/noncopyable.hpp>
namespace orocos_cpp
{
class Spawner : public boost::noncopyable
{
std::string logDir;
//list of tasks that were spawned, but are not yet reachable.
std::vector<std::string> notReadyList;
NameService *nameService;
/**
* Default constructor
* */
Spawner();
public:
class ProcessHandle
{
pid_t pid;
void redirectOutput(const std::string &filename);
std::string processName;
Deployment *deployment;
public:
ProcessHandle(Deployment *deployment, bool redirectOutput, const std::string &logDir);
const Deployment &getDeployment() const;
bool alive() const;
bool wait(int cycles=500, int usecs_between_cycles=500) const;
void sendSigInt() const;
void sendSigTerm() const;
void sendSigKill() const;
};
public:
/**
* Singleton pattern, returns the ONE instance of
* the spawner.
* */
static Spawner &getInstance();
/**
* This method spawns a default deployment matching the given componente description.
* If a second argument is given, the task will be renamed to the given name.
* The method will throw if any error occures.
*
* @arg cmp1 The task model name e.g Hokuyo::Task
* @arg as The name unter wich the taskmodel should be registered a the nameservice
* @return A Process handle, which may be used to request the process status
* */
ProcessHandle &spawnTask(const std::string &cmp1, const std::string &as = std::string(), bool redirectOutput = true);
/**
* This method spawns a process that executes the deployment
* with the given name. This method will throw if any error
* occures.
*
* @arg dplName Name of the deployment executable that should be started
* @return A Process handle, which may be used to request the process status
* */
ProcessHandle &spawnDeployment(const std::string &dplName, bool redirectOutput = true);
/**
* This method spawns a process that executes the given
* deployment. This method will throw if any error
* occures.
*
* @arg dplName The deployment that should be started. The ownership of the deployment will be taken over by the spawner.
* @return A Process handle, which may be used to request the process status
* */
ProcessHandle &spawnDeployment(Deployment *deployment, bool redirectOutput = true);
/**
* Get a deployment by its name
* @arg dplName
*/
ProcessHandle &getDeployment(const std::string &dplName);
/**
* This method checks if all spawened processes are still alive
* @return false if any process died
* */
bool checkAllProcesses();
/**
* This method checks if all spawned tasks registered
* at the nameservice.
* */
bool allReady();
/**
* Waits up to the given timeout for all tasks, to
* be connectable via the nameservice.
* Will throw an runtime error if not all tasks could
* be reached.
* */
void waitUntilAllReady(const base::Time &timeout);
/**
* Kill deployment with the given name
* @arg dplName
*/
bool killDeployment(const std::string &dplName);
/**
* This method first sends a sigterm to all processes
* and waits for the processes to terminate. If this
* did not happen, it will send a sigkill and return.
* */
void killAll();
/**
* This method sends a sigterm to all child processes.
* */
void sendSigTerm();
/**
* Returns a vector of all deployments, that are currently running.
* */
std::vector<const Deployment *> getRunningDeployments();
/**
* Returns, if the given instance of a deployment is running.
* */
bool isRunning(const Deployment *instance);
/**
* Sets the default log directory.
* If no log directory is set it will be determined using bundles.
*/
void setLogDirectory(const std::string& log_folder);
private:
std::vector<ProcessHandle *> handles;
};
}//end of namespace.
#endif // SPAWNER_H