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 }
const yarp::os::LogComponent & SHMEMCARRIER()
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
bool expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
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...
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
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...
Carrier * create() const override
Factory method.
virtual ~ShmemCarrier()
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
virtual int getSpecifierCode() const
A stream abstraction for shared memory communication.
int getSpecifier(const Bytes &b) const
void writeYarpInt(int n, ConnectionState &proto)
Write n as an 8 bytes yarp number.
void createStandardHeader(int specifier, yarp::os::Bytes &header) const
int readYarpInt(ConnectionState &proto)
Read 8 bytes and interpret them as a YARP number.
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.
virtual TwoWayStream & getStreams()=0
Access the streams associated with the connection.
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
std::string getHost() const
Get the host name associated with this Contact for socket communication.
Definition: Contact.cpp:231
virtual const Contact & getLocalAddress() const =0
Get the address of the local side of the stream.
#define yCAssert(component, x)
Definition: LogComponent.h:172
#define YARP_UNUSED(var)
Definition: api.h:159