YARP
Yet Another Robot Platform
RunReadWrite.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_RUN_IMPL_RUNREADWRITE_H
8 #define YARP_RUN_IMPL_RUNREADWRITE_H
9 
10 #include <string>
11 #include <yarp/os/Port.h>
12 #include <yarp/os/Thread.h>
13 #include <yarp/os/PortReader.h>
14 #include <string>
15 #include <yarp/os/Network.h>
18 
19 #include <cstdlib>
20 #include <fcntl.h>
21 
22 #if defined(_WIN32)
23 #include <windows.h>
24 #include <process.h>
25 #include <io.h>
26 #else
27 #include <unistd.h>
28 #endif
29 
31 
32 class RunStdio
33 {
34 public:
35  virtual ~RunStdio(){}
36  virtual void exit()=0;
37 };
38 
39 /*
40 class RunTerminator : public yarp::os::Thread
41 {
42 public:
43  RunTerminator(RunStdio* pStdio)
44  {
45  mStdio=pStdio;
46  CreatePipe(&hReadPipe, &hWritePipe, nullptr, 0);
47  }
48 
49  ~RunTerminator()
50  {
51  CloseHandle(hReadPipe);
52  CloseHandle(hWritePipe);
53  }
54 
55  void run()
56  {
57  DWORD nr;
58  char dummy[24];
59  ReadFile(hReadPipe, dummy, 1, &nr, nullptr);
60  RUNLOG("mStdio->exit()")
61  mStdio->exit();
62  }
63 
64  void exit()
65  {
66  DWORD nw;
67  WriteFile(hWritePipe, "*", 1, &nw, nullptr);
68  }
69 
70 protected:
71  HANDLE hReadPipe, hWritePipe;
72  RunStdio* mStdio;
73 };
74 */
75 
77 #if !defined(_WIN32)
78  : public yarp::os::Thread
79 #endif
80 {
81 public:
83  {
84  mStdio=pStdio;
85 #if !defined(_WIN32)
86  int pipe_block[2];
87  int warn_suppress = yarp::run::impl::pipe(pipe_block);
88  YARP_UNUSED(warn_suppress);
89  fwait=fdopen(pipe_block[0], "r");
90  fpost=fdopen(pipe_block[1], "w");
91 #endif
92  }
93 
94 #if defined(_WIN32)
95  void start(){}
96 #endif
97 
99  {
100 #if !defined(_WIN32)
101  fclose(fwait);
102  fclose(fpost);
103 #endif
104  }
105 
106  void run()
107 #if !defined(_WIN32)
108  override
109 #endif
110  {
111 #if !defined(_WIN32)
112  char dummy[24];
113  char* warn_suppress = fgets(dummy, 16, fwait);
114  YARP_UNUSED(warn_suppress);
115  RUNLOG("mStdio->exit()")
116  mStdio->exit();
117 #endif
118  }
119 
120  void exit()
121  {
122 #if defined(_WIN32)
123  mStdio->exit();
124 #else
125  fprintf(fpost, "SHKIATTETE!\n");
126  fflush(fpost);
127 #endif
128  }
129 
130 protected:
131  FILE *fwait, *fpost;
133 };
134 
136 
137 class RunWrite : public RunStdio
138 {
139 public:
140  RunWrite(std::string& portName, std::string& loggerName)
141  {
142  mVerbose=true;
143 
144  char buff[16];
145  sprintf(buff, "/%d", getpid());
146  wPortName=portName+buff;
147  wLoggerName=loggerName;
148 
149  /*
150  //persistent connection to the logger, off by default
151  yarp::os::ContactStyle style;
152  style.persistent=true;
153  yarp::os::Network::connect(wPortName.c_str(), loggerName.c_str(), style);
154  */
155 
156  mRunning=true;
157  }
158 
159  RunWrite(std::string& portName)
160  {
161  mVerbose=false;
162 
163  wPortName=portName+"/stdout";
164 
165  mRunning=true;
166  }
167 
169 
170  int loop();
171 
172  void exit() override
173  {
174  RUNLOG("<<<exit()")
175 
176  mRunning=false;
177  wPort.close();
178 
179 #if defined(_WIN32)
180  RUNLOG(">>>exit()")
181  ::exit(0);
182 #else
183  int term_pipe[2];
184  int warn_suppress = yarp::run::impl::pipe(term_pipe);
185  YARP_UNUSED(warn_suppress);
186  yarp::run::impl::dup2(term_pipe[0], STDIN_FILENO);
187  FILE* file_term_pipe=fdopen(term_pipe[1], "w");
188  fprintf(file_term_pipe, "SHKIATTETE!\n");
189  fflush(file_term_pipe);
190  fclose(file_term_pipe);
191  RUNLOG(">>>exit()")
192 #endif
193  }
194 
195 protected:
196  bool mRunning;
197  bool mVerbose;
198 
199  std::string wPortName;
200  std::string wLoggerName;
202 };
203 
205 
206 class RunRead : public RunStdio
207 {
208 public:
209  RunRead(std::string &portName)
210  {
211  rPortName=portName+"/stdin";
212 
213  mRunning=true;
214  }
216 
217  int loop();
218 
219  void exit() override
220  {
221  RUNLOG("<<<exit()")
222 
223  mRunning=false;
224 
225  rPort.interrupt();
226 
227  RUNLOG(">>>exit()")
228  }
229 
230 protected:
231  bool mRunning;
232 
233  std::string rPortName;
235 };
236 
238 
239 class RunReadWrite : public RunStdio, public yarp::os::Thread
240 {
241 public:
242  RunReadWrite(std::string &portsName, std::string &fpName, std::string &lpName)
243  {
244  UUID=portsName;
245  wPortName=portsName+"/stdio:o";
246  rPortName=portsName+"/stdio:i";
247  mRunning=true;
248 
249  if (fpName!="")
250  {
251  char buff[16];
252  sprintf(buff, "/%d", getpid());
253  mForwarded=true;
254  fPortName=fpName+buff;
255 
257  style.persistent=true;
258  yarp::os::Network::connect(fPortName.c_str(), lpName.c_str(), style);
259  }
260  else
261  {
262  mForwarded=false;
263  }
264  }
265 
267 
268  void run() override;
269  int loop();
270 
271  void exit() override
272  {
273  RUNLOG("<<<exit()")
274 
275  mRunning=false;
276 
277  rPort.interrupt();
278 
279 #if defined(_WIN32)
280  for (int t=0; t<10; ++t) yarp::os::SystemClock::delaySystem(1.0);
281 #endif
282  RUNLOG(">>>exit()")
283  }
284 
285 protected:
286  bool mRunning;
288 
289  std::string UUID;
290  std::string wPortName;
291  std::string rPortName;
292  std::string fPortName;
296 };
297 
298 #endif // YARP_RUN_IMPL_RUNREADWRITE_H
float t
#define RUNLOG(msg)
std::string rPortName
Definition: RunReadWrite.h:291
std::string UUID
Definition: RunReadWrite.h:289
std::string wPortName
Definition: RunReadWrite.h:290
RunReadWrite(std::string &portsName, std::string &fpName, std::string &lpName)
Definition: RunReadWrite.h:242
void exit() override
Definition: RunReadWrite.h:271
yarp::os::Port fPort
Definition: RunReadWrite.h:295
yarp::os::Port rPort
Definition: RunReadWrite.h:294
void run() override
Main body of the new thread.
std::string fPortName
Definition: RunReadWrite.h:292
yarp::os::Port wPort
Definition: RunReadWrite.h:293
void exit() override
Definition: RunReadWrite.h:219
RunRead(std::string &portName)
Definition: RunReadWrite.h:209
int loop()
yarp::os::Port rPort
Definition: RunReadWrite.h:234
bool mRunning
Definition: RunReadWrite.h:231
std::string rPortName
Definition: RunReadWrite.h:233
virtual ~RunStdio()
Definition: RunReadWrite.h:35
virtual void exit()=0
void run() override
Main body of the new thread.
Definition: RunReadWrite.h:106
RunStdio * mStdio
Definition: RunReadWrite.h:132
RunTerminator(RunStdio *pStdio)
Definition: RunReadWrite.h:82
yarp::os::Port wPort
Definition: RunReadWrite.h:201
std::string wLoggerName
Definition: RunReadWrite.h:200
bool mRunning
Definition: RunReadWrite.h:196
RunWrite(std::string &portName)
Definition: RunReadWrite.h:159
std::string wPortName
Definition: RunReadWrite.h:199
bool mVerbose
Definition: RunReadWrite.h:197
void exit() override
Definition: RunReadWrite.h:172
RunWrite(std::string &portName, std::string &loggerName)
Definition: RunReadWrite.h:140
int loop()
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:24
bool persistent
Specify whether a requested connection should be persistent.
Definition: ContactStyle.h:63
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition: Network.cpp:682
A mini-server for network communication.
Definition: Port.h:47
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Port.cpp:374
void close() override
Stop port activity.
Definition: Port.cpp:354
static void delaySystem(double seconds)
Definition: SystemClock.cpp:29
An abstraction for a thread of execution.
Definition: Thread.h:22
bool start()
Start the new thread running.
Definition: Thread.cpp:93
int getpid()
Portable wrapper for the getppid() function.
Definition: Os.cpp:91
#define YARP_UNUSED(var)
Definition: api.h:162