blob: e9dec91fcb64c9736573ea0061c22fec76ff214b [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 Afanasyevdfe58192013-01-17 17:34:04 -080065{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080066}
67
68Fetcher::~Fetcher ()
69{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080070}
Alexander Afanasyev83531a42013-01-19 16:21:54 -080071
72void
73Fetcher::RestartPipeline ()
74{
75 m_active = true;
Alexander Afanasyev50547892013-01-19 22:03:45 -080076 m_minSendSeqNo = m_maxInOrderRecvSeqNo;
Alexander Afanasyev650ba282013-01-20 20:14:06 -080077 // cout << "Restart: " << m_minSendSeqNo << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080078 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -080079
80 FillPipeline ();
81}
82
83void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080084Fetcher::SetForwardingHint (const Ccnx::Name &forwardingHint)
85{
86 m_forwardingHint = forwardingHint;
87}
88
89void
Alexander Afanasyev50547892013-01-19 22:03:45 -080090Fetcher::FillPipeline ()
91{
92 for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++)
93 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -080094 if (m_outOfOrderRecvSeqNo.find (m_minSendSeqNo+1) != m_outOfOrderRecvSeqNo.end ())
95 continue;
96
Alexander Afanasyevfc720362013-01-24 21:49:48 -080097 _LOG_DEBUG (" >>> i " << Name (m_forwardingHint)(m_name) << ", seq = " << (m_minSendSeqNo + 1 ));
Alexander Afanasyev678f3502013-01-23 17:09:37 -080098
Alexander Afanasyev650ba282013-01-20 20:14:06 -080099 // cout << ">>> " << m_minSendSeqNo+1 << endl;
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800100 m_ccnx->sendInterest (Name (m_forwardingHint)(m_name)(m_minSendSeqNo+1),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800101 Closure (bind(&Fetcher::OnData, this, m_minSendSeqNo+1, _1, _2),
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800102 bind(&Fetcher::OnTimeout, this, m_minSendSeqNo+1, _1)),
103 Selectors().interestLifetime (1)); // Alex: this lifetime should be changed to RTO
Alexander Afanasyev50547892013-01-19 22:03:45 -0800104
105 m_activePipeline ++;
106 }
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800107}
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800108
109void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800110Fetcher::OnData (uint64_t seqno, const Ccnx::Name &name, PcoPtr data)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800111{
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800112 _LOG_DEBUG (" <<< d " << name.getPartialName (0, name.size () - 1) << ", seq = " << seqno);
113
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800114 if (m_forwardingHint == Name ())
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800115 {
116 // invoke callback
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800117 if (!m_segmentCallback.empty ())
118 {
119 m_segmentCallback (m_deviceName, m_name, seqno, data);
120 }
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800121 // we don't have to tell FetchManager about this
122 }
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800123 else
124 {
125 try {
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800126 PcoPtr pco = make_shared<ParsedContentObject> (*data->contentPtr ());
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800127 if (!m_segmentCallback.empty ())
128 {
129 m_segmentCallback (m_deviceName, m_name, seqno, pco);
130 }
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800131 }
132 catch (MisformedContentObjectException &e)
133 {
134 cerr << "MisformedContentObjectException..." << endl;
135 // no idea what should do...
136 // let's ignore for now
137 }
138 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800139
Alexander Afanasyev50547892013-01-19 22:03:45 -0800140 m_activePipeline --;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800141 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800142
143 ////////////////////////////////////////////////////////////////////////////
144 m_outOfOrderRecvSeqNo.insert (seqno);
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800145 set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin ();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800146 for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end ();
147 inOrderSeqNo++)
148 {
149 if (*inOrderSeqNo == m_maxInOrderRecvSeqNo+1)
150 {
151 m_maxInOrderRecvSeqNo = *inOrderSeqNo;
152 }
153 else
154 break;
155 }
156 m_outOfOrderRecvSeqNo.erase (m_outOfOrderRecvSeqNo.begin (), inOrderSeqNo);
157 ////////////////////////////////////////////////////////////////////////////
158
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800159 if (m_maxInOrderRecvSeqNo == m_maxSeqNo)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800160 {
161 m_active = false;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800162 // invoke callback
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800163 if (!m_finishCallback.empty ())
164 {
165 m_finishCallback(m_deviceName, m_name);
166 }
167
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800168 // tell FetchManager that we have finish our job
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800169 m_onFetchComplete (*this);
170 }
171 else
172 {
173 FillPipeline ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800174 }
175}
176
Alexander Afanasyev50547892013-01-19 22:03:45 -0800177Closure::TimeoutCallbackReturnValue
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800178Fetcher::OnTimeout (uint64_t seqno, const Ccnx::Name &name)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800179{
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800180 // cout << "Fetcher::OnTimeout: " << name << endl;
181 // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod
182 // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time()
183 // << ", oldest: " << (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod) << endl;
184
185 if (m_lastPositiveActivity <
186 (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod))
187 {
188 m_activePipeline --;
189 if (m_activePipeline == 0)
190 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800191 m_active = false;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800192 m_onFetchFailed (*this);
193 // this is not valid anymore, but we still should be able finish work
194 }
195 return Closure::RESULT_OK;
196 }
197 else
198 return Closure::RESULT_REEXPRESS;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800199}