blob: 40d5258aa895a9cae937a3143755b1fa1d5d0aa1 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wanga697cf22016-12-25 14:44:22 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyevdfe58192013-01-17 17:34:04 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyevdfe58192013-01-17 17:34:04 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Afanasyevdfe58192013-01-17 17:34:04 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Afanasyevdfe58192013-01-17 17:34:04 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Afanasyevdfe58192013-01-17 17:34:04 -080019 */
20
Lijing Wanga697cf22016-12-25 14:44:22 -080021#ifndef CHRONOSHARE_SRC_FETCHER_HPP
22#define CHRONOSHARE_SRC_FETCHER_HPP
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080023
Lijing Wanga697cf22016-12-25 14:44:22 -080024#include "core/chronoshare-common.hpp"
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080025
Lijing Wanga697cf22016-12-25 14:44:22 -080026#include <ndn-cxx/face.hpp>
27
28#include <set>
29#include <thread>
30#include <mutex>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080031#include <boost/intrusive/list.hpp>
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080032
Lijing Wanga697cf22016-12-25 14:44:22 -080033namespace ndn {
34namespace chronoshare {
Alexander Afanasyev8f459752013-03-19 12:13:25 -070035
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080036class FetchManager;
37
Alexander Afanasyev83531a42013-01-19 16:21:54 -080038class Fetcher
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080039{
40public:
Lijing Wanga697cf22016-12-25 14:44:22 -080041 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 Afanasyev21a166e2013-01-20 16:04:41 -080045
Lijing Wanga697cf22016-12-25 14:44:22 -080046 Fetcher(Face& face,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047 const SegmentCallback& segmentCallback, // callback passed by caller of FetchManager
48 const FinishCallback& finishCallback, // callback passed by caller of FetchManager
Lijing Wanga697cf22016-12-25 14:44:22 -080049 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080055 virtual ~Fetcher();
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080056
Lijing Wanga697cf22016-12-25 14:44:22 -080057 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 IsActive() const;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080059
Lijing Wanga697cf22016-12-25 14:44:22 -080060 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080061 IsTimedWait() const
62 {
63 return m_timedwait;
64 }
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080065
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080066 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080067 RestartPipeline();
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080068
69 void
Lijing Wanga697cf22016-12-25 14:44:22 -080070 SetForwardingHint(const Name& forwardingHint);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080071
Lijing Wanga697cf22016-12-25 14:44:22 -080072 const Name&
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080073 GetForwardingHint() const
74 {
75 return m_forwardingHint;
76 }
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080077
Lijing Wanga697cf22016-12-25 14:44:22 -080078 const Name&
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 GetName() const
80 {
81 return m_name;
82 }
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080083
Lijing Wanga697cf22016-12-25 14:44:22 -080084 const Name&
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080085 GetDeviceName() const
86 {
87 return m_deviceName;
88 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080089
Lijing Wanga697cf22016-12-25 14:44:22 -080090 time::seconds
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091 GetRetryPause() const
92 {
93 return m_retryPause;
94 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080095
96 void
Lijing Wanga697cf22016-12-25 14:44:22 -080097 SetRetryPause(time::seconds pause)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080098 {
99 m_retryPause = pause;
100 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800101
Lijing Wanga697cf22016-12-25 14:44:22 -0800102 const time::steady_clock::TimePoint&
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 GetNextScheduledRetry() const
104 {
105 return m_nextScheduledRetry;
106 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800107
108 void
Lijing Wanga697cf22016-12-25 14:44:22 -0800109 SetNextScheduledRetry(const time::steady_clock::TimePoint& nextScheduledRetry)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 {
111 m_nextScheduledRetry = nextScheduledRetry;
112 }
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800113
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800114private:
115 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800116 FillPipeline();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800117
118 void
Lijing Wanga697cf22016-12-25 14:44:22 -0800119 OnData(uint64_t seqno, const Interest& interest, Data& data);
Alexander Afanasyev50547892013-01-19 22:03:45 -0800120
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800121 void
Lijing Wanga697cf22016-12-25 14:44:22 -0800122 OnTimeout(uint64_t seqno, const Interest& interest);
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800123
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800124public:
125 boost::intrusive::list_member_hook<> m_managerListHook;
126
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800127private:
Lijing Wanga697cf22016-12-25 14:44:22 -0800128 Face& m_face;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800129
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800130 SegmentCallback m_segmentCallback;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800131 OnFetchCompleteCallback m_onFetchComplete;
132 OnFetchFailedCallback m_onFetchFailed;
133
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800134 FinishCallback m_finishCallback;
135
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800136 bool m_active;
Zhenkai Zhu354d46d2013-02-06 13:49:48 -0800137 bool m_timedwait;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800138
Lijing Wanga697cf22016-12-25 14:44:22 -0800139 Name m_name;
140 Name m_deviceName;
141 Name m_forwardingHint;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800142
Lijing Wanga697cf22016-12-25 14:44:22 -0800143 time::milliseconds m_maximumNoActivityPeriod;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800144
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800145 int64_t m_minSendSeqNo;
146 int64_t m_maxInOrderRecvSeqNo;
147 std::set<int64_t> m_outOfOrderRecvSeqNo;
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800148 std::set<int64_t> m_inActivePipeline;
Alexander Afanasyev50547892013-01-19 22:03:45 -0800149
Lijing Wanga697cf22016-12-25 14:44:22 -0800150 // int64_t m_minSeqNo;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800151 int64_t m_maxSeqNo;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800152
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800153 uint32_t m_pipeline;
154 uint32_t m_activePipeline;
Lijing Wanga697cf22016-12-25 14:44:22 -0800155 // double m_rto;
156 // double m_maxRto;
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700157 bool m_slowStart;
158 uint32_t m_threshold;
159 uint32_t m_roundCount;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800160
Lijing Wanga697cf22016-12-25 14:44:22 -0800161 time::steady_clock::TimePoint m_lastPositiveActivity;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800162
Lijing Wanga697cf22016-12-25 14:44:22 -0800163 time::seconds m_retryPause; // pause to stop trying to fetch(for fetch-manager)
164 time::steady_clock::TimePoint m_nextScheduledRetry;
Alexander Afanasyevca77d4e2013-01-28 22:50:05 -0800165
Lijing Wanga697cf22016-12-25 14:44:22 -0800166 std::mutex m_seqNoMutex;
Alexander Afanasyev2ec2c382013-01-29 20:09:00 -0800167
Lijing Wanga697cf22016-12-25 14:44:22 -0800168 boost::asio::io_service& m_ioService;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800169};
170
Lijing Wanga697cf22016-12-25 14:44:22 -0800171typedef shared_ptr<Fetcher> FetcherPtr;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800172
Lijing Wanga697cf22016-12-25 14:44:22 -0800173inline bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800174Fetcher::IsActive() const
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800175{
176 return m_active;
177}
178
Lijing Wanga697cf22016-12-25 14:44:22 -0800179} // namespace chronoshare
180} // namespace ndn
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800181
Lijing Wanga697cf22016-12-25 14:44:22 -0800182#endif // CHRONOSHARE_SRC_FETCHER_HPP