blob: 0c4a4c68f8bb0c9d357415fefe86e4494e9ce8a0 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- 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
30namespace nfd {
31namespace face {
32
33NFD_LOG_INIT("Transport");
34
35std::ostream&
36operator<<(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
54Transport::Packet::Packet(Block&& packet1)
55 : packet(std::move(packet1))
Junxiao Shi13546112015-10-14 19:33:07 -070056 , remoteEndpoint(0)
Eric Newberrya98bf932015-09-21 00:58:47 -070057{
58}
59
60Transport::Transport()
61 : m_face(nullptr)
62 , m_service(nullptr)
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070063 , m_scope(ndn::nfd::FACE_SCOPE_NONE)
64 , m_persistency(ndn::nfd::FACE_PERSISTENCY_NONE)
65 , m_linkType(ndn::nfd::LINK_TYPE_NONE)
66 , m_mtu(MTU_INVALID)
Eric Newberrya98bf932015-09-21 00:58:47 -070067 , m_state(TransportState::UP)
Junxiao Shi57df2882015-11-11 06:12:35 -070068 , m_oldCounters(nullptr)
Eric Newberrya98bf932015-09-21 00:58:47 -070069{
70}
71
72Transport::~Transport()
73{
74}
75
76void
77Transport::setFaceAndLinkService(LpFace& face, LinkService& service)
78{
79 BOOST_ASSERT(m_face == nullptr);
80 BOOST_ASSERT(m_service == nullptr);
81
82 m_face = &face;
83 m_service = &service;
Junxiao Shi57df2882015-11-11 06:12:35 -070084 m_oldCounters = &m_face->getMutableCounters();
Eric Newberrya98bf932015-09-21 00:58:47 -070085}
86
87void
88Transport::close()
89{
90 if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
91 return;
92 }
93
94 this->setState(TransportState::CLOSING);
95 this->doClose();
96 // warning: don't access any fields after this:
97 // the Transport may be deallocated if doClose changes state to CLOSED
98}
99
100void
Junxiao Shi13546112015-10-14 19:33:07 -0700101Transport::send(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700102{
Junxiao Shi13546112015-10-14 19:33:07 -0700103 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
104 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
105
106 TransportState state = this->getState();
107 if (state != TransportState::UP && state != TransportState::DOWN) {
108 NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
109 return;
110 }
111
Junxiao Shi57df2882015-11-11 06:12:35 -0700112 if (state == TransportState::UP) {
113 ++this->nOutPackets;
114 this->nOutBytes += packet.packet.size();
115 m_oldCounters->getNOutBytes() += packet.packet.size();
116 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700117
118 this->doSend(std::move(packet));
119}
120
121void
Junxiao Shi13546112015-10-14 19:33:07 -0700122Transport::receive(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700123{
Junxiao Shi13546112015-10-14 19:33:07 -0700124 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
125 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
126
Junxiao Shi57df2882015-11-11 06:12:35 -0700127 ++this->nInPackets;
128 this->nInBytes += packet.packet.size();
129 m_oldCounters->getNInBytes() += packet.packet.size();
Eric Newberrya98bf932015-09-21 00:58:47 -0700130
131 m_service->receivePacket(std::move(packet));
132}
133
134void
Davide Pesavento8728a252015-11-06 04:01:22 +0100135Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
136{
137 if (m_persistency == newPersistency) {
138 return;
139 }
140
141 if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
142 throw std::runtime_error("invalid persistency transition");
143 }
144
145 if (m_persistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
146 this->beforeChangePersistency(newPersistency);
147 NFD_LOG_FACE_DEBUG("setPersistency " << m_persistency << " -> " << newPersistency);
148 }
149
150 m_persistency = newPersistency;
151}
152
153void
Eric Newberrya98bf932015-09-21 00:58:47 -0700154Transport::setState(TransportState newState)
155{
156 if (m_state == newState) {
157 return;
158 }
159
160 bool isValid = false;
161 switch (m_state) {
162 case TransportState::UP:
163 isValid = newState == TransportState::DOWN ||
164 newState == TransportState::CLOSING ||
165 newState == TransportState::FAILED;
166 break;
167 case TransportState::DOWN:
168 isValid = newState == TransportState::UP ||
169 newState == TransportState::CLOSING ||
170 newState == TransportState::FAILED;
171 break;
172 case TransportState::CLOSING:
173 case TransportState::FAILED:
174 isValid = newState == TransportState::CLOSED;
175 break;
176 default:
177 break;
178 }
179
180 if (!isValid) {
181 throw std::runtime_error("invalid state transition");
182 }
183
184 NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
185
186 TransportState oldState = m_state;
187 m_state = newState;
188 afterStateChange(oldState, newState);
189 // warning: don't access any fields after this:
190 // the Transport may be deallocated in the signal handler if newState is CLOSED
191}
192
193std::ostream&
194operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
195{
196 const Transport& transport = flh.obj;
197 const LpFace* face = transport.getFace();
198 FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
199
200 os << "[id=" << faceId << ",local=" << transport.getLocalUri()
201 << ",remote=" << transport.getRemoteUri() << "] ";
202 return os;
203}
204
205} // namespace face
206} // namespace nfd