blob: c1da2a35a3f5f36aa72d495f0462329d0d7a25f5 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, 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
21#ifndef FETCHER_H
22#define FETCHER_H
23
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include "ccnx-name.h"
25#include "ccnx-wrapper.h"
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080026
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080027#include "executor.h"
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080028#include <boost/date_time/posix_time/posix_time_types.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080029#include <boost/intrusive/list.hpp>
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -070030#include <set>
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080031
Alexander Afanasyev8f459752013-03-19 12:13:25 -070032#include <set>
33
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080034class FetchManager;
35
Alexander Afanasyev83531a42013-01-19 16:21:54 -080036class Fetcher
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080037{
38public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039 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 Afanasyev21a166e2013-01-20 16:04:41 -080045
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080046 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 Afanasyev76d4fa42013-01-19 14:51:21 -080057
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080058 inline bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059 IsActive() const;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080060
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080061 inline bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062 IsTimedWait() const
63 {
64 return m_timedwait;
65 }
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080066
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080067 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080068 RestartPipeline();
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080069
70 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080071 SetForwardingHint(const Ccnx::Name& forwardingHint);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080072
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080073 const Ccnx::Name&
74 GetForwardingHint() const
75 {
76 return m_forwardingHint;
77 }
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080078
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 const Ccnx::Name&
80 GetName() const
81 {
82 return m_name;
83 }
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080084
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080085 const Ccnx::Name&
86 GetDeviceName() const
87 {
88 return m_deviceName;
89 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080090
91 double
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080092 GetRetryPause() const
93 {
94 return m_retryPause;
95 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080096
97 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080098 SetRetryPause(double pause)
99 {
100 m_retryPause = pause;
101 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800102
103 boost::posix_time::ptime
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800104 GetNextScheduledRetry() const
105 {
106 return m_nextScheduledRetry;
107 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800108
109 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 SetNextScheduledRetry(boost::posix_time::ptime nextScheduledRetry)
111 {
112 m_nextScheduledRetry = nextScheduledRetry;
113 }
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800114
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800115private:
116 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800117 FillPipeline();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800118
119 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120 OnData(uint64_t seqno, const Ccnx::Name& name, Ccnx::PcoPtr data);
Alexander Afanasyev50547892013-01-19 22:03:45 -0800121
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800122 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800123 OnData_Execute(uint64_t seqno, Ccnx::Name name, Ccnx::PcoPtr data);
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800124
125 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800126 OnTimeout(uint64_t seqno, const Ccnx::Name& name, const Ccnx::Closure& closure,
127 Ccnx::Selectors selectors);
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800128
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800129 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800130 OnTimeout_Execute(uint64_t seqno, Ccnx::Name name, Ccnx::Closure closure,
131 Ccnx::Selectors selectors);
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800132
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800133public:
134 boost::intrusive::list_member_hook<> m_managerListHook;
135
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800136private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700137 Ndnx::NdnxWrapperPtr m_ndnx;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800138
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800139 SegmentCallback m_segmentCallback;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800140 OnFetchCompleteCallback m_onFetchComplete;
141 OnFetchFailedCallback m_onFetchFailed;
142
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800143 FinishCallback m_finishCallback;
144
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800145 bool m_active;
Zhenkai Zhu354d46d2013-02-06 13:49:48 -0800146 bool m_timedwait;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800147
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700148 Ndnx::Name m_name;
149 Ndnx::Name m_deviceName;
150 Ndnx::Name m_forwardingHint;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800151
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800152 boost::posix_time::time_duration m_maximumNoActivityPeriod;
153
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800154 int64_t m_minSendSeqNo;
155 int64_t m_maxInOrderRecvSeqNo;
156 std::set<int64_t> m_outOfOrderRecvSeqNo;
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800157 std::set<int64_t> m_inActivePipeline;
Alexander Afanasyev50547892013-01-19 22:03:45 -0800158
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800159 int64_t m_minSeqNo;
160 int64_t m_maxSeqNo;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800161
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800162 uint32_t m_pipeline;
163 uint32_t m_activePipeline;
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700164 double m_rto;
165 double m_maxRto;
166 bool m_slowStart;
167 uint32_t m_threshold;
168 uint32_t m_roundCount;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800169
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800170 boost::posix_time::ptime m_lastPositiveActivity;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800171
172 double m_retryPause; // pause to stop trying to fetch (for fetch-manager)
173 boost::posix_time::ptime m_nextScheduledRetry;
Alexander Afanasyevca77d4e2013-01-28 22:50:05 -0800174
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800175 ExecutorPtr m_executor; // to serialize FillPipeline events
Alexander Afanasyev2ec2c382013-01-29 20:09:00 -0800176
177 boost::mutex m_seqNoMutex;
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700178 boost::mutex m_rtoMutex;
179 boost::mutex m_pipelineMutex;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800180};
181
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800182typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800183
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800184namespace Error {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800185struct Fetcher : virtual boost::exception, virtual std::exception
186{
187};
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800188}
189
190typedef boost::shared_ptr<Fetcher> FetcherPtr;
191
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800192bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800193Fetcher::IsActive() const
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800194{
195 return m_active;
196}
197
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800198
199#endif // FETCHER_H