Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 4 | * 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 Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 27 | #include "face.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace face { |
| 31 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 32 | NFD_LOG_INIT(Transport); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 3e7fc83 | 2018-09-08 14:07:29 -0400 | [diff] [blame] | 34 | const ssize_t Transport::MIN_MTU; |
| 35 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 36 | std::ostream& |
| 37 | operator<<(std::ostream& os, TransportState state) |
| 38 | { |
| 39 | switch (state) { |
| 40 | case TransportState::UP: |
| 41 | return os << "UP"; |
| 42 | case TransportState::DOWN: |
| 43 | return os << "DOWN"; |
| 44 | case TransportState::CLOSING: |
| 45 | return os << "CLOSING"; |
| 46 | case TransportState::FAILED: |
| 47 | return os << "FAILED"; |
| 48 | case TransportState::CLOSED: |
| 49 | return os << "CLOSED"; |
| 50 | default: |
| 51 | return os << "NONE"; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | Transport::Packet::Packet(Block&& packet1) |
| 56 | : packet(std::move(packet1)) |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 57 | , remoteEndpoint(0) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 58 | { |
| 59 | } |
| 60 | |
| 61 | Transport::Transport() |
| 62 | : m_face(nullptr) |
| 63 | , m_service(nullptr) |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 64 | , m_scope(ndn::nfd::FACE_SCOPE_NONE) |
| 65 | , m_persistency(ndn::nfd::FACE_PERSISTENCY_NONE) |
| 66 | , m_linkType(ndn::nfd::LINK_TYPE_NONE) |
| 67 | , m_mtu(MTU_INVALID) |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 68 | , m_sendQueueCapacity(QUEUE_UNSUPPORTED) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 69 | , m_state(TransportState::UP) |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 70 | , m_expirationTime(time::steady_clock::TimePoint::max()) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 71 | { |
| 72 | } |
| 73 | |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 74 | Transport::~Transport() = default; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 75 | |
| 76 | void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 77 | Transport::setFaceAndLinkService(Face& face, LinkService& service) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 78 | { |
| 79 | BOOST_ASSERT(m_face == nullptr); |
| 80 | BOOST_ASSERT(m_service == nullptr); |
| 81 | |
| 82 | m_face = &face; |
| 83 | m_service = &service; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void |
| 87 | Transport::close() |
| 88 | { |
| 89 | if (m_state != TransportState::UP && m_state != TransportState::DOWN) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | this->setState(TransportState::CLOSING); |
| 94 | this->doClose(); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 95 | // warning: don't access any members after this: |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 96 | // the Transport may be deallocated if doClose changes state to CLOSED |
| 97 | } |
| 98 | |
| 99 | void |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 100 | Transport::send(Packet&& packet) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 101 | { |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 102 | BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED || |
| 103 | packet.packet.size() <= static_cast<size_t>(this->getMtu())); |
| 104 | |
| 105 | TransportState state = this->getState(); |
| 106 | if (state != TransportState::UP && state != TransportState::DOWN) { |
| 107 | NFD_LOG_FACE_TRACE("send ignored in " << state << " state"); |
| 108 | return; |
| 109 | } |
| 110 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 111 | if (state == TransportState::UP) { |
| 112 | ++this->nOutPackets; |
| 113 | this->nOutBytes += packet.packet.size(); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 114 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 115 | |
| 116 | this->doSend(std::move(packet)); |
| 117 | } |
| 118 | |
| 119 | void |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 120 | Transport::receive(Packet&& packet) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 121 | { |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 122 | BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED || |
| 123 | packet.packet.size() <= static_cast<size_t>(this->getMtu())); |
| 124 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 125 | ++this->nInPackets; |
| 126 | this->nInBytes += packet.packet.size(); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 127 | |
| 128 | m_service->receivePacket(std::move(packet)); |
| 129 | } |
| 130 | |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 131 | bool |
| 132 | Transport::canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const |
| 133 | { |
| 134 | // not changing, or setting initial persistency in subclass constructor |
| 135 | if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) { |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) { |
| 140 | NFD_LOG_FACE_TRACE("cannot change persistency to NONE"); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | return this->canChangePersistencyToImpl(newPersistency); |
| 145 | } |
| 146 | |
| 147 | bool |
| 148 | Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const |
| 149 | { |
| 150 | return false; |
| 151 | } |
| 152 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 153 | void |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 154 | Transport::setPersistency(ndn::nfd::FacePersistency newPersistency) |
| 155 | { |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 156 | BOOST_ASSERT(canChangePersistencyTo(newPersistency)); |
| 157 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 158 | if (m_persistency == newPersistency) { |
| 159 | return; |
| 160 | } |
| 161 | |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 162 | auto oldPersistency = m_persistency; |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 163 | m_persistency = newPersistency; |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 164 | |
| 165 | if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) { |
| 166 | NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency); |
| 167 | this->afterChangePersistency(oldPersistency); |
| 168 | } |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void |
| 172 | Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) |
| 173 | { |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 177 | Transport::setState(TransportState newState) |
| 178 | { |
| 179 | if (m_state == newState) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | bool isValid = false; |
| 184 | switch (m_state) { |
| 185 | case TransportState::UP: |
| 186 | isValid = newState == TransportState::DOWN || |
| 187 | newState == TransportState::CLOSING || |
| 188 | newState == TransportState::FAILED; |
| 189 | break; |
| 190 | case TransportState::DOWN: |
| 191 | isValid = newState == TransportState::UP || |
| 192 | newState == TransportState::CLOSING || |
| 193 | newState == TransportState::FAILED; |
| 194 | break; |
| 195 | case TransportState::CLOSING: |
| 196 | case TransportState::FAILED: |
| 197 | isValid = newState == TransportState::CLOSED; |
| 198 | break; |
| 199 | default: |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | if (!isValid) { |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 204 | BOOST_THROW_EXCEPTION(std::runtime_error("invalid state transition")); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState); |
| 208 | |
| 209 | TransportState oldState = m_state; |
| 210 | m_state = newState; |
| 211 | afterStateChange(oldState, newState); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 212 | // warning: don't access any members after this: |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 213 | // the Transport may be deallocated in the signal handler if newState is CLOSED |
| 214 | } |
| 215 | |
| 216 | std::ostream& |
| 217 | operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh) |
| 218 | { |
| 219 | const Transport& transport = flh.obj; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 220 | const Face* face = transport.getFace(); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 221 | FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId(); |
| 222 | |
| 223 | os << "[id=" << faceId << ",local=" << transport.getLocalUri() |
| 224 | << ",remote=" << transport.getRemoteUri() << "] "; |
| 225 | return os; |
| 226 | } |
| 227 | |
| 228 | } // namespace face |
| 229 | } // namespace nfd |