blob: 185e2a19f4dfb373b0101146416ea54eff371790 [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 Afanasyev49a18522013-01-18 17:49:04 -080025#include "ccnx-wrapper.h"
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080026#include "ccnx-name.h"
27
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 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:
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080039 typedef boost::function<void(Ccnx::Name &deviceName, Ccnx::Name &baseName, uint64_t seq, Ccnx::PcoPtr pco)> SegmentCallback;
40 typedef boost::function<void(Ccnx::Name &deviceName, Ccnx::Name &baseName)> FinishCallback;
Zhenkai Zhuda686882013-01-29 22:32:24 -080041 typedef boost::function<void (Fetcher &, const Ccnx::Name &deviceName, const Ccnx::Name &baseName)> OnFetchCompleteCallback;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080042 typedef boost::function<void (Fetcher &)> OnFetchFailedCallback;
43
44 Fetcher (Ccnx::CcnxWrapperPtr ccnx,
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
49 const Ccnx::Name &deviceName, const Ccnx::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 Afanasyev49a18522013-01-18 17:49:04 -080052 const Ccnx::Name &forwardingHint = Ccnx::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 Afanasyev21a166e2013-01-20 16:04:41 -080065 SetForwardingHint (const Ccnx::Name &forwardingHint);
66
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -080067 const Ccnx::Name &
68 GetForwardingHint () const { return m_forwardingHint; }
69
70 const Ccnx::Name &
71 GetName () const { return m_name; }
72
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080073 const Ccnx::Name &
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 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
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080093 OnData (uint64_t seqno, const Ccnx::Name &name, Ccnx::PcoPtr data);
Alexander Afanasyev50547892013-01-19 22:03:45 -080094
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080095 void
Alexander Afanasyevddc283c2013-01-28 23:11:20 -080096 OnData_Execute (uint64_t seqno, Ccnx::Name name, Ccnx::PcoPtr data);
97
98 void
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080099 OnTimeout (uint64_t seqno, const Ccnx::Name &name, const Ccnx::Closure &closure, Ccnx::Selectors selectors);
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800100
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800101 void
Alexander Afanasyev67d757c2013-01-28 23:22:12 -0800102 OnTimeout_Execute (uint64_t seqno, Ccnx::Name name, Ccnx::Closure closure, Ccnx::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 Afanasyev21a166e2013-01-20 16:04:41 -0800108 Ccnx::CcnxWrapperPtr m_ccnx;
109
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 Afanasyev49a18522013-01-18 17:49:04 -0800119 Ccnx::Name m_name;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800120 Ccnx::Name m_deviceName;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800121 Ccnx::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;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800135
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800136 boost::posix_time::ptime m_lastPositiveActivity;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800137
138 double m_retryPause; // pause to stop trying to fetch (for fetch-manager)
139 boost::posix_time::ptime m_nextScheduledRetry;
Alexander Afanasyevca77d4e2013-01-28 22:50:05 -0800140
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800141 ExecutorPtr m_executor; // to serialize FillPipeline events
Alexander Afanasyev2ec2c382013-01-29 20:09:00 -0800142
143 boost::mutex m_seqNoMutex;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800144};
145
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800146typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800147
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800148namespace Error {
149struct Fetcher : virtual boost::exception, virtual std::exception { };
150}
151
152typedef boost::shared_ptr<Fetcher> FetcherPtr;
153
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800154bool
155Fetcher::IsActive () const
156{
157 return m_active;
158}
159
Alexander Afanasyevdfe58192013-01-17 17:34:04 -0800160
161#endif // FETCHER_H