Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, 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 | |
| 21 | #include "fetcher.h" |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 22 | #include "fetch-manager.h" |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 23 | #include "ndnx-pco.h" |
Alexander Afanasyev | 678f350 | 2013-01-23 17:09:37 -0800 | [diff] [blame] | 24 | #include "logging.h" |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 26 | #include <boost/make_shared.hpp> |
| 27 | #include <boost/ref.hpp> |
| 28 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 29 | #include <boost/date_time/posix_time/posix_time.hpp> |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 678f350 | 2013-01-23 17:09:37 -0800 | [diff] [blame] | 31 | INIT_LOGGER ("Fetcher"); |
| 32 | |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 33 | using namespace boost; |
| 34 | using namespace std; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 35 | using namespace Ndnx; |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 37 | Fetcher::Fetcher (Ndnx::NdnxWrapperPtr ndnx, |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 38 | ExecutorPtr executor, |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 39 | const SegmentCallback &segmentCallback, |
| 40 | const FinishCallback &finishCallback, |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 41 | OnFetchCompleteCallback onFetchComplete, OnFetchFailedCallback onFetchFailed, |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 42 | const Ndnx::Name &deviceName, const Ndnx::Name &name, int64_t minSeqNo, int64_t maxSeqNo, |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 43 | boost::posix_time::time_duration timeout/* = boost::posix_time::seconds (30)*/, |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 44 | const Ndnx::Name &forwardingHint/* = Ndnx::Name ()*/) |
| 45 | : m_ndnx (ndnx) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 46 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 47 | , m_segmentCallback (segmentCallback) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 48 | , m_onFetchComplete (onFetchComplete) |
| 49 | , m_onFetchFailed (onFetchFailed) |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 50 | , m_finishCallback (finishCallback) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 52 | , m_active (false) |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 53 | , m_timedwait (false) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 54 | , m_name (name) |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 55 | , m_deviceName (deviceName) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 56 | , m_forwardingHint (forwardingHint) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 57 | , m_maximumNoActivityPeriod (timeout) |
| 58 | |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 59 | , m_minSendSeqNo (minSeqNo-1) |
| 60 | , m_maxInOrderRecvSeqNo (minSeqNo-1) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 61 | , m_minSeqNo (minSeqNo) |
| 62 | , m_maxSeqNo (maxSeqNo) |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 63 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 64 | , m_pipeline (1) // initial "congestion window" |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 65 | , m_activePipeline (0) |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 66 | |
| 67 | , m_rto(1) // for temporary congestion control, should be removed when NDN provide transport functionality. |
| 68 | , m_maxRto(static_cast<double>(timeout.total_seconds())/2) |
| 69 | , m_slowStart(true) |
| 70 | , m_threshold(4) |
| 71 | , m_roundCount(0) |
| 72 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 73 | , m_retryPause (0) |
Alexander Afanasyev | f8b22bf | 2013-01-26 17:28:11 -0800 | [diff] [blame] | 74 | , m_nextScheduledRetry (date_time::second_clock<boost::posix_time::ptime>::universal_time ()) |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 75 | , m_executor (executor) // must be 1 |
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 | } |
| 78 | |
| 79 | Fetcher::~Fetcher () |
| 80 | { |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 81 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 82 | |
| 83 | void |
| 84 | Fetcher::RestartPipeline () |
| 85 | { |
| 86 | m_active = true; |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 87 | m_minSendSeqNo = m_maxInOrderRecvSeqNo; |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 88 | // cout << "Restart: " << m_minSendSeqNo << endl; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 89 | m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time(); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 90 | |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 91 | m_executor->execute (bind (&Fetcher::FillPipeline, this)); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 95 | Fetcher::SetForwardingHint (const Ndnx::Name &forwardingHint) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 96 | { |
| 97 | m_forwardingHint = forwardingHint; |
| 98 | } |
| 99 | |
| 100 | void |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 101 | Fetcher::FillPipeline () |
| 102 | { |
| 103 | for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++) |
| 104 | { |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 105 | unique_lock<mutex> lock (m_seqNoMutex); |
| 106 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 107 | if (m_outOfOrderRecvSeqNo.find (m_minSendSeqNo+1) != m_outOfOrderRecvSeqNo.end ()) |
| 108 | continue; |
| 109 | |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 110 | if (m_inActivePipeline.find (m_minSendSeqNo+1) != m_inActivePipeline.end ()) |
| 111 | continue; |
| 112 | |
| 113 | m_inActivePipeline.insert (m_minSendSeqNo+1); |
| 114 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 115 | _LOG_DEBUG (" >>> i " << Name (m_forwardingHint)(m_name) << ", seq = " << (m_minSendSeqNo + 1 )); |
Alexander Afanasyev | 678f350 | 2013-01-23 17:09:37 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 117 | // cout << ">>> " << m_minSendSeqNo+1 << endl; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 118 | m_ndnx->sendInterest (Name (m_forwardingHint)(m_name)(m_minSendSeqNo+1), |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 119 | Closure (bind(&Fetcher::OnData, this, m_minSendSeqNo+1, _1, _2), |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 120 | bind(&Fetcher::OnTimeout, this, m_minSendSeqNo+1, _1, _2, _3)), |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 121 | Selectors().interestLifetime (m_rto)); // Alex: this lifetime should be changed to RTO |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 122 | _LOG_DEBUG (" >>> i ok"); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 123 | |
| 124 | m_activePipeline ++; |
| 125 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 126 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 127 | |
| 128 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 129 | Fetcher::OnData (uint64_t seqno, const Ndnx::Name &name, PcoPtr data) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 130 | { |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 131 | m_executor->execute (bind (&Fetcher::OnData_Execute, this, seqno, name, data)); |
Alexander Afanasyev | ddc283c | 2013-01-28 23:11:20 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 135 | Fetcher::OnData_Execute (uint64_t seqno, Ndnx::Name name, Ndnx::PcoPtr data) |
Alexander Afanasyev | ddc283c | 2013-01-28 23:11:20 -0800 | [diff] [blame] | 136 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 137 | _LOG_DEBUG (" <<< d " << name.getPartialName (0, name.size () - 1) << ", seq = " << seqno); |
| 138 | |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 139 | if (m_forwardingHint == Name ()) |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 140 | { |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 141 | // TODO: check verified!!!! |
| 142 | if (true) |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 143 | { |
| 144 | if (!m_segmentCallback.empty ()) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 145 | { |
| 146 | m_segmentCallback (m_deviceName, m_name, seqno, data); |
| 147 | } |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 148 | } |
| 149 | else |
| 150 | { |
| 151 | _LOG_ERROR("Can not verify signature content. Name = " << data->name()); |
| 152 | // probably needs to do more in the future |
| 153 | } |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 154 | // we don't have to tell FetchManager about this |
| 155 | } |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 156 | else |
| 157 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 158 | // in this case we don't care whether "data" is verified, in fact, we expect it is unverified |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 159 | try { |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 160 | PcoPtr pco = make_shared<ParsedContentObject> (*data->contentPtr ()); |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 161 | |
| 162 | // we need to verify this pco and apply callback only when verified |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 163 | // TODO: check verified !!! |
| 164 | if (true) |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 165 | { |
| 166 | if (!m_segmentCallback.empty ()) |
| 167 | { |
| 168 | m_segmentCallback (m_deviceName, m_name, seqno, pco); |
| 169 | } |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | _LOG_ERROR("Can not verify signature content. Name = " << pco->name()); |
| 174 | // probably needs to do more in the future |
| 175 | } |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 176 | } |
| 177 | catch (MisformedContentObjectException &e) |
| 178 | { |
| 179 | cerr << "MisformedContentObjectException..." << endl; |
| 180 | // no idea what should do... |
| 181 | // let's ignore for now |
| 182 | } |
| 183 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 184 | |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 185 | m_activePipeline --; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 186 | m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time(); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 187 | |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 188 | { |
| 189 | unique_lock<mutex> lock (m_pipelineMutex); |
| 190 | if(m_slowStart){ |
| 191 | m_pipeline++; |
| 192 | if(m_pipeline == m_threshold) |
| 193 | m_slowStart = false; |
| 194 | } |
| 195 | else{ |
| 196 | m_roundCount++; |
| 197 | _LOG_DEBUG ("roundCount: " << m_roundCount); |
| 198 | if(m_roundCount == m_pipeline){ |
| 199 | m_pipeline++; |
| 200 | m_roundCount = 0; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | _LOG_DEBUG ("slowStart: " << boolalpha << m_slowStart << " pipeline: " << m_pipeline << " threshold: " << m_threshold); |
| 206 | |
| 207 | |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 208 | //////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 209 | unique_lock<mutex> lock (m_seqNoMutex); |
| 210 | |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 211 | m_outOfOrderRecvSeqNo.insert (seqno); |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 212 | m_inActivePipeline.erase (seqno); |
| 213 | _LOG_DEBUG ("Total segments received: " << m_outOfOrderRecvSeqNo.size ()); |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 214 | set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin (); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 215 | for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end (); |
| 216 | inOrderSeqNo++) |
| 217 | { |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 218 | _LOG_TRACE ("Checking " << *inOrderSeqNo << " and " << m_maxInOrderRecvSeqNo+1); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 219 | if (*inOrderSeqNo == m_maxInOrderRecvSeqNo+1) |
| 220 | { |
| 221 | m_maxInOrderRecvSeqNo = *inOrderSeqNo; |
| 222 | } |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 223 | else if (*inOrderSeqNo < m_maxInOrderRecvSeqNo+1) // not possible anymore, but just in case |
| 224 | { |
| 225 | continue; |
| 226 | } |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 227 | else |
| 228 | break; |
| 229 | } |
| 230 | m_outOfOrderRecvSeqNo.erase (m_outOfOrderRecvSeqNo.begin (), inOrderSeqNo); |
| 231 | //////////////////////////////////////////////////////////////////////////// |
| 232 | |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 233 | _LOG_TRACE ("Max in order received: " << m_maxInOrderRecvSeqNo << ", max seqNo to request: " << m_maxSeqNo); |
| 234 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 235 | if (m_maxInOrderRecvSeqNo == m_maxSeqNo) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 236 | { |
Alexander Afanasyev | bc8bf23 | 2013-01-29 11:19:17 -0800 | [diff] [blame] | 237 | _LOG_TRACE ("Fetch finished: " << m_name); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 238 | m_active = false; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 239 | // invoke callback |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 240 | if (!m_finishCallback.empty ()) |
| 241 | { |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 242 | _LOG_TRACE ("Notifying callback"); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 243 | m_finishCallback(m_deviceName, m_name); |
| 244 | } |
| 245 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 246 | // tell FetchManager that we have finish our job |
Zhenkai Zhu | 1f1def5 | 2013-01-29 00:01:03 -0800 | [diff] [blame] | 247 | // m_onFetchComplete (*this); |
| 248 | // using executor, so we won't be deleted if there is scheduled FillPipeline call |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 249 | if (!m_onFetchComplete.empty ()) |
| 250 | { |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 251 | m_timedwait = true; |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 252 | m_executor->execute (bind (m_onFetchComplete, ref(*this), m_deviceName, m_name)); |
| 253 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 254 | } |
| 255 | else |
| 256 | { |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 257 | m_executor->execute (bind (&Fetcher::FillPipeline, this)); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 261 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 262 | Fetcher::OnTimeout (uint64_t seqno, const Ndnx::Name &name, const Closure &closure, Selectors selectors) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 263 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 264 | _LOG_DEBUG (this << ", " << m_executor.get ()); |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 265 | m_executor->execute (bind (&Fetcher::OnTimeout_Execute, this, seqno, name, closure, selectors)); |
Alexander Afanasyev | ddc283c | 2013-01-28 23:11:20 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 269 | Fetcher::OnTimeout_Execute (uint64_t seqno, Ndnx::Name name, Ndnx::Closure closure, Ndnx::Selectors selectors) |
Alexander Afanasyev | ddc283c | 2013-01-28 23:11:20 -0800 | [diff] [blame] | 270 | { |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 271 | _LOG_DEBUG (" <<< :( timeout " << name.getPartialName (0, name.size () - 1) << ", seq = " << seqno); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 272 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 273 | // cout << "Fetcher::OnTimeout: " << name << endl; |
| 274 | // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod |
| 275 | // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time() |
| 276 | // << ", oldest: " << (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod) << endl; |
| 277 | |
| 278 | if (m_lastPositiveActivity < |
| 279 | (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod)) |
| 280 | { |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 281 | bool done = false; |
| 282 | { |
| 283 | unique_lock<mutex> lock (m_seqNoMutex); |
| 284 | m_inActivePipeline.erase (seqno); |
| 285 | m_activePipeline --; |
| 286 | |
| 287 | if (m_activePipeline == 0) |
| 288 | { |
| 289 | done = true; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (done) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 294 | { |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 295 | { |
| 296 | unique_lock<mutex> lock (m_seqNoMutex); |
| 297 | _LOG_DEBUG ("Telling that fetch failed"); |
| 298 | _LOG_DEBUG ("Active pipeline size should be zero: " << m_inActivePipeline.size ()); |
| 299 | } |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 300 | |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 301 | m_active = false; |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 302 | if (!m_onFetchFailed.empty ()) |
| 303 | { |
| 304 | m_onFetchFailed (ref (*this)); |
| 305 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 306 | // this is not valid anymore, but we still should be able finish work |
| 307 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 308 | } |
| 309 | else |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 310 | { |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 311 | _LOG_DEBUG ("Asking to reexpress seqno: " << seqno); |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 312 | { |
| 313 | unique_lock<mutex> lock (m_rtoMutex); |
| 314 | _LOG_DEBUG ("Interest Timeout"); |
| 315 | m_rto = m_rto * 2; |
| 316 | if(m_rto > m_maxRto) |
| 317 | { |
| 318 | m_rto = m_maxRto; |
| 319 | _LOG_DEBUG ("RTO is max: " << m_rto); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 320 | } |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 321 | else |
| 322 | { |
| 323 | _LOG_DEBUG ("RTO is doubled: " << m_rto); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 324 | } |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | { |
| 328 | unique_lock<mutex> lock (m_pipelineMutex); |
| 329 | m_threshold = m_pipeline / 2; |
| 330 | m_pipeline = 1; |
| 331 | m_roundCount = 0; |
| 332 | m_slowStart = true; |
| 333 | } |
| 334 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 335 | m_ndnx->sendInterest (name, closure, selectors.interestLifetime (m_rto)); |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 336 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 337 | } |