YARP
Yet Another Robot Platform
HumanStream.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 #include <yarp/os/TwoWayStream.h>
8 #include <yarp/os/InputStream.h>
9 #include <yarp/os/OutputStream.h>
11 #include <yarp/os/SystemClock.h>
12 
13 #include <iostream>
14 #include <string>
15 #include <cstdio>
16 
17 using namespace yarp::os;
18 
19 
20 class HumanStream : public TwoWayStream,
21  public InputStream,
22  public OutputStream
23 {
24 private:
25  bool interrupting;
26  bool needInterrupt;
27  std::string inputCache;
28  std::string outputCache;
29 public:
31  interrupting = false;
32  needInterrupt = false;
33  inputCache = outputCache = "";
34  }
35 
36  void close() override {
37  std::cout << "Bye bye" << std::endl;
38  }
39 
40  bool isOk() const override {
41  return true;
42  }
43 
44  void interrupt() override {
45  interrupting = true;
46  while (needInterrupt) {
47  std::cout << "*** INTERRUPT: Please hit enter ***" << std::endl;
48  for (int i=0; i<10 && needInterrupt; i++) {
50  }
51  }
52  }
53 
54  // InputStream
56  yarp::conf::ssize_t read(Bytes& b) override;
57 
58  // OutputStream
60  void write(const Bytes& b) override;
61 
62  // TwoWayStream
63 
65  return *this;
66  }
67 
69  return *this;
70  }
71 
72  const yarp::os::Contact& getLocalAddress() const override {
73  // left undefined
74  return local;
75  }
76 
77  const yarp::os::Contact& getRemoteAddress() const override {
78  // left undefined
79  return remote;
80  }
81 
82  void reset() override {
83  inputCache = outputCache = "";
84  std::cout << "Stream reset" << std::endl;
85  }
86 
87  void beginPacket() override {
88  std::cout << "Packet begins" << std::endl;
89  inputCache = "";
90  outputCache = "";
91  }
92 
93  void endPacket() override {
94  std::cout << "Packet ends" << std::endl;
95  }
96 
97 private:
98  yarp::os::Contact local, remote;
99 };
OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: HumanStream.h:68
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: HumanStream.h:93
InputStream & getInputStream() override
Get an InputStream to read from.
Definition: HumanStream.h:64
void beginPacket() override
Mark the beginning of a logical packet.
Definition: HumanStream.h:87
void reset() override
Reset the stream.
Definition: HumanStream.h:82
const yarp::os::Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: HumanStream.h:77
void close() override
Terminate the stream.
Definition: HumanStream.h:36
const yarp::os::Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: HumanStream.h:72
void interrupt() override
Interrupt the stream.
Definition: HumanStream.h:44
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: HumanStream.h:40
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:26
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:20
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:22
virtual void write(char ch)
Write a single byte to the stream.
static void delaySystem(double seconds)
Definition: SystemClock.cpp:29
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:26
::ssize_t ssize_t
Definition: numeric.h:86
An interface to the operating system, including Port based communication.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:922
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1098