* another bug in QueueNextDiff fixed
[ntk/apt.git] / apt-pkg / acquire.h
CommitLineData
0118833a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 3// $Id: acquire.h,v 1.29.2.1 2003/12/24 23:09:17 mdz 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
b4fc9b6f
AL
38using std::vector;
39using std::string;
40
0118833a
AL
41#ifdef __GNUG__
42#pragma interface "apt-pkg/acquire.h"
43#endif
44
b98f2859 45#include <sys/time.h>
0a8a80e5
AL
46#include <unistd.h>
47
8267fe24 48class pkgAcquireStatus;
0118833a
AL
49class pkgAcquire
50{
51 public:
52
53 class Item;
54 class Queue;
55 class Worker;
56 struct MethodConfig;
8267fe24 57 struct ItemDesc;
b2e465d6
AL
58 friend class Item;
59 friend class Queue;
b4fc9b6f
AL
60
61 typedef vector<Item *>::iterator ItemIterator;
62 typedef vector<Item *>::const_iterator ItemCIterator;
0118833a
AL
63
64 protected:
65
0a8a80e5 66 // List of items to fetch
0118833a 67 vector<Item *> Items;
0a8a80e5
AL
68
69 // List of active queues and fetched method configuration parameters
0118833a 70 Queue *Queues;
0a8a80e5 71 Worker *Workers;
0118833a 72 MethodConfig *Configs;
8267fe24 73 pkgAcquireStatus *Log;
0a8a80e5 74 unsigned long ToFetch;
8267fe24 75
0a8a80e5
AL
76 // Configurable parameters for the schedular
77 enum {QueueHost,QueueAccess} QueueMode;
78 bool Debug;
8b89e57f 79 bool Running;
0118833a
AL
80
81 void Add(Item *Item);
82 void Remove(Item *Item);
0a8a80e5
AL
83 void Add(Worker *Work);
84 void Remove(Worker *Work);
85
8267fe24 86 void Enqueue(ItemDesc &Item);
0a8a80e5 87 void Dequeue(Item *Item);
e331f6ed 88 string QueueName(string URI,MethodConfig const *&Config);
0a8a80e5
AL
89
90 // FDSET managers for derived classes
281daf46
AL
91 virtual void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
92 virtual void RunFds(fd_set *RSet,fd_set *WSet);
93bf083d
AL
93
94 // A queue calls this when it dequeues an item
95 void Bump();
0118833a
AL
96
97 public:
3b5421b4 98
0a8a80e5 99 MethodConfig *GetConfig(string Access);
024d1123
AL
100
101 enum RunResult {Continue,Failed,Cancelled};
102
1c5f7e5f
MV
103 RunResult Run() { return Run(500000); }; // Binary compatibility
104 RunResult Run(int PulseIntervall);
281daf46
AL
105 void Shutdown();
106
8267fe24
AL
107 // Simple iteration mechanism
108 inline Worker *WorkersBegin() {return Workers;};
109 Worker *WorkerStep(Worker *I);
b4fc9b6f
AL
110 inline ItemIterator ItemsBegin() {return Items.begin();};
111 inline ItemIterator ItemsEnd() {return Items.end();};
f7a08e33
AL
112
113 // Iterate over queued Item URIs
114 class UriIterator;
115 UriIterator UriBegin();
116 UriIterator UriEnd();
117
7a7fa5f0
AL
118 // Cleans out the download dir
119 bool Clean(string Dir);
a6568219
AL
120
121 // Returns the size of the total download set
b2e465d6
AL
122 double TotalNeeded();
123 double FetchNeeded();
124 double PartialPresent();
b3d44315 125
8267fe24 126 pkgAcquire(pkgAcquireStatus *Log = 0);
58d63ae6 127 virtual ~pkgAcquire();
0118833a
AL
128};
129
8267fe24
AL
130// Description of an Item+URI
131struct pkgAcquire::ItemDesc
132{
133 string URI;
134 string Description;
135 string ShortDesc;
136 Item *Owner;
137};
138
0118833a
AL
139// List of possible items queued for download.
140class pkgAcquire::Queue
141{
b2e465d6
AL
142 friend class pkgAcquire;
143 friend class pkgAcquire::UriIterator;
144 friend class pkgAcquire::Worker;
0118833a
AL
145 Queue *Next;
146
147 protected:
3b5421b4 148
0a8a80e5 149 // Queued item
8267fe24 150 struct QItem : pkgAcquire::ItemDesc
0a8a80e5 151 {
8267fe24 152 QItem *Next;
c88edf1d 153 pkgAcquire::Worker *Worker;
8267fe24
AL
154
155 void operator =(pkgAcquire::ItemDesc const &I)
156 {
157 URI = I.URI;
158 Description = I.Description;
159 ShortDesc = I.ShortDesc;
160 Owner = I.Owner;
161 };
162 };
0a8a80e5
AL
163
164 // Name of the queue
165 string Name;
166
167 // Items queued into this queue
168 QItem *Items;
169 pkgAcquire::Worker *Workers;
170 pkgAcquire *Owner;
e7432370 171 signed long PipeDepth;
b185acc2 172 unsigned long MaxPipeDepth;
0118833a
AL
173
174 public:
0a8a80e5
AL
175
176 // Put an item into this queue
8267fe24 177 void Enqueue(ItemDesc &Item);
bfd22fc0 178 bool Dequeue(Item *Owner);
0a8a80e5 179
c88edf1d
AL
180 // Find a Queued item
181 QItem *FindItem(string URI,pkgAcquire::Worker *Owner);
8267fe24 182 bool ItemStart(QItem *Itm,unsigned long Size);
c88edf1d
AL
183 bool ItemDone(QItem *Itm);
184
0a8a80e5 185 bool Startup();
8e5fc8f5 186 bool Shutdown(bool Final);
93bf083d 187 bool Cycle();
be4401bf 188 void Bump();
0a8a80e5
AL
189
190 Queue(string Name,pkgAcquire *Owner);
191 ~Queue();
0118833a
AL
192};
193
f7a08e33
AL
194class pkgAcquire::UriIterator
195{
196 pkgAcquire::Queue *CurQ;
197 pkgAcquire::Queue::QItem *CurItem;
198
199 public:
200
201 // Advance to the next item
202 inline void operator ++() {operator ++();};
203 void operator ++(int)
204 {
205 CurItem = CurItem->Next;
206 while (CurItem == 0 && CurQ != 0)
207 {
208 CurItem = CurQ->Items;
209 CurQ = CurQ->Next;
210 }
211 };
212
213 // Accessors
214 inline pkgAcquire::ItemDesc const *operator ->() const {return CurItem;};
215 inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;};
216 inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;};
217
218 UriIterator(pkgAcquire::Queue *Q) : CurQ(Q), CurItem(0)
219 {
220 while (CurItem == 0 && CurQ != 0)
221 {
222 CurItem = CurQ->Items;
223 CurQ = CurQ->Next;
224 }
225 }
226};
227
0118833a
AL
228// Configuration information from each method
229struct pkgAcquire::MethodConfig
230{
3b5421b4
AL
231 MethodConfig *Next;
232
0118833a
AL
233 string Access;
234
235 string Version;
236 bool SingleInstance;
0a8a80e5
AL
237 bool Pipeline;
238 bool SendConfig;
e331f6ed 239 bool LocalOnly;
8e5fc8f5 240 bool NeedsCleanup;
459681d3 241 bool Removable;
8e5fc8f5 242
0118833a 243 MethodConfig();
0118833a
AL
244};
245
8267fe24
AL
246class pkgAcquireStatus
247{
b98f2859
AL
248 protected:
249
250 struct timeval Time;
251 struct timeval StartTime;
b2e465d6 252 double LastBytes;
b98f2859 253 double CurrentCPS;
b2e465d6
AL
254 double CurrentBytes;
255 double TotalBytes;
256 double FetchedBytes;
b98f2859 257 unsigned long ElapsedTime;
d568ed2d
AL
258 unsigned long TotalItems;
259 unsigned long CurrentItems;
b98f2859 260
8267fe24
AL
261 public:
262
263 bool Update;
c5ccf175
AL
264 bool MorePulses;
265
b98f2859
AL
266 // Called by items when they have finished a real download
267 virtual void Fetched(unsigned long Size,unsigned long ResumePoint);
268
542ec555
AL
269 // Called to change media
270 virtual bool MediaChange(string Media,string Drive) = 0;
271
8267fe24 272 // Each of these is called by the workers when an event occures
727f18af
AL
273 virtual void IMSHit(pkgAcquire::ItemDesc &/*Itm*/) {};
274 virtual void Fetch(pkgAcquire::ItemDesc &/*Itm*/) {};
275 virtual void Done(pkgAcquire::ItemDesc &/*Itm*/) {};
276 virtual void Fail(pkgAcquire::ItemDesc &/*Itm*/) {};
024d1123 277 virtual bool Pulse(pkgAcquire *Owner); // returns false on user cancel
b98f2859
AL
278 virtual void Start();
279 virtual void Stop();
a6568219 280
b98f2859 281 pkgAcquireStatus();
8267fe24
AL
282 virtual ~pkgAcquireStatus() {};
283};
284
0118833a 285#endif