acquire-worker.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-worker.h,v 1.12 2001/02/20 07:03:17 jgg Exp $
  4. /* ######################################################################
  5. Acquire Worker - Worker process manager
  6. Each worker class is associated with exaclty one subprocess.
  7. ##################################################################### */
  8. /*}}}*/
  9. /** \addtogroup acquire
  10. * @{
  11. *
  12. * \file acquire-worker.h
  13. */
  14. #ifndef PKGLIB_ACQUIRE_WORKER_H
  15. #define PKGLIB_ACQUIRE_WORKER_H
  16. #include <apt-pkg/acquire.h>
  17. /** \brief A fetch subprocess.
  18. *
  19. * A worker process is responsible for one stage of the fetch. This
  20. * class encapsulates the communications protocol between the master
  21. * process and the worker, from the master end.
  22. *
  23. * Each worker is intrinsically placed on two linked lists. The
  24. * Queue list (maintained in the #NextQueue variable) is maintained
  25. * by the pkgAcquire::Queue class; it represents the set of workers
  26. * assigned to a particular queue. The Acquire list (maintained in
  27. * the #NextAcquire variable) is maintained by the pkgAcquire class;
  28. * it represents the set of active workers for a particular
  29. * pkgAcquire object.
  30. *
  31. * \todo Like everything else in the Acquire system, this has way too
  32. * many protected items.
  33. *
  34. * \sa pkgAcqMethod, pkgAcquire::Item, pkgAcquire
  35. */
  36. class pkgAcquire::Worker
  37. {
  38. friend class pkgAcquire;
  39. protected:
  40. friend class Queue;
  41. /** \brief The next link on the Queue list.
  42. *
  43. * \todo This is always NULL; is it just for future use?
  44. */
  45. Worker *NextQueue;
  46. /** \brief The next link on the Acquire list. */
  47. Worker *NextAcquire;
  48. /** \brief The Queue with which this worker is associated. */
  49. Queue *OwnerQ;
  50. /** \brief The download progress indicator to which progress
  51. * messages should be sent.
  52. */
  53. pkgAcquireStatus *Log;
  54. /** \brief The configuration of this method. On startup, the
  55. * target of this pointer is filled in with basic data about the
  56. * method, as reported by the worker.
  57. */
  58. MethodConfig *Config;
  59. /** \brief The access method to be used by this worker.
  60. *
  61. * \todo Doesn't this duplicate Config->Access?
  62. */
  63. string Access;
  64. /** \brief The PID of the subprocess. */
  65. pid_t Process;
  66. /** \brief A file descriptor connected to the standard output of
  67. * the subprocess.
  68. *
  69. * Used to read messages and data from the subprocess.
  70. */
  71. int InFd;
  72. /** \brief A file descriptor connected to the standard input of the
  73. * subprocess.
  74. *
  75. * Used to send commands and configuration data to the subprocess.
  76. */
  77. int OutFd;
  78. /** \brief Set to \b true if the worker is in a state in which it
  79. * might generate data or command responses.
  80. *
  81. * \todo Is this right? It's a guess.
  82. */
  83. bool InReady;
  84. /** \brief Set to \b true if the worker is in a state in which it
  85. * is legal to send commands to it.
  86. *
  87. * \todo Is this right?
  88. */
  89. bool OutReady;
  90. /** If \b true, debugging output will be sent to std::clog. */
  91. bool Debug;
  92. /** \brief The raw text values of messages received from the
  93. * worker, in sequence.
  94. */
  95. vector<string> MessageQueue;
  96. /** \brief Buffers pending writes to the subprocess.
  97. *
  98. * \todo Wouldn't a std::dequeue be more appropriate?
  99. */
  100. string OutQueue;
  101. /** \brief Common code for the constructor.
  102. *
  103. * Initializes NextQueue and NextAcquire to NULL; Process, InFd,
  104. * and OutFd to -1, OutReady and InReady to \b false, and Debug
  105. * from _config.
  106. */
  107. void Construct();
  108. /** \brief Retrieve any available messages from the subprocess.
  109. *
  110. * The messages are retrieved as in ::ReadMessages(), and
  111. * MessageFailure() is invoked if an error occurs; in particular,
  112. * if the pipe to the subprocess dies unexpectedly while a message
  113. * is being read.
  114. *
  115. * \return \b true if the messages were successfully read, \b
  116. * false otherwise.
  117. */
  118. bool ReadMessages();
  119. /** \brief Parse and dispatch pending messages.
  120. *
  121. * This dispatches the message in a manner appropriate for its
  122. * type.
  123. *
  124. * \todo Several message types lack separate handlers.
  125. *
  126. * \sa Capabilities(), SendConfiguration(), MediaChange()
  127. */
  128. bool RunMessages();
  129. /** \brief Read and dispatch any pending messages from the
  130. * subprocess.
  131. *
  132. * \return \b false if the subprocess died unexpectedly while a
  133. * message was being transmitted.
  134. */
  135. bool InFdReady();
  136. /** \brief Send any pending commands to the subprocess.
  137. *
  138. * This method will fail if there is no pending output.
  139. *
  140. * \return \b true if all commands were succeeded, \b false if an
  141. * error occurred (in which case MethodFailure() will be invoked).
  142. */
  143. bool OutFdReady();
  144. /** \brief Handle a 100 Capabilities response from the subprocess.
  145. *
  146. * \param Message the raw text of the message from the subprocess.
  147. *
  148. * The message will be parsed and its contents used to fill
  149. * #Config. If #Config is NULL, this routine is a NOP.
  150. *
  151. * \return \b true.
  152. */
  153. bool Capabilities(string Message);
  154. /** \brief Send a 601 Configuration message (containing the APT
  155. * configuration) to the subprocess.
  156. *
  157. * The APT configuration will be send to the subprocess in a
  158. * message of the following form:
  159. *
  160. * <pre>
  161. * 601 Configuration
  162. * Config-Item: Fully-Qualified-Item=Val
  163. * Config-Item: Fully-Qualified-Item=Val
  164. * ...
  165. * </pre>
  166. *
  167. * \return \b true if the command was successfully sent, \b false
  168. * otherwise.
  169. */
  170. bool SendConfiguration();
  171. /** \brief Handle a 403 Media Change message.
  172. *
  173. * \param Message the raw text of the message; the Media field
  174. * indicates what type of media should be changed, and the Drive
  175. * field indicates where the media is located.
  176. *
  177. * Invokes pkgAcquireStatus::MediaChange(Media, Drive) to ask the
  178. * user to swap disks; informs the subprocess of the result (via
  179. * 603 Media Changed, with the Failed field set to \b true if the
  180. * user cancelled the media change).
  181. */
  182. bool MediaChange(string Message);
  183. /** \brief Invoked when the worked process dies unexpectedly.
  184. *
  185. * Waits for the subprocess to terminate and generates an error if
  186. * it terminated abnormally, then closes and blanks out all file
  187. * descriptors. Discards all pending messages from the
  188. * subprocess.
  189. *
  190. * \return \b false.
  191. */
  192. bool MethodFailure();
  193. /** \brief Invoked when a fetch job is completed, either
  194. * successfully or unsuccessfully.
  195. *
  196. * Resets the status information for the worker process.
  197. */
  198. void ItemDone();
  199. public:
  200. /** \brief The queue entry that is currently being downloaded. */
  201. pkgAcquire::Queue::QItem *CurrentItem;
  202. /** \brief The most recent status string received from the
  203. * subprocess.
  204. */
  205. string Status;
  206. /** \brief How many bytes of the file have been downloaded. Zero
  207. * if the current progress of the file cannot be determined.
  208. */
  209. unsigned long CurrentSize;
  210. /** \brief The total number of bytes to be downloaded. Zero if the
  211. * total size of the final is unknown.
  212. */
  213. unsigned long TotalSize;
  214. /** \brief How much of the file was already downloaded prior to
  215. * starting this worker.
  216. */
  217. unsigned long ResumePoint;
  218. /** \brief Tell the subprocess to download the given item.
  219. *
  220. * \param Item the item to queue up.
  221. * \return \b true if the item was successfully enqueued.
  222. *
  223. * Queues up a 600 URI Acquire message for the given item to be
  224. * sent at the next possible moment. Does \e not flush the output
  225. * queue.
  226. */
  227. bool QueueItem(pkgAcquire::Queue::QItem *Item);
  228. /** \brief Start up the worker and fill in #Config.
  229. *
  230. * Reads the first message from the worker, which is assumed to be
  231. * a 100 Capabilities message.
  232. *
  233. * \return \b true if all operations completed successfully.
  234. */
  235. bool Start();
  236. /** \brief Update the worker statistics (CurrentSize, TotalSize,
  237. * etc).
  238. */
  239. void Pulse();
  240. /** \return The fetch method configuration. */
  241. inline const MethodConfig *GetConf() const {return Config;};
  242. /** \brief Create a new Worker to download files.
  243. *
  244. * \param OwnerQ The queue into which this worker should be
  245. * placed.
  246. *
  247. * \param Config A location in which to store information about
  248. * the fetch method.
  249. *
  250. * \param Log The download progress indicator that should be used
  251. * to report the progress of this worker.
  252. */
  253. Worker(Queue *OwnerQ,MethodConfig *Config,pkgAcquireStatus *Log);
  254. /** \brief Create a new Worker that should just retrieve
  255. * information about the fetch method.
  256. *
  257. * Nothing in particular forces you to refrain from actually
  258. * downloading stuff, but the various status callbacks won't be
  259. * invoked.
  260. *
  261. * \param Config A location in which to store information about
  262. * the fetch method.
  263. */
  264. Worker(MethodConfig *Config);
  265. /** \brief Clean up this worker.
  266. *
  267. * Closes the file descriptors; if MethodConfig::NeedsCleanup is
  268. * \b false, also rudely interrupts the worker with a SIGINT.
  269. */
  270. ~Worker();
  271. };
  272. /** @} */
  273. #endif