]>
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 MV |
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 | */ | |
0a8a80e5 | 109 | bool InReady; |
3174e150 MV |
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 | */ | |
0a8a80e5 | 116 | bool OutReady; |
0118833a | 117 | |
3174e150 | 118 | /** If \b true, debugging output will be sent to std::clog. */ |
3b5421b4 | 119 | bool Debug; |
3174e150 MV |
120 | |
121 | /** \brief The raw text values of messages received from the | |
122 | * worker, in sequence. | |
123 | */ | |
8f3ba4e8 | 124 | std::vector<std::string> MessageQueue; |
3174e150 MV |
125 | |
126 | /** \brief Buffers pending writes to the subprocess. | |
127 | * | |
128 | * \todo Wouldn't a std::dequeue be more appropriate? | |
129 | */ | |
8f3ba4e8 | 130 | std::string OutQueue; |
0a8a80e5 | 131 | |
3174e150 MV |
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 | */ | |
3b5421b4 AL |
138 | void Construct(); |
139 | ||
3174e150 MV |
140 | /** \brief Retrieve any available messages from the subprocess. |
141 | * | |
255c9e4b DK |
142 | * The messages are retrieved as in \link strutl.h ReadMessages()\endlink, and |
143 | * #MethodFailure() is invoked if an error occurs; in particular, | |
3174e150 MV |
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 | */ | |
3b5421b4 | 150 | bool ReadMessages(); |
3174e150 MV |
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 | */ | |
3b5421b4 | 161 | bool RunMessages(); |
3174e150 MV |
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 | */ | |
0a8a80e5 | 169 | bool InFdReady(); |
3174e150 MV |
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 | */ | |
0a8a80e5 | 178 | bool OutFdReady(); |
3b5421b4 | 179 | |
3174e150 MV |
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 | */ | |
8f3ba4e8 | 189 | bool Capabilities(std::string Message); |
3174e150 MV |
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 | */ | |
0a8a80e5 | 207 | bool SendConfiguration(); |
3174e150 MV |
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 | */ | |
8f3ba4e8 | 220 | bool MediaChange(std::string Message); |
542ec555 | 221 | |
3174e150 MV |
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 | */ | |
0a8a80e5 | 231 | bool MethodFailure(); |
3174e150 MV |
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 | */ | |
8267fe24 | 238 | void ItemDone(); |
0118833a AL |
239 | |
240 | public: | |
241 | ||
3174e150 | 242 | /** \brief The queue entry that is currently being downloaded. */ |
0a8a80e5 | 243 | pkgAcquire::Queue::QItem *CurrentItem; |
3174e150 MV |
244 | |
245 | /** \brief The most recent status string received from the | |
246 | * subprocess. | |
247 | */ | |
8f3ba4e8 | 248 | std::string Status; |
3174e150 MV |
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 | */ | |
73da43e9 | 253 | unsigned long long CurrentSize; |
3174e150 MV |
254 | |
255 | /** \brief The total number of bytes to be downloaded. Zero if the | |
256 | * total size of the final is unknown. | |
257 | */ | |
73da43e9 | 258 | unsigned long long TotalSize; |
3174e150 MV |
259 | |
260 | /** \brief How much of the file was already downloaded prior to | |
261 | * starting this worker. | |
262 | */ | |
73da43e9 | 263 | unsigned long long ResumePoint; |
8b75eb1c | 264 | |
3174e150 MV |
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 | */ | |
0a8a80e5 | 274 | bool QueueItem(pkgAcquire::Queue::QItem *Item); |
3174e150 MV |
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 | */ | |
8267fe24 | 283 | bool Start(); |
3174e150 MV |
284 | |
285 | /** \brief Update the worker statistics (CurrentSize, TotalSize, | |
286 | * etc). | |
287 | */ | |
8267fe24 | 288 | void Pulse(); |
3174e150 MV |
289 | |
290 | /** \return The fetch method configuration. */ | |
8e5fc8f5 | 291 | inline const MethodConfig *GetConf() const {return Config;}; |
3174e150 MV |
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 | */ | |
8267fe24 | 304 | Worker(Queue *OwnerQ,MethodConfig *Config,pkgAcquireStatus *Log); |
3174e150 MV |
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 | */ | |
0118833a | 316 | Worker(MethodConfig *Config); |
3174e150 MV |
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 | */ | |
be9b62f7 | 323 | virtual ~Worker(); |
0118833a AL |
324 | }; |
325 | ||
3174e150 MV |
326 | /** @} */ |
327 | ||
0118833a | 328 | #endif |