OLD | NEW |
---|---|
(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_RANGE_HTTP_FETCHER_H__ | |
6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_RANGE_HTTP_FETCHER_H__ | |
7 | |
8 #include <deque> | |
9 #include <tr1/memory> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include <base/scoped_ptr.h> | |
14 | |
15 #include "update_engine/http_fetcher.h" | |
16 #include "update_engine/utils.h" | |
petkov
2010/12/16 18:30:57
do you need this include here?
adlr
2010/12/17 01:03:51
nope. cleaned up the headers
| |
17 | |
18 // This class is a simple wrapper around an HttpFetcher. The client | |
19 // specifies a vector of byte ranges. MultiRangeHTTPFetcher will fetch bytes | |
20 // from those offsets. Pass -1 as a length to specify unlimited length. | |
21 // It really only would make sense for the last range specified to have | |
22 // unlimited length, tho it is legal for other entries to have unlimited | |
23 // length. | |
24 | |
25 // There are three states a MultiRangeHTTPFetcher object will be in: | |
26 // - Stopped (start state) | |
27 // - Downloading | |
28 // - Pending transfer ended | |
29 // Various functions below that might change state indicate possible | |
30 // state changes. | |
31 | |
32 namespace chromeos_update_engine { | |
33 | |
34 class MultiRangeHTTPFetcher : public HttpFetcher, public HttpFetcherDelegate { | |
35 public: | |
36 typedef std::vector<std::pair<off_t, off_t> > RangesVect; | |
37 | |
38 // Takes ownership of the passed in fetcher. | |
39 explicit MultiRangeHTTPFetcher(HttpFetcher* base_fetcher) | |
40 : HttpFetcher(base_fetcher->proxy_resolver()), | |
41 base_fetcher_(base_fetcher), | |
42 base_fetcher_active_(false), | |
43 pending_transfer_ended_(false), | |
44 terminating_(false), | |
45 current_index_(0), | |
46 bytes_received_this_fetcher_(0) {} | |
47 ~MultiRangeHTTPFetcher() {} | |
48 | |
49 void set_ranges(const RangesVect& ranges) { | |
petkov
2010/12/16 18:30:57
Fits on one line?
Btw, have you considered provid
adlr
2010/12/17 01:03:51
interesting idea, done.
| |
50 ranges_ = ranges; | |
51 } | |
52 | |
53 void SetOffset(off_t offset) {} // for now, doesn't support this | |
petkov
2010/12/16 18:30:57
Per style, all inherited virtual methods should al
adlr
2010/12/17 01:03:51
Done.
| |
54 | |
55 // Begins the transfer to the specified URL. | |
56 // State change: Stopped -> Downloading | |
57 // (corner case: Stopped -> Stopped for an empty request) | |
58 void BeginTransfer(const std::string& url); | |
59 | |
60 // Stage change: Downloading -> Pending transfer ended | |
petkov
2010/12/16 18:30:57
typo: Stage->State
adlr
2010/12/17 01:03:51
Done.
| |
61 void TerminateTransfer(); | |
62 | |
63 void Pause() { | |
petkov
2010/12/16 18:30:57
Fits on one line?
adlr
2010/12/17 01:03:51
Done.
| |
64 base_fetcher_->Pause(); | |
65 } | |
66 | |
67 void Unpause() { | |
petkov
2010/12/16 18:30:57
Fits on one line?
adlr
2010/12/17 01:03:51
Done.
| |
68 base_fetcher_->Unpause(); | |
69 } | |
70 | |
71 // These functions are overloaded in LibcurlHttp fetcher for testing purposes. | |
72 void set_idle_seconds(int seconds) { | |
petkov
2010/12/16 18:30:57
Side note: It's kind of unfortunate that there's n
adlr
2010/12/17 01:03:51
I agree it's a bit annoying, but I like having the
| |
73 base_fetcher_->set_idle_seconds(seconds); | |
74 } | |
75 void set_retry_seconds(int seconds) { | |
76 base_fetcher_->set_retry_seconds(seconds); | |
77 } | |
78 void SetConnectionAsExpensive(bool is_expensive) { | |
79 base_fetcher_->SetConnectionAsExpensive(is_expensive); | |
80 } | |
81 void SetBuildType(bool is_official) { | |
82 base_fetcher_->SetBuildType(is_official); | |
83 } | |
84 virtual void SetProxies(const std::deque<std::string>& proxies) { | |
petkov
2010/12/16 18:30:57
Why isn't SetProxies just set_proxies? Because it'
adlr
2010/12/17 01:03:51
Good catch. I don't know how big a deal it is, but
| |
85 base_fetcher_->SetProxies(proxies); | |
86 } | |
87 | |
88 private: | |
89 // Stage change: Stopped or Downloading -> Downloading | |
petkov
2010/12/16 18:30:57
State
adlr
2010/12/17 01:03:51
Done.
| |
90 void StartTransfer(); | |
91 | |
92 // Stage change: Downloading -> Downloading | |
petkov
2010/12/16 18:30:57
State
adlr
2010/12/17 01:03:51
Done.
| |
93 void ReceivedBytes(HttpFetcher* fetcher, const char* bytes, int length); | |
94 | |
95 // Stage change: Pending transfer ended -> Stopped | |
petkov
2010/12/16 18:30:57
State
adlr
2010/12/17 01:03:51
Done.
| |
96 void TransferEnded(HttpFetcher* fetcher, bool successful); | |
97 void TransferComplete(HttpFetcher* fetcher, bool successful) { | |
98 LOG(INFO) << "Received transfer complete."; | |
petkov
2010/12/16 18:30:57
I'd move to the .cc file.
adlr
2010/12/17 01:03:51
Done.
| |
99 TransferEnded(fetcher, successful); | |
100 } | |
101 void TransferTerminated(HttpFetcher* fetcher) { | |
102 LOG(INFO) << "Received transfer terminated."; | |
petkov
2010/12/16 18:30:57
I'd move to the .cc file.
adlr
2010/12/17 01:03:51
Done.
| |
103 TransferEnded(fetcher, false); | |
104 } | |
105 | |
106 void Reset() { | |
107 base_fetcher_active_ = pending_transfer_ended_ = terminating_ = false; | |
petkov
2010/12/16 18:30:57
Move to the .cc files -- it's 3 lines of code :-)
adlr
2010/12/17 01:03:51
Done.
| |
108 current_index_ = 0; | |
109 bytes_received_this_fetcher_ = 0; | |
110 } | |
111 | |
112 scoped_ptr<HttpFetcher> base_fetcher_; | |
113 | |
114 // If true, do not send any more data or TransferComplete to the delegate. | |
115 bool base_fetcher_active_; | |
116 | |
117 // If true, the next fetcher needs to be started when TransferTerminated is | |
118 // received from the current fetcher. | |
119 bool pending_transfer_ended_; | |
120 | |
121 // True if we are waiting for base fetcher to terminate b/c we are | |
122 // ourselves terminating. | |
123 bool terminating_; | |
124 | |
125 RangesVect ranges_; | |
126 | |
127 RangesVect::size_type current_index_; // index into ranges_ | |
128 off_t bytes_received_this_fetcher_; | |
petkov
2010/12/16 18:30:57
It's more like bytes_received_this_range now.
adlr
2010/12/17 01:03:51
Done.
| |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(MultiRangeHTTPFetcher); | |
131 }; | |
132 | |
133 } // namespace chromeos_update_engine | |
134 | |
135 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_RANGE_HTTP_FETCHER_H__ | |
OLD | NEW |