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