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