blob: a67f76ae6a3d7d7187a0b0cc480a4076fd13b1f7 [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,
39 const SegmentCallback &segmentCallback,
40 const FinishCallback &finishCallback,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080041 OnFetchCompleteCallback onFetchComplete, OnFetchFailedCallback onFetchFailed,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080042 const Ccnx::Name &deviceName, const Ccnx::Name &name, int64_t minSeqNo, int64_t maxSeqNo,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080043 boost::posix_time::time_duration timeout/* = boost::posix_time::seconds (30)*/,
Alexander Afanasyev49a18522013-01-18 17:49:04 -080044 const Ccnx::Name &forwardingHint/* = Ccnx::Name ()*/)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080045 : m_ccnx (ccnx)
46
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080047 , m_segmentCallback (segmentCallback)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080048 , m_onFetchComplete (onFetchComplete)
49 , m_onFetchFailed (onFetchFailed)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080050 , m_finishCallback (finishCallback)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080051
Alexander Afanasyev83531a42013-01-19 16:21:54 -080052 , m_active (false)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080053 , m_name (name)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080054 , m_deviceName (deviceName)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080055 , m_forwardingHint (forwardingHint)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080056 , m_maximumNoActivityPeriod (timeout)
57
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080058 , m_minSendSeqNo (minSeqNo-1)
59 , m_maxInOrderRecvSeqNo (minSeqNo-1)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080060 , m_minSeqNo (minSeqNo)
61 , m_maxSeqNo (maxSeqNo)
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080062
Alexander Afanasyev49a18522013-01-18 17:49:04 -080063 , m_pipeline (6) // initial "congestion window"
Alexander Afanasyev50547892013-01-19 22:03:45 -080064 , m_activePipeline (0)
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080065 , m_retryPause (0)
Alexander Afanasyevf8b22bf2013-01-26 17:28:11 -080066 , m_nextScheduledRetry (date_time::second_clock<boost::posix_time::ptime>::universal_time ())
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080067{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080068}
69
70Fetcher::~Fetcher ()
71{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080072}
Alexander Afanasyev83531a42013-01-19 16:21:54 -080073
74void
75Fetcher::RestartPipeline ()
76{
77 m_active = true;
Alexander Afanasyev50547892013-01-19 22:03:45 -080078 m_minSendSeqNo = m_maxInOrderRecvSeqNo;
Alexander Afanasyev650ba282013-01-20 20:14:06 -080079 // cout << "Restart: " << m_minSendSeqNo << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080080 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -080081
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080082 // Scheduler::scheduleOneTimeTask ();
83 // m_scheduler
84 // m_executor.execute (bind (&Fetcher::FillPipeline, this));
85 FillPipeline ();
Alexander Afanasyev50547892013-01-19 22:03:45 -080086}
87
88void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080089Fetcher::SetForwardingHint (const Ccnx::Name &forwardingHint)
90{
91 m_forwardingHint = forwardingHint;
92}
93
94void
Alexander Afanasyev50547892013-01-19 22:03:45 -080095Fetcher::FillPipeline ()
96{
97 for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++)
98 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -080099 if (m_outOfOrderRecvSeqNo.find (m_minSendSeqNo+1) != m_outOfOrderRecvSeqNo.end ())
100 continue;
101
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800102 _LOG_DEBUG (" >>> i " << Name (m_forwardingHint)(m_name) << ", seq = " << (m_minSendSeqNo + 1 ));
Alexander Afanasyev678f3502013-01-23 17:09:37 -0800103
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800104 // cout << ">>> " << m_minSendSeqNo+1 << endl;
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800105 m_ccnx->sendInterest (Name (m_forwardingHint)(m_name)(m_minSendSeqNo+1),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800106 Closure (bind(&Fetcher::OnData, this, m_minSendSeqNo+1, _1, _2),
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800107 bind(&Fetcher::OnTimeout, this, m_minSendSeqNo+1, _1)),
108 Selectors().interestLifetime (1)); // Alex: this lifetime should be changed to RTO
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800109 _LOG_DEBUG (" >>> i ok");
Alexander Afanasyev50547892013-01-19 22:03:45 -0800110
111 m_activePipeline ++;
112 }
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800113}
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800114
115void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800116Fetcher::OnData (uint64_t seqno, const Ccnx::Name &name, PcoPtr data)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800117{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800118 _LOG_DEBUG (" <<< d " << name.getPartialName (0, name.size () - 1) << ", seq = " << seqno);
119
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800120 if (m_forwardingHint == Name ())
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800121 {
122 // invoke callback
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800123 if (!m_segmentCallback.empty ())
124 {
125 m_segmentCallback (m_deviceName, m_name, seqno, data);
126 }
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800127 // we don't have to tell FetchManager about this
128 }
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800129 else
130 {
131 try {
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800132 PcoPtr pco = make_shared<ParsedContentObject> (*data->contentPtr ());
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800133 if (!m_segmentCallback.empty ())
134 {
135 m_segmentCallback (m_deviceName, m_name, seqno, pco);
136 }
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800137 }
138 catch (MisformedContentObjectException &e)
139 {
140 cerr << "MisformedContentObjectException..." << endl;
141 // no idea what should do...
142 // let's ignore for now
143 }
144 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800145
Alexander Afanasyev50547892013-01-19 22:03:45 -0800146 m_activePipeline --;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800147 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800148
149 ////////////////////////////////////////////////////////////////////////////
150 m_outOfOrderRecvSeqNo.insert (seqno);
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800151 set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin ();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800152 for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end ();
153 inOrderSeqNo++)
154 {
155 if (*inOrderSeqNo == m_maxInOrderRecvSeqNo+1)
156 {
157 m_maxInOrderRecvSeqNo = *inOrderSeqNo;
158 }
159 else
160 break;
161 }
162 m_outOfOrderRecvSeqNo.erase (m_outOfOrderRecvSeqNo.begin (), inOrderSeqNo);
163 ////////////////////////////////////////////////////////////////////////////
164
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800165 if (m_maxInOrderRecvSeqNo == m_maxSeqNo)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800166 {
167 m_active = false;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800168 // invoke callback
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800169 if (!m_finishCallback.empty ())
170 {
171 m_finishCallback(m_deviceName, m_name);
172 }
173
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800174 // tell FetchManager that we have finish our job
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800175 m_onFetchComplete (*this);
176 }
177 else
178 {
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800179 FillPipeline ();
180 // m_executor.execute (bind (&Fetcher::FillPipeline, this));
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800181 }
182}
183
Alexander Afanasyev50547892013-01-19 22:03:45 -0800184Closure::TimeoutCallbackReturnValue
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800185Fetcher::OnTimeout (uint64_t seqno, const Ccnx::Name &name)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800186{
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800187 _LOG_DEBUG (" <<< :( timeout " << name.getPartialName (0, name.size () - 1) << ", seq = " << seqno);
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800188
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800189 // cout << "Fetcher::OnTimeout: " << name << endl;
190 // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod
191 // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time()
192 // << ", oldest: " << (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod) << endl;
193
194 if (m_lastPositiveActivity <
195 (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod))
196 {
197 m_activePipeline --;
198 if (m_activePipeline == 0)
199 {
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800200 _LOG_DEBUG ("Telling that fetch failed");
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800201 m_active = false;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800202 m_onFetchFailed (*this);
203 // this is not valid anymore, but we still should be able finish work
204 }
205 return Closure::RESULT_OK;
206 }
207 else
Alexander Afanasyevff8d9dc2013-01-26 00:45:08 -0800208 {
209 _LOG_DEBUG ("Asking to reexpress");
210 return Closure::RESULT_REEXPRESS;
211 }
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800212}