OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ |
6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ |
7 | 7 |
8 #include <tr1/memory> | 8 #include <tr1/memory> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "update_engine/http_fetcher.h" | 12 #include "update_engine/http_fetcher.h" |
13 #include "update_engine/utils.h" | 13 #include "update_engine/utils.h" |
14 | 14 |
15 // This class is a simple wrapper around an HttpFetcher. The client | 15 // This class is a simple wrapper around an HttpFetcher. The client |
16 // specifies a vector of byte ranges. MultiHttpFetcher will fetch bytes | 16 // specifies a vector of byte ranges. MultiHttpFetcher will fetch bytes |
17 // from those offsets. Pass -1 as a length to specify unlimited length. | 17 // from those offsets. Pass -1 as a length to specify unlimited length. |
18 // It really only would make sense for the last range specified to have | 18 // It really only would make sense for the last range specified to have |
19 // unlimited length. | 19 // unlimited length. |
20 | 20 |
21 namespace chromeos_update_engine { | 21 namespace chromeos_update_engine { |
22 | 22 |
23 template<typename BaseHttpFetcher> | 23 template<typename BaseHttpFetcher> |
petkov
2010/11/19 05:37:15
Longer term: this class might simpler if it didn't
petkov
2010/11/19 17:10:04
In fact, we can probably simplify this to reuse th
adlr
2010/11/20 02:52:29
Yeah, we could probably reuse fetchers, but we'd n
| |
24 class MultiHttpFetcher : public HttpFetcher, public HttpFetcherDelegate { | 24 class MultiHttpFetcher : public HttpFetcher, public HttpFetcherDelegate { |
25 public: | 25 public: |
26 typedef std::vector<std::pair<off_t, off_t> > RangesVect; | 26 typedef std::vector<std::pair<off_t, off_t> > RangesVect; |
27 typedef std::vector<std::tr1::shared_ptr<BaseHttpFetcher> > FetchersVect; | |
27 | 28 |
28 MultiHttpFetcher() | 29 MultiHttpFetcher(ProxyResolver* proxy_resolver) |
29 : sent_transfer_complete_(false), | 30 : HttpFetcher(proxy_resolver), |
31 sent_transfer_complete_(false), | |
30 pending_next_fetcher_(false), | 32 pending_next_fetcher_(false), |
31 current_index_(0), | 33 current_index_(0), |
32 bytes_received_this_fetcher_(0) {} | 34 bytes_received_this_fetcher_(0) {} |
33 ~MultiHttpFetcher() {} | 35 ~MultiHttpFetcher() {} |
34 | 36 |
35 void set_ranges(const RangesVect& ranges) { | 37 void set_ranges(const RangesVect& ranges) { |
36 ranges_ = ranges; | 38 ranges_ = ranges; |
37 fetchers_.resize(ranges_.size()); // Allocate the fetchers | 39 fetchers_.resize(ranges_.size()); // Allocate the fetchers |
38 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> | 40 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> |
39 >::iterator it = fetchers_.begin(), e = fetchers_.end(); | 41 >::iterator it = fetchers_.begin(), e = fetchers_.end(); |
40 it != e; ++it) { | 42 it != e; ++it) { |
41 (*it) = std::tr1::shared_ptr<BaseHttpFetcher>(new BaseHttpFetcher); | 43 (*it) = std::tr1::shared_ptr<BaseHttpFetcher>( |
44 new BaseHttpFetcher(proxy_resolver_)); | |
42 (*it)->set_delegate(this); | 45 (*it)->set_delegate(this); |
46 LOG(INFO) << "list of fetchers: " << (uint64_t)(it->get()); | |
petkov
2010/11/19 05:37:15
do you need this new logging? and below?
adlr
2010/11/20 02:52:29
oops. yeah, when i use c-style casts i'm being qui
| |
43 } | 47 } |
48 LOG(INFO) << "done w/ list"; | |
44 } | 49 } |
45 | 50 |
46 void SetOffset(off_t offset) {} // for now, doesn't support this | 51 void SetOffset(off_t offset) {} // for now, doesn't support this |
47 | 52 |
48 // Begins the transfer to the specified URL. | 53 // Begins the transfer to the specified URL. |
49 void BeginTransfer(const std::string& url) { | 54 void BeginTransfer(const std::string& url) { |
50 url_ = url; | 55 url_ = url; |
51 if (ranges_.empty()) { | 56 if (ranges_.empty()) { |
52 if (delegate_) | 57 if (delegate_) |
53 delegate_->TransferComplete(this, true); | 58 delegate_->TransferComplete(this, true); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 (*it)->SetConnectionAsExpensive(is_expensive); | 132 (*it)->SetConnectionAsExpensive(is_expensive); |
128 } | 133 } |
129 } | 134 } |
130 void SetBuildType(bool is_official) { | 135 void SetBuildType(bool is_official) { |
131 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> >::iterator | 136 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> >::iterator |
132 it = fetchers_.begin(), | 137 it = fetchers_.begin(), |
133 e = fetchers_.end(); it != e; ++it) { | 138 e = fetchers_.end(); it != e; ++it) { |
134 (*it)->SetBuildType(is_official); | 139 (*it)->SetBuildType(is_official); |
135 } | 140 } |
136 } | 141 } |
142 | |
143 virtual void SetProxies(const std::vector<std::string>& proxies) { | |
petkov
2010/11/19 05:37:15
Instead of overloading this method, can't you just
adlr
2010/11/20 02:52:29
We could do that. We would probably also want to d
petkov
2010/11/22 17:49:00
Yeah, we should have a cleanup. And we could renam
| |
144 for (typename FetchersVect::iterator it = fetchers_.begin(), | |
145 e = fetchers_.end(); | |
146 it != e; ++it) { | |
petkov
2010/11/19 05:37:15
move up?
adlr
2010/11/20 02:52:29
Done.
| |
147 (*it)->SetProxies(proxies); | |
148 } | |
149 } | |
137 | 150 |
138 private: | 151 private: |
139 void SendTransferComplete(HttpFetcher* fetcher, bool successful) { | 152 void SendTransferComplete(HttpFetcher* fetcher, bool successful) { |
140 if (sent_transfer_complete_) | 153 if (sent_transfer_complete_) |
141 return; | 154 return; |
142 LOG(INFO) << "Sending transfer complete"; | 155 LOG(INFO) << "Sending transfer complete"; |
143 sent_transfer_complete_ = true; | 156 sent_transfer_complete_ = true; |
144 http_response_code_ = fetcher->http_response_code(); | 157 http_response_code_ = fetcher->http_response_code(); |
145 if (delegate_) | 158 if (delegate_) |
146 delegate_->TransferComplete(this, successful); | 159 delegate_->TransferComplete(this, successful); |
147 } | 160 } |
148 | 161 |
149 void StartTransfer() { | 162 void StartTransfer() { |
150 if (current_index_ >= ranges_.size()) { | 163 if (current_index_ >= ranges_.size()) { |
151 return; | 164 return; |
152 } | 165 } |
153 LOG(INFO) << "Starting a transfer @" << ranges_[current_index_].first << "(" | 166 LOG(INFO) << "Starting a transfer @" << ranges_[current_index_].first << "(" |
154 << ranges_[current_index_].second << ")"; | 167 << ranges_[current_index_].second << ")"; |
155 bytes_received_this_fetcher_ = 0; | 168 bytes_received_this_fetcher_ = 0; |
156 fetchers_[current_index_]->SetOffset(ranges_[current_index_].first); | 169 fetchers_[current_index_]->SetOffset(ranges_[current_index_].first); |
157 if (delegate_) | 170 if (delegate_) |
158 delegate_->SeekToOffset(ranges_[current_index_].first); | 171 delegate_->SeekToOffset(ranges_[current_index_].first); |
159 fetchers_[current_index_]->BeginTransfer(url_); | 172 fetchers_[current_index_]->BeginTransfer(url_); |
160 } | 173 } |
161 | 174 |
162 void ReceivedBytes(HttpFetcher* fetcher, const char* bytes, int length) { | 175 void ReceivedBytes(HttpFetcher* fetcher, const char* bytes, int length) { |
163 TEST_AND_RETURN(current_index_ < ranges_.size()); | 176 TEST_AND_RETURN(current_index_ < ranges_.size()); |
164 TEST_AND_RETURN(fetcher == fetchers_[current_index_].get()); | 177 TEST_AND_RETURN(fetcher == fetchers_[current_index_].get()); |
165 TEST_AND_RETURN(!pending_next_fetcher_); | 178 TEST_AND_RETURN(!pending_next_fetcher_); |
179 if (fetcher != fetchers_[current_index_].get()) { | |
petkov
2010/11/19 05:37:15
When is this true given that there's a TEST_AND_RE
adlr
2010/11/20 02:52:29
i think this may have been an issue w/ a merge. re
| |
180 LOG(INFO) << "current index: " << current_index_; | |
181 LOG(WARNING) << "Received bytes from invalid fetcher"; | |
182 LOG(WARNING) << (uint64_t)(fetcher) << " exp " << (uint64_t)(fetchers_[cur rent_index_].get()); | |
183 return; | |
184 } | |
166 off_t next_size = length; | 185 off_t next_size = length; |
167 if (ranges_[current_index_].second >= 0) { | 186 if (ranges_[current_index_].second >= 0) { |
168 next_size = std::min(next_size, | 187 next_size = std::min(next_size, |
169 ranges_[current_index_].second - | 188 ranges_[current_index_].second - |
170 bytes_received_this_fetcher_); | 189 bytes_received_this_fetcher_); |
171 } | 190 } |
172 LOG_IF(WARNING, next_size <= 0) << "Asked to write length <= 0"; | 191 LOG_IF(WARNING, next_size <= 0) << "Asked to write length <= 0"; |
173 if (delegate_) { | 192 if (delegate_) { |
174 delegate_->ReceivedBytes(this, bytes, next_size); | 193 delegate_->ReceivedBytes(this, bytes, next_size); |
175 } | 194 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 } | 232 } |
214 | 233 |
215 // If true, do not send any more data or TransferComplete to the delegate. | 234 // If true, do not send any more data or TransferComplete to the delegate. |
216 bool sent_transfer_complete_; | 235 bool sent_transfer_complete_; |
217 | 236 |
218 // If true, the next fetcher needs to be started when TransferTerminated is | 237 // If true, the next fetcher needs to be started when TransferTerminated is |
219 // received from the current fetcher. | 238 // received from the current fetcher. |
220 bool pending_next_fetcher_; | 239 bool pending_next_fetcher_; |
221 | 240 |
222 RangesVect ranges_; | 241 RangesVect ranges_; |
223 std::vector<std::tr1::shared_ptr<BaseHttpFetcher> > fetchers_; | 242 FetchersVect fetchers_; |
224 | 243 |
225 RangesVect::size_type current_index_; // index into ranges_, fetchers_ | 244 RangesVect::size_type current_index_; // index into ranges_, fetchers_ |
226 off_t bytes_received_this_fetcher_; | 245 off_t bytes_received_this_fetcher_; |
227 | 246 |
228 DISALLOW_COPY_AND_ASSIGN(MultiHttpFetcher); | 247 DISALLOW_COPY_AND_ASSIGN(MultiHttpFetcher); |
229 }; | 248 }; |
230 | 249 |
231 } // namespace chromeos_update_engine | 250 } // namespace chromeos_update_engine |
232 | 251 |
233 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ | 252 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ |
OLD | NEW |