Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #ifndef FETCH_MANAGER_H |
| 23 | #define FETCH_MANAGER_H |
| 24 | |
| 25 | #include <boost/exception/all.hpp> |
| 26 | #include <boost/shared_ptr.hpp> |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 27 | #include <boost/function.hpp> |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 28 | #include <string> |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 29 | #include <list> |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 30 | #include <stdint.h> |
| 31 | #include "scheduler.h" |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 32 | #include "executor.h" |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 33 | #include "ndnx-wrapper.h" |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 34 | #include "fetch-task-db.h" |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 36 | #include "fetcher.h" |
| 37 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 38 | class FetchManager |
| 39 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 40 | public: |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 41 | enum |
| 42 | { |
| 43 | PRIORITY_NORMAL, |
| 44 | PRIORITY_HIGH |
| 45 | }; |
| 46 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 47 | typedef boost::function<Ndnx::Name(const Ndnx::Name &)> Mapping; |
| 48 | typedef boost::function<void(Ndnx::Name &deviceName, Ndnx::Name &baseName, uint64_t seq, Ndnx::PcoPtr pco)> SegmentCallback; |
| 49 | typedef boost::function<void(Ndnx::Name &deviceName, Ndnx::Name &baseName)> FinishCallback; |
| 50 | FetchManager (Ndnx::NdnxWrapperPtr ndnx, |
Alexander Afanasyev | 473346f | 2013-02-07 14:14:45 -0800 | [diff] [blame] | 51 | const Mapping &mapping, |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 52 | const Ndnx::Name &broadcastForwardingHint, |
Alexander Afanasyev | 473346f | 2013-02-07 14:14:45 -0800 | [diff] [blame] | 53 | uint32_t parallelFetches = 3, |
| 54 | const SegmentCallback &defaultSegmentCallback = SegmentCallback(), |
| 55 | const FinishCallback &defaultFinishCallback = FinishCallback(), |
| 56 | const FetchTaskDbPtr &taskDb = FetchTaskDbPtr() |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 57 | ); |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 58 | virtual ~FetchManager (); |
| 59 | |
| 60 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 61 | Enqueue (const Ndnx::Name &deviceName, const Ndnx::Name &baseName, |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 62 | const SegmentCallback &segmentCallback, const FinishCallback &finishCallback, |
| 63 | uint64_t minSeqNo, uint64_t maxSeqNo, int priority=PRIORITY_NORMAL); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 64 | |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 65 | // Enqueue using default callbacks |
| 66 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 67 | Enqueue (const Ndnx::Name &deviceName, const Ndnx::Name &baseName, |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 68 | uint64_t minSeqNo, uint64_t maxSeqNo, int priority=PRIORITY_NORMAL); |
| 69 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 70 | // only for Fetcher |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 71 | inline Ndnx::NdnxWrapperPtr |
| 72 | GetNdnx (); |
Alexander Afanasyev | e41e7d2 | 2013-01-19 15:13:47 -0800 | [diff] [blame] | 73 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 74 | private: |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 75 | // Fetch Events |
| 76 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 77 | DidDataSegmentFetched (Fetcher &fetcher, uint64_t seqno, const Ndnx::Name &basename, |
| 78 | const Ndnx::Name &name, Ndnx::PcoPtr data); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 80 | void |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 81 | DidNoDataTimeout (Fetcher &fetcher); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 82 | |
| 83 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 84 | DidFetchComplete (Fetcher &fetcher, const Ndnx::Name &deviceName, const Ndnx::Name &baseName); |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 86 | void |
| 87 | ScheduleFetches (); |
| 88 | |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 89 | void |
| 90 | TimedWait (Fetcher &fetcher); |
| 91 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 92 | private: |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 93 | Ndnx::NdnxWrapperPtr m_ndnx; |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 94 | Mapping m_mapping; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 96 | uint32_t m_maxParallelFetches; |
| 97 | uint32_t m_currentParallelFetches; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 98 | boost::mutex m_parellelFetchMutex; |
| 99 | |
| 100 | // optimized list structure for fetch queue |
| 101 | typedef boost::intrusive::member_hook< Fetcher, |
| 102 | boost::intrusive::list_member_hook<>, &Fetcher::m_managerListHook> MemberOption; |
| 103 | typedef boost::intrusive::list<Fetcher, MemberOption> FetchList; |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 105 | FetchList m_fetchList; |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 106 | SchedulerPtr m_scheduler; |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 107 | ExecutorPtr m_executor; |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 108 | TaskPtr m_scheduleFetchesTask; |
Zhenkai Zhu | a014738 | 2013-01-29 15:57:27 -0800 | [diff] [blame] | 109 | SegmentCallback m_defaultSegmentCallback; |
| 110 | FinishCallback m_defaultFinishCallback; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 111 | FetchTaskDbPtr m_taskDb; |
Alexander Afanasyev | 473346f | 2013-02-07 14:14:45 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 113 | const Ndnx::Name m_broadcastHint; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 116 | Ndnx::NdnxWrapperPtr |
| 117 | FetchManager::GetNdnx () |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 118 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 119 | return m_ndnx; |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 122 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 123 | namespace Error { |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 124 | struct FetchManager : virtual boost::exception, virtual std::exception { }; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | typedef boost::shared_ptr<FetchManager> FetchManagerPtr; |
| 128 | |
| 129 | |
| 130 | #endif // FETCHER_H |