Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: multi_http_fetcher.h

Issue 3591018: AU: MultiHttpFetcher, an HttpFetcher for specific byte ranges (Closed) Base URL: ssh://git@chromiumos-git/update_engine.git
Patch Set: fixes for rewview Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mock_http_fetcher.h ('k') | test_http_server.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__
6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__
7
8 #include <tr1/memory>
9 #include <utility>
10 #include <vector>
11
12 #include "update_engine/http_fetcher.h"
13
14 // This class is a simple wrapper around an HttpFetcher. The client
15 // specifies a vector of byte ranges. MultiHttpFetcher will fetch bytes
16 // from those offsets. Pass -1 as a length to specify unlimited length.
17 // It really only would make sense for the last range specified to have
18 // unlimited length.
19
20 namespace chromeos_update_engine {
21
22 template<typename BaseHttpFetcher>
23 class MultiHttpFetcher : public HttpFetcher, public HttpFetcherDelegate {
24 public:
25 typedef std::vector<std::pair<off_t, off_t> > RangesVect;
26
27 MultiHttpFetcher()
28 : sent_transfer_complete_(false),
29 current_index_(0),
30 bytes_received_this_fetcher_(0) {}
31 ~MultiHttpFetcher() {}
32
33 void set_ranges(const RangesVect& ranges) {
34 ranges_ = ranges;
35 fetchers_.resize(ranges_.size()); // Allocate the fetchers
36 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher>
37 >::iterator it = fetchers_.begin(), e = fetchers_.end();
38 it != e; ++it) {
39 (*it) = std::tr1::shared_ptr<BaseHttpFetcher>(new BaseHttpFetcher);
40 (*it)->set_delegate(this);
41 }
42 }
43
44 void SetOffset(off_t offset) {} // for now, doesn't support this
45
46 // Begins the transfer to the specified URL.
47 void BeginTransfer(const std::string& url) {
48 url_ = url;
49 if (ranges_.empty()) {
50 if (delegate_)
51 delegate_->TransferComplete(this, true);
52 return;
53 }
54 current_index_ = 0;
55 LOG(INFO) << "starting first transfer";
56 StartTransfer();
57 }
58
59 void TerminateTransfer() {
60 if (current_index_ < fetchers_.size())
61 fetchers_[current_index_]->TerminateTransfer();
62 current_index_ = ranges_.size();
63 sent_transfer_complete_ = true; // a fib
64 }
65
66 void Pause() {
67 if (current_index_ < fetchers_.size())
68 fetchers_[current_index_]->Pause();
69 }
70
71 void Unpause() {
72 if (current_index_ < fetchers_.size())
73 fetchers_[current_index_]->Unpause();
74 }
75
76 // These two function are overloaded in LibcurlHttp fetcher to speed
77 // testing.
78 void set_idle_seconds(int seconds) {
79 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> >::iterator
80 it = fetchers_.begin(),
81 e = fetchers_.end(); it != e; ++it) {
82 (*it)->set_idle_seconds(seconds);
83 }
84 }
85 void set_retry_seconds(int seconds) {
86 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> >::iterator
87 it = fetchers_.begin(),
88 e = fetchers_.end(); it != e; ++it) {
89 (*it)->set_retry_seconds(seconds);
90 }
91 }
92
93 private:
94 void SendTransferComplete(HttpFetcher* fetcher, bool successful) {
95 if (sent_transfer_complete_)
96 return;
97 LOG(INFO) << "Sending transfer complete";
98 sent_transfer_complete_ = true;
99 http_response_code_ = fetcher->http_response_code();
100 if (delegate_)
101 delegate_->TransferComplete(this, successful);
102 }
103
104 void StartTransfer() {
105 if (current_index_ >= ranges_.size()) {
106 return;
107 }
108 LOG(INFO) << "Starting a transfer";
109 bytes_received_this_fetcher_ = 0;
110 fetchers_[current_index_]->SetOffset(ranges_[current_index_].first);
111 fetchers_[current_index_]->BeginTransfer(url_);
112 }
113
114 void ReceivedBytes(HttpFetcher* fetcher,
115 const char* bytes,
116 int length) {
117 if (current_index_ >= ranges_.size())
118 return;
119 if (fetcher != fetchers_[current_index_].get()) {
120 LOG(WARNING) << "Received bytes from invalid fetcher";
121 return;
122 }
123 off_t next_size = length;
124 if (ranges_[current_index_].second >= 0) {
125 next_size = std::min(next_size,
126 ranges_[current_index_].second -
127 bytes_received_this_fetcher_);
128 }
129 LOG_IF(WARNING, next_size <= 0) << "Asked to write length <= 0";
130 if (delegate_)
131 delegate_->ReceivedBytes(this, bytes, next_size);
132 bytes_received_this_fetcher_ += length;
133 if (ranges_[current_index_].second >= 0 &&
134 bytes_received_this_fetcher_ >= ranges_[current_index_].second) {
135 fetchers_[current_index_]->TerminateTransfer();
136 current_index_++;
137 if (current_index_ == ranges_.size()) {
138 SendTransferComplete(fetchers_[current_index_ - 1].get(), true);
139 } else {
140 StartTransfer();
141 }
142 }
143 }
144
145 void TransferComplete(HttpFetcher* fetcher, bool successful) {
146 LOG(INFO) << "Received transfer complete";
147 if (current_index_ >= ranges_.size()) {
148 SendTransferComplete(fetcher, true);
149 return;
150 }
151
152 if (ranges_[current_index_].second < 0) {
153 // We're done with the current operation
154 current_index_++;
155 if (current_index_ >= ranges_.size() || !successful) {
156 SendTransferComplete(fetcher, successful);
157 } else {
158 // Do the next transfer
159 StartTransfer();
160 }
161 return;
162 }
163
164 if (bytes_received_this_fetcher_ < ranges_[current_index_].second) {
165 LOG(WARNING) << "Received insufficient bytes from fetcher. "
166 << "Ending early";
167 SendTransferComplete(fetcher, false);
168 return;
169 } else {
170 LOG(INFO) << "Got spurious TransferComplete. Ingoring.";
171 }
172 }
173
174 // If true, do not send any more data or TransferComplete to the delegate.
175 bool sent_transfer_complete_;
176
177 RangesVect ranges_;
178 std::vector<std::tr1::shared_ptr<BaseHttpFetcher> > fetchers_;
179
180 RangesVect::size_type current_index_; // index into ranges_, fetchers_
181 off_t bytes_received_this_fetcher_;
182
183 private:
184 DISALLOW_COPY_AND_ASSIGN(MultiHttpFetcher);
185 };
186
187 } // namespace chromeos_update_engine
188
189 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__
OLDNEW
« no previous file with comments | « mock_http_fetcher.h ('k') | test_http_server.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698