acquire.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire.h,v 1.13 1998/11/14 01:39:46 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. void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
  68. 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. bool Run();
  74. // Simple iteration mechanism
  75. inline Worker *WorkersBegin() {return Workers;};
  76. Worker *WorkerStep(Worker *I);
  77. inline Item **ItemsBegin() {return Items.begin();};
  78. inline Item **ItemsEnd() {return Items.end();};
  79. // Cleans out the download dir
  80. bool Clean(string Dir);
  81. // Returns the size of the total download set
  82. unsigned long TotalNeeded();
  83. unsigned long FetchNeeded();
  84. pkgAcquire(pkgAcquireStatus *Log = 0);
  85. ~pkgAcquire();
  86. };
  87. // Description of an Item+URI
  88. struct pkgAcquire::ItemDesc
  89. {
  90. string URI;
  91. string Description;
  92. string ShortDesc;
  93. Item *Owner;
  94. };
  95. // List of possible items queued for download.
  96. class pkgAcquire::Queue
  97. {
  98. friend pkgAcquire;
  99. Queue *Next;
  100. protected:
  101. // Queued item
  102. struct QItem : pkgAcquire::ItemDesc
  103. {
  104. QItem *Next;
  105. pkgAcquire::Worker *Worker;
  106. void operator =(pkgAcquire::ItemDesc const &I)
  107. {
  108. URI = I.URI;
  109. Description = I.Description;
  110. ShortDesc = I.ShortDesc;
  111. Owner = I.Owner;
  112. };
  113. };
  114. // Name of the queue
  115. string Name;
  116. // Items queued into this queue
  117. QItem *Items;
  118. pkgAcquire::Worker *Workers;
  119. pkgAcquire *Owner;
  120. public:
  121. // Put an item into this queue
  122. void Enqueue(ItemDesc &Item);
  123. bool Dequeue(Item *Owner);
  124. // Find a Queued item
  125. QItem *FindItem(string URI,pkgAcquire::Worker *Owner);
  126. bool ItemStart(QItem *Itm,unsigned long Size);
  127. bool ItemDone(QItem *Itm);
  128. bool Startup();
  129. bool Shutdown();
  130. bool Cycle();
  131. void Bump();
  132. Queue(string Name,pkgAcquire *Owner);
  133. ~Queue();
  134. };
  135. // Configuration information from each method
  136. struct pkgAcquire::MethodConfig
  137. {
  138. MethodConfig *Next;
  139. string Access;
  140. string Version;
  141. bool SingleInstance;
  142. bool PreScan;
  143. bool Pipeline;
  144. bool SendConfig;
  145. bool LocalOnly;
  146. MethodConfig();
  147. };
  148. class pkgAcquireStatus
  149. {
  150. protected:
  151. struct timeval Time;
  152. struct timeval StartTime;
  153. unsigned long LastBytes;
  154. double CurrentCPS;
  155. unsigned long CurrentBytes;
  156. unsigned long TotalBytes;
  157. unsigned long FetchedBytes;
  158. unsigned long ElapsedTime;
  159. public:
  160. bool Update;
  161. // Called by items when they have finished a real download
  162. virtual void Fetched(unsigned long Size,unsigned long ResumePoint);
  163. // Each of these is called by the workers when an event occures
  164. virtual void IMSHit(pkgAcquire::ItemDesc &Itm) {};
  165. virtual void Fetch(pkgAcquire::ItemDesc &Itm) {};
  166. virtual void Done(pkgAcquire::ItemDesc &Itm) {};
  167. virtual void Fail(pkgAcquire::ItemDesc &Itm) {};
  168. virtual void Pulse(pkgAcquire *Owner);
  169. virtual void Start();
  170. virtual void Stop();
  171. pkgAcquireStatus();
  172. virtual ~pkgAcquireStatus() {};
  173. };
  174. #endif