blob: bbdd6a8f1ec05058bcf725eb12908697468019a9 [file] [log] [blame]
Alexander Afanasyev8811b352013-01-02 12:51:15 -08001/* -*- 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 Afanasyev49a18522013-01-18 17:49:04 -080022#include "fetch-manager.h"
23#include <boost/make_shared.hpp>
24#include <boost/ref.hpp>
25#include <boost/throw_exception.hpp>
Alexander Afanasyevfc720362013-01-24 21:49:48 -080026#include "logging.h"
27
28INIT_LOGGER ("Fetch.Manager");
Alexander Afanasyev49a18522013-01-18 17:49:04 -080029
30using namespace boost;
31using namespace std;
32using namespace Ccnx;
33
Alexander Afanasyev83531a42013-01-19 16:21:54 -080034//The disposer object function
35struct fetcher_disposer { void operator() (Fetcher *delete_this) { delete delete_this; } };
36
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080037FetchManager::FetchManager (CcnxWrapperPtr ccnx, const Mapping &mapping, uint32_t parallelFetches/* = 3*/)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080038 : m_ccnx (ccnx)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080039 , m_mapping (mapping)
Alexander Afanasyev83531a42013-01-19 16:21:54 -080040 , m_maxParallelFetches (parallelFetches)
41 , m_currentParallelFetches (0)
42
Alexander Afanasyev8811b352013-01-02 12:51:15 -080043{
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080044 m_scheduler = make_shared<Scheduler> ();
45 m_scheduler->start ();
Alexander Afanasyev49a18522013-01-18 17:49:04 -080046}
Alexander Afanasyeva199f972013-01-02 19:37:26 -080047
Alexander Afanasyev49a18522013-01-18 17:49:04 -080048FetchManager::~FetchManager ()
49{
Alexander Afanasyev83531a42013-01-19 16:21:54 -080050 m_fetchList.clear_and_dispose (fetcher_disposer ());
51
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080052 m_scheduler->shutdown ();
53 m_scheduler.reset ();
Alexander Afanasyev49a18522013-01-18 17:49:04 -080054}
Alexander Afanasyev8811b352013-01-02 12:51:15 -080055
Alexander Afanasyev49a18522013-01-18 17:49:04 -080056void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080057FetchManager::Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName,
58 const SegmentCallback &segmentCallback, const FinishCallback &finishCallback,
59 uint64_t minSeqNo, uint64_t maxSeqNo, int priority/*PRIORITY_NORMAL*/)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080060{
Zhenkai Zhu454bae22013-01-24 19:27:54 -080061 // Assumption for the following code is minSeqNo <= maxSeqNo
62 if (minSeqNo > maxSeqNo)
63 {
64 return;
65 }
66
Alexander Afanasyev83531a42013-01-19 16:21:54 -080067 // we may need to guarantee that LookupLocator will gives an answer and not throw exception...
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080068 Name forwardingHint;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080069 forwardingHint = m_mapping (deviceName);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080070
71 Fetcher &fetcher = *(new Fetcher (m_ccnx,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080072 segmentCallback,
73 finishCallback,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080074 bind (&FetchManager::DidFetchComplete, this, _1),
75 bind (&FetchManager::DidNoDataTimeout, this, _1),
Alexander Afanasyev9247c522013-01-23 17:17:36 -080076 deviceName, baseName, minSeqNo, maxSeqNo));
77 fetcher.SetForwardingHint (forwardingHint);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080078
79 switch (priority)
80 {
81 case PRIORITY_HIGH:
82 m_fetchList.push_front (fetcher);
83 break;
84
85 case PRIORITY_NORMAL:
86 default:
87 m_fetchList.push_back (fetcher);
88 break;
89 }
90
91 ScheduleFetches (); // will start a fetch if m_currentParallelFetches is less than max, otherwise does nothing
Alexander Afanasyev49a18522013-01-18 17:49:04 -080092}
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080093
Alexander Afanasyev83531a42013-01-19 16:21:54 -080094void
95FetchManager::ScheduleFetches ()
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080096{
Alexander Afanasyev83531a42013-01-19 16:21:54 -080097 unique_lock<mutex> lock (m_parellelFetchMutex);
98
99 for (FetchList::iterator item = m_fetchList.begin ();
100 m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end ();
101 item++)
102 {
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800103 if (item->IsActive ())
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800104 continue;
105
106 m_currentParallelFetches ++;
107 item->RestartPipeline ();
108 }
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800109}
110
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800111void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800112FetchManager::DidNoDataTimeout (Fetcher &fetcher)
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800113{
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800114 fetcher.SetForwardingHint (Ccnx::Name ("/ndn/broadcast"));
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800115 {
116 unique_lock<mutex> lock (m_parellelFetchMutex);
117 m_currentParallelFetches --;
118 // no need to do anything with the m_fetchList
119 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800120
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800121 ScheduleFetches ();
122}
123
124void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800125FetchManager::DidFetchComplete (Fetcher &fetcher)
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800126{
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800127 {
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 Afanasyevd6c2a902013-01-19 21:24:30 -0800132
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800133}