acquire-worker.h 9.5 KB

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