YARP
Yet Another Robot Platform
TcpFace.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  * Copyright (C) 2010 Daniel Krieg <krieg@fias.uni-frankfurt.de>
5  * All rights reserved.
6  *
7  * This software may be modified and distributed under the terms of the
8  * BSD-3-Clause license. See the accompanying LICENSE file for details.
9  */
10 
11 #include <yarp/os/impl/TcpFace.h>
12 
15 #include <yarp/os/impl/Protocol.h>
17 
18 #include <cstdio>
19 
20 using namespace yarp::os::impl;
21 using namespace yarp::os;
22 
23 namespace {
24 YARP_OS_LOG_COMPONENT(TCPFACE, "yarp.os.impl.TcpFace")
25 } // namespace
26 
27 
28 TcpFace::TcpFace() = default;
29 
31 {
32  closeFace();
33 }
34 
35 
36 bool TcpFace::open(const Contact& address)
37 {
38  yCDebug(TCPFACE, "opening for address %s", address.toURI().c_str());
39 
40  this->address = address;
41 #ifdef YARP_HAS_ACE
42  ACE_INET_Addr serverAddr((address.getPort() > 0) ? address.getPort() : 0);
43  int result = peerAcceptor.open(serverAddr, 1);
44  if (address.getPort() <= 0) {
45  ACE_INET_Addr localAddr;
46  peerAcceptor.get_local_addr(localAddr);
47  this->address = address;
48  this->address.setSocket("tcp",
50  localAddr.get_port_number());
51  }
52 #else
53  int result = peerAcceptor.open(address);
54  if (address.getPort() <= 0) {
55  this->address = address;
56  this->address.setSocket("tcp",
58  peerAcceptor.get_port_number());
59  }
60 #endif
61  yCDebug(TCPFACE, "Opened at address %s", this->address.toURI().c_str());
62 
63  return result != -1;
64 }
65 
67 {
68  closeFace();
69 }
70 
71 void TcpFace::closeFace()
72 {
73  if (address.isValid()) {
74  peerAcceptor.close();
75 
76  address = Contact();
77  }
78 }
79 
80 static void showError()
81 {
82  yCError(TCPFACE, "Authentication failed.");
83  yCError(TCPFACE, "Authentication was enabled in the auth.conf file.");
84  yCError(TCPFACE, "If you do not want to use authentication, please");
85  yCError(TCPFACE, "remove this file.");
86  yCError(TCPFACE, "If you do want to set up authentication, check:");
87  yCError(TCPFACE, " http://www.yarp.it/yarp_port_auth.html");
88 }
89 
94 {
95  auto* stream = new SocketTwoWayStream();
96  yCAssert(TCPFACE, stream != nullptr);
97 
98  int result = stream->open(peerAcceptor);
99  if (result < 0) {
100  //printf("exception on tcp stream read: %s\n", e.toString().c_str());
101  stream->close();
102  delete stream;
103  stream = nullptr;
104  }
105 
106  if (stream != nullptr) {
107  stream->setReadTimeout(2.0);
108  stream->setWriteTimeout(2.0);
109 
110  bool success = auth.authSource(&(stream->getInputStream()), &(stream->getOutputStream()));
111  if (!success) {
112  showError();
113  return nullptr;
114  }
115  stream->setReadTimeout(0.);
116  stream->setWriteTimeout(0.);
117 
118  return new Protocol(stream);
119  }
120  return nullptr;
121 }
122 
124 {
125  auto* stream = new SocketTwoWayStream();
126  int result = stream->open(address);
127  if (result < 0) {
128  stream->close();
129  delete stream;
130  return nullptr;
131  }
132 
133  if (stream != nullptr) {
134  stream->setReadTimeout(2.0);
135  stream->setWriteTimeout(2.0);
136 
137  bool success = auth.authDest(&(stream->getInputStream()), &(stream->getOutputStream()));
138  if (!success) {
139  showError();
140  return nullptr;
141  }
142  stream->setReadTimeout(0.);
143  stream->setWriteTimeout(0.);
144 
145  return new Protocol(stream);
146  }
147  return nullptr;
148 }
149 
150 
152 {
153  return address;
154 }
yarp::os::impl::TcpFace::close
void close() override
Stop listening.
Definition: TcpFace.cpp:66
yarp::os::impl::TcpFace::getLocalAddress
Contact getLocalAddress() const override
Get address after open(), if more specific that the address provided to open() - otherwise an invalid...
Definition: TcpFace.cpp:151
yarp::os::OutputProtocol
The output side of an active connection between two ports.
Definition: OutputProtocol.h:33
SocketTwoWayStream.h
yarp::os::impl::NameConfig::getHostName
static std::string getHostName(bool prefer_loopback=false, const std::string &seed="")
Definition: NameConfig.cpp:207
LogComponent.h
yarp::os::impl::TcpFace::write
OutputProtocol * write(const Contact &address) override
Try to reach out and talk to someone.
Definition: TcpFace.cpp:123
Protocol.h
yarp::os::Contact::toURI
std::string toURI(bool includeCarrier=true) const
Get a representation of the Contact as a URI.
Definition: Contact.cpp:316
yarp::os::impl::SocketTwoWayStream
A stream abstraction for socket communication.
Definition: SocketTwoWayStream.h:45
yarp::os::impl::TcpFace::~TcpFace
~TcpFace() override
Definition: TcpFace.cpp:30
yarp::os::impl::Protocol
Connection choreographer.
Definition: Protocol.h:34
yarp::os::Contact::getPort
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition: Contact.cpp:242
yarp::os::impl::TcpFace::open
bool open(const Contact &address) override
Start listening to the given address.
Definition: TcpFace.cpp:36
TcpFace.h
NameConfig.h
yarp::os::Contact::setSocket
void setSocket(const std::string &carrier, const std::string &hostname, int port)
Set information to a Contact about how to reach it using socket communication.
Definition: Contact.cpp:291
showError
static void showError()
Definition: TcpFace.cpp:80
yCAssert
#define yCAssert(component, x)
Definition: LogComponent.h:172
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::impl::TcpFace::TcpFace
TcpFace()
yarp::os::InputProtocol
The input side of an active connection between two ports.
Definition: InputProtocol.h:38
YARP_OS_LOG_COMPONENT
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:37
yarp::os::impl
The components from which ports and connections are built.
yarp::os::impl::TcpFace::read
InputProtocol * read() override
return nullptr on failure.
Definition: TcpFace.cpp:93