Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 22 | #include "fetch-manager.h" |
| 23 | #include <boost/make_shared.hpp> |
| 24 | #include <boost/ref.hpp> |
| 25 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 26 | #include "logging.h" |
| 27 | |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame^] | 28 | INIT_LOGGER ("FetchManager"); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace boost; |
| 31 | using namespace std; |
| 32 | using namespace Ccnx; |
| 33 | |
Zhenkai Zhu | 3f64d2d | 2013-01-25 14:34:59 -0800 | [diff] [blame] | 34 | static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare"; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 35 | //The disposer object function |
| 36 | struct fetcher_disposer { void operator() (Fetcher *delete_this) { delete delete_this; } }; |
| 37 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 38 | FetchManager::FetchManager (CcnxWrapperPtr ccnx, const Mapping &mapping, uint32_t parallelFetches/* = 3*/) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 39 | : m_ccnx (ccnx) |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 40 | , m_mapping (mapping) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 41 | , m_maxParallelFetches (parallelFetches) |
| 42 | , m_currentParallelFetches (0) |
| 43 | |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 44 | { |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 45 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 47 | FetchManager::~FetchManager () |
| 48 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 49 | m_fetchList.clear_and_dispose (fetcher_disposer ()); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 50 | } |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 52 | void |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 53 | FetchManager::Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName, |
| 54 | const SegmentCallback &segmentCallback, const FinishCallback &finishCallback, |
| 55 | uint64_t minSeqNo, uint64_t maxSeqNo, int priority/*PRIORITY_NORMAL*/) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 56 | { |
Zhenkai Zhu | 454bae2 | 2013-01-24 19:27:54 -0800 | [diff] [blame] | 57 | // Assumption for the following code is minSeqNo <= maxSeqNo |
| 58 | if (minSeqNo > maxSeqNo) |
| 59 | { |
| 60 | return; |
| 61 | } |
| 62 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 63 | // 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] | 64 | Name forwardingHint; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 65 | forwardingHint = m_mapping (deviceName); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 66 | |
| 67 | Fetcher &fetcher = *(new Fetcher (m_ccnx, |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 68 | segmentCallback, |
| 69 | finishCallback, |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 70 | bind (&FetchManager::DidFetchComplete, this, _1), |
| 71 | bind (&FetchManager::DidNoDataTimeout, this, _1), |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 72 | deviceName, baseName, minSeqNo, maxSeqNo, |
| 73 | boost::posix_time::seconds (30), |
| 74 | forwardingHint)); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 75 | |
| 76 | switch (priority) |
| 77 | { |
| 78 | case PRIORITY_HIGH: |
| 79 | m_fetchList.push_front (fetcher); |
| 80 | break; |
| 81 | |
| 82 | case PRIORITY_NORMAL: |
| 83 | default: |
| 84 | m_fetchList.push_back (fetcher); |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | 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] | 89 | } |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 91 | void |
| 92 | FetchManager::ScheduleFetches () |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 93 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 94 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 95 | |
| 96 | for (FetchList::iterator item = m_fetchList.begin (); |
| 97 | m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end (); |
| 98 | item++) |
| 99 | { |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 100 | if (item->IsActive ()) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 101 | continue; |
| 102 | |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame^] | 103 | _LOG_DEBUG ("Start fetching of " << item->GetName ()); |
| 104 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 105 | m_currentParallelFetches ++; |
| 106 | item->RestartPipeline (); |
| 107 | } |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 110 | void |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 111 | FetchManager::DidNoDataTimeout (Fetcher &fetcher) |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 112 | { |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame^] | 113 | _LOG_DEBUG ("No data timeout for " << fetcher.GetName () << " with forwarding hint: " << fetcher.GetForwardingHint ()); |
| 114 | |
Zhenkai Zhu | 3f64d2d | 2013-01-25 14:34:59 -0800 | [diff] [blame] | 115 | fetcher.SetForwardingHint (Ccnx::Name (BROADCAST_DOMAIN)); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 116 | { |
| 117 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 118 | m_currentParallelFetches --; |
| 119 | // no need to do anything with the m_fetchList |
| 120 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 122 | ScheduleFetches (); |
| 123 | } |
| 124 | |
| 125 | void |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 126 | FetchManager::DidFetchComplete (Fetcher &fetcher) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 127 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 128 | { |
| 129 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 130 | m_currentParallelFetches --; |
| 131 | m_fetchList.erase_and_dispose (FetchList::s_iterator_to (fetcher), fetcher_disposer ()); |
| 132 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 133 | |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 134 | } |