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