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