blob: f20ae44bb661409e0a7bc5f734707411b4cf236a [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 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#include "fetcher.h"
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080023#include "fetch-manager.h"
Alexander Afanasyev66f4c492013-01-20 23:32:50 -080024#include "ccnx-pco.h"
Alexander Afanasyev678f3502013-01-23 17:09:37 -080025#include "logging.h"
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080026
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080027#include <boost/make_shared.hpp>
28#include <boost/ref.hpp>
29#include <boost/throw_exception.hpp>
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080030#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080031
Alexander Afanasyev678f3502013-01-23 17:09:37 -080032INIT_LOGGER ("Fetcher");
33
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080034using namespace boost;
35using namespace std;
Alexander Afanasyev50547892013-01-19 22:03:45 -080036using namespace Ccnx;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080037
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080038Fetcher::Fetcher (Ccnx::CcnxWrapperPtr ccnx,
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080039 ExecutorPtr executor,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080040 const SegmentCallback &segmentCallback,
41 const FinishCallback &finishCallback,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080042 OnFetchCompleteCallback onFetchComplete, OnFetchFailedCallback onFetchFailed,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080043 const Ccnx::Name &deviceName, const Ccnx::Name &name, int64_t minSeqNo, int64_t maxSeqNo,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080044 boost::posix_time::time_duration timeout/* = boost::posix_time::seconds (30)*/,
Alexander Afanasyev49a18522013-01-18 17:49:04 -080045 const Ccnx::Name &forwardingHint/* = Ccnx::Name ()*/)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080046 : m_ccnx (ccnx)
47
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080048 , m_segmentCallback (segmentCallback)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080049 , m_onFetchComplete (onFetchComplete)
50 , m_onFetchFailed (onFetchFailed)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080051 , m_finishCallback (finishCallback)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080052
Alexander Afanasyev83531a42013-01-19 16:21:54 -080053 , m_active (false)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080054 , m_name (name)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080055 , m_deviceName (deviceName)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080056 , m_forwardingHint (forwardingHint)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080057 , m_maximumNoActivityPeriod (timeout)
58
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080059 , m_minSendSeqNo (minSeqNo-1)
60 , m_maxInOrderRecvSeqNo (minSeqNo-1)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080061 , m_minSeqNo (minSeqNo)
62 , m_maxSeqNo (maxSeqNo)
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080063
Alexander Afanasyev49a18522013-01-18 17:49:04 -080064 , m_pipeline (6) // initial "congestion window"
Alexander Afanasyev50547892013-01-19 22:03:45 -080065 , m_activePipeline (0)
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080066 , m_retryPause (0)
Alexander Afanasyevf8b22bf2013-01-26 17:28:11 -080067 , m_nextScheduledRetry (date_time::second_clock<boost::posix_time::ptime>::universal_time ())
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080068 , m_executor (executor) // must be 1
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080069{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080070}
71
72Fetcher::~Fetcher ()
73{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080074}
Alexander Afanasyev83531a42013-01-19 16:21:54 -080075
76void
77Fetcher::RestartPipeline ()
78{
79 m_active = true;
Alexander Afanasyev50547892013-01-19 22:03:45 -080080 m_minSendSeqNo = m_maxInOrderRecvSeqNo;
Alexander Afanasyev650ba282013-01-20 20:14:06 -080081 // cout << "Restart: " << m_minSendSeqNo << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080082 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -080083
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080084 m_executor->execute (bind (&Fetcher::FillPipeline, this));
Alexander Afanasyev50547892013-01-19 22:03:45 -080085}
86
87void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080088Fetcher::SetForwardingHint (const Ccnx::Name &forwardingHint)
89{
90 m_forwardingHint = forwardingHint;
91}
92
93void
Alexander Afanasyev50547892013-01-19 22:03:45 -080094Fetcher::FillPipeline ()
95{
96 for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++)
97 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -080098 if (m_outOfOrderRecvSeqNo.find (m_minSendSeqNo+1) != m_outOfOrderRecvSeqNo.end ())
99 continue;
100
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800101 if (m_inActivePipeline.find (m_minSendSeqNo+1) != m_inActivePipeline.end ())
102 continue;
103
104 m_inActivePipeline.insert (m_minSendSeqNo+1);
105
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800106 _LOG_DEBUG (" >>> i " << Name (m_forwardingHint)(m_name) << ", seq = " << (m_minSendSeqNo + 1 ));
Alexander Afanasyev678f3502013-01-23 17:09:37 -0800107
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800108 // cout << ">>> " << m_minSendSeqNo+1 << endl;
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800109 m_ccnx->sendInterest (Name (m_forwardingHint)(m_name)(m_minSendSeqNo+1),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800110 Closure (bind(&Fetcher::OnData, this, m_minSendSeqNo+1, _1, _2),
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800111 bind(&Fetcher::OnTimeout, this, m_minSendSeqNo+1, _1, _2, _3)),
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800112 Selectors().interestLifetime (1)); // Alex: this lifetime should be changed to RTO
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800113 _LOG_DEBUG (" >>> i ok");
Alexander Afanasyev50547892013-01-19 22:03:45 -0800114
115 m_activePipeline ++;
116 }
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800117}
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800118
119void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800120Fetcher::OnData (uint64_t seqno, const Ccnx::Name &name, PcoPtr data)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800121{
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800122 m_executor->execute (bind (&Fetcher::OnData_Execute, this, seqno, name, data));
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800123}
124
125void
126Fetcher::OnData_Execute (uint64_t seqno, Ccnx::Name name, Ccnx::PcoPtr data)
127{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800128 _LOG_DEBUG (" <<< d " << name.getPartialName (0, name.size () - 1) << ", seq = " << seqno);
129
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800130 if (m_forwardingHint == Name ())
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800131 {
132 // invoke callback
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800133 if (!m_segmentCallback.empty ())
134 {
135 m_segmentCallback (m_deviceName, m_name, seqno, data);
136 }
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800137 // we don't have to tell FetchManager about this
138 }
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800139 else
140 {
141 try {
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800142 PcoPtr pco = make_shared<ParsedContentObject> (*data->contentPtr ());
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800143 if (!m_segmentCallback.empty ())
144 {
145 m_segmentCallback (m_deviceName, m_name, seqno, pco);
146 }
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800147 }
148 catch (MisformedContentObjectException &e)
149 {
150 cerr << "MisformedContentObjectException..." << endl;
151 // no idea what should do...
152 // let's ignore for now
153 }
154 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800155
Alexander Afanasyev50547892013-01-19 22:03:45 -0800156 m_activePipeline --;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800157 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800158
159 ////////////////////////////////////////////////////////////////////////////
160 m_outOfOrderRecvSeqNo.insert (seqno);
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800161 m_inActivePipeline.erase (seqno);
162 _LOG_DEBUG ("Total segments received: " << m_outOfOrderRecvSeqNo.size ());
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800163 set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin ();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800164 for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end ();
165 inOrderSeqNo++)
166 {
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800167 _LOG_TRACE ("Checking " << *inOrderSeqNo << " and " << m_maxInOrderRecvSeqNo+1);
Alexander Afanasyev50547892013-01-19 22:03:45 -0800168 if (*inOrderSeqNo == m_maxInOrderRecvSeqNo+1)
169 {
170 m_maxInOrderRecvSeqNo = *inOrderSeqNo;
171 }
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800172 else if (*inOrderSeqNo < m_maxInOrderRecvSeqNo+1) // not possible anymore, but just in case
173 {
174 continue;
175 }
Alexander Afanasyev50547892013-01-19 22:03:45 -0800176 else
177 break;
178 }
179 m_outOfOrderRecvSeqNo.erase (m_outOfOrderRecvSeqNo.begin (), inOrderSeqNo);
180 ////////////////////////////////////////////////////////////////////////////
181
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800182 _LOG_TRACE ("Max in order received: " << m_maxInOrderRecvSeqNo << ", max seqNo to request: " << m_maxSeqNo);
183
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800184 if (m_maxInOrderRecvSeqNo == m_maxSeqNo)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800185 {
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800186 _LOG_TRACE ("Fetch finished");
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800187 m_active = false;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800188 // invoke callback
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800189 if (!m_finishCallback.empty ())
190 {
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800191 _LOG_TRACE ("Notifying callback");
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800192 m_finishCallback(m_deviceName, m_name);
193 }
194
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800195 // tell FetchManager that we have finish our job
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800196 m_onFetchComplete (*this);
197 }
198 else
199 {
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800200 m_executor->execute (bind (&Fetcher::FillPipeline, this));
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800201 }
202}
203
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800204void
205Fetcher::OnTimeout (uint64_t seqno, const Ccnx::Name &name, const Closure &closure, Selectors selectors)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800206{
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800207 m_executor->execute (bind (&Fetcher::OnTimeout_Execute, this, seqno, name, closure, selectors));
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800208}
209
210void
Alexander Afanasyev67d757c2013-01-28 23:22:12 -0800211Fetcher::OnTimeout_Execute (uint64_t seqno, Ccnx::Name name, Ccnx::Closure closure, Ccnx::Selectors selectors)
Alexander Afanasyevddc283c2013-01-28 23:11:20 -0800212{
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800213 _LOG_DEBUG (" <<< :( timeout " << name.getPartialName (0, name.size () - 1) << ", seq = " << seqno);
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800214
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800215 // cout << "Fetcher::OnTimeout: " << name << endl;
216 // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod
217 // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time()
218 // << ", oldest: " << (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod) << endl;
219
220 if (m_lastPositiveActivity <
221 (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod))
222 {
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800223 m_inActivePipeline.erase (seqno);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800224 m_activePipeline --;
225 if (m_activePipeline == 0)
226 {
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800227 _LOG_DEBUG ("Telling that fetch failed");
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800228 _LOG_DEBUG ("Active pipeline size should be zero: " << m_inActivePipeline.size ());
229
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800230 m_active = false;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800231 m_onFetchFailed (*this);
232 // this is not valid anymore, but we still should be able finish work
233 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800234 }
235 else
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800236 {
Alexander Afanasyevcfd5dfc2013-01-28 22:21:31 -0800237 _LOG_DEBUG ("Asking to reexpress seqno: " << seqno);
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800238 m_ccnx->sendInterest (name, closure, selectors);
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800239 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800240}