YARP  2.3.70.2
Yet Another Robot Platform
Run.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2009 RobotCub Consortium
3  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
4  * Author: Alessandro Scalzo <alessandro.scalzo@iit.it>
5  */
6 
7 #ifndef YARP_OS_RUN_H
8 #define YARP_OS_RUN_H
9 
10 #include <cstring>
11 #include <yarp/os/api.h>
12 #include <yarp/os/RpcServer.h>
13 #include <yarp/os/Property.h>
14 #include <yarp/os/Semaphore.h>
15 
16 namespace yarp {
17  namespace os {
18  class Run;
19  }
20 }
21 
22 // watch out for ACE randomly redefining main, depending on configuration
23 #ifdef main
24 #undef main
25 #endif
26 
27 class YARP_OS_API YarpRunInfoVector;
28 class YARP_OS_API ZombieHunterThread;
29 
30 /*
31  * Typical Yarp applications consist of several intercommunicating modules distributed on different machines.
32  * If a yarprun server is running on each machine, distributed applications can be remotely launched,
33  * monitored and terminated by yarprun commands.
34  *
35  * - To run a yarprun server on a machine:
36  * $ yarprun --server /SERVERPORT
37  *
38  * /SERVERPORT must be unique and identifies the remote machine.
39  *
40  * - The basic command to run a command/application on a remote machine is:
41  * $ yarprun --on /SERVERPORT --as TAG --cmd COMMAND [ARGLIST]
42  *
43  * /SERVERPORT is the name of the server that actually runs the command
44  * TAG identifies the application process set, and must be unique
45  * COMMAND is the application that has to be executed, followed by the optional argument list
46  *
47  * Some options can be added to the basic format of yarprun:
48  * $ yarprun --on /SERVERPORT1 --as TAG --cmd COMMAND [ARGLIST] --stdio /SERVERPORT2
49  *
50  * opens a remote shell window where the stdin, stdout and stderr of the application will be redirected.
51  * /SERVERPORT2 specifies the machine where the IO shell will be executed, and can be either a remote machine or
52  * be equal to /SERVERPORT1 itself.
53  *
54  * If --stdio is specified, there are two useful sub-options (linux only):
55  * - --hold keep the stdio window open even if the command is terminated or aborted.
56  * - --geometry WxH+X+Y set the stdio window size and position. Example: --geometry 320x240+80+20
57  *
58  * Other yarprun commands:
59  *
60  * - To terminate an application, the yarprun syntax is:
61  * $ yarprun --on /SERVERPORT --sigterm TAG
62  *
63  * - To send a signal to an application (usually SIGKILL) use:
64  * $ yarprun --on /SERVERPORT --kill TAG SIGNUM
65  *
66  * - To terminate all the applications managed by a yarprun server, use:
67  * $ yarprun --on /SERVERPORT --sigtermall
68  *
69  * - To check if an application is still running on a yarprun server, use:
70  * $ yarprun --on /SERVERPORT --isrunning TAG
71  *
72  * - To get a report of applications running on a yarprun server, use:
73  * $ yarprun --on /SERVERPORT --ps
74  *
75  * - To shutdown a yarprun server, use:
76  * $ yarprun --on /SERVERPORT --exit
77  *
78  */
79 
85 class YARP_OS_API yarp::os::Run
86 {
87 public:
88  // API
89 
103  static int start(const ConstString &node, Property &command, ConstString &keyv);
110  static int sigterm(const ConstString &node, const ConstString &keyv);
116  static int sigterm(const ConstString &node);
124  static int kill(const ConstString &node, const ConstString &keyv, int s);
133  // static int ps(const ConstString &node, ConstString** &processes, int &num_processes);
140  static bool isRunning(const ConstString &node, ConstString &keyv);
141 
142  // end API
143 
155  static int client(Property& config);
156 
157 #ifndef DOXYGEN_SHOULD_SKIP_THIS
158 
159  static int main(int argc, char *argv[]);
160  static yarp::os::RpcServer *pServerPort;
161 
162  static bool mStresstest;
163 
164  static bool mLogged;
165  static yarp::os::ConstString mLoggerPort;
166 
167 #if defined(_WIN32)
168  static YarpRunInfoVector mProcessVector;
169  static YarpRunInfoVector mStdioVector;
170 #else
171  static YarpRunInfoVector *mProcessVector;
172  static YarpRunInfoVector *mStdioVector;
173  static ZombieHunterThread *mBraveZombieHunter;
174  static void CleanZombie(int pid);
175 #endif
176 
177  static yarp::os::Bottle sendMsg(Bottle& msg, yarp::os::ConstString target, int RETRY=20, double DELAY=0.5);
178 
179 protected:
180  static void Help(const char* msg="");
181  static int server();
182  static int executeCmdAndStdio(Bottle& msg, Bottle& result);
183  static int executeCmdStdout(Bottle& msg, Bottle& result, yarp::os::ConstString& loggerName);
184  static int executeCmd(Bottle& msg, Bottle& result);
185  static int userStdio(Bottle& msg, Bottle& result);
186 
187  static inline bool IS_PARENT_OF(int pid){ return pid>0; }
188  static inline bool IS_NEW_PROCESS(int pid){ return !pid; }
189  static inline bool IS_INVALID(int pid){ return pid<0; }
190 
191  static ConstString mPortName;
192  static int mProcCNT;
193 
194 #if !defined(_WIN32)
195  static void cleanBeforeExec();
196  static void writeToPipe(int fd, yarp::os::ConstString str);
197  static int readFromPipe(int fd, char* &data, int& buffsize);
198 #endif
199 
200  static void cmdcpy(char* &dst, const char* src)
201  {
202  dst=new char[(strlen(src)/8+2)*16];
203  strcpy(dst, src);
204  }
205 
206  static void cmdclean(char **cmd)
207  {
208  while (*cmd)
209  {
210  delete [] *cmd++;
211  }
212  }
213 
214 #endif /*DOXYGEN_SHOULD_SKIP_THIS*/
215 };
216 
217 #endif // YARP_OS_RUN_H
A port that is specialized as an RPC server.
Definition: RpcServer.h:26
A class for storing options and configuration information.
Definition: Property.h:32
The main, catch-all namespace for YARP.
A string with almost the same api as std::string.
Definition: ConstString.h:44
A simple collection of objects that can be described and transmitted in a portable way...
Definition: Bottle.h:57
yarprun provides the APIs to a client-server environment that is able to run, kill and monitor applic...
Definition: Run.h:85
int main()