blob: 59a3b35a64cb5825a4666c53d8327d95710f10a9 [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 Afanasyevd6c2a902013-01-19 21:24:30 -080025
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080026#include <boost/make_shared.hpp>
27#include <boost/ref.hpp>
28#include <boost/throw_exception.hpp>
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080029#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080030
31using namespace boost;
32using namespace std;
Alexander Afanasyev50547892013-01-19 22:03:45 -080033using namespace Ccnx;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080034
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080035Fetcher::Fetcher (Ccnx::CcnxWrapperPtr ccnx,
36 const SegmentCallback &segmentCallback,
37 const FinishCallback &finishCallback,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080038 OnFetchCompleteCallback onFetchComplete, OnFetchFailedCallback onFetchFailed,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080039 const Ccnx::Name &deviceName, const Ccnx::Name &name, int64_t minSeqNo, int64_t maxSeqNo,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080040 boost::posix_time::time_duration timeout/* = boost::posix_time::seconds (30)*/,
Alexander Afanasyev49a18522013-01-18 17:49:04 -080041 const Ccnx::Name &forwardingHint/* = Ccnx::Name ()*/)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080042 : m_ccnx (ccnx)
43
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080044 , m_segmentCallback (segmentCallback)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080045 , m_onFetchComplete (onFetchComplete)
46 , m_onFetchFailed (onFetchFailed)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080047 , m_finishCallback (finishCallback)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080048
Alexander Afanasyev83531a42013-01-19 16:21:54 -080049 , m_active (false)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080050 , m_name (name)
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080051 , m_deviceName (deviceName)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080052 , m_forwardingHint (forwardingHint)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080053 , m_maximumNoActivityPeriod (timeout)
54
Alexander Afanasyev49a18522013-01-18 17:49:04 -080055 , m_minSendSeqNo (-1)
Alexander Afanasyev50547892013-01-19 22:03:45 -080056 , m_maxInOrderRecvSeqNo (-1)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080057 , m_minSeqNo (minSeqNo)
58 , m_maxSeqNo (maxSeqNo)
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080059
Alexander Afanasyev49a18522013-01-18 17:49:04 -080060 , m_pipeline (6) // initial "congestion window"
Alexander Afanasyev50547892013-01-19 22:03:45 -080061 , m_activePipeline (0)
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080062{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080063}
64
65Fetcher::~Fetcher ()
66{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080067}
Alexander Afanasyev83531a42013-01-19 16:21:54 -080068
69void
70Fetcher::RestartPipeline ()
71{
72 m_active = true;
Alexander Afanasyev50547892013-01-19 22:03:45 -080073 m_minSendSeqNo = m_maxInOrderRecvSeqNo;
Alexander Afanasyev650ba282013-01-20 20:14:06 -080074 // cout << "Restart: " << m_minSendSeqNo << endl;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080075 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -080076
77 FillPipeline ();
78}
79
80void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080081Fetcher::SetForwardingHint (const Ccnx::Name &forwardingHint)
82{
83 m_forwardingHint = forwardingHint;
84}
85
86void
Alexander Afanasyev50547892013-01-19 22:03:45 -080087Fetcher::FillPipeline ()
88{
89 for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++)
90 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -080091 if (m_outOfOrderRecvSeqNo.find (m_minSendSeqNo+1) != m_outOfOrderRecvSeqNo.end ())
92 continue;
93
94 // cout << ">>> " << m_minSendSeqNo+1 << endl;
Alexander Afanasyev66f4c492013-01-20 23:32:50 -080095 m_ccnx->sendInterest (Name (m_forwardingHint)(m_name)(m_minSendSeqNo+1),
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080096 Closure (bind(&Fetcher::OnData, this, m_minSendSeqNo+1, _1, _2),
Alexander Afanasyev650ba282013-01-20 20:14:06 -080097 bind(&Fetcher::OnTimeout, this, m_minSendSeqNo+1, _1)),
98 Selectors().interestLifetime (1)); // Alex: this lifetime should be changed to RTO
Alexander Afanasyev50547892013-01-19 22:03:45 -080099
100 m_activePipeline ++;
101 }
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800102}
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800103
104void
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800105Fetcher::OnData (uint64_t seqno, const Ccnx::Name &name, PcoPtr data)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800106{
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800107 if (m_forwardingHint == Name ())
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800108 {
109 // invoke callback
110 m_segmentCallback (m_deviceName, m_name, seqno, data);
111 // we don't have to tell FetchManager about this
112 }
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800113 else
114 {
115 try {
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800116 PcoPtr pco = make_shared<ParsedContentObject> (*data->contentPtr ());
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800117 m_segmentCallback (m_deviceName, m_name, seqno, pco);
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800118 }
119 catch (MisformedContentObjectException &e)
120 {
121 cerr << "MisformedContentObjectException..." << endl;
122 // no idea what should do...
123 // let's ignore for now
124 }
125 }
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800126
Alexander Afanasyev50547892013-01-19 22:03:45 -0800127 m_activePipeline --;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800128 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800129
130 ////////////////////////////////////////////////////////////////////////////
131 m_outOfOrderRecvSeqNo.insert (seqno);
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800132 set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin ();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800133 for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end ();
134 inOrderSeqNo++)
135 {
136 if (*inOrderSeqNo == m_maxInOrderRecvSeqNo+1)
137 {
138 m_maxInOrderRecvSeqNo = *inOrderSeqNo;
139 }
140 else
141 break;
142 }
143 m_outOfOrderRecvSeqNo.erase (m_outOfOrderRecvSeqNo.begin (), inOrderSeqNo);
144 ////////////////////////////////////////////////////////////////////////////
145
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800146 if (m_maxInOrderRecvSeqNo == m_maxSeqNo)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800147 {
148 m_active = false;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800149 // invoke callback
150 m_finishCallback(m_deviceName, m_name);
151 // tell FetchManager that we have finish our job
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800152 m_onFetchComplete (*this);
153 }
154 else
155 {
156 FillPipeline ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800157 }
158}
159
Alexander Afanasyev50547892013-01-19 22:03:45 -0800160Closure::TimeoutCallbackReturnValue
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -0800161Fetcher::OnTimeout (uint64_t seqno, const Ccnx::Name &name)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800162{
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800163 // cout << "Fetcher::OnTimeout: " << name << endl;
164 // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod
165 // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time()
166 // << ", oldest: " << (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod) << endl;
167
168 if (m_lastPositiveActivity <
169 (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod))
170 {
171 m_activePipeline --;
172 if (m_activePipeline == 0)
173 {
Alexander Afanasyev650ba282013-01-20 20:14:06 -0800174 m_active = false;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800175 m_onFetchFailed (*this);
176 // this is not valid anymore, but we still should be able finish work
177 }
178 return Closure::RESULT_OK;
179 }
180 else
181 return Closure::RESULT_REEXPRESS;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800182}