1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.h,v 1.29.2.1 2003/12/24 23:09:17 mdz Exp $
4 /* ######################################################################
6 Acquire - File Acquiration
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.
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.
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.
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.
30 ##################################################################### */
32 #ifndef PKGLIB_ACQUIRE_H
33 #define PKGLIB_ACQUIRE_H
42 #pragma interface "apt-pkg/acquire.h"
48 class pkgAcquireStatus
;
61 typedef vector
<Item
*>::iterator ItemIterator
;
62 typedef vector
<Item
*>::const_iterator ItemCIterator
;
66 // List of items to fetch
69 // List of active queues and fetched method configuration parameters
72 MethodConfig
*Configs
;
73 pkgAcquireStatus
*Log
;
74 unsigned long ToFetch
;
76 // Configurable parameters for the schedular
77 enum {QueueHost
,QueueAccess
} QueueMode
;
82 void Remove(Item
*Item
);
83 void Add(Worker
*Work
);
84 void Remove(Worker
*Work
);
86 void Enqueue(ItemDesc
&Item
);
87 void Dequeue(Item
*Item
);
88 string
QueueName(string URI
,MethodConfig
const *&Config
);
90 // FDSET managers for derived classes
91 virtual void SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
);
92 virtual void RunFds(fd_set
*RSet
,fd_set
*WSet
);
94 // A queue calls this when it dequeues an item
99 MethodConfig
*GetConfig(string Access
);
101 enum RunResult
{Continue
,Failed
,Cancelled
};
106 // Simple iteration mechanism
107 inline Worker
*WorkersBegin() {return Workers
;};
108 Worker
*WorkerStep(Worker
*I
);
109 inline ItemIterator
ItemsBegin() {return Items
.begin();};
110 inline ItemIterator
ItemsEnd() {return Items
.end();};
112 // Iterate over queued Item URIs
114 UriIterator
UriBegin();
115 UriIterator
UriEnd();
117 // Cleans out the download dir
118 bool Clean(string Dir
);
120 // Returns the size of the total download set
121 double TotalNeeded();
122 double FetchNeeded();
123 double PartialPresent();
125 pkgAcquire(pkgAcquireStatus
*Log
= 0);
126 virtual ~pkgAcquire();
129 // Description of an Item+URI
130 struct pkgAcquire::ItemDesc
138 // List of possible items queued for download.
139 class pkgAcquire::Queue
141 friend class pkgAcquire
;
142 friend class pkgAcquire::UriIterator
;
143 friend class pkgAcquire::Worker
;
149 struct QItem
: pkgAcquire::ItemDesc
152 pkgAcquire::Worker
*Worker
;
154 void operator =(pkgAcquire::ItemDesc
const &I
)
157 Description
= I
.Description
;
158 ShortDesc
= I
.ShortDesc
;
166 // Items queued into this queue
168 pkgAcquire::Worker
*Workers
;
170 signed long PipeDepth
;
171 unsigned long MaxPipeDepth
;
175 // Put an item into this queue
176 void Enqueue(ItemDesc
&Item
);
177 bool Dequeue(Item
*Owner
);
179 // Find a Queued item
180 QItem
*FindItem(string URI
,pkgAcquire::Worker
*Owner
);
181 bool ItemStart(QItem
*Itm
,unsigned long Size
);
182 bool ItemDone(QItem
*Itm
);
185 bool Shutdown(bool Final
);
189 Queue(string Name
,pkgAcquire
*Owner
);
193 class pkgAcquire::UriIterator
195 pkgAcquire::Queue
*CurQ
;
196 pkgAcquire::Queue::QItem
*CurItem
;
200 // Advance to the next item
201 inline void operator ++() {operator ++();};
202 void operator ++(int)
204 CurItem
= CurItem
->Next
;
205 while (CurItem
== 0 && CurQ
!= 0)
207 CurItem
= CurQ
->Items
;
213 inline pkgAcquire::ItemDesc
const *operator ->() const {return CurItem
;};
214 inline bool operator !=(UriIterator
const &rhs
) const {return rhs
.CurQ
!= CurQ
|| rhs
.CurItem
!= CurItem
;};
215 inline bool operator ==(UriIterator
const &rhs
) const {return rhs
.CurQ
== CurQ
&& rhs
.CurItem
== CurItem
;};
217 UriIterator(pkgAcquire::Queue
*Q
) : CurQ(Q
), CurItem(0)
219 while (CurItem
== 0 && CurQ
!= 0)
221 CurItem
= CurQ
->Items
;
227 // Configuration information from each method
228 struct pkgAcquire::MethodConfig
245 class pkgAcquireStatus
250 struct timeval StartTime
;
256 unsigned long ElapsedTime
;
257 unsigned long TotalItems
;
258 unsigned long CurrentItems
;
265 // Called by items when they have finished a real download
266 virtual void Fetched(unsigned long Size
,unsigned long ResumePoint
);
268 // Called to change media
269 virtual bool MediaChange(string Media
,string Drive
) = 0;
271 // Each of these is called by the workers when an event occures
272 virtual void IMSHit(pkgAcquire::ItemDesc
&/*Itm*/) {};
273 virtual void Fetch(pkgAcquire::ItemDesc
&/*Itm*/) {};
274 virtual void Done(pkgAcquire::ItemDesc
&/*Itm*/) {};
275 virtual void Fail(pkgAcquire::ItemDesc
&/*Itm*/) {};
276 virtual bool Pulse(pkgAcquire
*Owner
); // returns false on user cancel
277 virtual void Start();
281 virtual ~pkgAcquireStatus() {};