Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * 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 Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * 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 Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * 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 Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #ifndef FETCH_MANAGER_H |
| 22 | #define FETCH_MANAGER_H |
| 23 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 24 | #include "fetch-task-db.hpp" |
| 25 | #include "fetcher.hpp" |
| 26 | #include "core/chronoshare-common.hpp" |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 27 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 28 | #include <ndn-cxx/util/scheduler-scoped-event-id.hpp> |
| 29 | #include <ndn-cxx/util/scheduler.hpp> |
| 30 | |
| 31 | #include <list> |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace chronoshare { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 36 | class FetchManager |
| 37 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 38 | public: |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 39 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 48 | enum { PRIORITY_NORMAL, PRIORITY_HIGH }; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 49 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 50 | 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 | |
| 54 | public: |
| 55 | FetchManager(Face& face, const Mapping& mapping, const Name& broadcastForwardingHint, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 56 | uint32_t parallelFetches = 3, |
| 57 | const SegmentCallback& defaultSegmentCallback = SegmentCallback(), |
| 58 | const FinishCallback& defaultFinishCallback = FinishCallback(), |
| 59 | const FetchTaskDbPtr& taskDb = FetchTaskDbPtr()); |
| 60 | virtual ~FetchManager(); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 61 | |
| 62 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 63 | 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 Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 66 | |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 67 | // Enqueue using default callbacks |
| 68 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 69 | Enqueue(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo, |
| 70 | int priority = PRIORITY_NORMAL); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 71 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 72 | private: |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 73 | // Fetch Events |
| 74 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 75 | DidDataSegmentFetched(Fetcher& fetcher, uint64_t seqno, const Name& basename, const Name& name, |
| 76 | shared_ptr<Data> data); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 78 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 79 | DidNoDataTimeout(Fetcher& fetcher); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 80 | |
| 81 | void |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 82 | DidFetchComplete(Fetcher& fetcher, const Name& deviceName, const Name& baseName); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 84 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 85 | ScheduleFetches(); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 86 | |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 87 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 88 | TimedWait(Fetcher& fetcher); |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 90 | private: |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 91 | Face& m_face; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 92 | Mapping m_mapping; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 94 | uint32_t m_maxParallelFetches; |
| 95 | uint32_t m_currentParallelFetches; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 96 | std::mutex m_parellelFetchMutex; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 97 | |
| 98 | // optimized list structure for fetch queue |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 99 | typedef boost::intrusive::member_hook<Fetcher, boost::intrusive::list_member_hook<>, |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 100 | &Fetcher::m_managerListHook> MemberOption; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 101 | typedef boost::intrusive::list<Fetcher, MemberOption> FetchList; |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 103 | FetchList m_fetchList; |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 104 | Scheduler m_scheduler; |
| 105 | util::scheduler::ScopedEventId m_scheduledFetchesEvent; |
| 106 | |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 107 | SegmentCallback m_defaultSegmentCallback; |
| 108 | FinishCallback m_defaultFinishCallback; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 109 | FetchTaskDbPtr m_taskDb; |
Alexander Afanasyev | 473346f | 2013-02-07 14:14:45 -0800 | [diff] [blame] | 110 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 111 | const Name m_broadcastHint; |
| 112 | boost::asio::io_service& m_ioService; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 115 | typedef shared_ptr<FetchManager> FetchManagerPtr; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 116 | |
Lijing Wang | a697cf2 | 2016-12-25 14:44:22 -0800 | [diff] [blame] | 117 | } // namespace chronoshare |
| 118 | } // namespace ndn |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 119 | |
| 120 | #endif // FETCHER_H |