blob: c26b78dff7aedc83fabd9ba46932d837ed2857ad [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"
Alexander Afanasyev49a18522013-01-18 17:49:04 -080032#include "ccnx-wrapper.h"
Alexander Afanasyev49a18522013-01-18 17:49:04 -080033
Alexander Afanasyev83531a42013-01-19 16:21:54 -080034#include "fetcher.h"
35
Alexander Afanasyev49a18522013-01-18 17:49:04 -080036class FetchManager
37{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080038public:
Alexander Afanasyev49a18522013-01-18 17:49:04 -080039 enum
40 {
41 PRIORITY_NORMAL,
42 PRIORITY_HIGH
43 };
44
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080045 typedef boost::function<Ccnx::Name(const Ccnx::Name &)> Mapping;
46 typedef boost::function<void(Ccnx::Name &deviceName, Ccnx::Name &baseName, uint64_t seq, Ccnx::PcoPtr pco)> SegmentCallback;
47 typedef boost::function<void(Ccnx::Name &deviceName, Ccnx::Name &baseName)> FinishCallback;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080048 FetchManager (Ccnx::CcnxWrapperPtr ccnx, const Mapping &mapping, uint32_t parallelFetches = 3);
Alexander Afanasyev49a18522013-01-18 17:49:04 -080049 virtual ~FetchManager ();
50
51 void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080052 Enqueue (const Ccnx::Name &deviceName, const Ccnx::Name &baseName,
53 const SegmentCallback &segmentCallback, const FinishCallback &finishCallback,
54 uint64_t minSeqNo, uint64_t maxSeqNo, int priority=PRIORITY_NORMAL);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080055
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080056 // only for Fetcher
Alexander Afanasyev83531a42013-01-19 16:21:54 -080057 inline Ccnx::CcnxWrapperPtr
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080058 GetCcnx ();
59
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080060private:
61
Alexander Afanasyev83531a42013-01-19 16:21:54 -080062 inline SchedulerPtr
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080063 GetScheduler ();
Alexander Afanasyev83531a42013-01-19 16:21:54 -080064
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080065 // Fetch Events
66 void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080067 DidDataSegmentFetched (Fetcher &fetcher, uint64_t seqno, const Ccnx::Name &basename,
Alexander Afanasyevf278db32013-01-21 14:41:01 -080068 const Ccnx::Name &name, Ccnx::PcoPtr data);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080069
Alexander Afanasyev83531a42013-01-19 16:21:54 -080070 void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080071 DidNoDataTimeout (Fetcher &fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080072
73 void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080074 DidFetchComplete (Fetcher &fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080075
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080076 void
77 ScheduleFetches ();
78
79private:
Alexander Afanasyev49a18522013-01-18 17:49:04 -080080 Ccnx::CcnxWrapperPtr m_ccnx;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080081 Mapping m_mapping;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080082
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080083 uint32_t m_maxParallelFetches;
84 uint32_t m_currentParallelFetches;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080085 boost::mutex m_parellelFetchMutex;
86
87 // optimized list structure for fetch queue
88 typedef boost::intrusive::member_hook< Fetcher,
89 boost::intrusive::list_member_hook<>, &Fetcher::m_managerListHook> MemberOption;
90 typedef boost::intrusive::list<Fetcher, MemberOption> FetchList;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080091
Alexander Afanasyev83531a42013-01-19 16:21:54 -080092 FetchList m_fetchList;
Alexander Afanasyev49a18522013-01-18 17:49:04 -080093};
94
Alexander Afanasyev83531a42013-01-19 16:21:54 -080095Ccnx::CcnxWrapperPtr
96FetchManager::GetCcnx ()
97{
98 return m_ccnx;
99}
100
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800101typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800102namespace Error {
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800103struct FetchManager : virtual boost::exception, virtual std::exception { };
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800104}
105
106typedef boost::shared_ptr<FetchManager> FetchManagerPtr;
107
108
109#endif // FETCHER_H