]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.h
adapt to the new CacheSetHelper API
[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 Set to \b true if the worker is in a state in which it
105 * might generate data or command responses.
106 *
107 * \todo Is this right? It's a guess.
108 */
109 bool InReady;
110
111 /** \brief Set to \b true if the worker is in a state in which it
112 * is legal to send commands to it.
113 *
114 * \todo Is this right?
115 */
116 bool OutReady;
117
118 /** If \b true, debugging output will be sent to std::clog. */
119 bool Debug;
120
121 /** \brief The raw text values of messages received from the
122 * worker, in sequence.
123 */
124 std::vector<std::string> MessageQueue;
125
126 /** \brief Buffers pending writes to the subprocess.
127 *
128 * \todo Wouldn't a std::dequeue be more appropriate?
129 */
130 std::string OutQueue;
131
132 /** \brief Common code for the constructor.
133 *
134 * Initializes NextQueue and NextAcquire to NULL; Process, InFd,
135 * and OutFd to -1, OutReady and InReady to \b false, and Debug
136 * from _config.
137 */
138 void Construct();
139
140 /** \brief Retrieve any available messages from the subprocess.
141 *
142 * The messages are retrieved as in \link strutl.h ReadMessages()\endlink, and
143 * #MethodFailure() is invoked if an error occurs; in particular,
144 * if the pipe to the subprocess dies unexpectedly while a message
145 * is being read.
146 *
147 * \return \b true if the messages were successfully read, \b
148 * false otherwise.
149 */
150 bool ReadMessages();
151
152 /** \brief Parse and dispatch pending messages.
153 *
154 * This dispatches the message in a manner appropriate for its
155 * type.
156 *
157 * \todo Several message types lack separate handlers.
158 *
159 * \sa Capabilities(), SendConfiguration(), MediaChange()
160 */
161 bool RunMessages();
162
163 /** \brief Read and dispatch any pending messages from the
164 * subprocess.
165 *
166 * \return \b false if the subprocess died unexpectedly while a
167 * message was being transmitted.
168 */
169 bool InFdReady();
170
171 /** \brief Send any pending commands to the subprocess.
172 *
173 * This method will fail if there is no pending output.
174 *
175 * \return \b true if all commands were succeeded, \b false if an
176 * error occurred (in which case MethodFailure() will be invoked).
177 */
178 bool OutFdReady();
179
180 /** \brief Handle a 100 Capabilities response from the subprocess.
181 *
182 * \param Message the raw text of the message from the subprocess.
183 *
184 * The message will be parsed and its contents used to fill
185 * #Config. If #Config is NULL, this routine is a NOP.
186 *
187 * \return \b true.
188 */
189 bool Capabilities(std::string Message);
190
191 /** \brief Send a 601 Configuration message (containing the APT
192 * configuration) to the subprocess.
193 *
194 * The APT configuration will be send to the subprocess in a
195 * message of the following form:
196 *
197 * <pre>
198 * 601 Configuration
199 * Config-Item: Fully-Qualified-Item=Val
200 * Config-Item: Fully-Qualified-Item=Val
201 * ...
202 * </pre>
203 *
204 * \return \b true if the command was successfully sent, \b false
205 * otherwise.
206 */
207 bool SendConfiguration();
208
209 /** \brief Handle a 403 Media Change message.
210 *
211 * \param Message the raw text of the message; the Media field
212 * indicates what type of media should be changed, and the Drive
213 * field indicates where the media is located.
214 *
215 * Invokes pkgAcquireStatus::MediaChange(Media, Drive) to ask the
216 * user to swap disks; informs the subprocess of the result (via
217 * 603 Media Changed, with the Failed field set to \b true if the
218 * user cancelled the media change).
219 */
220 bool MediaChange(std::string Message);
221
222 /** \brief Invoked when the worked process dies unexpectedly.
223 *
224 * Waits for the subprocess to terminate and generates an error if
225 * it terminated abnormally, then closes and blanks out all file
226 * descriptors. Discards all pending messages from the
227 * subprocess.
228 *
229 * \return \b false.
230 */
231 bool MethodFailure();
232
233 /** \brief Invoked when a fetch job is completed, either
234 * successfully or unsuccessfully.
235 *
236 * Resets the status information for the worker process.
237 */
238 void ItemDone();
239
240 public:
241
242 /** \brief The queue entry that is currently being downloaded. */
243 pkgAcquire::Queue::QItem *CurrentItem;
244
245 /** \brief The most recent status string received from the
246 * subprocess.
247 */
248 std::string Status;
249
250 /** \brief How many bytes of the file have been downloaded. Zero
251 * if the current progress of the file cannot be determined.
252 */
253 unsigned long long CurrentSize;
254
255 /** \brief The total number of bytes to be downloaded. Zero if the
256 * total size of the final is unknown.
257 */
258 unsigned long long TotalSize;
259
260 /** \brief How much of the file was already downloaded prior to
261 * starting this worker.
262 */
263 unsigned long long ResumePoint;
264
265 /** \brief Tell the subprocess to download the given item.
266 *
267 * \param Item the item to queue up.
268 * \return \b true if the item was successfully enqueued.
269 *
270 * Queues up a 600 URI Acquire message for the given item to be
271 * sent at the next possible moment. Does \e not flush the output
272 * queue.
273 */
274 bool QueueItem(pkgAcquire::Queue::QItem *Item);
275
276 /** \brief Start up the worker and fill in #Config.
277 *
278 * Reads the first message from the worker, which is assumed to be
279 * a 100 Capabilities message.
280 *
281 * \return \b true if all operations completed successfully.
282 */
283 bool Start();
284
285 /** \brief Update the worker statistics (CurrentSize, TotalSize,
286 * etc).
287 */
288 void Pulse();
289
290 /** \return The fetch method configuration. */
291 inline const MethodConfig *GetConf() const {return Config;};
292
293 /** \brief Create a new Worker to download files.
294 *
295 * \param OwnerQ The queue into which this worker should be
296 * placed.
297 *
298 * \param Config A location in which to store information about
299 * the fetch method.
300 *
301 * \param Log The download progress indicator that should be used
302 * to report the progress of this worker.
303 */
304 Worker(Queue *OwnerQ,MethodConfig *Config,pkgAcquireStatus *Log);
305
306 /** \brief Create a new Worker that should just retrieve
307 * information about the fetch method.
308 *
309 * Nothing in particular forces you to refrain from actually
310 * downloading stuff, but the various status callbacks won't be
311 * invoked.
312 *
313 * \param Config A location in which to store information about
314 * the fetch method.
315 */
316 Worker(MethodConfig *Config);
317
318 /** \brief Clean up this worker.
319 *
320 * Closes the file descriptors; if MethodConfig::NeedsCleanup is
321 * \b false, also rudely interrupts the worker with a SIGINT.
322 */
323 virtual ~Worker();
324 };
325
326 /** @} */
327
328 #endif