blob: 8372e9d6337ddaf2a900a4798bbedad1b5daa5c5 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyev49a18522013-01-18 17:49:04 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev49a18522013-01-18 17:49:04 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev49a18522013-01-18 17:49:04 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev49a18522013-01-18 17:49:04 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyev49a18522013-01-18 17:49:04 -080019 */
20
21#ifndef FETCH_MANAGER_H
22#define FETCH_MANAGER_H
23
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include "ccnx-wrapper.h"
25#include "executor.h"
26#include "fetch-task-db.h"
27#include "scheduler.h"
Alexander Afanasyev49a18522013-01-18 17:49:04 -080028#include <boost/exception/all.hpp>
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080029#include <boost/function.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080030#include <boost/shared_ptr.hpp>
Alexander Afanasyev83531a42013-01-19 16:21:54 -080031#include <list>
Alexander Afanasyev49a18522013-01-18 17:49:04 -080032#include <stdint.h>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080033#include <string>
Alexander Afanasyev49a18522013-01-18 17:49:04 -080034
Alexander Afanasyev83531a42013-01-19 16:21:54 -080035#include "fetcher.h"
36
Alexander Afanasyev49a18522013-01-18 17:49:04 -080037class FetchManager
38{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080039public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080040 enum { PRIORITY_NORMAL, PRIORITY_HIGH };
Alexander Afanasyev49a18522013-01-18 17:49:04 -080041
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080042 typedef boost::function<Ccnx::Name(const Ccnx::Name&)> Mapping;
43 typedef boost::function<void(Ccnx::Name& deviceName, Ccnx::Name& baseName, uint64_t seq, Ccnx::PcoPtr pco)>
44 SegmentCallback;
45 typedef boost::function<void(Ccnx::Name& deviceName, Ccnx::Name& baseName)> FinishCallback;
46 FetchManager(Ccnx::CcnxWrapperPtr ccnx,
47 const Mapping& mapping,
48 const Ccnx::Name& broadcastForwardingHint,
49 uint32_t parallelFetches = 3,
50 const SegmentCallback& defaultSegmentCallback = SegmentCallback(),
51 const FinishCallback& defaultFinishCallback = FinishCallback(),
52 const FetchTaskDbPtr& taskDb = FetchTaskDbPtr());
53 virtual ~FetchManager();
Alexander Afanasyev49a18522013-01-18 17:49:04 -080054
55 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056 Enqueue(const Ccnx::Name& deviceName, const Ccnx::Name& baseName,
57 const SegmentCallback& segmentCallback, const FinishCallback& finishCallback,
58 uint64_t minSeqNo, uint64_t maxSeqNo, int priority = PRIORITY_NORMAL);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080059
Zhenkai Zhua0147382013-01-29 15:57:27 -080060 // Enqueue using default callbacks
61 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062 Enqueue(const Ccnx::Name& deviceName, const Ccnx::Name& baseName, uint64_t minSeqNo,
63 uint64_t maxSeqNo, int priority = PRIORITY_NORMAL);
Zhenkai Zhua0147382013-01-29 15:57:27 -080064
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080065 // only for Fetcher
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080066 inline Ccnx::CcnxWrapperPtr
67 GetCcnx();
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080068
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080069private:
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080070 // Fetch Events
71 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080072 DidDataSegmentFetched(Fetcher& fetcher, uint64_t seqno, const Ccnx::Name& basename,
73 const Ccnx::Name& name, Ccnx::PcoPtr data);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080074
Alexander Afanasyev83531a42013-01-19 16:21:54 -080075 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 DidNoDataTimeout(Fetcher& fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080077
78 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 DidFetchComplete(Fetcher& fetcher, const Ccnx::Name& deviceName, const Ccnx::Name& baseName);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080080
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080081 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080082 ScheduleFetches();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080083
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080084 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080085 TimedWait(Fetcher& fetcher);
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080086
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080087private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070088 Ndnx::NdnxWrapperPtr m_ndnx;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080089 Mapping m_mapping;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080090
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080091 uint32_t m_maxParallelFetches;
92 uint32_t m_currentParallelFetches;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080093 boost::mutex m_parellelFetchMutex;
94
95 // optimized list structure for fetch queue
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080096 typedef boost::intrusive::member_hook<Fetcher, boost::intrusive::list_member_hook<>,
97 &Fetcher::m_managerListHook>
98 MemberOption;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080099 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 Afanasyev473346f2013-02-07 14:14:45 -0800108
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700109 const Ndnx::Name m_broadcastHint;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800110};
111
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112Ccnx::CcnxWrapperPtr
113FetchManager::GetCcnx()
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800114{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700115 return m_ndnx;
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800116}
117
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800118typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800119namespace Error {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120struct FetchManager : virtual boost::exception, virtual std::exception
121{
122};
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800123}
124
125typedef boost::shared_ptr<FetchManager> FetchManagerPtr;
126
127
128#endif // FETCHER_H