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