blob: c62a4b49dff6143b9f5f245c52d70f66fa19b0e6 [file] [log] [blame]
Alexander Afanasyevdfe58192013-01-17 17:34:04 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
20 */
21
22#ifndef FETCHER_H
23#define FETCHER_H
24
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070025#include "ndnx-wrapper.h"
26#include "ndnx-name.h"
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080027
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080028#include "executor.h"
Alexander Afanasyev83531a42013-01-19 16:21:54 -080029#include <boost/intrusive/list.hpp>
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080030#include <boost/date_time/posix_time/posix_time_types.hpp>
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -070031#include <set>
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080032
Alexander Afanasyev8f459752013-03-19 12:13:25 -070033#include <set>
34
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080035class FetchManager;
36
Alexander Afanasyev83531a42013-01-19 16:21:54 -080037class Fetcher
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080038{
39public:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070040 typedef boost::function<void(Ndnx::Name &deviceName, Ndnx::Name &baseName, uint64_t seq, Ndnx::PcoPtr pco)> SegmentCallback;
41 typedef boost::function<void(Ndnx::Name &deviceName, Ndnx::Name &baseName)> FinishCallback;
42 typedef boost::function<void (Fetcher &, const Ndnx::Name &deviceName, const Ndnx::Name &baseName)> OnFetchCompleteCallback;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080043 typedef boost::function<void (Fetcher &)> OnFetchFailedCallback;
44
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070045 Fetcher (Ndnx::NdnxWrapperPtr ndnx,
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080046 ExecutorPtr executor,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080047 const SegmentCallback &segmentCallback, // callback passed by caller of FetchManager
48 const FinishCallback &finishCallback, // callback passed by caller of FetchManager
49 OnFetchCompleteCallback onFetchComplete, OnFetchFailedCallback onFetchFailed, // callbacks provided by FetchManager
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070050 const Ndnx::Name &deviceName, const Ndnx::Name &name, int64_t minSeqNo, int64_t maxSeqNo,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080051 boost::posix_time::time_duration timeout = boost::posix_time::seconds (30), // this time is not precise, but sets min bound
52 // actual time depends on how fast Interests timeout
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070053 const Ndnx::Name &forwardingHint = Ndnx::Name ());
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080054 virtual ~Fetcher ();
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080055
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080056 inline bool
57 IsActive () const;
58
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080059 inline bool
60 IsTimedWait() const { return m_timedwait; }
61
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080062 void
Alexander Afanasyev83531a42013-01-19 16:21:54 -080063 RestartPipeline ();
Alexander Afanasyev76d4fa42013-01-19 14:51:21 -080064
65 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070066 SetForwardingHint (const Ndnx::Name &forwardingHint);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080067
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070068 const Ndnx::Name &
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080069 GetForwardingHint () const { return m_forwardingHint; }
70
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070071 const Ndnx::Name &
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080072 GetName () const { return m_name; }
73
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070074 const Ndnx::Name &
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080075 GetDeviceName () const { return m_deviceName; }
76
77 double
78 GetRetryPause () const { return m_retryPause; }
79
80 void
81 SetRetryPause (double pause) { m_retryPause = pause; }
82
83 boost::posix_time::ptime
84 GetNextScheduledRetry () const { return m_nextScheduledRetry; }
85
86 void
87 SetNextScheduledRetry (boost::posix_time::ptime nextScheduledRetry) { m_nextScheduledRetry = nextScheduledRetry; }
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080088
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080089private:
90 void
Alexander Afanasyev50547892013-01-19 22:03:45 -080091 FillPipeline ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080092
93 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070094 OnData (uint64_t seqno, const Ndnx::Name &name, Ndnx::PcoPtr data);
Alexander Afanasyev50547892013-01-19 22:03:45 -080095
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080096 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070097 OnData_Execute (uint64_t seqno, Ndnx::Name name, Ndnx::PcoPtr data);
Alexander Afanasyevddc283c2013-01-28 23:11:20 -080098
99 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700100 OnTimeout (uint64_t seqno, const Ndnx::Name &name, const Ndnx::Closure &closure, Ndnx::Selectors selectors);
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800101
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800102 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700103 OnTimeout_Execute (uint64_t seqno, Ndnx::Name name, Ndnx::Closure closure, Ndnx::Selectors selectors);
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800104
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800105public:
106 boost::intrusive::list_member_hook<> m_managerListHook;
107
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800108private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700109 Ndnx::NdnxWrapperPtr m_ndnx;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800110
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800111 SegmentCallback m_segmentCallback;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800112 OnFetchCompleteCallback m_onFetchComplete;
113 OnFetchFailedCallback m_onFetchFailed;
114
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800115 FinishCallback m_finishCallback;
116
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800117 bool m_active;
Zhenkai Zhu354d46d2013-02-06 13:49:48 -0800118 bool m_timedwait;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800119
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700120 Ndnx::Name m_name;
121 Ndnx::Name m_deviceName;
122 Ndnx::Name m_forwardingHint;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800123
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800124 boost::posix_time::time_duration m_maximumNoActivityPeriod;
125
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800126 int64_t m_minSendSeqNo;
127 int64_t m_maxInOrderRecvSeqNo;
128 std::set<int64_t> m_outOfOrderRecvSeqNo;
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800129 std::set<int64_t> m_inActivePipeline;
Alexander Afanasyev50547892013-01-19 22:03:45 -0800130
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800131 int64_t m_minSeqNo;
132 int64_t m_maxSeqNo;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800133
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800134 uint32_t m_pipeline;
135 uint32_t m_activePipeline;
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700136 double m_rto;
137 double m_maxRto;
138 bool m_slowStart;
139 uint32_t m_threshold;
140 uint32_t m_roundCount;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800141
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800142 boost::posix_time::ptime m_lastPositiveActivity;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800143
144 double m_retryPause; // pause to stop trying to fetch (for fetch-manager)
145 boost::posix_time::ptime m_nextScheduledRetry;
Alexander Afanasyevca77d4e2013-01-28 22:50:05 -0800146
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800147 ExecutorPtr m_executor; // to serialize FillPipeline events
Alexander Afanasyev2ec2c382013-01-29 20:09:00 -0800148
149 boost::mutex m_seqNoMutex;
Yingdi Yuf0b3de32013-07-11 12:59:00 -0700150 boost::mutex m_rtoMutex;
151 boost::mutex m_pipelineMutex;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800152};
153
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800154typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800155
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800156namespace Error {
157struct Fetcher : virtual boost::exception, virtual std::exception { };
158}
159
160typedef boost::shared_ptr<Fetcher> FetcherPtr;
161
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800162bool
163Fetcher::IsActive () const
164{
165 return m_active;
166}
167
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800168
169#endif // FETCHER_H