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 #include <sys/time.h>
00076 #include <unistd.h>
00077 
00078 class pkgAcquireStatus;
00079 
00088 class pkgAcquire
00089 {   
00090    private:
00092    int LockFD;
00094    void *d;
00095 
00096    public:
00097    
00098    class Item;
00099    class Queue;
00100    class Worker;
00101    struct MethodConfig;
00102    struct ItemDesc;
00103    friend class Item;
00104    friend class Queue;
00105 
00106    typedef std::vector<Item *>::iterator ItemIterator;
00107    typedef std::vector<Item *>::const_iterator ItemCIterator;
00108 
00109    protected:
00110    
00116    std::vector<Item *> Items;
00117    
00123    Queue *Queues;
00124 
00130    Worker *Workers;
00131 
00142    MethodConfig *Configs;
00143 
00145    pkgAcquireStatus *Log;
00146 
00148    unsigned long ToFetch;
00149 
00150    // Configurable parameters for the scheduler
00151 
00153    enum QueueStrategy {
00157      QueueHost,
00161      QueueAccess} QueueMode;
00162 
00164    bool const Debug;
00166    bool Running;
00167 
00169    void Add(Item *Item);
00170 
00172    void Remove(Item *Item);
00173 
00175    void Add(Worker *Work);
00176 
00178    void Remove(Worker *Work);
00179    
00186    void Enqueue(ItemDesc &Item);
00187 
00189    void Dequeue(Item *Item);
00190 
00201    std::string QueueName(std::string URI,MethodConfig const *&Config);
00202 
00217    virtual void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
00218 
00229    virtual void RunFds(fd_set *RSet,fd_set *WSet);   
00230 
00237    void Bump();
00238    
00239    public:
00240 
00247    MethodConfig *GetConfig(std::string Access);
00248 
00250    enum RunResult {
00252      Continue,
00253 
00255      Failed,
00256 
00260      Cancelled};
00261 
00273    RunResult Run(int PulseInterval=500000);
00274 
00278    void Shutdown();
00279    
00284    inline Worker *WorkersBegin() {return Workers;};
00285 
00291    Worker *WorkerStep(Worker *I);
00292 
00294    inline ItemIterator ItemsBegin() {return Items.begin();};
00295 
00297    inline ItemIterator ItemsEnd() {return Items.end();};
00298    
00299    // Iterate over queued Item URIs
00300    class UriIterator;
00306    UriIterator UriBegin();
00308    UriIterator UriEnd();
00309    
00318    bool Clean(std::string Dir);
00319 
00323    unsigned long long TotalNeeded();
00324 
00328    unsigned long long FetchNeeded();
00329 
00333    unsigned long long PartialPresent();
00334 
00346    bool Setup(pkgAcquireStatus *Progress = NULL, std::string const &Lock = "");
00347 
00348    void SetLog(pkgAcquireStatus *Progress) { Log = Progress; }
00349 
00351    pkgAcquire(pkgAcquireStatus *Log) __deprecated;
00352    pkgAcquire();
00353 
00359    virtual ~pkgAcquire();
00360 
00361 };
00362 
00368 struct pkgAcquire::ItemDesc : public WeakPointable
00369 {
00371    std::string URI;
00373    std::string Description;
00375    std::string ShortDesc;
00377    Item *Owner;
00378 };
00379                                                                         /*}}}*/
00384 class pkgAcquire::Queue
00385 {
00386    friend class pkgAcquire;
00387    friend class pkgAcquire::UriIterator;
00388    friend class pkgAcquire::Worker;
00389 
00391    void *d;
00392 
00394    Queue *Next;
00395    
00396    protected:
00397 
00399    struct QItem : pkgAcquire::ItemDesc
00400    {
00402       QItem *Next;
00404       pkgAcquire::Worker *Worker;
00405 
00409       void operator =(pkgAcquire::ItemDesc const &I)
00410       {
00411          URI = I.URI;
00412          Description = I.Description;
00413          ShortDesc = I.ShortDesc;
00414          Owner = I.Owner;
00415       };
00416    };
00417    
00419    std::string Name;
00420 
00425    QItem *Items;
00426 
00436    pkgAcquire::Worker *Workers;
00437 
00439    pkgAcquire *Owner;
00440 
00444    signed long PipeDepth;
00445 
00449    unsigned long MaxPipeDepth;
00450    
00451    public:
00452    
00458    bool Enqueue(ItemDesc &Item);
00459 
00464    bool Dequeue(Item *Owner);
00465 
00474    QItem *FindItem(std::string URI,pkgAcquire::Worker *Owner);
00475 
00480    bool ItemStart(QItem *Itm,unsigned long long Size);
00481 
00492    bool ItemDone(QItem *Itm);
00493    
00501    bool Startup();
00502 
00512    bool Shutdown(bool Final);
00513 
00518    bool Cycle();
00519 
00530    void Bump();
00531    
00537    Queue(std::string Name,pkgAcquire *Owner);
00538 
00542    virtual ~Queue();
00543 };
00544                                                                         /*}}}*/
00546 class pkgAcquire::UriIterator
00547 {
00549    void *d;
00550 
00552    pkgAcquire::Queue *CurQ;
00554    pkgAcquire::Queue::QItem *CurItem;
00555    
00556    public:
00557    
00558    inline void operator ++() {operator ++(0);};
00559 
00560    void operator ++(int)
00561    {
00562       CurItem = CurItem->Next;
00563       while (CurItem == 0 && CurQ != 0)
00564       {
00565          CurItem = CurQ->Items;
00566          CurQ = CurQ->Next;
00567       }
00568    };
00569    
00570    inline pkgAcquire::ItemDesc const *operator ->() const {return CurItem;};
00571    inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;};
00572    inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;};
00573    
00578    UriIterator(pkgAcquire::Queue *Q) : CurQ(Q), CurItem(0)
00579    {
00580       while (CurItem == 0 && CurQ != 0)
00581       {
00582          CurItem = CurQ->Items;
00583          CurQ = CurQ->Next;
00584       }
00585    }   
00586    virtual ~UriIterator() {};
00587 };
00588                                                                         /*}}}*/
00590 struct pkgAcquire::MethodConfig
00591 {
00593    void *d;
00594    
00599    MethodConfig *Next;
00600    
00602    std::string Access;
00603 
00605    std::string Version;
00606 
00610    bool SingleInstance;
00611 
00613    bool Pipeline;
00614 
00619    bool SendConfig;
00620 
00624    bool LocalOnly;
00625 
00632    bool NeedsCleanup;
00633 
00635    bool Removable;
00636    
00642    MethodConfig();
00643 
00644    /* \brief Destructor, empty currently */
00645    virtual ~MethodConfig() {};
00646 };
00647                                                                         /*}}}*/
00652 class pkgAcquireStatus
00653 {
00655    void *d;
00656 
00657    protected:
00658    
00660    struct timeval Time;
00661 
00663    struct timeval StartTime;
00664 
00668    unsigned long long LastBytes;
00669 
00673    unsigned long long CurrentCPS;
00674 
00678    unsigned long long CurrentBytes;
00679 
00685    unsigned long long TotalBytes;
00686 
00690    unsigned long long FetchedBytes;
00691 
00695    unsigned long long ElapsedTime;
00696 
00702    unsigned long TotalItems;
00703 
00705    unsigned long CurrentItems;
00706    
00707    public:
00708 
00712    bool Update;
00713 
00720    bool MorePulses;
00721       
00728    virtual void Fetched(unsigned long long Size,unsigned long long ResumePoint);
00729    
00747    virtual bool MediaChange(std::string Media,std::string Drive) = 0;
00748    
00754    virtual void IMSHit(pkgAcquire::ItemDesc &/*Itm*/) {};
00755 
00757    virtual void Fetch(pkgAcquire::ItemDesc &/*Itm*/) {};
00758 
00760    virtual void Done(pkgAcquire::ItemDesc &/*Itm*/) {};
00761 
00765    virtual void Fail(pkgAcquire::ItemDesc &/*Itm*/) {};
00766 
00777    virtual bool Pulse(pkgAcquire *Owner);
00778 
00780    virtual void Start();
00781 
00783    virtual void Stop();
00784    
00786    pkgAcquireStatus();
00787    virtual ~pkgAcquireStatus() {};
00788 };
00789                                                                         /*}}}*/
00792 #endif