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 };
OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: HumanStream.h:71
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: HumanStream.h:96
InputStream & getInputStream() override
Get an InputStream to read from.
Definition: HumanStream.h:67
void beginPacket() override
Mark the beginning of a logical packet.
Definition: HumanStream.h:90
void reset() override
Reset the stream.
Definition: HumanStream.h:85
const yarp::os::Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: HumanStream.h:80
void close() override
Terminate the stream.
Definition: HumanStream.h:39
const yarp::os::Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: HumanStream.h:75
void interrupt() override
Interrupt the stream.
Definition: HumanStream.h:47
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: HumanStream.h:43
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
virtual void write(char ch)
Write a single byte to the stream.
static void delaySystem(double seconds)
Definition: SystemClock.cpp:32
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:29
::ssize_t ssize_t
Definition: numeric.h:60
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:656
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:804