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 | 8811b35 | 2013-01-02 12:51:15 -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 | 8811b35 | 2013-01-02 12:51:15 -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 | 8811b35 | 2013-01-02 12:51:15 -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 | 8811b35 | 2013-01-02 12:51:15 -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 | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "fetch-manager.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 22 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 23 | #include <boost/make_shared.hpp> |
| 24 | #include <boost/ref.hpp> |
| 25 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 27 | #include "logging.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 28 | #include "simple-interval-generator.hpp" |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 30 | _LOG_INIT(FetchManager); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace boost; |
| 33 | using namespace std; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 34 | using namespace Ndnx; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 36 | //The disposer object function |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 37 | struct fetcher_disposer |
| 38 | { |
| 39 | void |
| 40 | operator()(Fetcher* delete_this) |
| 41 | { |
| 42 | delete delete_this; |
| 43 | } |
| 44 | }; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 46 | static const string SCHEDULE_FETCHES_TAG = "ScheduleFetches"; |
| 47 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 48 | FetchManager::FetchManager(Ccnx::CcnxWrapperPtr ccnx, |
| 49 | const Mapping& mapping, |
| 50 | const Name& broadcastForwardingHint, |
| 51 | uint32_t parallelFetches, // = 3 |
| 52 | const SegmentCallback& defaultSegmentCallback, |
| 53 | const FinishCallback& defaultFinishCallback, |
| 54 | const FetchTaskDbPtr& taskDb) |
| 55 | : m_ccnx(ccnx) |
| 56 | , m_mapping(mapping) |
| 57 | , m_maxParallelFetches(parallelFetches) |
| 58 | , m_currentParallelFetches(0) |
| 59 | , m_scheduler(new Scheduler) |
| 60 | , m_executor(new Executor(1)) |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 61 | , m_defaultSegmentCallback(defaultSegmentCallback) |
| 62 | , m_defaultFinishCallback(defaultFinishCallback) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 63 | , m_taskDb(taskDb) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 64 | , m_broadcastHint(broadcastForwardingHint) |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 65 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 66 | m_scheduler->start(); |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 67 | m_executor->start(); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 69 | m_scheduleFetchesTask = |
| 70 | Scheduler::schedulePeriodicTask(m_scheduler, |
| 71 | make_shared<SimpleIntervalGenerator>( |
| 72 | 300), // no need to check to often. if needed, will be rescheduled |
| 73 | bind(&FetchManager::ScheduleFetches, this), |
| 74 | SCHEDULE_FETCHES_TAG); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 75 | // resume un-finished fetches if there is any |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | if (m_taskDb) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 77 | m_taskDb->foreachTask(bind(&FetchManager::Enqueue, this, _1, _2, _3, _4, _5)); |
| 78 | } |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 79 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 81 | FetchManager::~FetchManager() |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 82 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 83 | m_scheduler->shutdown(); |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 84 | m_executor->shutdown(); |
| 85 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 86 | m_ccnx.reset(); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 88 | m_fetchList.clear_and_dispose(fetcher_disposer()); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 89 | } |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 90 | |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 91 | // Enqueue using default callbacks |
| 92 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | FetchManager::Enqueue(const Ccnx::Name& deviceName, const Ccnx::Name& baseName, uint64_t minSeqNo, |
| 94 | uint64_t maxSeqNo, int priority) |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 95 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 96 | Enqueue(deviceName, baseName, m_defaultSegmentCallback, m_defaultFinishCallback, minSeqNo, |
| 97 | maxSeqNo, priority); |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 100 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 101 | FetchManager::Enqueue(const Ccnx::Name& deviceName, const Ccnx::Name& baseName, |
| 102 | const SegmentCallback& segmentCallback, const FinishCallback& finishCallback, |
| 103 | uint64_t minSeqNo, uint64_t maxSeqNo, int priority /*PRIORITY_NORMAL*/) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 104 | { |
Zhenkai Zhu | 454bae2 | 2013-01-24 19:27:54 -0800 | [diff] [blame] | 105 | // Assumption for the following code is minSeqNo <= maxSeqNo |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 106 | if (minSeqNo > maxSeqNo) { |
Zhenkai Zhu | 454bae2 | 2013-01-24 19:27:54 -0800 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 110 | // we may need to guarantee that LookupLocator will gives an answer and not throw exception... |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 111 | Name forwardingHint; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 112 | forwardingHint = m_mapping(deviceName); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 114 | if (m_taskDb) { |
| 115 | m_taskDb->addTask(deviceName, baseName, minSeqNo, maxSeqNo, priority); |
| 116 | } |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | unique_lock<mutex> lock(m_parellelFetchMutex); |
Alexander Afanasyev | bc8bf23 | 2013-01-29 11:19:17 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 120 | _LOG_TRACE("++++ Create fetcher: " << baseName); |
| 121 | Fetcher* fetcher = |
| 122 | new Fetcher(m_ccnx, m_executor, segmentCallback, finishCallback, |
| 123 | bind(&FetchManager::DidFetchComplete, this, _1, _2, _3), |
| 124 | bind(&FetchManager::DidNoDataTimeout, this, _1), deviceName, baseName, minSeqNo, |
| 125 | maxSeqNo, boost::posix_time::seconds(30), forwardingHint); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 127 | switch (priority) { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 128 | case PRIORITY_HIGH: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 129 | _LOG_TRACE("++++ Push front fetcher: " << fetcher->GetName()); |
| 130 | m_fetchList.push_front(*fetcher); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 131 | break; |
| 132 | |
| 133 | case PRIORITY_NORMAL: |
| 134 | default: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 135 | _LOG_TRACE("++++ Push back fetcher: " << fetcher->GetName()); |
| 136 | m_fetchList.push_back(*fetcher); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 137 | break; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 138 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 140 | _LOG_DEBUG("++++ Reschedule fetcher task"); |
| 141 | m_scheduler->rescheduleTaskAt(m_scheduleFetchesTask, 0); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 142 | // ScheduleFetches (); // will start a fetch if m_currentParallelFetches is less than max, otherwise does nothing |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 143 | } |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 145 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 146 | FetchManager::ScheduleFetches() |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 147 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 148 | unique_lock<mutex> lock(m_parellelFetchMutex); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 150 | boost::posix_time::ptime currentTime = |
| 151 | date_time::second_clock<boost::posix_time::ptime>::universal_time(); |
| 152 | boost::posix_time::ptime nextSheduleCheck = |
| 153 | currentTime + posix_time::seconds(300); // no reason to have anything, but just in case |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 154 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 155 | for (FetchList::iterator item = m_fetchList.begin(); |
| 156 | m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end(); |
| 157 | item++) { |
| 158 | if (item->IsActive()) { |
| 159 | _LOG_DEBUG("Item is active"); |
| 160 | continue; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 161 | } |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 163 | if (item->IsTimedWait()) { |
| 164 | _LOG_DEBUG("Item is in timed-wait"); |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | if (currentTime < item->GetNextScheduledRetry()) { |
| 169 | if (item->GetNextScheduledRetry() < nextSheduleCheck) |
| 170 | nextSheduleCheck = item->GetNextScheduledRetry(); |
| 171 | |
| 172 | _LOG_DEBUG("Item is delayed"); |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | _LOG_DEBUG("Start fetching of " << item->GetName()); |
| 177 | |
| 178 | m_currentParallelFetches++; |
| 179 | _LOG_TRACE("++++ RESTART PIPELINE: " << item->GetName()); |
| 180 | item->RestartPipeline(); |
| 181 | } |
| 182 | |
| 183 | m_scheduler->rescheduleTaskAt(m_scheduleFetchesTask, |
| 184 | (nextSheduleCheck - currentTime).total_seconds()); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 187 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 188 | FetchManager::DidNoDataTimeout(Fetcher& fetcher) |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 189 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 190 | _LOG_DEBUG("No data timeout for " << fetcher.GetName() << " with forwarding hint: " |
| 191 | << fetcher.GetForwardingHint()); |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 192 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 193 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 194 | unique_lock<mutex> lock(m_parellelFetchMutex); |
| 195 | m_currentParallelFetches--; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 196 | // no need to do anything with the m_fetchList |
| 197 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 198 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 199 | if (fetcher.GetForwardingHint().size() == 0) { |
| 200 | // will be tried initially and again after empty forwarding hint |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 201 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 202 | /// @todo Handle potential exception |
| 203 | Name forwardingHint; |
| 204 | forwardingHint = m_mapping(fetcher.GetDeviceName()); |
Alexander Afanasyev | 84894d3 | 2013-02-08 22:27:12 -0800 | [diff] [blame] | 205 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 206 | if (forwardingHint.size() == 0) { |
| 207 | // make sure that we will try broadcast forwarding hint eventually |
| 208 | fetcher.SetForwardingHint(m_broadcastHint); |
Alexander Afanasyev | 84894d3 | 2013-02-08 22:27:12 -0800 | [diff] [blame] | 209 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 210 | else { |
| 211 | fetcher.SetForwardingHint(forwardingHint); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 212 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 213 | } |
| 214 | else if (fetcher.GetForwardingHint() == m_broadcastHint) { |
| 215 | // will be tried after broadcast forwarding hint |
| 216 | fetcher.SetForwardingHint(Name("/")); |
| 217 | } |
| 218 | else { |
| 219 | // will be tried after normal forwarding hint |
| 220 | fetcher.SetForwardingHint(m_broadcastHint); |
| 221 | } |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 223 | double delay = fetcher.GetRetryPause(); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 224 | if (delay < 1) // first time |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 225 | { |
| 226 | delay = 1; |
| 227 | } |
| 228 | else { |
| 229 | delay = std::min(2 * delay, 300.0); // 5 minutes max |
| 230 | } |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 231 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 232 | fetcher.SetRetryPause(delay); |
| 233 | fetcher.SetNextScheduledRetry(date_time::second_clock<boost::posix_time::ptime>::universal_time() + |
| 234 | posix_time::seconds(delay)); |
Alexander Afanasyev | f975623 | 2013-01-28 16:42:20 -0800 | [diff] [blame] | 235 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 236 | m_scheduler->rescheduleTaskAt(m_scheduleFetchesTask, 0); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 240 | FetchManager::DidFetchComplete(Fetcher& fetcher, const Name& deviceName, const Name& baseName) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 241 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 242 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 243 | unique_lock<mutex> lock(m_parellelFetchMutex); |
| 244 | m_currentParallelFetches--; |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 245 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 246 | if (m_taskDb) { |
| 247 | m_taskDb->deleteTask(deviceName, baseName); |
| 248 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 249 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 250 | |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 251 | // like TCP timed-wait |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 252 | m_scheduler->scheduleOneTimeTask(m_scheduler, 10, |
| 253 | boost::bind(&FetchManager::TimedWait, this, ref(fetcher)), |
| 254 | boost::lexical_cast<string>(baseName)); |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 255 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 256 | m_scheduler->rescheduleTaskAt(m_scheduleFetchesTask, 0); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 257 | } |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 258 | |
| 259 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 260 | FetchManager::TimedWait(Fetcher& fetcher) |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 261 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 262 | unique_lock<mutex> lock(m_parellelFetchMutex); |
| 263 | _LOG_TRACE("+++++ removing fetcher: " << fetcher.GetName()); |
| 264 | m_fetchList.erase_and_dispose(FetchList::s_iterator_to(fetcher), fetcher_disposer()); |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 265 | } |