blob: 31cb063118d45b9fcb6251573376b6e79d9fbe67 [file] [log] [blame]
Alexander Afanasyev49a18522013-01-18 17:49:04 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2013 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
22#ifndef FETCH_MANAGER_H
23#define FETCH_MANAGER_H
24
25#include <boost/exception/all.hpp>
26#include <boost/shared_ptr.hpp>
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080027#include <boost/function.hpp>
Alexander Afanasyev49a18522013-01-18 17:49:04 -080028#include <string>
Alexander Afanasyev83531a42013-01-19 16:21:54 -080029#include <list>
Alexander Afanasyev49a18522013-01-18 17:49:04 -080030#include <stdint.h>
31#include "scheduler.h"
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080032#include "executor.h"
Alexander Afanasyev49a18522013-01-18 17:49:04 -080033#include "ccnx-wrapper.h"
Zhenkai Zhuda686882013-01-29 22:32:24 -080034#include "fetch-task-db.h"
Alexander Afanasyev49a18522013-01-18 17:49:04 -080035
Alexander Afanasyev83531a42013-01-19 16:21:54 -080036#include "fetcher.h"
37
Alexander Afanasyev49a18522013-01-18 17:49:04 -080038class FetchManager
39{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080040public:
Alexander Afanasyev49a18522013-01-18 17:49:04 -080041 enum
42 {
43 PRIORITY_NORMAL,
44 PRIORITY_HIGH
45 };
46
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080047 typedef boost::function<Ccnx::Name(const Ccnx::Name &)> Mapping;
48 typedef boost::function<void(Ccnx::Name &deviceName, Ccnx::Name &baseName, uint64_t seq, Ccnx::PcoPtr pco)> SegmentCallback;
49 typedef boost::function<void(Ccnx::Name &deviceName, Ccnx::Name &baseName)> FinishCallback;
Zhenkai Zhua0147382013-01-29 15:57:27 -080050 FetchManager (Ccnx::CcnxWrapperPtr ccnx
51 , const Mapping &mapping
52 , uint32_t parallelFetches = 3
53 , const SegmentCallback &defaultSegmentCallback = SegmentCallback()
54 , const FinishCallback &defaultFinishCallback = FinishCallback()
Zhenkai Zhuda686882013-01-29 22:32:24 -080055 , const FetchTaskDbPtr &taskDb = FetchTaskDbPtr()
Zhenkai Zhua0147382013-01-29 15:57:27 -080056 );
Alexander Afanasyev49a18522013-01-18 17:49:04 -080057 virtual ~FetchManager ();
58
59 void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080060 Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName,
61 const SegmentCallback &segmentCallback, const FinishCallback &finishCallback,
62 uint64_t minSeqNo, uint64_t maxSeqNo, int priority=PRIORITY_NORMAL);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080063
Zhenkai Zhua0147382013-01-29 15:57:27 -080064 // Enqueue using default callbacks
65 void
66 Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName,
67 uint64_t minSeqNo, uint64_t maxSeqNo, int priority=PRIORITY_NORMAL);
68
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080069 // only for Fetcher
Alexander Afanasyev83531a42013-01-19 16:21:54 -080070 inline Ccnx::CcnxWrapperPtr
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080071 GetCcnx ();
72
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080073private:
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080074 // Fetch Events
75 void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080076 DidDataSegmentFetched (Fetcher &fetcher, uint64_t seqno, const Ccnx::Name &basename,
Alexander Afanasyevf278db32013-01-21 14:41:01 -080077 const Ccnx::Name &name, Ccnx::PcoPtr data);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080078
Alexander Afanasyev83531a42013-01-19 16:21:54 -080079 void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080080 DidNoDataTimeout (Fetcher &fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080081
82 void
Zhenkai Zhuda686882013-01-29 22:32:24 -080083 DidFetchComplete (Fetcher &fetcher, const Ccnx::Name &deviceName, const Ccnx::Name &baseName);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080084
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080085 void
86 ScheduleFetches ();
87
88private:
Alexander Afanasyev49a18522013-01-18 17:49:04 -080089 Ccnx::CcnxWrapperPtr m_ccnx;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080090 Mapping m_mapping;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080091
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080092 uint32_t m_maxParallelFetches;
93 uint32_t m_currentParallelFetches;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080094 boost::mutex m_parellelFetchMutex;
95
96 // optimized list structure for fetch queue
97 typedef boost::intrusive::member_hook< Fetcher,
98 boost::intrusive::list_member_hook<>, &Fetcher::m_managerListHook> MemberOption;
99 typedef boost::intrusive::list<Fetcher, MemberOption> FetchList;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800100
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800101 FetchList m_fetchList;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800102 SchedulerPtr m_scheduler;
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800103 ExecutorPtr m_executor;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800104 TaskPtr m_scheduleFetchesTask;
Zhenkai Zhua0147382013-01-29 15:57:27 -0800105 SegmentCallback m_defaultSegmentCallback;
106 FinishCallback m_defaultFinishCallback;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800107 FetchTaskDbPtr m_taskDb;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800108};
109
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800110Ccnx::CcnxWrapperPtr
111FetchManager::GetCcnx ()
112{
113 return m_ccnx;
114}
115
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800116typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800117namespace Error {
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800118struct FetchManager : virtual boost::exception, virtual std::exception { };
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800119}
120
121typedef boost::shared_ptr<FetchManager> FetchManagerPtr;
122
123
124#endif // FETCHER_H