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