blob: 3842e2e333da6e4d36e76913718a49731c9ab74c [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
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080034static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare";
Alexander Afanasyev83531a42013-01-19 16:21:54 -080035//The disposer object function
36struct fetcher_disposer { void operator() (Fetcher *delete_this) { delete delete_this; } };
37
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080038FetchManager::FetchManager (CcnxWrapperPtr ccnx, const Mapping &mapping, uint32_t parallelFetches/* = 3*/)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080039 : m_ccnx (ccnx)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080040 , m_mapping (mapping)
Alexander Afanasyev83531a42013-01-19 16:21:54 -080041 , m_maxParallelFetches (parallelFetches)
42 , m_currentParallelFetches (0)
43
Alexander Afanasyev8811b352013-01-02 12:51:15 -080044{
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080045 m_scheduler = make_shared<Scheduler> ();
46 m_scheduler->start ();
Alexander Afanasyev49a18522013-01-18 17:49:04 -080047}
Alexander Afanasyeva199f972013-01-02 19:37:26 -080048
Alexander Afanasyev49a18522013-01-18 17:49:04 -080049FetchManager::~FetchManager ()
50{
Alexander Afanasyev83531a42013-01-19 16:21:54 -080051 m_fetchList.clear_and_dispose (fetcher_disposer ());
52
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080053 m_scheduler->shutdown ();
54 m_scheduler.reset ();
Alexander Afanasyev49a18522013-01-18 17:49:04 -080055}
Alexander Afanasyev8811b352013-01-02 12:51:15 -080056
Alexander Afanasyev49a18522013-01-18 17:49:04 -080057void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080058FetchManager::Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName,
59 const SegmentCallback &segmentCallback, const FinishCallback &finishCallback,
60 uint64_t minSeqNo, uint64_t maxSeqNo, int priority/*PRIORITY_NORMAL*/)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080061{
Zhenkai Zhu454bae22013-01-24 19:27:54 -080062 // Assumption for the following code is minSeqNo <= maxSeqNo
63 if (minSeqNo > maxSeqNo)
64 {
65 return;
66 }
67
Alexander Afanasyev83531a42013-01-19 16:21:54 -080068 // we may need to guarantee that LookupLocator will gives an answer and not throw exception...
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080069 Name forwardingHint;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080070 forwardingHint = m_mapping (deviceName);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080071
72 Fetcher &fetcher = *(new Fetcher (m_ccnx,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080073 segmentCallback,
74 finishCallback,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080075 bind (&FetchManager::DidFetchComplete, this, _1),
76 bind (&FetchManager::DidNoDataTimeout, this, _1),
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080077 deviceName, baseName, minSeqNo, maxSeqNo,
78 boost::posix_time::seconds (30),
79 forwardingHint));
Alexander Afanasyev83531a42013-01-19 16:21:54 -080080
81 switch (priority)
82 {
83 case PRIORITY_HIGH:
84 m_fetchList.push_front (fetcher);
85 break;
86
87 case PRIORITY_NORMAL:
88 default:
89 m_fetchList.push_back (fetcher);
90 break;
91 }
92
93 ScheduleFetches (); // will start a fetch if m_currentParallelFetches is less than max, otherwise does nothing
Alexander Afanasyev49a18522013-01-18 17:49:04 -080094}
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080095
Alexander Afanasyev83531a42013-01-19 16:21:54 -080096void
97FetchManager::ScheduleFetches ()
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080098{
Alexander Afanasyev83531a42013-01-19 16:21:54 -080099 unique_lock<mutex> lock (m_parellelFetchMutex);
100
101 for (FetchList::iterator item = m_fetchList.begin ();
102 m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end ();
103 item++)
104 {
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800105 if (item->IsActive ())
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800106 continue;
107
108 m_currentParallelFetches ++;
109 item->RestartPipeline ();
110 }
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800111}
112
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800113void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800114FetchManager::DidNoDataTimeout (Fetcher &fetcher)
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800115{
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800116 fetcher.SetForwardingHint (Ccnx::Name (BROADCAST_DOMAIN));
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800117 {
118 unique_lock<mutex> lock (m_parellelFetchMutex);
119 m_currentParallelFetches --;
120 // no need to do anything with the m_fetchList
121 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800122
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800123 ScheduleFetches ();
124}
125
126void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800127FetchManager::DidFetchComplete (Fetcher &fetcher)
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800128{
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800129 {
130 unique_lock<mutex> lock (m_parellelFetchMutex);
131 m_currentParallelFetches --;
132 m_fetchList.erase_and_dispose (FetchList::s_iterator_to (fetcher), fetcher_disposer ());
133 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800134
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -0800135}