]>
Commit | Line | Data |
---|---|---|
0118833a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
f7a08e33 | 3 | // $Id: acquire.h,v 1.19 1999/01/30 06:07:24 jgg Exp $ |
0118833a AL |
4 | /* ###################################################################### |
5 | ||
6 | Acquire - File Acquiration | |
7 | ||
8 | This module contians the Acquire system. It is responsible for bringing | |
9 | files into the local pathname space. It deals with URIs for files and | |
10 | URI handlers responsible for downloading or finding the URIs. | |
11 | ||
12 | Each file to download is represented by an Acquire::Item class subclassed | |
13 | into a specialization. The Item class can add itself to several URI | |
14 | acquire queues each prioritized by the download scheduler. When the | |
15 | system is run the proper URI handlers are spawned and the the acquire | |
16 | queues are fed into the handlers by the schedular until the queues are | |
17 | empty. This allows for an Item to be downloaded from an alternate source | |
18 | if the first try turns out to fail. It also alows concurrent downloading | |
19 | of multiple items from multiple sources as well as dynamic balancing | |
20 | of load between the sources. | |
21 | ||
22 | Schedualing of downloads is done on a first ask first get basis. This | |
23 | preserves the order of the download as much as possible. And means the | |
24 | fastest source will tend to process the largest number of files. | |
25 | ||
26 | Internal methods and queues for performing gzip decompression, | |
27 | md5sum hashing and file copying are provided to allow items to apply | |
28 | a number of transformations to the data files they are working with. | |
29 | ||
30 | ##################################################################### */ | |
31 | /*}}}*/ | |
32 | #ifndef PKGLIB_ACQUIRE_H | |
33 | #define PKGLIB_ACQUIRE_H | |
34 | ||
35 | #include <vector> | |
36 | #include <string> | |
37 | ||
38 | #ifdef __GNUG__ | |
39 | #pragma interface "apt-pkg/acquire.h" | |
40 | #endif | |
41 | ||
b98f2859 | 42 | #include <sys/time.h> |
0a8a80e5 AL |
43 | #include <unistd.h> |
44 | ||
8267fe24 | 45 | class pkgAcquireStatus; |
0118833a AL |
46 | class pkgAcquire |
47 | { | |
48 | public: | |
49 | ||
50 | class Item; | |
51 | class Queue; | |
52 | class Worker; | |
53 | struct MethodConfig; | |
8267fe24 | 54 | struct ItemDesc; |
0118833a | 55 | friend Item; |
0a8a80e5 | 56 | friend Queue; |
0118833a AL |
57 | |
58 | protected: | |
59 | ||
0a8a80e5 | 60 | // List of items to fetch |
0118833a | 61 | vector<Item *> Items; |
0a8a80e5 AL |
62 | |
63 | // List of active queues and fetched method configuration parameters | |
0118833a | 64 | Queue *Queues; |
0a8a80e5 | 65 | Worker *Workers; |
0118833a | 66 | MethodConfig *Configs; |
8267fe24 | 67 | pkgAcquireStatus *Log; |
0a8a80e5 | 68 | unsigned long ToFetch; |
8267fe24 | 69 | |
0a8a80e5 AL |
70 | // Configurable parameters for the schedular |
71 | enum {QueueHost,QueueAccess} QueueMode; | |
72 | bool Debug; | |
8b89e57f | 73 | bool Running; |
0118833a AL |
74 | |
75 | void Add(Item *Item); | |
76 | void Remove(Item *Item); | |
0a8a80e5 AL |
77 | void Add(Worker *Work); |
78 | void Remove(Worker *Work); | |
79 | ||
8267fe24 | 80 | void Enqueue(ItemDesc &Item); |
0a8a80e5 | 81 | void Dequeue(Item *Item); |
e331f6ed | 82 | string QueueName(string URI,MethodConfig const *&Config); |
0a8a80e5 AL |
83 | |
84 | // FDSET managers for derived classes | |
85 | void SetFds(int &Fd,fd_set *RSet,fd_set *WSet); | |
86 | void RunFds(fd_set *RSet,fd_set *WSet); | |
93bf083d AL |
87 | |
88 | // A queue calls this when it dequeues an item | |
89 | void Bump(); | |
0118833a AL |
90 | |
91 | public: | |
3b5421b4 | 92 | |
0a8a80e5 AL |
93 | MethodConfig *GetConfig(string Access); |
94 | bool Run(); | |
8267fe24 AL |
95 | |
96 | // Simple iteration mechanism | |
97 | inline Worker *WorkersBegin() {return Workers;}; | |
98 | Worker *WorkerStep(Worker *I); | |
99 | inline Item **ItemsBegin() {return Items.begin();}; | |
100 | inline Item **ItemsEnd() {return Items.end();}; | |
f7a08e33 AL |
101 | |
102 | // Iterate over queued Item URIs | |
103 | class UriIterator; | |
104 | UriIterator UriBegin(); | |
105 | UriIterator UriEnd(); | |
106 | ||
7a7fa5f0 AL |
107 | // Cleans out the download dir |
108 | bool Clean(string Dir); | |
a6568219 AL |
109 | |
110 | // Returns the size of the total download set | |
111 | unsigned long TotalNeeded(); | |
112 | unsigned long FetchNeeded(); | |
0118833a | 113 | |
8267fe24 | 114 | pkgAcquire(pkgAcquireStatus *Log = 0); |
0118833a AL |
115 | ~pkgAcquire(); |
116 | }; | |
117 | ||
8267fe24 AL |
118 | // Description of an Item+URI |
119 | struct pkgAcquire::ItemDesc | |
120 | { | |
121 | string URI; | |
122 | string Description; | |
123 | string ShortDesc; | |
124 | Item *Owner; | |
125 | }; | |
126 | ||
0118833a AL |
127 | // List of possible items queued for download. |
128 | class pkgAcquire::Queue | |
129 | { | |
130 | friend pkgAcquire; | |
f7a08e33 | 131 | friend pkgAcquire::UriIterator; |
0118833a AL |
132 | Queue *Next; |
133 | ||
134 | protected: | |
3b5421b4 | 135 | |
0a8a80e5 | 136 | // Queued item |
8267fe24 | 137 | struct QItem : pkgAcquire::ItemDesc |
0a8a80e5 | 138 | { |
8267fe24 | 139 | QItem *Next; |
c88edf1d | 140 | pkgAcquire::Worker *Worker; |
8267fe24 AL |
141 | |
142 | void operator =(pkgAcquire::ItemDesc const &I) | |
143 | { | |
144 | URI = I.URI; | |
145 | Description = I.Description; | |
146 | ShortDesc = I.ShortDesc; | |
147 | Owner = I.Owner; | |
148 | }; | |
149 | }; | |
0a8a80e5 AL |
150 | |
151 | // Name of the queue | |
152 | string Name; | |
153 | ||
154 | // Items queued into this queue | |
155 | QItem *Items; | |
156 | pkgAcquire::Worker *Workers; | |
157 | pkgAcquire *Owner; | |
e7432370 | 158 | signed long PipeDepth; |
b185acc2 | 159 | unsigned long MaxPipeDepth; |
0118833a AL |
160 | |
161 | public: | |
0a8a80e5 AL |
162 | |
163 | // Put an item into this queue | |
8267fe24 | 164 | void Enqueue(ItemDesc &Item); |
bfd22fc0 | 165 | bool Dequeue(Item *Owner); |
0a8a80e5 | 166 | |
c88edf1d AL |
167 | // Find a Queued item |
168 | QItem *FindItem(string URI,pkgAcquire::Worker *Owner); | |
8267fe24 | 169 | bool ItemStart(QItem *Itm,unsigned long Size); |
c88edf1d AL |
170 | bool ItemDone(QItem *Itm); |
171 | ||
0a8a80e5 AL |
172 | bool Startup(); |
173 | bool Shutdown(); | |
93bf083d | 174 | bool Cycle(); |
be4401bf | 175 | void Bump(); |
0a8a80e5 AL |
176 | |
177 | Queue(string Name,pkgAcquire *Owner); | |
178 | ~Queue(); | |
0118833a AL |
179 | }; |
180 | ||
f7a08e33 AL |
181 | class pkgAcquire::UriIterator |
182 | { | |
183 | pkgAcquire::Queue *CurQ; | |
184 | pkgAcquire::Queue::QItem *CurItem; | |
185 | ||
186 | public: | |
187 | ||
188 | // Advance to the next item | |
189 | inline void operator ++() {operator ++();}; | |
190 | void operator ++(int) | |
191 | { | |
192 | CurItem = CurItem->Next; | |
193 | while (CurItem == 0 && CurQ != 0) | |
194 | { | |
195 | CurItem = CurQ->Items; | |
196 | CurQ = CurQ->Next; | |
197 | } | |
198 | }; | |
199 | ||
200 | // Accessors | |
201 | inline pkgAcquire::ItemDesc const *operator ->() const {return CurItem;}; | |
202 | inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;}; | |
203 | inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;}; | |
204 | ||
205 | UriIterator(pkgAcquire::Queue *Q) : CurQ(Q), CurItem(0) | |
206 | { | |
207 | while (CurItem == 0 && CurQ != 0) | |
208 | { | |
209 | CurItem = CurQ->Items; | |
210 | CurQ = CurQ->Next; | |
211 | } | |
212 | } | |
213 | }; | |
214 | ||
0118833a AL |
215 | // Configuration information from each method |
216 | struct pkgAcquire::MethodConfig | |
217 | { | |
3b5421b4 AL |
218 | MethodConfig *Next; |
219 | ||
0118833a AL |
220 | string Access; |
221 | ||
222 | string Version; | |
223 | bool SingleInstance; | |
0a8a80e5 AL |
224 | bool Pipeline; |
225 | bool SendConfig; | |
e331f6ed AL |
226 | bool LocalOnly; |
227 | ||
0118833a | 228 | MethodConfig(); |
0118833a AL |
229 | }; |
230 | ||
8267fe24 AL |
231 | class pkgAcquireStatus |
232 | { | |
b98f2859 AL |
233 | protected: |
234 | ||
235 | struct timeval Time; | |
236 | struct timeval StartTime; | |
237 | unsigned long LastBytes; | |
238 | double CurrentCPS; | |
239 | unsigned long CurrentBytes; | |
240 | unsigned long TotalBytes; | |
241 | unsigned long FetchedBytes; | |
242 | unsigned long ElapsedTime; | |
d568ed2d AL |
243 | unsigned long TotalItems; |
244 | unsigned long CurrentItems; | |
b98f2859 | 245 | |
8267fe24 AL |
246 | public: |
247 | ||
248 | bool Update; | |
249 | ||
b98f2859 AL |
250 | // Called by items when they have finished a real download |
251 | virtual void Fetched(unsigned long Size,unsigned long ResumePoint); | |
252 | ||
542ec555 AL |
253 | // Called to change media |
254 | virtual bool MediaChange(string Media,string Drive) = 0; | |
255 | ||
8267fe24 AL |
256 | // Each of these is called by the workers when an event occures |
257 | virtual void IMSHit(pkgAcquire::ItemDesc &Itm) {}; | |
258 | virtual void Fetch(pkgAcquire::ItemDesc &Itm) {}; | |
259 | virtual void Done(pkgAcquire::ItemDesc &Itm) {}; | |
542ec555 | 260 | virtual void Fail(pkgAcquire::ItemDesc &Itm) {}; |
b98f2859 AL |
261 | virtual void Pulse(pkgAcquire *Owner); |
262 | virtual void Start(); | |
263 | virtual void Stop(); | |
a6568219 | 264 | |
b98f2859 | 265 | pkgAcquireStatus(); |
8267fe24 AL |
266 | virtual ~pkgAcquireStatus() {}; |
267 | }; | |
268 | ||
0118833a | 269 | #endif |