acquire.h 7.1 KB

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