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