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> |
| 26 | |
| 27 | using namespace boost; |
| 28 | using namespace std; |
| 29 | using namespace Ccnx; |
| 30 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 31 | //The disposer object function |
| 32 | struct fetcher_disposer { void operator() (Fetcher *delete_this) { delete delete_this; } }; |
| 33 | |
| 34 | FetchManager::FetchManager (CcnxWrapperPtr ccnx, SyncLogPtr sync, uint32_t parallelFetches/* = 3*/) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 35 | : m_ccnx (ccnx) |
| 36 | , m_sync (sync) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 37 | , m_maxParallelFetches (parallelFetches) |
| 38 | , m_currentParallelFetches (0) |
| 39 | |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 40 | { |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 41 | m_scheduler = make_shared<Scheduler> (); |
| 42 | m_scheduler->start (); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 43 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 45 | FetchManager::~FetchManager () |
| 46 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 47 | m_fetchList.clear_and_dispose (fetcher_disposer ()); |
| 48 | |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 49 | m_scheduler->shutdown (); |
| 50 | m_scheduler.reset (); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 51 | } |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 53 | void |
| 54 | FetchManager::Enqueue (const Ccnx::Name &deviceName, uint32_t minSeqNo, uint32_t maxSeqNo, int priority/*=PRIORITY_NORMAL*/) |
| 55 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 56 | // 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] | 57 | Name forwardingHint; |
| 58 | try { |
| 59 | forwardingHint = m_sync->LookupLocator (deviceName); |
| 60 | } |
| 61 | catch (Error::Db &exception) { |
| 62 | // just ignore for now |
| 63 | } |
| 64 | |
| 65 | Fetcher &fetcher = *(new Fetcher (m_ccnx, |
| 66 | bind (&FetchManager::DidDataSegmentFetched, this, _1, _2, _3, _4, _5), |
| 67 | bind (&FetchManager::DidFetchComplete, this, _1), |
| 68 | bind (&FetchManager::DidNoDataTimeout, this, _1), |
| 69 | deviceName, minSeqNo, maxSeqNo |
| 70 | /* Alex: should or should not include hint initially?*/)); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 71 | |
| 72 | switch (priority) |
| 73 | { |
| 74 | case PRIORITY_HIGH: |
| 75 | m_fetchList.push_front (fetcher); |
| 76 | break; |
| 77 | |
| 78 | case PRIORITY_NORMAL: |
| 79 | default: |
| 80 | m_fetchList.push_back (fetcher); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | 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] | 85 | } |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 87 | void |
| 88 | FetchManager::ScheduleFetches () |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 89 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 90 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 91 | |
| 92 | for (FetchList::iterator item = m_fetchList.begin (); |
| 93 | m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end (); |
| 94 | item++) |
| 95 | { |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 96 | if (item->IsActive ()) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 97 | continue; |
| 98 | |
| 99 | m_currentParallelFetches ++; |
| 100 | item->RestartPipeline (); |
| 101 | } |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 104 | void |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 105 | FetchManager::DidDataSegmentFetched (Fetcher &fetcher, uint32_t seqno, const Ccnx::Name &basename, |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 106 | const Ccnx::Name &name, Ccnx::PcoPtr data) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 107 | { |
| 108 | // do something |
| 109 | } |
| 110 | |
| 111 | void |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 112 | FetchManager::DidNoDataTimeout (Fetcher &fetcher) |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 113 | { |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 114 | fetcher.SetForwardingHint (Ccnx::Name ("/ndn/broadcast")); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 115 | { |
| 116 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 117 | m_currentParallelFetches --; |
| 118 | // no need to do anything with the m_fetchList |
| 119 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 121 | ScheduleFetches (); |
| 122 | } |
| 123 | |
| 124 | void |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 125 | FetchManager::DidFetchComplete (Fetcher &fetcher) |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 126 | { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 127 | { |
| 128 | unique_lock<mutex> lock (m_parellelFetchMutex); |
| 129 | m_currentParallelFetches --; |
| 130 | m_fetchList.erase_and_dispose (FetchList::s_iterator_to (fetcher), fetcher_disposer ()); |
| 131 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 133 | // ? do something else |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 134 | } |