Yet Another Robot Platform
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  */
11 #include <yarp/os/InputStream.h>
12 #include <yarp/os/OutputStream.h>
13 #include <yarp/os/Portable.h>
14 #include <yarp/os/Semaphore.h>
15 #include <yarp/os/TwoWayStream.h>
17 #include <mutex>
19 namespace yarp {
20 namespace os {
21 namespace impl {
23 class LocalCarrier;
29 {
30 public:
33  void setSender(LocalCarrier* sender);
36  void revoke(LocalCarrier* carrier);
38 private:
39  std::mutex senderMutex;
40  std::mutex receiverMutex;
41  yarp::os::Semaphore received;
42  LocalCarrier *sender, *receiver;
43 };
50  public TwoWayStream,
51  public InputStream,
52  public OutputStream
53 {
54 public:
55  void attach(LocalCarrier* owner, bool sender);
57  InputStream& getInputStream() override;
58  OutputStream& getOutputStream() override;
59  const Contact& getLocalAddress() const override;
60  const Contact& getRemoteAddress() const override;
61  bool setTypeOfService(int tos) override;
67  void write(const yarp::os::Bytes& b) override;
69  void reset() override;
70  void beginPacket() override;
71  void endPacket() override;
72  void interrupt() override;
73  void close() override;
74  bool isOk() const override;
76 private:
77  Contact localAddress, remoteAddress;
78  LocalCarrier* owner;
79  bool sender;
80  bool done;
81 };
86 class LocalCarrier :
87  public AbstractCarrier
88 {
89 public:
90  LocalCarrier();
92  virtual ~LocalCarrier();
94  Carrier* create() const override;
96  std::string getName() const override;
98  bool requireAck() const override;
99  bool isConnectionless() const override;
100  bool canEscape() const override;
101  bool isLocal() const override;
102  virtual std::string getSpecifierName() const;
103  bool checkHeader(const Bytes& header) override;
104  void getHeader(Bytes& header) const override;
105  void setParameters(const Bytes& header) override;
106  bool sendHeader(ConnectionState& proto) override;
107  bool expectExtraHeader(ConnectionState& proto) override;
108  virtual bool becomeLocal(ConnectionState& proto, bool sender);
109  bool write(ConnectionState& proto, SizedWriter& writer) override;
110  bool respondToHeader(ConnectionState& proto) override;
111  bool expectReplyToHeader(ConnectionState& proto) override;
112  bool expectIndex(ConnectionState& proto) override;
114  void removePeer();
115  void shutdown();
118 protected:
119  bool doomed;
122  std::mutex peerMutex;
125  std::string portName;
128 };
130 } // namespace impl
131 } // namespace os
132 } // namespace yarp
A starter class for implementing simple carriers.
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:45
The basic state of a connection - route, streams in use, etc.
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.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:26
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:26
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:33
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:26
Coordinate ports communicating locally within a process.
Definition: LocalCarrier.h:29
void setSender(LocalCarrier *sender)
void revoke(LocalCarrier *carrier)
LocalCarrier * getSender(LocalCarrier *receiver)
A stream for communicating locally within a process.
Definition: LocalCarrier.h:53
bool isOk() const override
Check if the stream is ok or in an error state.
void interrupt() override
Interrupt the stream.
InputStream & getInputStream() override
Get an InputStream to read from.
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
OutputStream & getOutputStream() override
Get an OutputStream to write to.
void close() override
Terminate the stream.
void beginPacket() override
Mark the beginning of a logical packet.
void write(const yarp::os::Bytes &b) override
Write a block of bytes to the stream.
void attach(LocalCarrier *owner, bool sender)
void endPacket() override
Mark the end of a logical packet (see beginPacket).
bool setTypeOfService(int tos) override
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
void reset() override
Reset the stream.
A carrier for communicating locally within a process.
Definition: LocalCarrier.h:88
bool sendHeader(ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
static LocalCarrierManager manager
Definition: LocalCarrier.h:127
bool write(ConnectionState &proto, SizedWriter &writer) override
Write a message.
yarp::os::Semaphore received
Definition: LocalCarrier.h:124
void accept(yarp::os::Portable *ref)
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
bool expectExtraHeader(ConnectionState &proto) override
Receive any carrier-specific header.
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
bool checkHeader(const Bytes &header) override
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
yarp::os::Portable * ref
Definition: LocalCarrier.h:120
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
void setParameters(const Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Carrier * create() const override
Factory method.
virtual bool becomeLocal(ConnectionState &proto, bool sender)
virtual std::string getSpecifierName() const
bool expectIndex(ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
void getHeader(Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
yarp::os::Semaphore sent
Definition: LocalCarrier.h:123
bool isLocal() const override
Check if carrier operates within a single process.
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
::ssize_t ssize_t
Definition: numeric.h:86
The main, catch-all namespace for YARP.
Definition: dirs.h:16