YARP
Yet Another Robot Platform
localbroker.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_MANAGER_LOCALBROKER
7 #define YARP_MANAGER_LOCALBROKER
8 
9 #include <cstdio>
10 #include <string>
11 #include <iostream>
12 
13 #include <yarp/os/Time.h>
14 #include <yarp/os/Port.h>
15 #include <yarp/os/BufferedPort.h>
16 #include <yarp/os/Network.h>
17 #include <yarp/os/Property.h>
18 #include <string>
19 #include <yarp/os/PeriodicThread.h>
20 #include <yarp/os/Thread.h>
21 
22 #include <yarp/manager/broker.h>
23 
24 #if defined(_WIN32)
25  #include<Windows.h>
26 #endif
27 
28 namespace yarp {
29 namespace manager {
30 
34 class LocalBroker: public Broker, public yarp::os::Thread {
35 
36 public:
37 
38  typedef enum { WINDOW_HIDDEN=0, //window is hidden, catch stdout
39  WINDOW_VISIBLE=1, //window is visible
40  WINDOW_MINIMIZED=2 //window is started minimized
42 
43 
44  LocalBroker();
45  virtual ~LocalBroker();
46  bool init() override;
47  bool init(const char* szcmd, const char* szparam,
48  const char* szhost, const char* szstdio,
49  const char* szworkdir, const char* szenv) override;
50  void fini() override;
51  bool start() override;
52  bool stop() override;
53  bool kill() override;
54  bool connect(const char* from, const char* to,
55  const char* carrier, bool persist=false) override;
56  bool disconnect(const char* from, const char* to,
57  const char *carrier) override;
58  int running() override;
59  bool exists(const char* port) override;
60  const char* requestRpc(const char* szport, const char* request, double timeout) override;
61  bool connected(const char* from, const char* to,
62  const char* carrier) override;
63  const char* error() override;
64  bool initialized() override { return bInitialized;}
65  bool attachStdout() override;
66  void detachStdout() override;
67 
69  void setWindowMode(WindowMode m);
70 
71 public: // for rate thread
72  void run() override;
73  bool threadInit() override;
74  void threadRelease() override;
75 
76 protected:
77 
78 private:
79  std::string strCmd;
80  std::string strParam;
81  std::string strHost;
82  std::string strStdio;
83  std::string strWorkdir;
84  std::string strTag;
85  std::string strEnv;
86  int ID;
87  std::string strError;
88  bool bOnlyConnector;
89  bool bInitialized;
90  int pipe_to_stdout[2];
91  FILE* fd_stdout;
92  WindowMode windowMode;
93 
94  bool timeout(double base, double timeout);
95  int ExecuteCmd();
96  bool psCmd(int pid);
97  bool killCmd(int pid);
98  bool stopCmd(int pid);
99 #if defined(_WIN32)
100  HANDLE read_from_pipe_cmd_to_stdout;
101  HANDLE write_to_pipe_cmd_to_stdout;
102  std::string lastError2String();
103 #else
104  int waitPipe(int pipe_fd);
105  int waitPipeSignal(int pipe_fd);
106  void splitLine(char *pLine, char **pArgs);
107  void parseArguments(char *io_pLine, int *o_pArgc, char **o_pArgv);
108 #endif
109  bool startStdout();
110  void stopStdout();
111 
112  inline bool IS_PARENT_OF(int pid){ return pid>0; }
113  inline bool IS_NEW_PROCESS(int pid){ return !pid; }
114  inline bool IS_INVALID(int pid){ return pid<0; }
115 };
116 
117 } // namespace yarp
118 } // namespace manager
119 
120 
121 #endif // __YARP_MANAGER_LOCALBROKER__
void * HANDLE
Class Broker.
Definition: broker.h:31
Class LocalBroker.
Definition: localbroker.h:34
const char * requestRpc(const char *szport, const char *request, double timeout) override
bool exists(const char *port) override
bool connected(const char *from, const char *to, const char *carrier) override
void run() override
Main body of the new thread.
void setWindowMode(WindowMode m)
Define if the application will be visible or not.
bool initialized() override
Definition: localbroker.h:64
bool threadInit() override
Initialization method.
bool disconnect(const char *from, const char *to, const char *carrier) override
void threadRelease() override
Release method.
bool connect(const char *from, const char *to, const char *carrier, bool persist=false) override
connection broker
void detachStdout() override
const char * error() override
bool attachStdout() override
An abstraction for a thread of execution.
Definition: Thread.h:22
The main, catch-all namespace for YARP.
Definition: dirs.h:16