Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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 | |
| 32 | NFD_LOG_INIT("Transport"); |
| 33 | |
| 34 | std::ostream& |
| 35 | operator<<(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 | |
| 53 | Transport::Packet::Packet(Block&& packet1) |
| 54 | : packet(std::move(packet1)) |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 55 | , remoteEndpoint(0) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | Transport::Transport() |
| 60 | : m_face(nullptr) |
| 61 | , m_service(nullptr) |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 62 | , 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 Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 66 | , m_state(TransportState::UP) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
| 70 | Transport::~Transport() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 75 | Transport::setFaceAndLinkService(Face& face, LinkService& service) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 76 | { |
| 77 | BOOST_ASSERT(m_face == nullptr); |
| 78 | BOOST_ASSERT(m_service == nullptr); |
| 79 | |
| 80 | m_face = &face; |
| 81 | m_service = &service; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | Transport::close() |
| 86 | { |
| 87 | if (m_state != TransportState::UP && m_state != TransportState::DOWN) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | this->setState(TransportState::CLOSING); |
| 92 | this->doClose(); |
| 93 | // warning: don't access any fields after this: |
| 94 | // the Transport may be deallocated if doClose changes state to CLOSED |
| 95 | } |
| 96 | |
| 97 | void |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 98 | Transport::send(Packet&& packet) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 99 | { |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 100 | 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 Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 109 | if (state == TransportState::UP) { |
| 110 | ++this->nOutPackets; |
| 111 | this->nOutBytes += packet.packet.size(); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 112 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 113 | |
| 114 | this->doSend(std::move(packet)); |
| 115 | } |
| 116 | |
| 117 | void |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 118 | Transport::receive(Packet&& packet) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 119 | { |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 120 | BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED || |
| 121 | packet.packet.size() <= static_cast<size_t>(this->getMtu())); |
| 122 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 123 | ++this->nInPackets; |
| 124 | this->nInBytes += packet.packet.size(); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 125 | |
| 126 | m_service->receivePacket(std::move(packet)); |
| 127 | } |
| 128 | |
| 129 | void |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 130 | Transport::setPersistency(ndn::nfd::FacePersistency newPersistency) |
| 131 | { |
| 132 | if (m_persistency == newPersistency) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) { |
| 137 | throw std::runtime_error("invalid persistency transition"); |
| 138 | } |
| 139 | |
| 140 | if (m_persistency != ndn::nfd::FACE_PERSISTENCY_NONE) { |
| 141 | this->beforeChangePersistency(newPersistency); |
| 142 | NFD_LOG_FACE_DEBUG("setPersistency " << m_persistency << " -> " << newPersistency); |
| 143 | } |
| 144 | |
| 145 | m_persistency = newPersistency; |
| 146 | } |
| 147 | |
| 148 | void |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 149 | Transport::setState(TransportState newState) |
| 150 | { |
| 151 | if (m_state == newState) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | bool isValid = false; |
| 156 | switch (m_state) { |
| 157 | case TransportState::UP: |
| 158 | isValid = newState == TransportState::DOWN || |
| 159 | newState == TransportState::CLOSING || |
| 160 | newState == TransportState::FAILED; |
| 161 | break; |
| 162 | case TransportState::DOWN: |
| 163 | isValid = newState == TransportState::UP || |
| 164 | newState == TransportState::CLOSING || |
| 165 | newState == TransportState::FAILED; |
| 166 | break; |
| 167 | case TransportState::CLOSING: |
| 168 | case TransportState::FAILED: |
| 169 | isValid = newState == TransportState::CLOSED; |
| 170 | break; |
| 171 | default: |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | if (!isValid) { |
| 176 | throw std::runtime_error("invalid state transition"); |
| 177 | } |
| 178 | |
| 179 | NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState); |
| 180 | |
| 181 | TransportState oldState = m_state; |
| 182 | m_state = newState; |
| 183 | afterStateChange(oldState, newState); |
| 184 | // warning: don't access any fields after this: |
| 185 | // the Transport may be deallocated in the signal handler if newState is CLOSED |
| 186 | } |
| 187 | |
| 188 | std::ostream& |
| 189 | operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh) |
| 190 | { |
| 191 | const Transport& transport = flh.obj; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 192 | const Face* face = transport.getFace(); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 193 | FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId(); |
| 194 | |
| 195 | os << "[id=" << faceId << ",local=" << transport.getLocalUri() |
| 196 | << ",remote=" << transport.getRemoteUri() << "] "; |
| 197 | return os; |
| 198 | } |
| 199 | |
| 200 | } // namespace face |
| 201 | } // namespace nfd |