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)) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | Transport::Transport() |
| 60 | : m_face(nullptr) |
| 61 | , m_service(nullptr) |
| 62 | , m_scope(ndn::nfd::FACE_SCOPE_NON_LOCAL) |
| 63 | , m_persistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 64 | , m_linkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT) |
| 65 | , m_state(TransportState::UP) |
| 66 | , m_counters(nullptr) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | Transport::~Transport() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | Transport::setFaceAndLinkService(LpFace& face, LinkService& service) |
| 76 | { |
| 77 | BOOST_ASSERT(m_face == nullptr); |
| 78 | BOOST_ASSERT(m_service == nullptr); |
| 79 | |
| 80 | m_face = &face; |
| 81 | m_service = &service; |
| 82 | m_counters = &m_face->getMutableCounters(); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | Transport::close() |
| 87 | { |
| 88 | if (m_state != TransportState::UP && m_state != TransportState::DOWN) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | this->setState(TransportState::CLOSING); |
| 93 | this->doClose(); |
| 94 | // warning: don't access any fields after this: |
| 95 | // the Transport may be deallocated if doClose changes state to CLOSED |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | Transport::send(Transport::Packet&& packet) |
| 100 | { |
| 101 | // TODO#3177 increment LpPacket counter |
| 102 | m_counters->getNOutBytes() += packet.packet.size(); |
| 103 | |
| 104 | this->doSend(std::move(packet)); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | Transport::receive(Transport::Packet&& packet) |
| 109 | { |
| 110 | // TODO#3177 increment LpPacket counter |
| 111 | m_counters->getNInBytes() += packet.packet.size(); |
| 112 | |
| 113 | m_service->receivePacket(std::move(packet)); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | Transport::setState(TransportState newState) |
| 118 | { |
| 119 | if (m_state == newState) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | bool isValid = false; |
| 124 | switch (m_state) { |
| 125 | case TransportState::UP: |
| 126 | isValid = newState == TransportState::DOWN || |
| 127 | newState == TransportState::CLOSING || |
| 128 | newState == TransportState::FAILED; |
| 129 | break; |
| 130 | case TransportState::DOWN: |
| 131 | isValid = newState == TransportState::UP || |
| 132 | newState == TransportState::CLOSING || |
| 133 | newState == TransportState::FAILED; |
| 134 | break; |
| 135 | case TransportState::CLOSING: |
| 136 | case TransportState::FAILED: |
| 137 | isValid = newState == TransportState::CLOSED; |
| 138 | break; |
| 139 | default: |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | if (!isValid) { |
| 144 | throw std::runtime_error("invalid state transition"); |
| 145 | } |
| 146 | |
| 147 | NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState); |
| 148 | |
| 149 | TransportState oldState = m_state; |
| 150 | m_state = newState; |
| 151 | afterStateChange(oldState, newState); |
| 152 | // warning: don't access any fields after this: |
| 153 | // the Transport may be deallocated in the signal handler if newState is CLOSED |
| 154 | } |
| 155 | |
| 156 | std::ostream& |
| 157 | operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh) |
| 158 | { |
| 159 | const Transport& transport = flh.obj; |
| 160 | const LpFace* face = transport.getFace(); |
| 161 | FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId(); |
| 162 | |
| 163 | os << "[id=" << faceId << ",local=" << transport.getLocalUri() |
| 164 | << ",remote=" << transport.getRemoteUri() << "] "; |
| 165 | return os; |
| 166 | } |
| 167 | |
| 168 | } // namespace face |
| 169 | } // namespace nfd |