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