]>
Commit | Line | Data |
---|---|---|
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 | |
453b82a3 DK |
25 | #include <sys/types.h> |
26 | #include <string> | |
27 | #include <vector> | |
0118833a | 28 | |
3174e150 MV |
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 | */ | |
229fb1a3 | 48 | class pkgAcquire::Worker : public WeakPointable |
0118833a | 49 | { |
be9b62f7 MV |
50 | /** \brief dpointer placeholder (for later in case we need it) */ |
51 | void *d; | |
52 | ||
b2e465d6 | 53 | friend class pkgAcquire; |
0a8a80e5 | 54 | |
0118833a | 55 | protected: |
b2e465d6 | 56 | friend class Queue; |
3b5421b4 | 57 | |
3174e150 MV |
58 | /** \brief The next link on the Queue list. |
59 | * | |
60 | * \todo This is always NULL; is it just for future use? | |
61 | */ | |
0a8a80e5 | 62 | Worker *NextQueue; |
3174e150 MV |
63 | |
64 | /** \brief The next link on the Acquire list. */ | |
0a8a80e5 | 65 | Worker *NextAcquire; |
0118833a | 66 | |
3174e150 | 67 | /** \brief The Queue with which this worker is associated. */ |
0118833a | 68 | Queue *OwnerQ; |
3174e150 MV |
69 | |
70 | /** \brief The download progress indicator to which progress | |
71 | * messages should be sent. | |
72 | */ | |
8267fe24 | 73 | pkgAcquireStatus *Log; |
3174e150 MV |
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 | */ | |
3b5421b4 | 79 | MethodConfig *Config; |
3174e150 MV |
80 | |
81 | /** \brief The access method to be used by this worker. | |
82 | * | |
83 | * \todo Doesn't this duplicate Config->Access? | |
84 | */ | |
8f3ba4e8 | 85 | std::string Access; |
c46824ce | 86 | |
3174e150 | 87 | /** \brief The PID of the subprocess. */ |
3b5421b4 | 88 | pid_t Process; |
3174e150 MV |
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 | */ | |
3b5421b4 | 95 | int InFd; |
3174e150 MV |
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 | */ | |
3b5421b4 | 102 | int OutFd; |
3174e150 | 103 | |
da6f750f MV |
104 | /** \brief The socket to send SCM_RIGHTS message through |
105 | */ | |
106 | int PrivSepSocketFd; | |
107 | int PrivSepSocketFdChild; | |
108 | ||
3174e150 MV |
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 | */ | |
0a8a80e5 | 114 | bool InReady; |
3174e150 MV |
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 | */ | |
0a8a80e5 | 121 | bool OutReady; |
0118833a | 122 | |
3174e150 | 123 | /** If \b true, debugging output will be sent to std::clog. */ |
3b5421b4 | 124 | bool Debug; |
3174e150 MV |
125 | |
126 | /** \brief The raw text values of messages received from the | |
127 | * worker, in sequence. | |
128 | */ | |
8f3ba4e8 | 129 | std::vector<std::string> MessageQueue; |
3174e150 MV |
130 | |
131 | /** \brief Buffers pending writes to the subprocess. | |
132 | * | |
133 | * \todo Wouldn't a std::dequeue be more appropriate? | |
134 | */ | |
8f3ba4e8 | 135 | std::string OutQueue; |
0a8a80e5 | 136 | |
3174e150 MV |
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 | */ | |
3b5421b4 AL |
143 | void Construct(); |
144 | ||
3174e150 MV |
145 | /** \brief Retrieve any available messages from the subprocess. |
146 | * | |
255c9e4b DK |
147 | * The messages are retrieved as in \link strutl.h ReadMessages()\endlink, and |
148 | * #MethodFailure() is invoked if an error occurs; in particular, | |
3174e150 MV |
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 | */ | |
3b5421b4 | 155 | bool ReadMessages(); |
3174e150 MV |
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 | */ | |
3b5421b4 | 166 | bool RunMessages(); |
3174e150 MV |
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 | */ | |
0a8a80e5 | 174 | bool InFdReady(); |
3174e150 MV |
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 | */ | |
0a8a80e5 | 183 | bool OutFdReady(); |
3b5421b4 | 184 | |
3174e150 MV |
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 | */ | |
8f3ba4e8 | 194 | bool Capabilities(std::string Message); |
3174e150 MV |
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 | */ | |
0a8a80e5 | 212 | bool SendConfiguration(); |
3174e150 MV |
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 | */ | |
8f3ba4e8 | 225 | bool MediaChange(std::string Message); |
542ec555 | 226 | |
3174e150 MV |
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 | */ | |
0a8a80e5 | 236 | bool MethodFailure(); |
3174e150 MV |
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 | */ | |
8267fe24 | 243 | void ItemDone(); |
0118833a AL |
244 | |
245 | public: | |
246 | ||
3174e150 | 247 | /** \brief The queue entry that is currently being downloaded. */ |
0a8a80e5 | 248 | pkgAcquire::Queue::QItem *CurrentItem; |
3174e150 MV |
249 | |
250 | /** \brief The most recent status string received from the | |
251 | * subprocess. | |
252 | */ | |
8f3ba4e8 | 253 | std::string Status; |
3174e150 MV |
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 | */ | |
73da43e9 | 258 | unsigned long long CurrentSize; |
3174e150 MV |
259 | |
260 | /** \brief The total number of bytes to be downloaded. Zero if the | |
261 | * total size of the final is unknown. | |
262 | */ | |
73da43e9 | 263 | unsigned long long TotalSize; |
3174e150 MV |
264 | |
265 | /** \brief How much of the file was already downloaded prior to | |
266 | * starting this worker. | |
267 | */ | |
73da43e9 | 268 | unsigned long long ResumePoint; |
8b75eb1c | 269 | |
3174e150 MV |
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 | */ | |
0a8a80e5 | 279 | bool QueueItem(pkgAcquire::Queue::QItem *Item); |
3174e150 MV |
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 | */ | |
8267fe24 | 288 | bool Start(); |
3174e150 MV |
289 | |
290 | /** \brief Update the worker statistics (CurrentSize, TotalSize, | |
291 | * etc). | |
292 | */ | |
8267fe24 | 293 | void Pulse(); |
3174e150 MV |
294 | |
295 | /** \return The fetch method configuration. */ | |
8e5fc8f5 | 296 | inline const MethodConfig *GetConf() const {return Config;}; |
3174e150 MV |
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 | */ | |
8267fe24 | 309 | Worker(Queue *OwnerQ,MethodConfig *Config,pkgAcquireStatus *Log); |
3174e150 MV |
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 | */ | |
0118833a | 321 | Worker(MethodConfig *Config); |
3174e150 MV |
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 | */ | |
be9b62f7 | 328 | virtual ~Worker(); |
0118833a AL |
329 | }; |
330 | ||
3174e150 MV |
331 | /** @} */ |
332 | ||
0118833a | 333 | #endif |