YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
33{
34public:
35 virtual ~RunStdio(){}
36 virtual void exit()=0;
37};
38
39/*
40class RunTerminator : public yarp::os::Thread
41{
42public:
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
70protected:
71 HANDLE hReadPipe, hWritePipe;
72 RunStdio* mStdio;
73};
74*/
75
77#if !defined(_WIN32)
78 : public yarp::os::Thread
79#endif
80{
81public:
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
130protected:
131 FILE *fwait, *fpost;
133};
134
136
137class RunWrite : public RunStdio
138{
139public:
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
195protected:
198
199 std::string wPortName;
200 std::string wLoggerName;
202};
203
205
206class RunRead : public RunStdio
207{
208public:
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
226
227 RUNLOG(">>>exit()")
228 }
229
230protected:
232
233 std::string rPortName;
235};
236
238
240{
241public:
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
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
285protected:
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
#define RUNLOG(msg)
std::string rPortName
std::string UUID
std::string wPortName
RunReadWrite(std::string &portsName, std::string &fpName, std::string &lpName)
void exit() override
yarp::os::Port fPort
yarp::os::Port rPort
void run() override
Main body of the new thread.
std::string fPortName
yarp::os::Port wPort
void exit() override
RunRead(std::string &portName)
yarp::os::Port rPort
bool mRunning
std::string rPortName
virtual ~RunStdio()
virtual void exit()=0
void run() override
Main body of the new thread.
RunStdio * mStdio
RunTerminator(RunStdio *pStdio)
yarp::os::Port wPort
std::string wLoggerName
bool mRunning
RunWrite(std::string &portName)
std::string wPortName
bool mVerbose
void exit() override
RunWrite(std::string &portName, std::string &loggerName)
Preferences for how to communicate with a contact.
bool persistent
Specify whether a requested connection should be persistent.
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:46
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition Port.cpp:383
void close() override
Stop port activity.
Definition Port.cpp:363
static void delaySystem(double seconds)
An abstraction for a thread of execution.
Definition Thread.h:21
bool start()
Start the new thread running.
Definition Thread.cpp:93
#define YARP_UNUSED(var)
Definition api.h:162