blob: 5ce47467933c743ddd5a0387482966110e4ae849 [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 Afanasyev1dd37ed2013-08-14 18:08:09 -070024#include "ndnx-wrapper.h"
25#include "ndnx-name.h"
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080026
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080027#include "executor.h"
Alexander Afanasyev83531a42013-01-19 16:21:54 -080028#include <boost/intrusive/list.hpp>
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080029#include <boost/date_time/posix_time/posix_time_types.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 Afanasyev1dd37ed2013-08-14 18:08:09 -070039 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 Afanasyev21a166e2013-01-20 16:04:41 -080042 typedef boost::function<void (Fetcher &)> OnFetchFailedCallback;
43
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070044 Fetcher (Ndnx::NdnxWrapperPtr ndnx,
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080045 ExecutorPtr executor,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080046 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 Afanasyev1dd37ed2013-08-14 18:08:09 -070049 const Ndnx::Name &deviceName, const Ndnx::Name &name, int64_t minSeqNo, int64_t maxSeqNo,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080050 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 Afanasyev1dd37ed2013-08-14 18:08:09 -070052 const Ndnx::Name &forwardingHint = Ndnx::Name ());
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080053 virtual ~Fetcher ();
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080054
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080055 inline bool
56 IsActive () const;
57
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080058 inline bool
59 IsTimedWait() const { return m_timedwait; }
60
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080061 void
Alexander Afanasyev83531a42013-01-19 16:21:54 -080062 RestartPipeline ();
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080063
64 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070065 SetForwardingHint (const Ndnx::Name &forwardingHint);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080066
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070067 const Ndnx::Name &
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080068 GetForwardingHint () const { return m_forwardingHint; }
69
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070070 const Ndnx::Name &
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080071 GetName () const { return m_name; }
72
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070073 const Ndnx::Name &
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080074 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 Afanasyevff8d9dc2013-01-26 00:45:08 -080087
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080088private:
89 void
Alexander Afanasyev50547892013-01-19 22:03:45 -080090 FillPipeline ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080091
92 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070093 OnData (uint64_t seqno, const Ndnx::Name &name, Ndnx::PcoPtr data);
Alexander Afanasyev50547892013-01-19 22:03:45 -080094
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080095 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070096 OnData_Execute (uint64_t seqno, Ndnx::Name name, Ndnx::PcoPtr data);
Alexander Afanasyevddc283c2013-01-28 23:11:20 -080097
98 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070099 OnTimeout (uint64_t seqno, const Ndnx::Name &name, const Ndnx::Closure &closure, Ndnx::Selectors selectors);
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800100
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800101 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700102 OnTimeout_Execute (uint64_t seqno, Ndnx::Name name, Ndnx::Closure closure, Ndnx::Selectors selectors);
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800103
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800104public:
105 boost::intrusive::list_member_hook<> m_managerListHook;
106
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800107private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700108 Ndnx::NdnxWrapperPtr m_ndnx;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800109
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800110 SegmentCallback m_segmentCallback;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800111 OnFetchCompleteCallback m_onFetchComplete;
112 OnFetchFailedCallback m_onFetchFailed;
113
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800114 FinishCallback m_finishCallback;
115
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800116 bool m_active;
Zhenkai Zhu354d46d2013-02-06 13:49:48 -0800117 bool m_timedwait;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800118
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700119 Ndnx::Name m_name;
120 Ndnx::Name m_deviceName;
121 Ndnx::Name m_forwardingHint;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800122
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800123 boost::posix_time::time_duration m_maximumNoActivityPeriod;
124
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800125 int64_t m_minSendSeqNo;
126 int64_t m_maxInOrderRecvSeqNo;
127 std::set<int64_t> m_outOfOrderRecvSeqNo;
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800128 std::set<int64_t> m_inActivePipeline;
Alexander Afanasyev50547892013-01-19 22:03:45 -0800129
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800130 int64_t m_minSeqNo;
131 int64_t m_maxSeqNo;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800132
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800133 uint32_t m_pipeline;
134 uint32_t m_activePipeline;
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700135 double m_rto;
136 double m_maxRto;
137 bool m_slowStart;
138 uint32_t m_threshold;
139 uint32_t m_roundCount;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800140
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800141 boost::posix_time::ptime m_lastPositiveActivity;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800142
143 double m_retryPause; // pause to stop trying to fetch (for fetch-manager)
144 boost::posix_time::ptime m_nextScheduledRetry;
Alexander Afanasyevca77d4e2013-01-28 22:50:05 -0800145
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800146 ExecutorPtr m_executor; // to serialize FillPipeline events
Alexander Afanasyev2ec2c382013-01-29 20:09:00 -0800147
148 boost::mutex m_seqNoMutex;
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700149 boost::mutex m_rtoMutex;
150 boost::mutex m_pipelineMutex;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800151};
152
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800153typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800154
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800155namespace Error {
156struct Fetcher : virtual boost::exception, virtual std::exception { };
157}
158
159typedef boost::shared_ptr<Fetcher> FetcherPtr;
160
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800161bool
162Fetcher::IsActive () const
163{
164 return m_active;
165}
166
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800167
168#endif // FETCHER_H