blob: f615a10f9642477a18fbfc6b1e7c38cfd7c906d3 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryb49313d2017-12-24 20:22:27 -07002/*
3 * Copyright (c) 2014-2018, 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
Davide Pesaventoa3148082018-04-12 18:21:54 -040032NFD_LOG_INIT(Transport);
Eric Newberrya98bf932015-09-21 00:58:47 -070033
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 Newberryb49313d2017-12-24 20:22:27 -070066 , m_sendQueueCapacity(QUEUE_UNSUPPORTED)
Eric Newberrya98bf932015-09-21 00:58:47 -070067 , m_state(TransportState::UP)
Eric Newberryc64d30a2015-12-26 11:07:27 -070068 , m_expirationTime(time::steady_clock::TimePoint::max())
Eric Newberrya98bf932015-09-21 00:58:47 -070069{
70}
71
Davide Pesavento32065652017-01-15 01:52:21 -050072Transport::~Transport() = default;
Eric Newberrya98bf932015-09-21 00:58:47 -070073
74void
Junxiao Shicde37ad2015-12-24 01:02:05 -070075Transport::setFaceAndLinkService(Face& face, LinkService& service)
Eric Newberrya98bf932015-09-21 00:58:47 -070076{
77 BOOST_ASSERT(m_face == nullptr);
78 BOOST_ASSERT(m_service == nullptr);
79
80 m_face = &face;
81 m_service = &service;
Eric Newberrya98bf932015-09-21 00:58:47 -070082}
83
84void
85Transport::close()
86{
87 if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
88 return;
89 }
90
91 this->setState(TransportState::CLOSING);
92 this->doClose();
Davide Pesavento32065652017-01-15 01:52:21 -050093 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -070094 // the Transport may be deallocated if doClose changes state to CLOSED
95}
96
97void
Junxiao Shi13546112015-10-14 19:33:07 -070098Transport::send(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -070099{
Junxiao Shi13546112015-10-14 19:33:07 -0700100 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
101 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
102
103 TransportState state = this->getState();
104 if (state != TransportState::UP && state != TransportState::DOWN) {
105 NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
106 return;
107 }
108
Junxiao Shi57df2882015-11-11 06:12:35 -0700109 if (state == TransportState::UP) {
110 ++this->nOutPackets;
111 this->nOutBytes += packet.packet.size();
Junxiao Shi57df2882015-11-11 06:12:35 -0700112 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700113
114 this->doSend(std::move(packet));
115}
116
117void
Junxiao Shi13546112015-10-14 19:33:07 -0700118Transport::receive(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700119{
Junxiao Shi13546112015-10-14 19:33:07 -0700120 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
121 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
122
Junxiao Shi57df2882015-11-11 06:12:35 -0700123 ++this->nInPackets;
124 this->nInBytes += packet.packet.size();
Eric Newberrya98bf932015-09-21 00:58:47 -0700125
126 m_service->receivePacket(std::move(packet));
127}
128
Eric Newberryb49313d2017-12-24 20:22:27 -0700129ssize_t
130Transport::getSendQueueLength()
131{
132 return QUEUE_UNSUPPORTED;
133}
134
Yanbiao Li32dab972016-11-27 12:26:09 +0800135bool
136Transport::canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
137{
138 // not changing, or setting initial persistency in subclass constructor
139 if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
140 return true;
141 }
142
143 if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
144 NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
145 return false;
146 }
147
148 return this->canChangePersistencyToImpl(newPersistency);
149}
150
151bool
152Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
153{
154 return false;
155}
156
Eric Newberrya98bf932015-09-21 00:58:47 -0700157void
Davide Pesavento8728a252015-11-06 04:01:22 +0100158Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
159{
Yanbiao Li32dab972016-11-27 12:26:09 +0800160 BOOST_ASSERT(canChangePersistencyTo(newPersistency));
161
Davide Pesavento8728a252015-11-06 04:01:22 +0100162 if (m_persistency == newPersistency) {
163 return;
164 }
165
Yanbiao Li32dab972016-11-27 12:26:09 +0800166 auto oldPersistency = m_persistency;
Davide Pesavento8728a252015-11-06 04:01:22 +0100167 m_persistency = newPersistency;
Davide Pesavento32065652017-01-15 01:52:21 -0500168
169 if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
170 NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
171 this->afterChangePersistency(oldPersistency);
172 }
Yanbiao Li32dab972016-11-27 12:26:09 +0800173}
174
175void
176Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
177{
Davide Pesavento8728a252015-11-06 04:01:22 +0100178}
179
180void
Eric Newberrya98bf932015-09-21 00:58:47 -0700181Transport::setState(TransportState newState)
182{
183 if (m_state == newState) {
184 return;
185 }
186
187 bool isValid = false;
188 switch (m_state) {
189 case TransportState::UP:
190 isValid = newState == TransportState::DOWN ||
191 newState == TransportState::CLOSING ||
192 newState == TransportState::FAILED;
193 break;
194 case TransportState::DOWN:
195 isValid = newState == TransportState::UP ||
196 newState == TransportState::CLOSING ||
197 newState == TransportState::FAILED;
198 break;
199 case TransportState::CLOSING:
200 case TransportState::FAILED:
201 isValid = newState == TransportState::CLOSED;
202 break;
203 default:
204 break;
205 }
206
207 if (!isValid) {
Davide Pesavento32065652017-01-15 01:52:21 -0500208 BOOST_THROW_EXCEPTION(std::runtime_error("invalid state transition"));
Eric Newberrya98bf932015-09-21 00:58:47 -0700209 }
210
211 NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
212
213 TransportState oldState = m_state;
214 m_state = newState;
215 afterStateChange(oldState, newState);
Davide Pesavento32065652017-01-15 01:52:21 -0500216 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -0700217 // the Transport may be deallocated in the signal handler if newState is CLOSED
218}
219
220std::ostream&
221operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
222{
223 const Transport& transport = flh.obj;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700224 const Face* face = transport.getFace();
Eric Newberrya98bf932015-09-21 00:58:47 -0700225 FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
226
227 os << "[id=" << faceId << ",local=" << transport.getLocalUri()
228 << ",remote=" << transport.getRemoteUri() << "] ";
229 return os;
230}
231
232} // namespace face
233} // namespace nfd