blob: 9c054fa9c0124eb17e99ee07668f28ca6f5b091a [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Li32dab972016-11-27 12:26:09 +08003 * Copyright (c) 2014-2017, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "transport.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070027#include "face.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070028
29namespace nfd {
30namespace face {
31
32NFD_LOG_INIT("Transport");
33
34std::ostream&
35operator<<(std::ostream& os, TransportState state)
36{
37 switch (state) {
38 case TransportState::UP:
39 return os << "UP";
40 case TransportState::DOWN:
41 return os << "DOWN";
42 case TransportState::CLOSING:
43 return os << "CLOSING";
44 case TransportState::FAILED:
45 return os << "FAILED";
46 case TransportState::CLOSED:
47 return os << "CLOSED";
48 default:
49 return os << "NONE";
50 }
51}
52
53Transport::Packet::Packet(Block&& packet1)
54 : packet(std::move(packet1))
Junxiao Shi13546112015-10-14 19:33:07 -070055 , remoteEndpoint(0)
Eric Newberrya98bf932015-09-21 00:58:47 -070056{
57}
58
59Transport::Transport()
60 : m_face(nullptr)
61 , m_service(nullptr)
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070062 , m_scope(ndn::nfd::FACE_SCOPE_NONE)
63 , m_persistency(ndn::nfd::FACE_PERSISTENCY_NONE)
64 , m_linkType(ndn::nfd::LINK_TYPE_NONE)
65 , m_mtu(MTU_INVALID)
Eric Newberrya98bf932015-09-21 00:58:47 -070066 , m_state(TransportState::UP)
Eric Newberryc64d30a2015-12-26 11:07:27 -070067 , m_expirationTime(time::steady_clock::TimePoint::max())
Eric Newberrya98bf932015-09-21 00:58:47 -070068{
69}
70
Davide Pesavento32065652017-01-15 01:52:21 -050071Transport::~Transport() = default;
Eric Newberrya98bf932015-09-21 00:58:47 -070072
73void
Junxiao Shicde37ad2015-12-24 01:02:05 -070074Transport::setFaceAndLinkService(Face& face, LinkService& service)
Eric Newberrya98bf932015-09-21 00:58:47 -070075{
76 BOOST_ASSERT(m_face == nullptr);
77 BOOST_ASSERT(m_service == nullptr);
78
79 m_face = &face;
80 m_service = &service;
Eric Newberrya98bf932015-09-21 00:58:47 -070081}
82
83void
84Transport::close()
85{
86 if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
87 return;
88 }
89
90 this->setState(TransportState::CLOSING);
91 this->doClose();
Davide Pesavento32065652017-01-15 01:52:21 -050092 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -070093 // the Transport may be deallocated if doClose changes state to CLOSED
94}
95
96void
Junxiao Shi13546112015-10-14 19:33:07 -070097Transport::send(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -070098{
Junxiao Shi13546112015-10-14 19:33:07 -070099 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
100 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
101
102 TransportState state = this->getState();
103 if (state != TransportState::UP && state != TransportState::DOWN) {
104 NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
105 return;
106 }
107
Junxiao Shi57df2882015-11-11 06:12:35 -0700108 if (state == TransportState::UP) {
109 ++this->nOutPackets;
110 this->nOutBytes += packet.packet.size();
Junxiao Shi57df2882015-11-11 06:12:35 -0700111 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700112
113 this->doSend(std::move(packet));
114}
115
116void
Junxiao Shi13546112015-10-14 19:33:07 -0700117Transport::receive(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700118{
Junxiao Shi13546112015-10-14 19:33:07 -0700119 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
120 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
121
Junxiao Shi57df2882015-11-11 06:12:35 -0700122 ++this->nInPackets;
123 this->nInBytes += packet.packet.size();
Eric Newberrya98bf932015-09-21 00:58:47 -0700124
125 m_service->receivePacket(std::move(packet));
126}
127
Yanbiao Li32dab972016-11-27 12:26:09 +0800128bool
129Transport::canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
130{
131 // not changing, or setting initial persistency in subclass constructor
132 if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
133 return true;
134 }
135
136 if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
137 NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
138 return false;
139 }
140
141 return this->canChangePersistencyToImpl(newPersistency);
142}
143
144bool
145Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
146{
147 return false;
148}
149
Eric Newberrya98bf932015-09-21 00:58:47 -0700150void
Davide Pesavento8728a252015-11-06 04:01:22 +0100151Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
152{
Yanbiao Li32dab972016-11-27 12:26:09 +0800153 BOOST_ASSERT(canChangePersistencyTo(newPersistency));
154
Davide Pesavento8728a252015-11-06 04:01:22 +0100155 if (m_persistency == newPersistency) {
156 return;
157 }
158
Yanbiao Li32dab972016-11-27 12:26:09 +0800159 auto oldPersistency = m_persistency;
Davide Pesavento8728a252015-11-06 04:01:22 +0100160 m_persistency = newPersistency;
Davide Pesavento32065652017-01-15 01:52:21 -0500161
162 if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
163 NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
164 this->afterChangePersistency(oldPersistency);
165 }
Yanbiao Li32dab972016-11-27 12:26:09 +0800166}
167
168void
169Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
170{
Davide Pesavento8728a252015-11-06 04:01:22 +0100171}
172
173void
Eric Newberrya98bf932015-09-21 00:58:47 -0700174Transport::setState(TransportState newState)
175{
176 if (m_state == newState) {
177 return;
178 }
179
180 bool isValid = false;
181 switch (m_state) {
182 case TransportState::UP:
183 isValid = newState == TransportState::DOWN ||
184 newState == TransportState::CLOSING ||
185 newState == TransportState::FAILED;
186 break;
187 case TransportState::DOWN:
188 isValid = newState == TransportState::UP ||
189 newState == TransportState::CLOSING ||
190 newState == TransportState::FAILED;
191 break;
192 case TransportState::CLOSING:
193 case TransportState::FAILED:
194 isValid = newState == TransportState::CLOSED;
195 break;
196 default:
197 break;
198 }
199
200 if (!isValid) {
Davide Pesavento32065652017-01-15 01:52:21 -0500201 BOOST_THROW_EXCEPTION(std::runtime_error("invalid state transition"));
Eric Newberrya98bf932015-09-21 00:58:47 -0700202 }
203
204 NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
205
206 TransportState oldState = m_state;
207 m_state = newState;
208 afterStateChange(oldState, newState);
Davide Pesavento32065652017-01-15 01:52:21 -0500209 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -0700210 // the Transport may be deallocated in the signal handler if newState is CLOSED
211}
212
213std::ostream&
214operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
215{
216 const Transport& transport = flh.obj;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700217 const Face* face = transport.getFace();
Eric Newberrya98bf932015-09-21 00:58:47 -0700218 FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
219
220 os << "[id=" << faceId << ",local=" << transport.getLocalUri()
221 << ",remote=" << transport.getRemoteUri() << "] ";
222 return os;
223}
224
225} // namespace face
226} // namespace nfd