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 | dfe5819 | 2013-01-17 17:34: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 | dfe5819 | 2013-01-17 17:34: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 | dfe5819 | 2013-01-17 17:34: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 | dfe5819 | 2013-01-17 17:34: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 | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #ifndef FETCHER_H |
| 22 | #define FETCHER_H |
| 23 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 24 | #include "ccnx-name.h" |
| 25 | #include "ccnx-wrapper.h" |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 27 | #include "executor.h" |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 28 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 29 | #include <boost/intrusive/list.hpp> |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 30 | #include <set> |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 8f45975 | 2013-03-19 12:13:25 -0700 | [diff] [blame] | 32 | #include <set> |
| 33 | |
Alexander Afanasyev | 76d4fa4 | 2013-01-19 14:51:21 -0800 | [diff] [blame] | 34 | class FetchManager; |
| 35 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 36 | class Fetcher |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 37 | { |
| 38 | public: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 39 | typedef boost::function<void(Ccnx::Name& deviceName, Ccnx::Name& baseName, uint64_t seq, Ccnx::PcoPtr pco)> |
| 40 | SegmentCallback; |
| 41 | typedef boost::function<void(Ccnx::Name& deviceName, Ccnx::Name& baseName)> FinishCallback; |
| 42 | typedef boost::function<void(Fetcher&, const Ccnx::Name& deviceName, const Ccnx::Name& baseName)> |
| 43 | OnFetchCompleteCallback; |
| 44 | typedef boost::function<void(Fetcher&)> OnFetchFailedCallback; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 46 | Fetcher(Ccnx::CcnxWrapperPtr ccnx, ExecutorPtr executor, |
| 47 | const SegmentCallback& segmentCallback, // callback passed by caller of FetchManager |
| 48 | const FinishCallback& finishCallback, // callback passed by caller of FetchManager |
| 49 | OnFetchCompleteCallback onFetchComplete, |
| 50 | OnFetchFailedCallback onFetchFailed, // callbacks provided by FetchManager |
| 51 | const Ccnx::Name& deviceName, const Ccnx::Name& name, int64_t minSeqNo, int64_t maxSeqNo, |
| 52 | boost::posix_time::time_duration timeout = |
| 53 | boost::posix_time::seconds(30), // this time is not precise, but sets min bound |
| 54 | // actual time depends on how fast Interests timeout |
| 55 | const Ccnx::Name& forwardingHint = Ccnx::Name()); |
| 56 | virtual ~Fetcher(); |
Alexander Afanasyev | 76d4fa4 | 2013-01-19 14:51:21 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 58 | inline bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | IsActive() const; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 60 | |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 61 | inline bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 62 | IsTimedWait() const |
| 63 | { |
| 64 | return m_timedwait; |
| 65 | } |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 76d4fa4 | 2013-01-19 14:51:21 -0800 | [diff] [blame] | 67 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 68 | RestartPipeline(); |
Alexander Afanasyev | 76d4fa4 | 2013-01-19 14:51:21 -0800 | [diff] [blame] | 69 | |
| 70 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 71 | SetForwardingHint(const Ccnx::Name& forwardingHint); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | const Ccnx::Name& |
| 74 | GetForwardingHint() const |
| 75 | { |
| 76 | return m_forwardingHint; |
| 77 | } |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 78 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 79 | const Ccnx::Name& |
| 80 | GetName() const |
| 81 | { |
| 82 | return m_name; |
| 83 | } |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 85 | const Ccnx::Name& |
| 86 | GetDeviceName() const |
| 87 | { |
| 88 | return m_deviceName; |
| 89 | } |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 90 | |
| 91 | double |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 92 | GetRetryPause() const |
| 93 | { |
| 94 | return m_retryPause; |
| 95 | } |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 96 | |
| 97 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 98 | SetRetryPause(double pause) |
| 99 | { |
| 100 | m_retryPause = pause; |
| 101 | } |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 102 | |
| 103 | boost::posix_time::ptime |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 104 | GetNextScheduledRetry() const |
| 105 | { |
| 106 | return m_nextScheduledRetry; |
| 107 | } |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 108 | |
| 109 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 110 | SetNextScheduledRetry(boost::posix_time::ptime nextScheduledRetry) |
| 111 | { |
| 112 | m_nextScheduledRetry = nextScheduledRetry; |
| 113 | } |
Alexander Afanasyev | ff8d9dc | 2013-01-26 00:45:08 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 115 | private: |
| 116 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 117 | FillPipeline(); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 118 | |
| 119 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 120 | OnData(uint64_t seqno, const Ccnx::Name& name, Ccnx::PcoPtr data); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 121 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 122 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 123 | OnData_Execute(uint64_t seqno, Ccnx::Name name, Ccnx::PcoPtr data); |
Alexander Afanasyev | ddc283c | 2013-01-28 23:11:20 -0800 | [diff] [blame] | 124 | |
| 125 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 126 | OnTimeout(uint64_t seqno, const Ccnx::Name& name, const Ccnx::Closure& closure, |
| 127 | Ccnx::Selectors selectors); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 128 | |
Alexander Afanasyev | ddc283c | 2013-01-28 23:11:20 -0800 | [diff] [blame] | 129 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 130 | OnTimeout_Execute(uint64_t seqno, Ccnx::Name name, Ccnx::Closure closure, |
| 131 | Ccnx::Selectors selectors); |
Alexander Afanasyev | ddc283c | 2013-01-28 23:11:20 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 133 | public: |
| 134 | boost::intrusive::list_member_hook<> m_managerListHook; |
| 135 | |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 136 | private: |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 137 | Ndnx::NdnxWrapperPtr m_ndnx; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 138 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 139 | SegmentCallback m_segmentCallback; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 140 | OnFetchCompleteCallback m_onFetchComplete; |
| 141 | OnFetchFailedCallback m_onFetchFailed; |
| 142 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 143 | FinishCallback m_finishCallback; |
| 144 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 145 | bool m_active; |
Zhenkai Zhu | 354d46d | 2013-02-06 13:49:48 -0800 | [diff] [blame] | 146 | bool m_timedwait; |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 148 | Ndnx::Name m_name; |
| 149 | Ndnx::Name m_deviceName; |
| 150 | Ndnx::Name m_forwardingHint; |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 152 | boost::posix_time::time_duration m_maximumNoActivityPeriod; |
| 153 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 154 | int64_t m_minSendSeqNo; |
| 155 | int64_t m_maxInOrderRecvSeqNo; |
| 156 | std::set<int64_t> m_outOfOrderRecvSeqNo; |
Alexander Afanasyev | cfd5dfc | 2013-01-28 22:21:31 -0800 | [diff] [blame] | 157 | std::set<int64_t> m_inActivePipeline; |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 158 | |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 159 | int64_t m_minSeqNo; |
| 160 | int64_t m_maxSeqNo; |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 162 | uint32_t m_pipeline; |
| 163 | uint32_t m_activePipeline; |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 164 | double m_rto; |
| 165 | double m_maxRto; |
| 166 | bool m_slowStart; |
| 167 | uint32_t m_threshold; |
| 168 | uint32_t m_roundCount; |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 170 | boost::posix_time::ptime m_lastPositiveActivity; |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 171 | |
| 172 | double m_retryPause; // pause to stop trying to fetch (for fetch-manager) |
| 173 | boost::posix_time::ptime m_nextScheduledRetry; |
Alexander Afanasyev | ca77d4e | 2013-01-28 22:50:05 -0800 | [diff] [blame] | 174 | |
Zhenkai Zhu | ab9215c | 2013-01-28 23:42:28 -0800 | [diff] [blame] | 175 | ExecutorPtr m_executor; // to serialize FillPipeline events |
Alexander Afanasyev | 2ec2c38 | 2013-01-29 20:09:00 -0800 | [diff] [blame] | 176 | |
| 177 | boost::mutex m_seqNoMutex; |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 178 | boost::mutex m_rtoMutex; |
| 179 | boost::mutex m_pipelineMutex; |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 182 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 183 | |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 184 | namespace Error { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 185 | struct Fetcher : virtual boost::exception, virtual std::exception |
| 186 | { |
| 187 | }; |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | typedef boost::shared_ptr<Fetcher> FetcherPtr; |
| 191 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 192 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 193 | Fetcher::IsActive() const |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 194 | { |
| 195 | return m_active; |
| 196 | } |
| 197 | |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 198 | |
| 199 | #endif // FETCHER_H |