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
A starter class for implementing simple carriers.
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
The basic state of a connection - route, streams in use, etc.
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.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:29
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:29
Coordinate ports communicating locally within a process.
Definition: LocalCarrier.h:32
void setSender(LocalCarrier *sender)
void revoke(LocalCarrier *carrier)
LocalCarrier * getSender(LocalCarrier *receiver)
A stream for communicating locally within a process.
Definition: LocalCarrier.h:56
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:91
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:130
bool write(ConnectionState &proto, SizedWriter &writer) override
Write a message.
yarp::os::Semaphore received
Definition: LocalCarrier.h:127
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:123
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:126
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:60
The main, catch-all namespace for YARP.
Definition: environment.h:18