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