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" |
| 27 | #include "lp-face.hpp" |
| 28 | #include "link-service.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace face { |
| 32 | |
| 33 | NFD_LOG_INIT("Transport"); |
| 34 | |
| 35 | std::ostream& |
| 36 | operator<<(std::ostream& os, TransportState state) |
| 37 | { |
| 38 | switch (state) { |
| 39 | case TransportState::UP: |
| 40 | return os << "UP"; |
| 41 | case TransportState::DOWN: |
| 42 | return os << "DOWN"; |
| 43 | case TransportState::CLOSING: |
| 44 | return os << "CLOSING"; |
| 45 | case TransportState::FAILED: |
| 46 | return os << "FAILED"; |
| 47 | case TransportState::CLOSED: |
| 48 | return os << "CLOSED"; |
| 49 | default: |
| 50 | return os << "NONE"; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | Transport::Packet::Packet(Block&& packet1) |
| 55 | : packet(std::move(packet1)) |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 56 | , remoteEndpoint(0) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 57 | { |
| 58 | } |
| 59 | |
| 60 | Transport::Transport() |
| 61 | : m_face(nullptr) |
| 62 | , m_service(nullptr) |
| 63 | , m_scope(ndn::nfd::FACE_SCOPE_NON_LOCAL) |
| 64 | , m_persistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 65 | , m_linkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT) |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 66 | , m_mtu(MTU_UNLIMITED) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 67 | , m_state(TransportState::UP) |
| 68 | , m_counters(nullptr) |
| 69 | { |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 70 | // warning: Subclass constructor must explicitly initialize all static properties |
| 71 | // using setters, and should not rely on the defaults here. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | Transport::~Transport() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | Transport::setFaceAndLinkService(LpFace& face, LinkService& service) |
| 80 | { |
| 81 | BOOST_ASSERT(m_face == nullptr); |
| 82 | BOOST_ASSERT(m_service == nullptr); |
| 83 | |
| 84 | m_face = &face; |
| 85 | m_service = &service; |
| 86 | m_counters = &m_face->getMutableCounters(); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | Transport::close() |
| 91 | { |
| 92 | if (m_state != TransportState::UP && m_state != TransportState::DOWN) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | this->setState(TransportState::CLOSING); |
| 97 | this->doClose(); |
| 98 | // warning: don't access any fields after this: |
| 99 | // the Transport may be deallocated if doClose changes state to CLOSED |
| 100 | } |
| 101 | |
| 102 | void |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 103 | Transport::send(Packet&& packet) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 104 | { |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 105 | BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED || |
| 106 | packet.packet.size() <= static_cast<size_t>(this->getMtu())); |
| 107 | |
| 108 | TransportState state = this->getState(); |
| 109 | if (state != TransportState::UP && state != TransportState::DOWN) { |
| 110 | NFD_LOG_FACE_TRACE("send ignored in " << state << " state"); |
| 111 | return; |
| 112 | } |
| 113 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 114 | // TODO#3177 increment LpPacket counter |
| 115 | m_counters->getNOutBytes() += packet.packet.size(); |
| 116 | |
| 117 | this->doSend(std::move(packet)); |
| 118 | } |
| 119 | |
| 120 | void |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 121 | Transport::receive(Packet&& packet) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 122 | { |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 123 | BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED || |
| 124 | packet.packet.size() <= static_cast<size_t>(this->getMtu())); |
| 125 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 126 | // TODO#3177 increment LpPacket counter |
| 127 | m_counters->getNInBytes() += packet.packet.size(); |
| 128 | |
| 129 | m_service->receivePacket(std::move(packet)); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | Transport::setState(TransportState newState) |
| 134 | { |
| 135 | if (m_state == newState) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | bool isValid = false; |
| 140 | switch (m_state) { |
| 141 | case TransportState::UP: |
| 142 | isValid = newState == TransportState::DOWN || |
| 143 | newState == TransportState::CLOSING || |
| 144 | newState == TransportState::FAILED; |
| 145 | break; |
| 146 | case TransportState::DOWN: |
| 147 | isValid = newState == TransportState::UP || |
| 148 | newState == TransportState::CLOSING || |
| 149 | newState == TransportState::FAILED; |
| 150 | break; |
| 151 | case TransportState::CLOSING: |
| 152 | case TransportState::FAILED: |
| 153 | isValid = newState == TransportState::CLOSED; |
| 154 | break; |
| 155 | default: |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | if (!isValid) { |
| 160 | throw std::runtime_error("invalid state transition"); |
| 161 | } |
| 162 | |
| 163 | NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState); |
| 164 | |
| 165 | TransportState oldState = m_state; |
| 166 | m_state = newState; |
| 167 | afterStateChange(oldState, newState); |
| 168 | // warning: don't access any fields after this: |
| 169 | // the Transport may be deallocated in the signal handler if newState is CLOSED |
| 170 | } |
| 171 | |
| 172 | std::ostream& |
| 173 | operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh) |
| 174 | { |
| 175 | const Transport& transport = flh.obj; |
| 176 | const LpFace* face = transport.getFace(); |
| 177 | FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId(); |
| 178 | |
| 179 | os << "[id=" << faceId << ",local=" << transport.getLocalUri() |
| 180 | << ",remote=" << transport.getRemoteUri() << "] "; |
| 181 | return os; |
| 182 | } |
| 183 | |
| 184 | } // namespace face |
| 185 | } // namespace nfd |