YARP
Yet Another Robot Platform
RunReadWrite.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
11 
12 #include <yarp/os/Time.h>
13 #include <yarp/os/Bottle.h>
14 #include <yarp/os/Property.h>
16 
17 #include <cstdio>
18 
20 {
21  RUNLOG("<<<loop()")
22 
23  if (!wPort.open(wPortName))
24  {
25  RUNLOG("RunWrite: could not open output port")
26  fprintf(stderr, "RunWrite: could not open output port\n");
27  return 1;
28  }
30  {
32  {
33  fprintf(stderr, "RunWrite: could not mmake connection with the logger\n");
34  }
35  }
36 
37  char txt[2048];
38 
39  std::string tag=std::string("[")+wPortName+std::string("]");
40 
41  while (mRunning)
42  {
43  if (!fgets(txt, 2048, stdin) || ferror(stdin) || feof(stdin)) break;
44 
45  if (!mRunning) break;
46 
47  yarp::os::Bottle bot;
48  if (mVerbose) bot.addString(tag.c_str());
49  bot.addString(txt);
50  wPort.write(bot);
51  }
52 
53  RUNLOG(">>>loop()")
54 
55  return 0;
56 }
57 
59 
61 {
62  RUNLOG("<<<loop()")
63 
64  if (!rPort.open(rPortName))
65  {
66  RUNLOG("RunRead: could not open input port")
67  fprintf(stderr, "RunRead: could not open input port\n");
68  return 1;
69  }
70 
71  while (mRunning)
72  {
73  yarp::os::Bottle bot;
74  if (!rPort.read(bot, true))
75  {
76  RUNLOG("!rPort.read(bot, true)")
77  break;
78  }
79 
80  if (!mRunning) break;
81 
82  if (bot.size()==1)
83  {
84  printf("%s", bot.get(0).asString().c_str());
85  }
86  else
87  {
88  printf("%s\n", bot.toString().c_str());
89  }
90 
91  fflush(stdout);
92  }
93 
94  rPort.close();
95 
96  RUNLOG(">>>loop()")
97 
98  return 0;
99 }
100 
102 
104 {
105  RUNLOG("<<<loop()")
106 
107  if (!rPort.open(rPortName))
108  {
109  RUNLOG("RunReadWrite: could not open input port")
110  fprintf(stderr, "RunReadWrite: could not open input port\n");
111  return 1;
112  }
113 
115  style.persistent=true;
116 
117  yarp::os::Network::connect(UUID+"/stdout", rPortName, style);
118 
119  // forwarded section
120  std::string tag;
121 
122  if (mForwarded)
123  {
124  tag=std::string("[")+fPortName+std::string("]");
125  if (!fPort.open(fPortName))
126  {
127  RUNLOG("RunReadWrite: could not open forward port")
128  fprintf(stderr, "RunReadWrite: could not open forward port\n");
129 
130  rPort.close();
131 
132  return 1;
133  }
134  }
136 
137  #if !defined(_WIN32)
138  if (yarp::os::impl::getppid()!=1)
139  #endif
140  {
141  RUNLOG("start()")
142  start();
143 
144  while (mRunning)
145  {
146  #if !defined(_WIN32)
147  if (yarp::os::impl::getppid()==1) break;
148  #endif
149 
150  yarp::os::Bottle bot;
151 
152  if (!rPort.read(bot, true))
153  {
154  RUNLOG("!rPort.read(bot, true)")
155  break;
156  }
157 
158  if (!mRunning) break;
159 
160  #if !defined(_WIN32)
161  if (yarp::os::impl::getppid()==1) break;
162  #endif
163 
164  if (bot.size()==1)
165  {
166  printf("%s", bot.get(0).asString().c_str());
167  fflush(stdout);
168 
169  if (mForwarded)
170  {
171  yarp::os::Bottle fwd;
172  fwd.addString(tag.c_str());
173  fwd.addString(bot.get(0).asString().c_str());
174  fPort.write(fwd);
175  }
176  }
177  else
178  {
179  printf("%s\n", bot.toString().c_str());
180  fflush(stdout);
181 
182  if (mForwarded)
183  {
184  yarp::os::Bottle fwd;
185  fwd.addString(tag.c_str());
186  fwd.addString(bot.toString().c_str());
187  fPort.write(fwd);
188  }
189  }
190  }
191 
192  rPort.close();
193 
194  wPort.close();
195 
196  if (mForwarded) fPort.close();
197 
198 #if defined(_WIN32)
199  ::exit(0);
200 #else
201  int term_pipe[2];
202  int warn_suppress = yarp::run::impl::pipe(term_pipe);
203  YARP_UNUSED(warn_suppress);
204  yarp::run::impl::dup2(term_pipe[0], STDIN_FILENO);
205  FILE* file_term_pipe=fdopen(term_pipe[1], "w");
206  fprintf(file_term_pipe, "SHKIATTETE!\n");
207  fflush(file_term_pipe);
208  fclose(file_term_pipe);
209 #endif
210  }
211 
212  RUNLOG(">>>loop()")
213 
214  return 0;
215 }
216 
218 {
219  char txt[2048];
220 
221  RUNLOG("<<<run()")
222 
223  if (!wPort.open(wPortName))
224  {
225  RUNLOG("RunReadWrite: could not open output port")
226  fprintf(stderr, "RunReadWrite: could not open output port\n");
227  return;
228  }
229 
231  style.persistent=true;
232 
233  yarp::os::Network::connect(wPortName, UUID+"/stdin", style);
234 
235  while (mRunning)
236  {
237  RUNLOG("mRunning")
238 
239  #if !defined(_WIN32)
240  if (yarp::os::impl::getppid()==1) break;
241  #endif
242 
243  if (!fgets(txt, 2048, stdin) || ferror(stdin) || feof(stdin)) break;
244 
245  RUNLOG(txt)
246 
247  #if !defined(_WIN32)
248  if (yarp::os::impl::getppid()==1) break;
249  #endif
250 
251  if (!mRunning) break;
252 
253  RUNLOG(txt)
254 
255  yarp::os::Bottle bot;
256  bot.addString(txt);
257 
258  RUNLOG("<<<wPort.write(bot)")
259  wPort.write(bot);
260  RUNLOG(">>>wPort.write(bot)")
261  }
262 
263  RUNLOG(">>>run()")
264 }
yarp::os::Port::close
void close() override
Stop port activity.
Definition: Port.cpp:357
RunReadWrite::fPortName
std::string fPortName
Definition: RunReadWrite.h:295
RUNLOG
#define RUNLOG(msg)
Definition: RunCheckpoints.h:35
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
RunReadWrite::UUID
std::string UUID
Definition: RunReadWrite.h:292
yarp::os::ContactStyle
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:27
yarp::os::Bottle::size
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
RunWrite::wPort
yarp::os::Port wPort
Definition: RunReadWrite.h:204
RunWrite::wPortName
std::string wPortName
Definition: RunReadWrite.h:202
RunWrite::mRunning
bool mRunning
Definition: RunReadWrite.h:199
RunRead::rPortName
std::string rPortName
Definition: RunReadWrite.h:236
RunReadWrite::rPortName
std::string rPortName
Definition: RunReadWrite.h:294
RunReadWrite::fPort
yarp::os::Port fPort
Definition: RunReadWrite.h:298
RunReadWrite::exit
void exit() override
Definition: RunReadWrite.h:274
RunReadWrite::wPort
yarp::os::Port wPort
Definition: RunReadWrite.h:296
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::os::Port::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:82
RunWrite::mVerbose
bool mVerbose
Definition: RunReadWrite.h:200
RunReadWrite::rPort
yarp::os::Port rPort
Definition: RunReadWrite.h:297
RunReadWrite::mForwarded
bool mForwarded
Definition: RunReadWrite.h:290
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
Property.h
RunReadWrite::run
void run() override
Main body of the new thread.
Definition: RunReadWrite.cpp:217
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::NetworkBase::connect
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:685
yarp::os::ContactStyle::persistent
bool persistent
Specify whether a requested connection should be persistent.
Definition: ContactStyle.h:66
RunWrite::wLoggerName
std::string wLoggerName
Definition: RunReadWrite.h:203
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
yarp::os::Port::read
bool read(PortReader &reader, bool willReply=false) override
Read an object from the port.
Definition: Port.cpp:475
RunReadWrite::mRunning
bool mRunning
Definition: RunReadWrite.h:289
RunReadWrite::loop
int loop()
Definition: RunReadWrite.cpp:103
yarp::os::Port::write
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: Port.cpp:430
RunReadWrite::wPortName
std::string wPortName
Definition: RunReadWrite.h:293
RunReadWrite.h
PlatformSignal.h
yarp::os::Thread::start
bool start()
Start the new thread running.
Definition: Thread.cpp:96
Time.h
RunRead::loop
int loop()
Definition: RunReadWrite.cpp:60
RunWrite::loop
int loop()
Definition: RunReadWrite.cpp:19
Bottle.h
RunRead::mRunning
bool mRunning
Definition: RunReadWrite.h:234
RunRead::rPort
yarp::os::Port rPort
Definition: RunReadWrite.h:237
yarp::os::NetworkBase::exists
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:749