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__
yarp::manager::LocalBroker::connected
bool connected(const char *from, const char *to, const char *carrier) override
Definition: localbroker.cpp:467
Network.h
yarp::manager::LocalBroker::kill
bool kill() override
Definition: localbroker.cpp:295
yarp::manager::LocalBroker::running
int running() override
Definition: localbroker.cpp:325
yarp::manager::LocalBroker::WINDOW_MINIMIZED
@ WINDOW_MINIMIZED
Definition: localbroker.h:43
Port.h
yarp::manager::LocalBroker::setWindowMode
void setWindowMode(WindowMode m)
Define if the application will be visible or not.
Definition: localbroker.cpp:556
yarp::manager::LocalBroker::WindowMode
WindowMode
Definition: localbroker.h:41
yarp::manager::LocalBroker::attachStdout
bool attachStdout() override
Definition: localbroker.cpp:480
yarp::manager::LocalBroker::connect
bool connect(const char *from, const char *to, const char *carrier, bool persist=false) override
connection broker
Definition: localbroker.cpp:336
yarp::manager::Broker
Class Broker.
Definition: broker.h:34
yarp::os::Thread
An abstraction for a thread of execution.
Definition: Thread.h:25
yarp::manager::LocalBroker::~LocalBroker
virtual ~LocalBroker()
Definition: localbroker.cpp:153
yarp::manager::LocalBroker::requestRpc
const char * requestRpc(const char *szport, const char *request, double timeout) override
Definition: localbroker.cpp:425
yarp::manager::LocalBroker::detachStdout
void detachStdout() override
Definition: localbroker.cpp:492
yarp::manager::LocalBroker::error
const char * error() override
Definition: localbroker.cpp:475
yarp::manager::LocalBroker::init
bool init() override
Definition: localbroker.cpp:164
yarp::manager::LocalBroker::run
void run() override
Main body of the new thread.
Definition: localbroker.cpp:512
Property.h
yarp::manager::LocalBroker::exists
bool exists(const char *port) override
Definition: localbroker.cpp:419
HANDLE
void * HANDLE
Definition: RunProcManager.h:42
Thread.h
BufferedPort.h
yarp::manager::LocalBroker::WINDOW_HIDDEN
@ WINDOW_HIDDEN
Definition: localbroker.h:41
yarp::manager::LocalBroker::start
bool start() override
Definition: localbroker.cpp:247
PeriodicThread.h
yarp::manager::LocalBroker::LocalBroker
LocalBroker()
Definition: localbroker.cpp:144
yarp::manager::LocalBroker::disconnect
bool disconnect(const char *from, const char *to, const char *carrier) override
Definition: localbroker.cpp:376
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
broker.h
yarp::manager::LocalBroker::threadRelease
void threadRelease() override
Release method.
Definition: localbroker.cpp:551
yarp::manager::LocalBroker::threadInit
bool threadInit() override
Initialization method.
Definition: localbroker.cpp:506
yarp::manager::LocalBroker::WINDOW_VISIBLE
@ WINDOW_VISIBLE
Definition: localbroker.h:42
yarp::manager::LocalBroker
Class LocalBroker.
Definition: localbroker.h:37
Time.h
yarp::manager::LocalBroker::initialized
bool initialized() override
Definition: localbroker.h:67
yarp::manager::LocalBroker::fini
void fini() override
Definition: localbroker.cpp:158
yarp::manager::LocalBroker::stop
bool stop() override
Definition: localbroker.cpp:267