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