Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, 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 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 24 | #include "ccnx-wrapper.h" |
| 25 | #include "executor.h" |
| 26 | #include "fetch-task-db.h" |
| 27 | #include "scheduler.h" |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 28 | #include <boost/exception/all.hpp> |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 29 | #include <boost/function.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 30 | #include <boost/shared_ptr.hpp> |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 31 | #include <list> |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 32 | #include <stdint.h> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 33 | #include <string> |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 35 | #include "fetcher.h" |
| 36 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 37 | class FetchManager |
| 38 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 39 | public: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 40 | enum { PRIORITY_NORMAL, PRIORITY_HIGH }; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 42 | 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 Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 54 | |
| 55 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 56 | 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 Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 59 | |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 60 | // Enqueue using default callbacks |
| 61 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 62 | Enqueue(const Ccnx::Name& deviceName, const Ccnx::Name& baseName, uint64_t minSeqNo, |
| 63 | uint64_t maxSeqNo, int priority = PRIORITY_NORMAL); |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 64 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 65 | // only for Fetcher |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 66 | inline Ccnx::CcnxWrapperPtr |
| 67 | GetCcnx(); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 68 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 69 | private: |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 70 | // Fetch Events |
| 71 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 72 | DidDataSegmentFetched(Fetcher& fetcher, uint64_t seqno, const Ccnx::Name& basename, |
| 73 | const Ccnx::Name& name, Ccnx::PcoPtr data); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 75 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | DidNoDataTimeout(Fetcher& fetcher); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 77 | |
| 78 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 79 | DidFetchComplete(Fetcher& fetcher, const Ccnx::Name& deviceName, const Ccnx::Name& baseName); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 81 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 82 | ScheduleFetches(); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 83 | |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 84 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 85 | TimedWait(Fetcher& fetcher); |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 87 | private: |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 88 | Ndnx::NdnxWrapperPtr m_ndnx; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 89 | Mapping m_mapping; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 91 | uint32_t m_maxParallelFetches; |
| 92 | uint32_t m_currentParallelFetches; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 93 | boost::mutex m_parellelFetchMutex; |
| 94 | |
| 95 | // optimized list structure for fetch queue |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 96 | typedef boost::intrusive::member_hook<Fetcher, boost::intrusive::list_member_hook<>, |
| 97 | &Fetcher::m_managerListHook> |
| 98 | MemberOption; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 99 | typedef boost::intrusive::list<Fetcher, MemberOption> FetchList; |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 101 | FetchList m_fetchList; |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 102 | SchedulerPtr m_scheduler; |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 103 | ExecutorPtr m_executor; |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 104 | TaskPtr m_scheduleFetchesTask; |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 105 | SegmentCallback m_defaultSegmentCallback; |
| 106 | FinishCallback m_defaultFinishCallback; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 107 | FetchTaskDbPtr m_taskDb; |
Alexander Afanasyev | 473346f | 2013-02-07 14:14:45 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 109 | const Ndnx::Name m_broadcastHint; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 112 | Ccnx::CcnxWrapperPtr |
| 113 | FetchManager::GetCcnx() |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 114 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 115 | return m_ndnx; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 118 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 119 | namespace Error { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 120 | struct FetchManager : virtual boost::exception, virtual std::exception |
| 121 | { |
| 122 | }; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | typedef boost::shared_ptr<FetchManager> FetchManagerPtr; |
| 126 | |
| 127 | |
| 128 | #endif // FETCHER_H |