YARP
Yet Another Robot Platform
LocalCarrier.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 #ifndef YARP_OS_IMPL_LOCALCARRIER_H
11 #define YARP_OS_IMPL_LOCALCARRIER_H
12 
14 #include <yarp/os/InputStream.h>
15 #include <yarp/os/OutputStream.h>
16 #include <yarp/os/Portable.h>
17 #include <yarp/os/Semaphore.h>
18 #include <yarp/os/TwoWayStream.h>
19 
20 #include <mutex>
21 
22 namespace yarp {
23 namespace os {
24 namespace impl {
25 
26 class LocalCarrier;
27 
32 {
33 public:
35 
36  void setSender(LocalCarrier* sender);
39  void revoke(LocalCarrier* carrier);
40 
41 private:
42  std::mutex senderMutex;
43  std::mutex receiverMutex;
44  yarp::os::Semaphore received;
45  LocalCarrier *sender, *receiver;
46 };
47 
48 
53  public TwoWayStream,
54  public InputStream,
55  public OutputStream
56 {
57 public:
58  void attach(LocalCarrier* owner, bool sender);
59 
60  InputStream& getInputStream() override;
61  OutputStream& getOutputStream() override;
62  const Contact& getLocalAddress() const override;
63  const Contact& getRemoteAddress() const override;
64  bool setTypeOfService(int tos) override;
65 
68 
70  void write(const yarp::os::Bytes& b) override;
71 
72  void reset() override;
73  void beginPacket() override;
74  void endPacket() override;
75  void interrupt() override;
76  void close() override;
77  bool isOk() const override;
78 
79 private:
80  Contact localAddress, remoteAddress;
81  LocalCarrier* owner;
82  bool sender;
83  bool done;
84 };
85 
89 class LocalCarrier :
90  public AbstractCarrier
91 {
92 public:
93  LocalCarrier();
94 
95  virtual ~LocalCarrier();
96 
97  Carrier* create() const override;
98 
99  std::string getName() const override;
100 
101  bool requireAck() const override;
102  bool isConnectionless() const override;
103  bool canEscape() const override;
104  bool isLocal() const override;
105  virtual std::string getSpecifierName() const;
106  bool checkHeader(const Bytes& header) override;
107  void getHeader(Bytes& header) const override;
108  void setParameters(const Bytes& header) override;
109  bool sendHeader(ConnectionState& proto) override;
110  bool expectExtraHeader(ConnectionState& proto) override;
111  virtual bool becomeLocal(ConnectionState& proto, bool sender);
112  bool write(ConnectionState& proto, SizedWriter& writer) override;
113  bool respondToHeader(ConnectionState& proto) override;
114  bool expectReplyToHeader(ConnectionState& proto) override;
115  bool expectIndex(ConnectionState& proto) override;
116 
117  void removePeer();
118  void shutdown();
120 
121 protected:
122  bool doomed;
125  std::mutex peerMutex;
128  std::string portName;
129 
131 };
132 
133 } // namespace impl
134 } // namespace os
135 } // namespace yarp
136 
137 #endif // YARP_OS_IMPL_LOCALCARRIER_H
yarp::os::TwoWayStream
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:29
yarp::os::Portable
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
yarp::os::impl::LocalCarrierManager::getSender
LocalCarrier * getSender(LocalCarrier *receiver)
Definition: LocalCarrier.cpp:51
yarp::os::impl::LocalCarrier::getSpecifierName
virtual std::string getSpecifierName() const
Definition: LocalCarrier.cpp:214
yarp::os::impl::LocalCarrierManager::getReceiver
LocalCarrier * getReceiver()
Definition: LocalCarrier.cpp:42
yarp::os::Semaphore
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:29
yarp::os::Carrier
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
yarp::os::impl::LocalCarrier::respondToHeader
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
Definition: LocalCarrier.cpp:316
yarp::os::OutputStream::write
virtual void write(char ch)
Write a single byte to the stream.
Definition: OutputStream.cpp:17
yarp::os::impl::LocalCarrier::create
Carrier * create() const override
Factory method.
Definition: LocalCarrier.cpp:160
yarp::os::impl::LocalCarrier::accept
void accept(yarp::os::Portable *ref)
Definition: LocalCarrier.cpp:348
yarp::os::OutputStream
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
yarp::os::impl::LocalCarrier::ref
yarp::os::Portable * ref
Definition: LocalCarrier.h:123
Portable.h
yarp::os::impl::LocalCarrierStream::attach
void attach(LocalCarrier *owner, bool sender)
Definition: LocalCarrier.cpp:69
OutputStream.h
yarp::os::impl::LocalCarrierManager
Coordinate ports communicating locally within a process.
Definition: LocalCarrier.h:32
yarp::os::impl::LocalCarrier::expectExtraHeader
bool expectExtraHeader(ConnectionState &proto) override
Receive any carrier-specific header.
Definition: LocalCarrier.cpp:267
yarp::os::impl::LocalCarrierStream::setTypeOfService
bool setTypeOfService(int tos) override
Definition: LocalCarrier.cpp:96
AbstractCarrier.h
yarp::os::AbstractCarrier
A starter class for implementing simple carriers.
Definition: AbstractCarrier.h:25
yarp::os::impl::LocalCarrierManager::LocalCarrierManager
LocalCarrierManager()
Definition: LocalCarrier.cpp:27
TwoWayStream.h
yarp::os::impl::LocalCarrierStream::write
void write(const yarp::os::Bytes &b) override
Write a block of bytes to the stream.
Definition: LocalCarrier.cpp:108
yarp::os::impl::LocalCarrier::LocalCarrier
LocalCarrier()
Definition: LocalCarrier.cpp:147
yarp::os::impl::LocalCarrierManager::revoke
void revoke(LocalCarrier *carrier)
Definition: LocalCarrier.cpp:61
yarp::os::impl::LocalCarrier::~LocalCarrier
virtual ~LocalCarrier()
Definition: LocalCarrier.cpp:155
yarp::os::impl::LocalCarrier
A carrier for communicating locally within a process.
Definition: LocalCarrier.h:91
yarp::os::impl::LocalCarrier::isConnectionless
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: LocalCarrier.cpp:199
yarp::os::impl::LocalCarrierStream::interrupt
void interrupt() override
Interrupt the stream.
Definition: LocalCarrier.cpp:126
yarp::os::impl::LocalCarrier::expectReplyToHeader
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: LocalCarrier.cpp:323
yarp::os::impl::LocalCarrierStream::beginPacket
void beginPacket() override
Mark the beginning of a logical packet.
Definition: LocalCarrier.cpp:118
yarp::os::impl::LocalCarrierStream::getLocalAddress
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: LocalCarrier.cpp:86
yarp::os::impl::LocalCarrierStream::getOutputStream
OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: LocalCarrier.cpp:81
yarp::os::impl::LocalCarrier::portName
std::string portName
Definition: LocalCarrier.h:128
yarp::os::impl::LocalCarrier::setParameters
void setParameters(const Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: LocalCarrier.cpp:243
yarp::conf::ssize_t
::ssize_t ssize_t
Definition: numeric.h:60
yarp::os::impl::LocalCarrier::peerMutex
std::mutex peerMutex
Definition: LocalCarrier.h:125
yarp::os::impl::LocalCarrierStream::getRemoteAddress
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: LocalCarrier.cpp:91
yarp::os::impl::LocalCarrier::write
bool write(ConnectionState &proto, SizedWriter &writer) override
Write a message.
Definition: LocalCarrier.cpp:297
yarp::os::impl::LocalCarrierStream::endPacket
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: LocalCarrier.cpp:122
yarp::os::impl::LocalCarrierStream
A stream for communicating locally within a process.
Definition: LocalCarrier.h:56
yarp::os::impl::LocalCarrier::sendHeader
bool sendHeader(ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
Definition: LocalCarrier.cpp:248
yarp::os::impl::LocalCarrier::peer
LocalCarrier * peer
Definition: LocalCarrier.h:124
yarp::os::impl::LocalCarrier::manager
static LocalCarrierManager manager
Definition: LocalCarrier.h:130
yarp::os::impl::LocalCarrierManager::setSender
void setSender(LocalCarrier *sender)
Definition: LocalCarrier.cpp:36
yarp::os::impl::LocalCarrier::canEscape
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
Definition: LocalCarrier.cpp:204
Semaphore.h
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yarp::os::impl::LocalCarrierStream::reset
void reset() override
Reset the stream.
Definition: LocalCarrier.cpp:114
yarp::os::impl::LocalCarrier::doomed
bool doomed
Definition: LocalCarrier.h:122
yarp::os::ConnectionState
The basic state of a connection - route, streams in use, etc.
Definition: ConnectionState.h:31
yarp::os::impl::LocalCarrierStream::isOk
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: LocalCarrier.cpp:141
yarp::os::impl::LocalCarrierStream::close
void close() override
Terminate the stream.
Definition: LocalCarrier.cpp:131
yarp::os::InputStream::read
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
yarp::os::impl::LocalCarrier::getHeader
void getHeader(Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Definition: LocalCarrier.cpp:233
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::impl::LocalCarrier::isLocal
bool isLocal() const override
Check if carrier operates within a single process.
Definition: LocalCarrier.cpp:209
InputStream.h
yarp::os::impl::LocalCarrier::received
yarp::os::Semaphore received
Definition: LocalCarrier.h:127
yarp::os::impl::LocalCarrierStream::getInputStream
InputStream & getInputStream() override
Get an InputStream to read from.
Definition: LocalCarrier.cpp:76
yarp::os::impl::LocalCarrier::requireAck
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: LocalCarrier.cpp:194
yarp::os::impl::LocalCarrier::removePeer
void removePeer()
Definition: LocalCarrier.cpp:165
yarp::os::impl::LocalCarrier::getName
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: LocalCarrier.cpp:189
yarp::os::InputStream
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
yarp::os::impl::LocalCarrier::sent
yarp::os::Semaphore sent
Definition: LocalCarrier.h:126
yarp::os::impl::LocalCarrier::becomeLocal
virtual bool becomeLocal(ConnectionState &proto, bool sender)
Definition: LocalCarrier.cpp:287
yarp::os::impl::LocalCarrier::checkHeader
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...
Definition: LocalCarrier.cpp:219
yarp::os::impl::LocalCarrier::expectIndex
bool expectIndex(ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
Definition: LocalCarrier.cpp:329
yarp::os::SizedWriter
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
yarp::os::impl::LocalCarrier::shutdown
void shutdown()
Definition: LocalCarrier.cpp:174