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, |
Alexander Afanasyev | c91fc33 | 2017-02-10 17:44:41 -0800 | [diff] [blame^] | 34 | bool isSegment, |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 35 | const SegmentCallback& segmentCallback, |
| 36 | const FinishCallback& finishCallback, |
| 37 | const OnFetchCompleteCallback& onFetchComplete, |
| 38 | const OnFetchFailedCallback& onFetchFailed, |
| 39 | const Name& deviceName, const Name& name, int64_t minSeqNo, int64_t maxSeqNo, |
| 40 | time::milliseconds timeout, const Name& forwardingHint) |
| 41 | : m_face(face) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 43 | , m_segmentCallback(segmentCallback) |
| 44 | , m_onFetchComplete(onFetchComplete) |
| 45 | , m_onFetchFailed(onFetchFailed) |
| 46 | , m_finishCallback(finishCallback) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 48 | , m_active(false) |
| 49 | , m_timedwait(false) |
| 50 | , m_name(name) |
| 51 | , m_deviceName(deviceName) |
| 52 | , m_forwardingHint(forwardingHint) |
| 53 | , m_maximumNoActivityPeriod(timeout) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 55 | , m_minSendSeqNo(minSeqNo - 1) |
| 56 | , m_maxInOrderRecvSeqNo(minSeqNo - 1) |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 57 | // , m_minSeqNo(minSeqNo) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 58 | , m_maxSeqNo(maxSeqNo) |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 60 | , m_pipeline(6) // initial "congestion window" |
| 61 | , m_activePipeline(0) |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 62 | |
| 63 | , m_slowStart(false) |
| 64 | , m_threshold(32767) // TODO make these values dynamic |
| 65 | , m_roundCount(32767) |
| 66 | |
| 67 | , m_retryPause(time::seconds::zero()) |
| 68 | , m_nextScheduledRetry(time::steady_clock::now()) |
| 69 | |
| 70 | , m_ioService(m_face.getIoService()) |
Alexander Afanasyev | c91fc33 | 2017-02-10 17:44:41 -0800 | [diff] [blame^] | 71 | , m_isSegment(isSegment) |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 72 | { |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 75 | Fetcher::~Fetcher() |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 76 | { |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 77 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 78 | |
| 79 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 80 | Fetcher::RestartPipeline() |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 81 | { |
| 82 | m_active = true; |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 83 | m_minSendSeqNo = m_maxInOrderRecvSeqNo; |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 84 | // cout << "Restart: " << m_minSendSeqNo << endl; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 85 | m_lastPositiveActivity = time::steady_clock::now(); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 86 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 87 | m_ioService.post(bind(&Fetcher::FillPipeline, this)); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 91 | Fetcher::SetForwardingHint(const Name& forwardingHint) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 92 | { |
| 93 | m_forwardingHint = forwardingHint; |
| 94 | } |
| 95 | |
| 96 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 97 | Fetcher::FillPipeline() |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 98 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 99 | for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++) { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 100 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 102 | if (m_outOfOrderRecvSeqNo.find(m_minSendSeqNo + 1) != m_outOfOrderRecvSeqNo.end()) |
| 103 | continue; |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 105 | if (m_inActivePipeline.find(m_minSendSeqNo + 1) != m_inActivePipeline.end()) |
| 106 | continue; |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 108 | m_inActivePipeline.insert(m_minSendSeqNo + 1); |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 109 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 110 | _LOG_DEBUG( |
| 111 | " >>> i " << Name(m_forwardingHint).append(m_name) << ", seq = " << (m_minSendSeqNo + 1)); |
Alexander Afanasyev | 678f350 | 2013-01-23 17:09:37 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 113 | // cout << ">>> " << m_minSendSeqNo+1 << endl; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | c91fc33 | 2017-02-10 17:44:41 -0800 | [diff] [blame^] | 115 | Name name = Name(m_forwardingHint).append(m_name); |
| 116 | if (m_isSegment) { |
| 117 | name.appendSegment(m_minSendSeqNo + 1); |
| 118 | } |
| 119 | else { |
| 120 | name.appendNumber(m_minSendSeqNo + 1); |
| 121 | } |
| 122 | Interest interest(name); |
| 123 | interest.setInterestLifetime(time::seconds(1)); // Alex: this lifetime should be changed to RTO |
| 124 | _LOG_DEBUG("interest: " << interest); |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 125 | m_face.expressInterest(interest, |
| 126 | bind(&Fetcher::OnData, this, m_minSendSeqNo + 1, _1, _2), |
| 127 | bind(&Fetcher::OnTimeout, this, m_minSendSeqNo + 1, _1)); |
| 128 | |
Alexander Afanasyev | c91fc33 | 2017-02-10 17:44:41 -0800 | [diff] [blame^] | 129 | _LOG_TRACE(" >>> i ok"); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 131 | m_activePipeline++; |
| 132 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 133 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 134 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 135 | Fetcher::OnData(uint64_t seqno, const Interest& interest, Data& data) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 136 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 137 | const Name& name = data.getName(); |
| 138 | _LOG_DEBUG(" <<< d " << name.getSubName(0, name.size() - 1) << ", seq = " << seqno); |
Alexander Afanasyev | ddc283c | 2013-01-28 23:11:20 -0800 | [diff] [blame] | 139 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 140 | shared_ptr<Data> pco = make_shared<Data>(data.wireEncode()); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 142 | if (m_forwardingHint == Name()) { |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 143 | // TODO: check verified!!!! |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 144 | if (true) { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 145 | if (m_segmentCallback != nullptr) { |
| 146 | m_segmentCallback(m_deviceName, m_name, seqno, pco); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 147 | } |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 148 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 149 | else { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 150 | _LOG_ERROR("Can not verify signature content. Name = " << data.getName()); |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 151 | // probably needs to do more in the future |
| 152 | } |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 153 | // we don't have to tell FetchManager about this |
| 154 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 155 | else { |
| 156 | // 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] | 157 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 158 | // we need to verify this pco and apply callback only when verified |
| 159 | // TODO: check verified !!! |
| 160 | if (true) { |
| 161 | if (m_segmentCallback != nullptr) { |
| 162 | m_segmentCallback(m_deviceName, m_name, seqno, pco); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 163 | } |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 164 | } |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 165 | else { |
| 166 | _LOG_ERROR("Can not verify signature content. Name = " << pco->getName()); |
| 167 | // probably needs to do more in the future |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 170 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 171 | m_activePipeline--; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 172 | m_lastPositiveActivity = time::steady_clock::now(); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 173 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 174 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 175 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 176 | if(m_slowStart){ |
| 177 | m_pipeline++; |
| 178 | if(m_pipeline == m_threshold) |
| 179 | m_slowStart = false; |
| 180 | } |
| 181 | else{ |
| 182 | m_roundCount++; |
| 183 | _LOG_DEBUG ("roundCount: " << m_roundCount); |
| 184 | if(m_roundCount == m_pipeline){ |
| 185 | m_pipeline++; |
| 186 | m_roundCount = 0; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 191 | _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] | 192 | |
| 193 | |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 194 | //////////////////////////////////////////////////////////////////////////// |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 195 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 196 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 197 | m_outOfOrderRecvSeqNo.insert(seqno); |
| 198 | m_inActivePipeline.erase(seqno); |
| 199 | _LOG_DEBUG("Total segments received: " << m_outOfOrderRecvSeqNo.size()); |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 200 | std::set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 201 | for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end(); inOrderSeqNo++) { |
| 202 | _LOG_TRACE("Checking " << *inOrderSeqNo << " and " << m_maxInOrderRecvSeqNo + 1); |
| 203 | if (*inOrderSeqNo == m_maxInOrderRecvSeqNo + 1) { |
| 204 | m_maxInOrderRecvSeqNo = *inOrderSeqNo; |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 205 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 206 | else if (*inOrderSeqNo < m_maxInOrderRecvSeqNo + 1) // not possible anymore, but just in case |
| 207 | { |
| 208 | continue; |
| 209 | } |
| 210 | else |
| 211 | break; |
| 212 | } |
| 213 | m_outOfOrderRecvSeqNo.erase(m_outOfOrderRecvSeqNo.begin(), inOrderSeqNo); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 214 | //////////////////////////////////////////////////////////////////////////// |
| 215 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 216 | _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] | 217 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 218 | if (m_maxInOrderRecvSeqNo == m_maxSeqNo) { |
| 219 | _LOG_TRACE("Fetch finished: " << m_name); |
| 220 | m_active = false; |
| 221 | // invoke callback |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 222 | if (m_finishCallback != nullptr) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 223 | _LOG_TRACE("Notifying callback"); |
| 224 | m_finishCallback(m_deviceName, m_name); |
| 225 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 226 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 227 | // tell FetchManager that we have finish our job |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 228 | // m_onFetchComplete(*this); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 229 | // 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] | 230 | if (m_onFetchComplete != nullptr) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 231 | m_timedwait = true; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 232 | 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] | 233 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 234 | } |
| 235 | else { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 236 | m_ioService.post(bind(&Fetcher::FillPipeline, this)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 237 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 240 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 241 | Fetcher::OnTimeout(uint64_t seqno, const Interest& interest) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 242 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 243 | const Name name = interest.getName(); |
| 244 | _LOG_DEBUG(" <<< :( timeout " << name.getSubName(0, name.size() - 1) << ", seq = " << seqno); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 245 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 246 | // cout << "Fetcher::OnTimeout: " << name << endl; |
| 247 | // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod |
| 248 | // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time() |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 249 | // << ", oldest: " <<(date_time::second_clock<boost::posix_time::ptime>::universal_time() - |
| 250 | // m_maximumNoActivityPeriod) << endl; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 251 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 252 | if (m_lastPositiveActivity < |
| 253 | (time::steady_clock::now() - m_maximumNoActivityPeriod)) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 254 | bool done = false; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 255 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 256 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 257 | m_inActivePipeline.erase(seqno); |
| 258 | m_activePipeline--; |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 259 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 260 | if (m_activePipeline == 0) { |
| 261 | done = true; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if (done) { |
| 266 | { |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 267 | std::unique_lock<std::mutex> lock(m_seqNoMutex); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 268 | _LOG_DEBUG("Telling that fetch failed"); |
| 269 | _LOG_DEBUG("Active pipeline size should be zero: " << m_inActivePipeline.size()); |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 272 | m_active = false; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 273 | if (m_onFetchFailed != nullptr) { |
| 274 | m_onFetchFailed(std::ref(*this)); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 275 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 276 | // 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] | 277 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 278 | } |
| 279 | else { |
| 280 | _LOG_DEBUG("Asking to reexpress seqno: " << seqno); |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 281 | m_face.expressInterest(interest, |
| 282 | bind(&Fetcher::OnData, this, seqno, _1, _2), // TODO: correct? |
| 283 | bind(&Fetcher::OnTimeout, this, seqno, _1)); // TODO: correct? |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 284 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 285 | } |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 286 | |
| 287 | } // namespace chronoshare |
| 288 | } // namespace ndn |