blob: 26cc0d77d587f2e6856a94deb7ddf7ed6042db83 [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"
Junxiao Shicde37ad2015-12-24 01:02:05 -070027#include "face.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070028
29namespace nfd {
30namespace face {
31
32NFD_LOG_INIT("Transport");
33
34std::ostream&
35operator<<(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
53Transport::Packet::Packet(Block&& packet1)
54 : packet(std::move(packet1))
Junxiao Shi13546112015-10-14 19:33:07 -070055 , remoteEndpoint(0)
Eric Newberrya98bf932015-09-21 00:58:47 -070056{
57}
58
59Transport::Transport()
60 : m_face(nullptr)
61 , m_service(nullptr)
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070062 , 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 Newberrya98bf932015-09-21 00:58:47 -070066 , m_state(TransportState::UP)
Eric Newberryc64d30a2015-12-26 11:07:27 -070067 , m_expirationTime(time::steady_clock::TimePoint::max())
Eric Newberrya98bf932015-09-21 00:58:47 -070068{
69}
70
71Transport::~Transport()
72{
73}
74
75void
Junxiao Shicde37ad2015-12-24 01:02:05 -070076Transport::setFaceAndLinkService(Face& face, LinkService& service)
Eric Newberrya98bf932015-09-21 00:58:47 -070077{
78 BOOST_ASSERT(m_face == nullptr);
79 BOOST_ASSERT(m_service == nullptr);
80
81 m_face = &face;
82 m_service = &service;
Eric Newberrya98bf932015-09-21 00:58:47 -070083}
84
85void
86Transport::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
98void
Junxiao Shi13546112015-10-14 19:33:07 -070099Transport::send(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700100{
Junxiao Shi13546112015-10-14 19:33:07 -0700101 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
102 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
103
104 TransportState state = this->getState();
105 if (state != TransportState::UP && state != TransportState::DOWN) {
106 NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
107 return;
108 }
109
Junxiao Shi57df2882015-11-11 06:12:35 -0700110 if (state == TransportState::UP) {
111 ++this->nOutPackets;
112 this->nOutBytes += packet.packet.size();
Junxiao Shi57df2882015-11-11 06:12:35 -0700113 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700114
115 this->doSend(std::move(packet));
116}
117
118void
Junxiao Shi13546112015-10-14 19:33:07 -0700119Transport::receive(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700120{
Junxiao Shi13546112015-10-14 19:33:07 -0700121 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
122 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
123
Junxiao Shi57df2882015-11-11 06:12:35 -0700124 ++this->nInPackets;
125 this->nInBytes += packet.packet.size();
Eric Newberrya98bf932015-09-21 00:58:47 -0700126
127 m_service->receivePacket(std::move(packet));
128}
129
130void
Davide Pesavento8728a252015-11-06 04:01:22 +0100131Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
132{
133 if (m_persistency == newPersistency) {
134 return;
135 }
136
137 if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
138 throw std::runtime_error("invalid persistency transition");
139 }
140
141 if (m_persistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
142 this->beforeChangePersistency(newPersistency);
143 NFD_LOG_FACE_DEBUG("setPersistency " << m_persistency << " -> " << newPersistency);
144 }
145
146 m_persistency = newPersistency;
147}
148
149void
Eric Newberrya98bf932015-09-21 00:58:47 -0700150Transport::setState(TransportState newState)
151{
152 if (m_state == newState) {
153 return;
154 }
155
156 bool isValid = false;
157 switch (m_state) {
158 case TransportState::UP:
159 isValid = newState == TransportState::DOWN ||
160 newState == TransportState::CLOSING ||
161 newState == TransportState::FAILED;
162 break;
163 case TransportState::DOWN:
164 isValid = newState == TransportState::UP ||
165 newState == TransportState::CLOSING ||
166 newState == TransportState::FAILED;
167 break;
168 case TransportState::CLOSING:
169 case TransportState::FAILED:
170 isValid = newState == TransportState::CLOSED;
171 break;
172 default:
173 break;
174 }
175
176 if (!isValid) {
177 throw std::runtime_error("invalid state transition");
178 }
179
180 NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
181
182 TransportState oldState = m_state;
183 m_state = newState;
184 afterStateChange(oldState, newState);
185 // warning: don't access any fields after this:
186 // the Transport may be deallocated in the signal handler if newState is CLOSED
187}
188
189std::ostream&
190operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
191{
192 const Transport& transport = flh.obj;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700193 const Face* face = transport.getFace();
Eric Newberrya98bf932015-09-21 00:58:47 -0700194 FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
195
196 os << "[id=" << faceId << ",local=" << transport.getLocalUri()
197 << ",remote=" << transport.getRemoteUri() << "] ";
198 return os;
199}
200
201} // namespace face
202} // namespace nfd