apt @VERSION@

acquire.h

Go to the documentation of this file.
00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 // $Id: acquire.h,v 1.29.2.1 2003/12/24 23:09:17 mdz Exp $
00004 /* ######################################################################
00005 
00006    Acquire - File Acquiration
00007    
00008    This module contians the Acquire system. It is responsible for bringing
00009    files into the local pathname space. It deals with URIs for files and
00010    URI handlers responsible for downloading or finding the URIs.
00011    
00012    Each file to download is represented by an Acquire::Item class subclassed
00013    into a specialization. The Item class can add itself to several URI
00014    acquire queues each prioritized by the download scheduler. When the 
00015    system is run the proper URI handlers are spawned and the the acquire 
00016    queues are fed into the handlers by the schedular until the queues are
00017    empty. This allows for an Item to be downloaded from an alternate source
00018    if the first try turns out to fail. It also alows concurrent downloading
00019    of multiple items from multiple sources as well as dynamic balancing
00020    of load between the sources.
00021    
00022    Schedualing of downloads is done on a first ask first get basis. This
00023    preserves the order of the download as much as possible. And means the
00024    fastest source will tend to process the largest number of files.
00025    
00026    Internal methods and queues for performing gzip decompression,
00027    md5sum hashing and file copying are provided to allow items to apply
00028    a number of transformations to the data files they are working with.
00029    
00030    ##################################################################### */
00031                                                                         /*}}}*/
00032                                                                         /*}}}*/
00058 
00066 #ifndef PKGLIB_ACQUIRE_H
00067 #define PKGLIB_ACQUIRE_H
00068 
00069 #include <apt-pkg/macros.h>
00070 #include <apt-pkg/weakptr.h>
00071 
00072 #include <vector>
00073 #include <string>
00074 
00075 using std::vector;
00076 using std::string;
00077 
00078 
00079 #include <sys/time.h>
00080 #include <unistd.h>
00081 
00082 class pkgAcquireStatus;
00083 
00092 class pkgAcquire
00093 {   
00094    public:
00095    
00096    class Item;
00097    class Queue;
00098    class Worker;
00099    struct MethodConfig;
00100    struct ItemDesc;
00101    friend class Item;
00102    friend class Queue;
00103 
00104    typedef vector<Item *>::iterator ItemIterator;
00105    typedef vector<Item *>::const_iterator ItemCIterator;
00106 
00107    protected:
00108    
00114    vector<Item *> Items;
00115    
00121    Queue *Queues;
00122 
00128    Worker *Workers;
00129 
00140    MethodConfig *Configs;
00141 
00143    pkgAcquireStatus *Log;
00144 
00151    unsigned long ToFetch;
00152 
00153    // Configurable parameters for the scheduler
00154 
00156    enum QueueStrategy {
00160      QueueHost,
00164      QueueAccess} QueueMode;
00165 
00167    bool const Debug;
00169    bool Running;
00170 
00172    void Add(Item *Item);
00173 
00175    void Remove(Item *Item);
00176 
00178    void Add(Worker *Work);
00179 
00181    void Remove(Worker *Work);
00182    
00189    void Enqueue(ItemDesc &Item);
00190 
00192    void Dequeue(Item *Item);
00193 
00204    string QueueName(string URI,MethodConfig const *&Config);
00205 
00220    virtual void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
00221 
00232    virtual void RunFds(fd_set *RSet,fd_set *WSet);   
00233 
00240    void Bump();
00241    
00242    public:
00243 
00250    MethodConfig *GetConfig(string Access);
00251 
00253    enum RunResult {
00255      Continue,
00256 
00258      Failed,
00259 
00263      Cancelled};
00264 
00276    RunResult Run(int PulseInterval=500000);
00277 
00281    void Shutdown();
00282    
00287    inline Worker *WorkersBegin() {return Workers;};
00288 
00294    Worker *WorkerStep(Worker *I);
00295 
00297    inline ItemIterator ItemsBegin() {return Items.begin();};
00298 
00300    inline ItemIterator ItemsEnd() {return Items.end();};
00301    
00302    // Iterate over queued Item URIs
00303    class UriIterator;
00309    UriIterator UriBegin();
00311    UriIterator UriEnd();
00312    
00321    bool Clean(string Dir);
00322 
00326    unsigned long long TotalNeeded();
00327 
00331    unsigned long long FetchNeeded();
00332 
00336    unsigned long long PartialPresent();
00337 
00349    bool Setup(pkgAcquireStatus *Progress = NULL, string const &Lock = "");
00350 
00352    pkgAcquire(pkgAcquireStatus *Log) __deprecated;
00353    pkgAcquire();
00354 
00360    virtual ~pkgAcquire();
00361 
00362    private:
00364    int LockFD;
00365 };
00366 
00372 struct pkgAcquire::ItemDesc : public WeakPointable
00373 {
00375    string URI;
00377    string Description;
00379    string ShortDesc;
00381    Item *Owner;
00382 };
00383                                                                         /*}}}*/
00388 class pkgAcquire::Queue
00389 {
00390    friend class pkgAcquire;
00391    friend class pkgAcquire::UriIterator;
00392    friend class pkgAcquire::Worker;
00393 
00395    Queue *Next;
00396    
00397    protected:
00398 
00400    struct QItem : pkgAcquire::ItemDesc
00401    {
00403       QItem *Next;
00405       pkgAcquire::Worker *Worker;
00406 
00410       void operator =(pkgAcquire::ItemDesc const &I)
00411       {
00412          URI = I.URI;
00413          Description = I.Description;
00414          ShortDesc = I.ShortDesc;
00415          Owner = I.Owner;
00416       };
00417    };
00418    
00420    string Name;
00421 
00426    QItem *Items;
00427 
00437    pkgAcquire::Worker *Workers;
00438 
00440    pkgAcquire *Owner;
00441 
00445    signed long PipeDepth;
00446 
00450    unsigned long MaxPipeDepth;
00451    
00452    public:
00453    
00459    bool Enqueue(ItemDesc &Item);
00460 
00465    bool Dequeue(Item *Owner);
00466 
00475    QItem *FindItem(string URI,pkgAcquire::Worker *Owner);
00476 
00481    bool ItemStart(QItem *Itm,unsigned long Size);
00482 
00493    bool ItemDone(QItem *Itm);
00494    
00502    bool Startup();
00503 
00513    bool Shutdown(bool Final);
00514 
00519    bool Cycle();
00520 
00531    void Bump();
00532    
00538    Queue(string Name,pkgAcquire *Owner);
00539 
00543    ~Queue();
00544 };
00545                                                                         /*}}}*/
00547 class pkgAcquire::UriIterator
00548 {
00550    pkgAcquire::Queue *CurQ;
00552    pkgAcquire::Queue::QItem *CurItem;
00553    
00554    public:
00555    
00556    inline void operator ++() {operator ++(0);};
00557 
00558    void operator ++(int)
00559    {
00560       CurItem = CurItem->Next;
00561       while (CurItem == 0 && CurQ != 0)
00562       {
00563          CurItem = CurQ->Items;
00564          CurQ = CurQ->Next;
00565       }
00566    };
00567    
00568    inline pkgAcquire::ItemDesc const *operator ->() const {return CurItem;};
00569    inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;};
00570    inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;};
00571    
00576    UriIterator(pkgAcquire::Queue *Q) : CurQ(Q), CurItem(0)
00577    {
00578       while (CurItem == 0 && CurQ != 0)
00579       {
00580          CurItem = CurQ->Items;
00581          CurQ = CurQ->Next;
00582       }
00583    }   
00584 };
00585                                                                         /*}}}*/
00587 struct pkgAcquire::MethodConfig
00588 {
00593    MethodConfig *Next;
00594    
00596    string Access;
00597 
00599    string Version;
00600 
00604    bool SingleInstance;
00605 
00607    bool Pipeline;
00608 
00613    bool SendConfig;
00614 
00618    bool LocalOnly;
00619 
00626    bool NeedsCleanup;
00627 
00629    bool Removable;
00630    
00636    MethodConfig();
00637 };
00638                                                                         /*}}}*/
00645 class pkgAcquireStatus
00646 {
00647    protected:
00648    
00650    struct timeval Time;
00651 
00653    struct timeval StartTime;
00654 
00658    double LastBytes;
00659 
00663    double CurrentCPS;
00664 
00668    double CurrentBytes;
00669 
00675    double TotalBytes;
00676 
00680    double FetchedBytes;
00681 
00685    unsigned long ElapsedTime;
00686 
00692    unsigned long TotalItems;
00693 
00695    unsigned long CurrentItems;
00696    
00697    public:
00698 
00702    bool Update;
00703 
00710    bool MorePulses;
00711       
00718    virtual void Fetched(unsigned long Size,unsigned long ResumePoint);
00719    
00737    virtual bool MediaChange(string Media,string Drive) = 0;
00738    
00744    virtual void IMSHit(pkgAcquire::ItemDesc &/*Itm*/) {};
00745 
00747    virtual void Fetch(pkgAcquire::ItemDesc &/*Itm*/) {};
00748 
00750    virtual void Done(pkgAcquire::ItemDesc &/*Itm*/) {};
00751 
00755    virtual void Fail(pkgAcquire::ItemDesc &/*Itm*/) {};
00756 
00767    virtual bool Pulse(pkgAcquire *Owner);
00768 
00770    virtual void Start();
00771 
00773    virtual void Stop();
00774    
00776    pkgAcquireStatus();
00777    virtual ~pkgAcquireStatus() {};
00778 };
00779                                                                         /*}}}*/
00782 #endif