acquire-worker.h 9.5 KB

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