blob: 5dae0ffe420c37c05f1662aedb4c29bae5f5d789 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wanga697cf22016-12-25 14:44:22 -08003 * Copyright (c) 2013-2017, 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
Lijing Wanga697cf22016-12-25 14:44:22 -080024#include "fetch-task-db.hpp"
25#include "fetcher.hpp"
26#include "core/chronoshare-common.hpp"
Alexander Afanasyev49a18522013-01-18 17:49:04 -080027
Lijing Wanga697cf22016-12-25 14:44:22 -080028#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
29#include <ndn-cxx/util/scheduler.hpp>
30
31#include <list>
32
33namespace ndn {
34namespace chronoshare {
Alexander Afanasyev83531a42013-01-19 16:21:54 -080035
Alexander Afanasyev49a18522013-01-18 17:49:04 -080036class FetchManager
37{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080038public:
Lijing Wanga697cf22016-12-25 14:44:22 -080039 class Error : public std::runtime_error
40 {
41 public:
42 explicit Error(const std::string& what)
43 : std::runtime_error(what)
44 {
45 }
46 };
47
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048 enum { PRIORITY_NORMAL, PRIORITY_HIGH };
Alexander Afanasyev49a18522013-01-18 17:49:04 -080049
Lijing Wanga697cf22016-12-25 14:44:22 -080050 typedef function<Name(const Name&)> Mapping;
51 typedef function<void(Name& deviceName, Name& baseName, uint64_t seq, shared_ptr<Data> data)> SegmentCallback;
52 typedef function<void(Name& deviceName, Name& baseName)> FinishCallback;
53
54public:
55 FetchManager(Face& face, const Mapping& mapping, const Name& broadcastForwardingHint,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056 uint32_t parallelFetches = 3,
57 const SegmentCallback& defaultSegmentCallback = SegmentCallback(),
58 const FinishCallback& defaultFinishCallback = FinishCallback(),
59 const FetchTaskDbPtr& taskDb = FetchTaskDbPtr());
60 virtual ~FetchManager();
Alexander Afanasyev49a18522013-01-18 17:49:04 -080061
62 void
Lijing Wanga697cf22016-12-25 14:44:22 -080063 Enqueue(const Name& deviceName, const Name& baseName, const SegmentCallback& segmentCallback,
64 const FinishCallback& finishCallback, uint64_t minSeqNo, uint64_t maxSeqNo,
65 int priority = PRIORITY_NORMAL);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080066
Zhenkai Zhua0147382013-01-29 15:57:27 -080067 // Enqueue using default callbacks
68 void
Lijing Wanga697cf22016-12-25 14:44:22 -080069 Enqueue(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo,
70 int priority = PRIORITY_NORMAL);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080071
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080072private:
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080073 // Fetch Events
74 void
Lijing Wanga697cf22016-12-25 14:44:22 -080075 DidDataSegmentFetched(Fetcher& fetcher, uint64_t seqno, const Name& basename, const Name& name,
76 shared_ptr<Data> data);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080077
Alexander Afanasyev83531a42013-01-19 16:21:54 -080078 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 DidNoDataTimeout(Fetcher& fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080080
81 void
Lijing Wanga697cf22016-12-25 14:44:22 -080082 DidFetchComplete(Fetcher& fetcher, const Name& deviceName, const Name& baseName);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080083
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080084 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080085 ScheduleFetches();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080086
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080087 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080088 TimedWait(Fetcher& fetcher);
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080089
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080090private:
Lijing Wanga697cf22016-12-25 14:44:22 -080091 Face& m_face;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080092 Mapping m_mapping;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080093
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080094 uint32_t m_maxParallelFetches;
95 uint32_t m_currentParallelFetches;
Lijing Wanga697cf22016-12-25 14:44:22 -080096 std::mutex m_parellelFetchMutex;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080097
98 // optimized list structure for fetch queue
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080099 typedef boost::intrusive::member_hook<Fetcher, boost::intrusive::list_member_hook<>,
Lijing Wanga697cf22016-12-25 14:44:22 -0800100 &Fetcher::m_managerListHook> MemberOption;
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800101 typedef boost::intrusive::list<Fetcher, MemberOption> FetchList;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800102
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800103 FetchList m_fetchList;
Lijing Wanga697cf22016-12-25 14:44:22 -0800104 Scheduler m_scheduler;
105 util::scheduler::ScopedEventId m_scheduledFetchesEvent;
106
Zhenkai Zhua0147382013-01-29 15:57:27 -0800107 SegmentCallback m_defaultSegmentCallback;
108 FinishCallback m_defaultFinishCallback;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800109 FetchTaskDbPtr m_taskDb;
Alexander Afanasyev473346f2013-02-07 14:14:45 -0800110
Lijing Wanga697cf22016-12-25 14:44:22 -0800111 const Name m_broadcastHint;
112 boost::asio::io_service& m_ioService;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800113};
114
Lijing Wanga697cf22016-12-25 14:44:22 -0800115typedef shared_ptr<FetchManager> FetchManagerPtr;
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800116
Lijing Wanga697cf22016-12-25 14:44:22 -0800117} // namespace chronoshare
118} // namespace ndn
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800119
120#endif // FETCHER_H