blob: 876cab467ca8853f32cc45c0a6e3a141210286f7 [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"
24
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080025#include <boost/make_shared.hpp>
26#include <boost/ref.hpp>
27#include <boost/throw_exception.hpp>
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080028#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080029
30using namespace boost;
31using namespace std;
Alexander Afanasyev50547892013-01-19 22:03:45 -080032using namespace Ccnx;
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080033
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080034Fetcher::Fetcher (CcnxWrapperPtr ccnx,
35 OnDataSegmentCallback onDataSegment,
36 OnFetchCompleteCallback onFetchComplete, OnFetchFailedCallback onFetchFailed,
Alexander Afanasyev49a18522013-01-18 17:49:04 -080037 const Ccnx::Name &name, int32_t minSeqNo, int32_t maxSeqNo,
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080038 boost::posix_time::time_duration timeout/* = boost::posix_time::seconds (30)*/,
Alexander Afanasyev49a18522013-01-18 17:49:04 -080039 const Ccnx::Name &forwardingHint/* = Ccnx::Name ()*/)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080040 : m_ccnx (ccnx)
41
42 , m_onDataSegment (onDataSegment)
43 , m_onFetchComplete (onFetchComplete)
44 , m_onFetchFailed (onFetchFailed)
45
Alexander Afanasyev83531a42013-01-19 16:21:54 -080046 , m_active (false)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080047 , m_name (name)
48 , m_forwardingHint (forwardingHint)
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080049 , m_maximumNoActivityPeriod (timeout)
50
Alexander Afanasyev49a18522013-01-18 17:49:04 -080051 , m_minSendSeqNo (-1)
Alexander Afanasyev50547892013-01-19 22:03:45 -080052 , m_maxInOrderRecvSeqNo (-1)
Alexander Afanasyev49a18522013-01-18 17:49:04 -080053 , m_minSeqNo (minSeqNo)
54 , m_maxSeqNo (maxSeqNo)
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080055
Alexander Afanasyev49a18522013-01-18 17:49:04 -080056 , m_pipeline (6) // initial "congestion window"
Alexander Afanasyev50547892013-01-19 22:03:45 -080057 , m_activePipeline (0)
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080058{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080059}
60
61Fetcher::~Fetcher ()
62{
Alexander Afanasyevdfe58192013-01-17 17:34:04 -080063}
Alexander Afanasyev83531a42013-01-19 16:21:54 -080064
65void
66Fetcher::RestartPipeline ()
67{
68 m_active = true;
Alexander Afanasyev50547892013-01-19 22:03:45 -080069 m_minSendSeqNo = m_maxInOrderRecvSeqNo;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080070 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -080071
72 FillPipeline ();
73}
74
75void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080076Fetcher::SetForwardingHint (const Ccnx::Name &forwardingHint)
77{
78 m_forwardingHint = forwardingHint;
79}
80
81void
Alexander Afanasyev50547892013-01-19 22:03:45 -080082Fetcher::FillPipeline ()
83{
84 for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++)
85 {
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080086 m_ccnx->sendInterest (Name (m_name)(m_minSendSeqNo+1),
87 Closure (bind(&Fetcher::OnData, this, m_minSendSeqNo+1, _1, _2),
88 bind(&Fetcher::OnTimeout, this, m_minSendSeqNo+1, _1)));
Alexander Afanasyev50547892013-01-19 22:03:45 -080089
90 m_activePipeline ++;
91 }
Alexander Afanasyev83531a42013-01-19 16:21:54 -080092}
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080093
94void
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080095Fetcher::OnData (uint32_t seqno, const Ccnx::Name &name, const Ccnx::Bytes &content)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080096{
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080097 m_onDataSegment (*this, seqno, m_name, name, content);
98
Alexander Afanasyev50547892013-01-19 22:03:45 -080099 m_activePipeline --;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800100 m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
Alexander Afanasyev50547892013-01-19 22:03:45 -0800101
102 ////////////////////////////////////////////////////////////////////////////
103 m_outOfOrderRecvSeqNo.insert (seqno);
104 set<int32_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin ();
105 for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end ();
106 inOrderSeqNo++)
107 {
108 if (*inOrderSeqNo == m_maxInOrderRecvSeqNo+1)
109 {
110 m_maxInOrderRecvSeqNo = *inOrderSeqNo;
111 }
112 else
113 break;
114 }
115 m_outOfOrderRecvSeqNo.erase (m_outOfOrderRecvSeqNo.begin (), inOrderSeqNo);
116 ////////////////////////////////////////////////////////////////////////////
117
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800118 if (m_maxInOrderRecvSeqNo == m_maxSeqNo)
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800119 {
120 m_active = false;
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800121 m_onFetchComplete (*this);
122 }
123 else
124 {
125 FillPipeline ();
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800126 }
127}
128
Alexander Afanasyev50547892013-01-19 22:03:45 -0800129Closure::TimeoutCallbackReturnValue
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800130Fetcher::OnTimeout (uint32_t seqno, const Ccnx::Name &name)
131{
Alexander Afanasyev21a166e2013-01-20 16:04:41 -0800132 // cout << "Fetcher::OnTimeout: " << name << endl;
133 // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod
134 // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time()
135 // << ", oldest: " << (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod) << endl;
136
137 if (m_lastPositiveActivity <
138 (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod))
139 {
140 m_activePipeline --;
141 if (m_activePipeline == 0)
142 {
143 m_onFetchFailed (*this);
144 // this is not valid anymore, but we still should be able finish work
145 }
146 return Closure::RESULT_OK;
147 }
148 else
149 return Closure::RESULT_REEXPRESS;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800150}