You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

275 lines
6.9 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire.h,v 1.22 1999/07/03 03:10:35 jgg 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. #ifdef __GNUG__
  31. #pragma interface "apt-pkg/acquire.h"
  32. #endif
  33. #include <sys/time.h>
  34. #include <unistd.h>
  35. class pkgAcquireStatus;
  36. class pkgAcquire
  37. {
  38. public:
  39. class Item;
  40. class Queue;
  41. class Worker;
  42. struct MethodConfig;
  43. struct ItemDesc;
  44. friend Item;
  45. friend Queue;
  46. protected:
  47. // List of items to fetch
  48. vector<Item *> Items;
  49. // List of active queues and fetched method configuration parameters
  50. Queue *Queues;
  51. Worker *Workers;
  52. MethodConfig *Configs;
  53. pkgAcquireStatus *Log;
  54. unsigned long ToFetch;
  55. // Configurable parameters for the schedular
  56. enum {QueueHost,QueueAccess} QueueMode;
  57. bool Debug;
  58. bool Running;
  59. void Add(Item *Item);
  60. void Remove(Item *Item);
  61. void Add(Worker *Work);
  62. void Remove(Worker *Work);
  63. void Enqueue(ItemDesc &Item);
  64. void Dequeue(Item *Item);
  65. string QueueName(string URI,MethodConfig const *&Config);
  66. // FDSET managers for derived classes
  67. virtual void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
  68. virtual void RunFds(fd_set *RSet,fd_set *WSet);
  69. // A queue calls this when it dequeues an item
  70. void Bump();
  71. public:
  72. MethodConfig *GetConfig(string Access);
  73. enum RunResult {Continue,Failed,Cancelled};
  74. RunResult Run();
  75. void Shutdown();
  76. // Simple iteration mechanism
  77. inline Worker *WorkersBegin() {return Workers;};
  78. Worker *WorkerStep(Worker *I);
  79. inline Item **ItemsBegin() {return Items.begin();};
  80. inline Item **ItemsEnd() {return Items.end();};
  81. // Iterate over queued Item URIs
  82. class UriIterator;
  83. UriIterator UriBegin();
  84. UriIterator UriEnd();
  85. // Cleans out the download dir
  86. bool Clean(string Dir);
  87. // Returns the size of the total download set
  88. unsigned long TotalNeeded();
  89. unsigned long FetchNeeded();
  90. unsigned long PartialPresent();
  91. pkgAcquire(pkgAcquireStatus *Log = 0);
  92. ~pkgAcquire();
  93. };
  94. // Description of an Item+URI
  95. struct pkgAcquire::ItemDesc
  96. {
  97. string URI;
  98. string Description;
  99. string ShortDesc;
  100. Item *Owner;
  101. };
  102. // List of possible items queued for download.
  103. class pkgAcquire::Queue
  104. {
  105. friend pkgAcquire;
  106. friend pkgAcquire::UriIterator;
  107. Queue *Next;
  108. protected:
  109. // Queued item
  110. struct QItem : pkgAcquire::ItemDesc
  111. {
  112. QItem *Next;
  113. pkgAcquire::Worker *Worker;
  114. void operator =(pkgAcquire::ItemDesc const &I)
  115. {
  116. URI = I.URI;
  117. Description = I.Description;
  118. ShortDesc = I.ShortDesc;
  119. Owner = I.Owner;
  120. };
  121. };
  122. // Name of the queue
  123. string Name;
  124. // Items queued into this queue
  125. QItem *Items;
  126. pkgAcquire::Worker *Workers;
  127. pkgAcquire *Owner;
  128. signed long PipeDepth;
  129. unsigned long MaxPipeDepth;
  130. public:
  131. // Put an item into this queue
  132. void Enqueue(ItemDesc &Item);
  133. bool Dequeue(Item *Owner);
  134. // Find a Queued item
  135. QItem *FindItem(string URI,pkgAcquire::Worker *Owner);
  136. bool ItemStart(QItem *Itm,unsigned long Size);
  137. bool ItemDone(QItem *Itm);
  138. bool Startup();
  139. bool Shutdown();
  140. bool Cycle();
  141. void Bump();
  142. Queue(string Name,pkgAcquire *Owner);
  143. ~Queue();
  144. };
  145. class pkgAcquire::UriIterator
  146. {
  147. pkgAcquire::Queue *CurQ;
  148. pkgAcquire::Queue::QItem *CurItem;
  149. public:
  150. // Advance to the next item
  151. inline void operator ++() {operator ++();};
  152. void operator ++(int)
  153. {
  154. CurItem = CurItem->Next;
  155. while (CurItem == 0 && CurQ != 0)
  156. {
  157. CurItem = CurQ->Items;
  158. CurQ = CurQ->Next;
  159. }
  160. };
  161. // Accessors
  162. inline pkgAcquire::ItemDesc const *operator ->() const {return CurItem;};
  163. inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;};
  164. inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;};
  165. UriIterator(pkgAcquire::Queue *Q) : CurQ(Q), CurItem(0)
  166. {
  167. while (CurItem == 0 && CurQ != 0)
  168. {
  169. CurItem = CurQ->Items;
  170. CurQ = CurQ->Next;
  171. }
  172. }
  173. };
  174. // Configuration information from each method
  175. struct pkgAcquire::MethodConfig
  176. {
  177. MethodConfig *Next;
  178. string Access;
  179. string Version;
  180. bool SingleInstance;
  181. bool Pipeline;
  182. bool SendConfig;
  183. bool LocalOnly;
  184. MethodConfig();
  185. };
  186. class pkgAcquireStatus
  187. {
  188. protected:
  189. struct timeval Time;
  190. struct timeval StartTime;
  191. unsigned long LastBytes;
  192. double CurrentCPS;
  193. unsigned long CurrentBytes;
  194. unsigned long TotalBytes;
  195. unsigned long FetchedBytes;
  196. unsigned long ElapsedTime;
  197. unsigned long TotalItems;
  198. unsigned long CurrentItems;
  199. public:
  200. bool Update;
  201. // Called by items when they have finished a real download
  202. virtual void Fetched(unsigned long Size,unsigned long ResumePoint);
  203. // Called to change media
  204. virtual bool MediaChange(string Media,string Drive) = 0;
  205. // Each of these is called by the workers when an event occures
  206. virtual void IMSHit(pkgAcquire::ItemDesc &Itm) {};
  207. virtual void Fetch(pkgAcquire::ItemDesc &Itm) {};
  208. virtual void Done(pkgAcquire::ItemDesc &Itm) {};
  209. virtual void Fail(pkgAcquire::ItemDesc &Itm) {};
  210. virtual bool Pulse(pkgAcquire *Owner); // returns false on user cancel
  211. virtual void Start();
  212. virtual void Stop();
  213. pkgAcquireStatus();
  214. virtual ~pkgAcquireStatus() {};
  215. };
  216. #endif