YARP
Yet Another Robot Platform
ShmemCarrier.cpp
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 "ShmemCarrier.h"
11 #include "ShmemHybridStream.h"
12 #include "ShmemLogComponent.h"
13 
15 #include <yarp/os/Log.h>
16 
17 #include <string>
18 #include <cstdlib>
19 
20 
21 ShmemCarrier::ShmemCarrier() = default;
22 
23 ShmemCarrier::~ShmemCarrier() = default;
24 
26 {
27  return new ShmemCarrier();
28 }
29 
30 std::string ShmemCarrier::getName() const
31 {
32  return "shmem";
33 }
34 
36 {
37  // specifier codes are a very old yarp feature,
38  // not necessary any more really, should be replaced.
39  return 14;
40 }
41 
43 {
44  return true;
45 }
46 
48 {
49  return false;
50 }
51 
53 {
54  return getSpecifier(header) % 16 == getSpecifierCode();
55 }
56 
58 {
60 }
61 
63 {
64  YARP_UNUSED(header);
65 }
66 
67 bool ShmemCarrier::becomeShmemVersionHybridStream(yarp::os::ConnectionState& proto, bool sender)
68 {
69  auto* stream = new ShmemHybridStream();
70  yCAssert(SHMEMCARRIER, stream != nullptr);
71  yarp::os::Contact base;
72 
73  bool ok = true;
74 
75  if (!sender) {
76  ACE_INET_Addr anywhere((u_short)0, (ACE_UINT32)INADDR_ANY);
77  base = yarp::os::Contact(anywhere.get_host_addr(), anywhere.get_port_number());
78  bool ok = stream->open(base, sender) == 0;
79  if (ok) {
80  int myPort = stream->getLocalAddress().getPort();
81  writeYarpInt(myPort, proto);
82  stream->accept();
83  proto.takeStreams(nullptr);
84  proto.takeStreams(stream);
85  }
86  } else {
87  int altPort = readYarpInt(proto);
88  std::string myName = proto.getStreams().getLocalAddress().getHost();
89  proto.takeStreams(nullptr);
90  base = yarp::os::Contact(myName, altPort);
91  ok = stream->open(base, sender) == 0;
92  if (ok) {
93  proto.takeStreams(stream);
94  }
95  }
96 
97  if (!ok) {
98  delete stream;
99  stream = nullptr;
100  return false;
101  }
102 
103  return true;
104 }
105 
106 bool ShmemCarrier::becomeShmem(yarp::os::ConnectionState& proto, bool sender)
107 {
108  return becomeShmemVersionHybridStream(proto, sender);
109 }
110 
112 {
113  // i am the receiver
114  return becomeShmem(proto, false);
115 }
116 
118 {
119  // i am the sender
120  return becomeShmem(proto, true);
121 }
yarp::os::TwoWayStream::getLocalAddress
virtual const Contact & getLocalAddress() const =0
Get the address of the local side of the stream.
ShmemHybridStream
A stream abstraction for shared memory communication.
Definition: ShmemHybridStream.h:28
yarp::os::AbstractCarrier::writeYarpInt
void writeYarpInt(int n, ConnectionState &proto)
Write n as an 8 bytes yarp number.
Definition: AbstractCarrier.cpp:354
yarp::os::Carrier
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
ShmemCarrier::requireAck
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: ShmemCarrier.cpp:42
ShmemCarrier::getSpecifierCode
virtual int getSpecifierCode() const
Definition: ShmemCarrier.cpp:35
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
ShmemCarrier::~ShmemCarrier
virtual ~ShmemCarrier()
ShmemCarrier::ShmemCarrier
ShmemCarrier()
Log.h
yarp::os::ConnectionState::takeStreams
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
ConnectionState.h
ShmemCarrier::isConnectionless
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: ShmemCarrier.cpp:47
yarp::os::AbstractCarrier::createStandardHeader
void createStandardHeader(int specifier, yarp::os::Bytes &header) const
Definition: AbstractCarrier.cpp:177
SHMEMCARRIER
const yarp::os::LogComponent & SHMEMCARRIER()
Definition: ShmemLogComponent.cpp:16
yarp::os::ConnectionState::getStreams
virtual TwoWayStream & getStreams()=0
Access the streams associated with the connection.
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yCAssert
#define yCAssert(component, x)
Definition: LogComponent.h:172
ShmemLogComponent.h
yarp::os::ConnectionState
The basic state of a connection - route, streams in use, etc.
Definition: ConnectionState.h:31
ShmemCarrier::respondToHeader
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
Definition: ShmemCarrier.cpp:111
ShmemCarrier::getHeader
void getHeader(yarp::os::Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Definition: ShmemCarrier.cpp:57
ShmemCarrier::create
Carrier * create() const override
Factory method.
Definition: ShmemCarrier.cpp:25
yarp::os::Contact::getHost
std::string getHost() const
Get the host name associated with this Contact for socket communication.
Definition: Contact.cpp:231
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
ShmemHybridStream.h
ShmemCarrier.h
ShmemCarrier::expectReplyToHeader
bool expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: ShmemCarrier.cpp:117
ShmemCarrier::getName
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: ShmemCarrier.cpp:30
ShmemCarrier::setParameters
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: ShmemCarrier.cpp:62
ShmemCarrier::checkHeader
bool checkHeader(const yarp::os::Bytes &header) override
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
Definition: ShmemCarrier.cpp:52
yarp::os::AbstractCarrier::getSpecifier
int getSpecifier(const Bytes &b) const
Definition: AbstractCarrier.cpp:168
yarp::os::AbstractCarrier::readYarpInt
int readYarpInt(ConnectionState &proto)
Read 8 bytes and interpret them as a YARP number.
Definition: AbstractCarrier.cpp:342