YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
RunReadWrite.cpp
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
8
9#include <yarp/os/Time.h>
10#include <yarp/os/Bottle.h>
11#include <yarp/os/Property.h>
13
14#include <cstdio>
15
17{
18 RUNLOG("<<<loop()")
19
20 if (!wPort.open(wPortName))
21 {
22 RUNLOG("RunWrite: could not open output port")
23 fprintf(stderr, "RunWrite: could not open output port\n");
24 return 1;
25 }
27 {
29 {
30 fprintf(stderr, "RunWrite: could not mmake connection with the logger\n");
31 }
32 }
33
34 char txt[2048];
35
36 std::string tag=std::string("[")+wPortName+std::string("]");
37
38 while (mRunning)
39 {
40 if (!fgets(txt, 2048, stdin) || ferror(stdin) || feof(stdin)) {
41 break;
42 }
43
44 if (!mRunning) {
45 break;
46 }
47
49 if (mVerbose) {
50 bot.addString(tag.c_str());
51 }
52 bot.addString(txt);
53 wPort.write(bot);
54 }
55
56 RUNLOG(">>>loop()")
57
58 return 0;
59}
60
62
64{
65 RUNLOG("<<<loop()")
66
67 if (!rPort.open(rPortName))
68 {
69 RUNLOG("RunRead: could not open input port")
70 fprintf(stderr, "RunRead: could not open input port\n");
71 return 1;
72 }
73
74 while (mRunning)
75 {
77 if (!rPort.read(bot, true))
78 {
79 RUNLOG("!rPort.read(bot, true)")
80 break;
81 }
82
83 if (!mRunning) {
84 break;
85 }
86
87 if (bot.size()==1)
88 {
89 printf("%s", bot.get(0).asString().c_str());
90 }
91 else
92 {
93 printf("%s\n", bot.toString().c_str());
94 }
95
96 fflush(stdout);
97 }
98
99 rPort.close();
100
101 RUNLOG(">>>loop()")
102
103 return 0;
104}
105
107
109{
110 RUNLOG("<<<loop()")
111
112 if (!rPort.open(rPortName))
113 {
114 RUNLOG("RunReadWrite: could not open input port")
115 fprintf(stderr, "RunReadWrite: could not open input port\n");
116 return 1;
117 }
118
120 style.persistent=true;
121
122 yarp::os::Network::connect(UUID+"/stdout", rPortName, style);
123
124 // forwarded section
125 std::string tag;
126
127 if (mForwarded)
128 {
129 tag=std::string("[")+fPortName+std::string("]");
130 if (!fPort.open(fPortName))
131 {
132 RUNLOG("RunReadWrite: could not open forward port")
133 fprintf(stderr, "RunReadWrite: could not open forward port\n");
134
135 rPort.close();
136
137 return 1;
138 }
139 }
141
142 #if !defined(_WIN32)
143 if (yarp::os::impl::getppid()!=1)
144 #endif
145 {
146 RUNLOG("start()")
147 start();
148
149 while (mRunning)
150 {
151 #if !defined(_WIN32)
152 if (yarp::os::impl::getppid() == 1) {
153 break;
154 }
155#endif
156
158
159 if (!rPort.read(bot, true))
160 {
161 RUNLOG("!rPort.read(bot, true)")
162 break;
163 }
164
165 if (!mRunning) {
166 break;
167 }
168
169#if !defined(_WIN32)
170 if (yarp::os::impl::getppid() == 1) {
171 break;
172 }
173#endif
174
175 if (bot.size()==1)
176 {
177 printf("%s", bot.get(0).asString().c_str());
178 fflush(stdout);
179
180 if (mForwarded)
181 {
183 fwd.addString(tag.c_str());
184 fwd.addString(bot.get(0).asString().c_str());
185 fPort.write(fwd);
186 }
187 }
188 else
189 {
190 printf("%s\n", bot.toString().c_str());
191 fflush(stdout);
192
193 if (mForwarded)
194 {
196 fwd.addString(tag.c_str());
197 fwd.addString(bot.toString().c_str());
198 fPort.write(fwd);
199 }
200 }
201 }
202
203 rPort.close();
204
205 wPort.close();
206
207 if (mForwarded) {
208 fPort.close();
209 }
210
211#if defined(_WIN32)
212 ::exit(0);
213#else
214 int term_pipe[2];
215 int warn_suppress = yarp::run::impl::pipe(term_pipe);
216 YARP_UNUSED(warn_suppress);
217 yarp::run::impl::dup2(term_pipe[0], STDIN_FILENO);
218 FILE* file_term_pipe=fdopen(term_pipe[1], "w");
219 fprintf(file_term_pipe, "SHKIATTETE!\n");
220 fflush(file_term_pipe);
221 fclose(file_term_pipe);
222#endif
223 }
224
225 RUNLOG(">>>loop()")
226
227 return 0;
228}
229
231{
232 char txt[2048];
233
234 RUNLOG("<<<run()")
235
236 if (!wPort.open(wPortName))
237 {
238 RUNLOG("RunReadWrite: could not open output port")
239 fprintf(stderr, "RunReadWrite: could not open output port\n");
240 return;
241 }
242
244 style.persistent=true;
245
246 yarp::os::Network::connect(wPortName, UUID+"/stdin", style);
247
248 while (mRunning)
249 {
250 RUNLOG("mRunning")
251
252 #if !defined(_WIN32)
253 if (yarp::os::impl::getppid() == 1) {
254 break;
255 }
256#endif
257
258 if (!fgets(txt, 2048, stdin) || ferror(stdin) || feof(stdin)) {
259 break;
260 }
261
262 RUNLOG(txt)
263
264 #if !defined(_WIN32)
265 if (yarp::os::impl::getppid() == 1) {
266 break;
267 }
268#endif
269
270 if (!mRunning) {
271 break;
272 }
273
274 RUNLOG(txt)
275
277 bot.addString(txt);
278
279 RUNLOG("<<<wPort.write(bot)")
280 wPort.write(bot);
281 RUNLOG(">>>wPort.write(bot)")
282 }
283
284 RUNLOG(">>>run()")
285}
#define RUNLOG(msg)
std::string rPortName
std::string UUID
std::string wPortName
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
yarp::os::Port rPort
bool mRunning
std::string rPortName
yarp::os::Port wPort
std::string wLoggerName
bool mRunning
std::string wPortName
bool mVerbose
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:246
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:211
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
static bool exists(const std::string &port, bool quiet=true, bool checkVer=true)
Check for a port to be ready and responsive.
Definition Network.cpp:746
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:436
bool read(PortReader &reader, bool willReply=false) override
Read an object from the port.
Definition Port.cpp:481
void close() override
Stop port activity.
Definition Port.cpp:363
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
bool start()
Start the new thread running.
Definition Thread.cpp:93
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
#define YARP_UNUSED(var)
Definition api.h:162