Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 21 | #include "fetcher.hpp" |
| 22 | #include "fetch-manager.hpp" |
| 23 | #include "core/logging.hpp" |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 24 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 25 | #include <boost/asio/io_service.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 26 | #include <boost/date_time/posix_time/posix_time.hpp> |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace chronoshare { |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 31 | _LOG_INIT(Fetcher); |
Alexander Afanasyev | 678f350 | 2013-01-23 17:09:37 -0800 | [diff] [blame] | 32 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 33 | Fetcher::Fetcher(Face& face, |
| 34 | const SegmentCallback& segmentCallback, |
| 35 | const FinishCallback& finishCallback, |
| 36 | const OnFetchCompleteCallback& onFetchComplete, |
| 37 | const OnFetchFailedCallback& onFetchFailed, |
| 38 | const Name& deviceName, const Name& name, int64_t minSeqNo, int64_t maxSeqNo, |
| 39 | time::milliseconds timeout, const Name& forwardingHint) |
| 40 | : m_face(face) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 42 | , m_segmentCallback(segmentCallback) |
| 43 | , m_onFetchComplete(onFetchComplete) |
| 44 | , m_onFetchFailed(onFetchFailed) |
| 45 | , m_finishCallback(finishCallback) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 47 | , m_active(false) |
| 48 | , m_timedwait(false) |
| 49 | , m_name(name) |
| 50 | , m_deviceName(deviceName) |
| 51 | , m_forwardingHint(forwardingHint) |
| 52 | , m_maximumNoActivityPeriod(timeout) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 54 | , m_minSendSeqNo(minSeqNo - 1) |
| 55 | , m_maxInOrderRecvSeqNo(minSeqNo - 1) |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 56 | // , m_minSeqNo(minSeqNo) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | , m_maxSeqNo(maxSeqNo) |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | , m_pipeline(6) // initial "congestion window" |
| 60 | , m_activePipeline(0) |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 61 | |
| 62 | , m_slowStart(false) |
| 63 | , m_threshold(32767) // TODO make these values dynamic |
| 64 | , m_roundCount(32767) |
| 65 | |
| 66 | , m_retryPause(time::seconds::zero()) |
| 67 | , m_nextScheduledRetry(time::steady_clock::now()) |
| 68 | |
| 69 | , m_ioService(m_face.getIoService()) |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | Fetcher::~Fetcher() |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 74 | { |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 76 | |
| 77 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 78 | Fetcher::RestartPipeline() |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 79 | { |
| 80 | m_active = true; |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 81 | m_minSendSeqNo = m_maxInOrderRecvSeqNo; |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 82 | // cout << "Restart: " << m_minSendSeqNo << endl; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 83 | m_lastPositiveActivity = time::steady_clock::now(); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 84 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 85 | m_ioService.post(bind(&Fetcher::FillPipeline, this)); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 89 | Fetcher::SetForwardingHint(const Name& forwardingHint) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 90 | { |
| 91 | m_forwardingHint = forwardingHint; |
| 92 | } |
| 93 | |
| 94 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 95 | Fetcher::FillPipeline() |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 96 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 97 | for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++) { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 98 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 100 | if (m_outOfOrderRecvSeqNo.find(m_minSendSeqNo + 1) != m_outOfOrderRecvSeqNo.end()) |
| 101 | continue; |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 103 | if (m_inActivePipeline.find(m_minSendSeqNo + 1) != m_inActivePipeline.end()) |
| 104 | continue; |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 106 | m_inActivePipeline.insert(m_minSendSeqNo + 1); |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 107 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 108 | _LOG_DEBUG( |
| 109 | " >>> i " << Name(m_forwardingHint).append(m_name) << ", seq = " << (m_minSendSeqNo + 1)); |
Alexander Afanasyev | 678f350 | 2013-01-23 17:09:37 -0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 111 | // cout << ">>> " << m_minSendSeqNo+1 << endl; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 112 | |
| 113 | Interest interest( |
| 114 | Name(m_forwardingHint).append(m_name).appendNumber(m_minSendSeqNo + 1)); // Alex: this lifetime should be changed to RTO |
| 115 | _LOG_DEBUG("interest Name: " << interest); |
| 116 | interest.setInterestLifetime(time::seconds(1)); |
| 117 | m_face.expressInterest(interest, |
| 118 | bind(&Fetcher::OnData, this, m_minSendSeqNo + 1, _1, _2), |
| 119 | bind(&Fetcher::OnTimeout, this, m_minSendSeqNo + 1, _1)); |
| 120 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 121 | _LOG_DEBUG(" >>> i ok"); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 123 | m_activePipeline++; |
| 124 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 125 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 126 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 127 | Fetcher::OnData(uint64_t seqno, const Interest& interest, Data& data) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 128 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 129 | const Name& name = data.getName(); |
| 130 | _LOG_DEBUG(" <<< d " << name.getSubName(0, name.size() - 1) << ", seq = " << seqno); |
Alexander Afanasyev | ddc283c | 2013-01-28 23:11:20 -0800 | [diff] [blame] | 131 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 132 | shared_ptr<Data> pco = make_shared<Data>(data.wireEncode()); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 133 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 134 | if (m_forwardingHint == Name()) { |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 135 | // TODO: check verified!!!! |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 136 | if (true) { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 137 | if (m_segmentCallback != nullptr) { |
| 138 | m_segmentCallback(m_deviceName, m_name, seqno, pco); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 139 | } |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 140 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 141 | else { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 142 | _LOG_ERROR("Can not verify signature content. Name = " << data.getName()); |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 143 | // probably needs to do more in the future |
| 144 | } |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 145 | // we don't have to tell FetchManager about this |
| 146 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 147 | else { |
| 148 | // in this case we don't care whether "data" is verified, in fact, we expect it is unverified |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 149 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 150 | // we need to verify this pco and apply callback only when verified |
| 151 | // TODO: check verified !!! |
| 152 | if (true) { |
| 153 | if (m_segmentCallback != nullptr) { |
| 154 | m_segmentCallback(m_deviceName, m_name, seqno, pco); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 155 | } |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 156 | } |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 157 | else { |
| 158 | _LOG_ERROR("Can not verify signature content. Name = " << pco->getName()); |
| 159 | // probably needs to do more in the future |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 163 | m_activePipeline--; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 164 | m_lastPositiveActivity = time::steady_clock::now(); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 165 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 166 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 167 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 168 | if(m_slowStart){ |
| 169 | m_pipeline++; |
| 170 | if(m_pipeline == m_threshold) |
| 171 | m_slowStart = false; |
| 172 | } |
| 173 | else{ |
| 174 | m_roundCount++; |
| 175 | _LOG_DEBUG ("roundCount: " << m_roundCount); |
| 176 | if(m_roundCount == m_pipeline){ |
| 177 | m_pipeline++; |
| 178 | m_roundCount = 0; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 183 | _LOG_DEBUG ("slowStart: " << std::boolalpha << m_slowStart << " pipeline: " << m_pipeline << " threshold: " << m_threshold); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 184 | |
| 185 | |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 186 | //////////////////////////////////////////////////////////////////////////// |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 187 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 189 | m_outOfOrderRecvSeqNo.insert(seqno); |
| 190 | m_inActivePipeline.erase(seqno); |
| 191 | _LOG_DEBUG("Total segments received: " << m_outOfOrderRecvSeqNo.size()); |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 192 | std::set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 193 | for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end(); inOrderSeqNo++) { |
| 194 | _LOG_TRACE("Checking " << *inOrderSeqNo << " and " << m_maxInOrderRecvSeqNo + 1); |
| 195 | if (*inOrderSeqNo == m_maxInOrderRecvSeqNo + 1) { |
| 196 | m_maxInOrderRecvSeqNo = *inOrderSeqNo; |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 197 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 198 | else if (*inOrderSeqNo < m_maxInOrderRecvSeqNo + 1) // not possible anymore, but just in case |
| 199 | { |
| 200 | continue; |
| 201 | } |
| 202 | else |
| 203 | break; |
| 204 | } |
| 205 | m_outOfOrderRecvSeqNo.erase(m_outOfOrderRecvSeqNo.begin(), inOrderSeqNo); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 206 | //////////////////////////////////////////////////////////////////////////// |
| 207 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 208 | _LOG_TRACE("Max in order received: " << m_maxInOrderRecvSeqNo << ", max seqNo to request: " << m_maxSeqNo); |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 209 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 210 | if (m_maxInOrderRecvSeqNo == m_maxSeqNo) { |
| 211 | _LOG_TRACE("Fetch finished: " << m_name); |
| 212 | m_active = false; |
| 213 | // invoke callback |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 214 | if (m_finishCallback != nullptr) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 215 | _LOG_TRACE("Notifying callback"); |
| 216 | m_finishCallback(m_deviceName, m_name); |
| 217 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 218 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 219 | // tell FetchManager that we have finish our job |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 220 | // m_onFetchComplete(*this); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 221 | // using executor, so we won't be deleted if there is scheduled FillPipeline call |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 222 | if (m_onFetchComplete != nullptr) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 223 | m_timedwait = true; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 224 | m_ioService.post(bind(m_onFetchComplete, std::ref(*this), m_deviceName, m_name)); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 225 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 226 | } |
| 227 | else { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 228 | m_ioService.post(bind(&Fetcher::FillPipeline, this)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 229 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 232 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 233 | Fetcher::OnTimeout(uint64_t seqno, const Interest& interest) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 234 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 235 | const Name name = interest.getName(); |
| 236 | _LOG_DEBUG(" <<< :( timeout " << name.getSubName(0, name.size() - 1) << ", seq = " << seqno); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 237 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 238 | // cout << "Fetcher::OnTimeout: " << name << endl; |
| 239 | // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod |
| 240 | // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time() |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 241 | // << ", oldest: " <<(date_time::second_clock<boost::posix_time::ptime>::universal_time() - |
| 242 | // m_maximumNoActivityPeriod) << endl; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 243 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 244 | if (m_lastPositiveActivity < |
| 245 | (time::steady_clock::now() - m_maximumNoActivityPeriod)) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 246 | bool done = false; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 247 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 248 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 249 | m_inActivePipeline.erase(seqno); |
| 250 | m_activePipeline--; |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 251 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 252 | if (m_activePipeline == 0) { |
| 253 | done = true; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (done) { |
| 258 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 259 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 260 | _LOG_DEBUG("Telling that fetch failed"); |
| 261 | _LOG_DEBUG("Active pipeline size should be zero: " << m_inActivePipeline.size()); |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 264 | m_active = false; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 265 | if (m_onFetchFailed != nullptr) { |
| 266 | m_onFetchFailed(std::ref(*this)); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 267 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 268 | // this is not valid anymore, but we still should be able finish work |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 269 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 270 | } |
| 271 | else { |
| 272 | _LOG_DEBUG("Asking to reexpress seqno: " << seqno); |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 273 | m_face.expressInterest(interest, |
| 274 | bind(&Fetcher::OnData, this, seqno, _1, _2), // TODO: correct? |
| 275 | bind(&Fetcher::OnTimeout, this, seqno, _1)); // TODO: correct? |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 276 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 277 | } |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 278 | |
| 279 | } // namespace chronoshare |
| 280 | } // namespace ndn |