blob: 6cd84c9282e14886691774639d44879c4250fbbf [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
Alexander Afanasyevc91fc332017-02-10 17:44:41 -080021#ifndef CHRONOSHARE_SRC_FETCH_MANAGER_HPP
22#define CHRONOSHARE_SRC_FETCH_MANAGER_HPP
Alexander Afanasyev49a18522013-01-18 17:49:04 -080023
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,
Alexander Afanasyevc91fc332017-02-10 17:44:41 -080057 bool isSegment = true,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 const SegmentCallback& defaultSegmentCallback = SegmentCallback(),
59 const FinishCallback& defaultFinishCallback = FinishCallback(),
60 const FetchTaskDbPtr& taskDb = FetchTaskDbPtr());
61 virtual ~FetchManager();
Alexander Afanasyev49a18522013-01-18 17:49:04 -080062
63 void
Lijing Wanga697cf22016-12-25 14:44:22 -080064 Enqueue(const Name& deviceName, const Name& baseName, const SegmentCallback& segmentCallback,
65 const FinishCallback& finishCallback, uint64_t minSeqNo, uint64_t maxSeqNo,
66 int priority = PRIORITY_NORMAL);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080067
Zhenkai Zhua0147382013-01-29 15:57:27 -080068 // Enqueue using default callbacks
69 void
Lijing Wanga697cf22016-12-25 14:44:22 -080070 Enqueue(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo,
71 int priority = PRIORITY_NORMAL);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080072
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080073private:
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080074 // Fetch Events
75 void
Lijing Wanga697cf22016-12-25 14:44:22 -080076 DidDataSegmentFetched(Fetcher& fetcher, uint64_t seqno, const Name& basename, const Name& name,
77 shared_ptr<Data> data);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080078
Alexander Afanasyev83531a42013-01-19 16:21:54 -080079 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080080 DidNoDataTimeout(Fetcher& fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080081
82 void
Lijing Wanga697cf22016-12-25 14:44:22 -080083 DidFetchComplete(Fetcher& fetcher, const Name& deviceName, const Name& baseName);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080084
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080085 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080086 ScheduleFetches();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080087
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080088 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080089 TimedWait(Fetcher& fetcher);
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080090
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080091private:
Lijing Wanga697cf22016-12-25 14:44:22 -080092 Face& m_face;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080093 Mapping m_mapping;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080094
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080095 uint32_t m_maxParallelFetches;
96 uint32_t m_currentParallelFetches;
Lijing Wanga697cf22016-12-25 14:44:22 -080097 std::mutex m_parellelFetchMutex;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080098
99 // optimized list structure for fetch queue
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 typedef boost::intrusive::member_hook<Fetcher, boost::intrusive::list_member_hook<>,
Lijing Wanga697cf22016-12-25 14:44:22 -0800101 &Fetcher::m_managerListHook> MemberOption;
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800102 typedef boost::intrusive::list<Fetcher, MemberOption> FetchList;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800103
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800104 FetchList m_fetchList;
Lijing Wanga697cf22016-12-25 14:44:22 -0800105 Scheduler m_scheduler;
106 util::scheduler::ScopedEventId m_scheduledFetchesEvent;
107
Zhenkai Zhua0147382013-01-29 15:57:27 -0800108 SegmentCallback m_defaultSegmentCallback;
109 FinishCallback m_defaultFinishCallback;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800110 FetchTaskDbPtr m_taskDb;
Alexander Afanasyev473346f2013-02-07 14:14:45 -0800111
Lijing Wanga697cf22016-12-25 14:44:22 -0800112 const Name m_broadcastHint;
113 boost::asio::io_service& m_ioService;
Alexander Afanasyevc91fc332017-02-10 17:44:41 -0800114
115 bool m_isSegment;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800116};
117
Lijing Wanga697cf22016-12-25 14:44:22 -0800118typedef shared_ptr<FetchManager> FetchManagerPtr;
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800119
Lijing Wanga697cf22016-12-25 14:44:22 -0800120} // namespace chronoshare
121} // namespace ndn
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800122
Alexander Afanasyevc91fc332017-02-10 17:44:41 -0800123#endif // CHRONOSHARE_SRC_FETCH_MANAGER_HPP