Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 23 | #include "fetch-manager.h" |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 24 | #include "ccnx-pco.h" |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 26 | #include <boost/make_shared.hpp> |
| 27 | #include <boost/ref.hpp> |
| 28 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 29 | #include <boost/date_time/posix_time/posix_time.hpp> |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 30 | |
| 31 | using namespace boost; |
| 32 | using namespace std; |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 33 | using namespace Ccnx; |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 35 | Fetcher::Fetcher (CcnxWrapperPtr ccnx, |
| 36 | OnDataSegmentCallback onDataSegment, |
| 37 | OnFetchCompleteCallback onFetchComplete, OnFetchFailedCallback onFetchFailed, |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 38 | const Ccnx::Name &name, int32_t minSeqNo, int32_t maxSeqNo, |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 39 | boost::posix_time::time_duration timeout/* = boost::posix_time::seconds (30)*/, |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 40 | const Ccnx::Name &forwardingHint/* = Ccnx::Name ()*/) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 41 | : m_ccnx (ccnx) |
| 42 | |
| 43 | , m_onDataSegment (onDataSegment) |
| 44 | , m_onFetchComplete (onFetchComplete) |
| 45 | , m_onFetchFailed (onFetchFailed) |
| 46 | |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 47 | , m_active (false) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 48 | , m_name (name) |
| 49 | , m_forwardingHint (forwardingHint) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 50 | , m_maximumNoActivityPeriod (timeout) |
| 51 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 52 | , m_minSendSeqNo (-1) |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 53 | , m_maxInOrderRecvSeqNo (-1) |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 54 | , m_minSeqNo (minSeqNo) |
| 55 | , m_maxSeqNo (maxSeqNo) |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | 49a1852 | 2013-01-18 17:49:04 -0800 | [diff] [blame] | 57 | , m_pipeline (6) // initial "congestion window" |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 58 | , m_activePipeline (0) |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 59 | { |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | Fetcher::~Fetcher () |
| 63 | { |
Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 64 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 65 | |
| 66 | void |
| 67 | Fetcher::RestartPipeline () |
| 68 | { |
| 69 | m_active = true; |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 70 | m_minSendSeqNo = m_maxInOrderRecvSeqNo; |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 71 | // cout << "Restart: " << m_minSendSeqNo << endl; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 72 | m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time(); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 73 | |
| 74 | FillPipeline (); |
| 75 | } |
| 76 | |
| 77 | void |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 78 | Fetcher::SetForwardingHint (const Ccnx::Name &forwardingHint) |
| 79 | { |
| 80 | m_forwardingHint = forwardingHint; |
| 81 | } |
| 82 | |
| 83 | void |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 84 | Fetcher::FillPipeline () |
| 85 | { |
| 86 | for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++) |
| 87 | { |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 88 | if (m_outOfOrderRecvSeqNo.find (m_minSendSeqNo+1) != m_outOfOrderRecvSeqNo.end ()) |
| 89 | continue; |
| 90 | |
| 91 | // cout << ">>> " << m_minSendSeqNo+1 << endl; |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 92 | m_ccnx->sendInterest (Name (m_forwardingHint)(m_name)(m_minSendSeqNo+1), |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 93 | Closure (bind(&Fetcher::OnData, this, m_minSendSeqNo+1, _1, _2), |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 94 | bind(&Fetcher::OnTimeout, this, m_minSendSeqNo+1, _1)), |
| 95 | Selectors().interestLifetime (1)); // Alex: this lifetime should be changed to RTO |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 96 | |
| 97 | m_activePipeline ++; |
| 98 | } |
Alexander Afanasyev | 83531a4 | 2013-01-19 16:21:54 -0800 | [diff] [blame] | 99 | } |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 100 | |
| 101 | void |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 102 | Fetcher::OnData (uint32_t seqno, const Ccnx::Name &name, const Ccnx::Bytes &content) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 103 | { |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 104 | if (m_forwardingHint == Name ()) |
| 105 | m_onDataSegment (*this, seqno, m_name, name, content); |
| 106 | else |
| 107 | { |
| 108 | try { |
| 109 | ParsedContentObject pco (content); |
| 110 | m_onDataSegment (*this, seqno, m_name, pco.name (), pco.content ()); |
| 111 | } |
| 112 | catch (MisformedContentObjectException &e) |
| 113 | { |
| 114 | cerr << "MisformedContentObjectException..." << endl; |
| 115 | // no idea what should do... |
| 116 | // let's ignore for now |
| 117 | } |
| 118 | } |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 120 | m_activePipeline --; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 121 | m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time(); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 122 | |
| 123 | //////////////////////////////////////////////////////////////////////////// |
| 124 | m_outOfOrderRecvSeqNo.insert (seqno); |
| 125 | set<int32_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin (); |
| 126 | for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end (); |
| 127 | inOrderSeqNo++) |
| 128 | { |
| 129 | if (*inOrderSeqNo == m_maxInOrderRecvSeqNo+1) |
| 130 | { |
| 131 | m_maxInOrderRecvSeqNo = *inOrderSeqNo; |
| 132 | } |
| 133 | else |
| 134 | break; |
| 135 | } |
| 136 | m_outOfOrderRecvSeqNo.erase (m_outOfOrderRecvSeqNo.begin (), inOrderSeqNo); |
| 137 | //////////////////////////////////////////////////////////////////////////// |
| 138 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 139 | if (m_maxInOrderRecvSeqNo == m_maxSeqNo) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 140 | { |
| 141 | m_active = false; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 142 | m_onFetchComplete (*this); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | FillPipeline (); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 150 | Closure::TimeoutCallbackReturnValue |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 151 | Fetcher::OnTimeout (uint32_t seqno, const Ccnx::Name &name) |
| 152 | { |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 153 | // cout << "Fetcher::OnTimeout: " << name << endl; |
| 154 | // cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod |
| 155 | // << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time() |
| 156 | // << ", oldest: " << (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod) << endl; |
| 157 | |
| 158 | if (m_lastPositiveActivity < |
| 159 | (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod)) |
| 160 | { |
| 161 | m_activePipeline --; |
| 162 | if (m_activePipeline == 0) |
| 163 | { |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 164 | m_active = false; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 165 | m_onFetchFailed (*this); |
| 166 | // this is not valid anymore, but we still should be able finish work |
| 167 | } |
| 168 | return Closure::RESULT_OK; |
| 169 | } |
| 170 | else |
| 171 | return Closure::RESULT_REEXPRESS; |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 172 | } |