apt  1.5
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 #ifndef APT_8_CLEANER_HEADERS
00079 using std::vector;
00080 using std::string;
00081 #endif
00082 
00083 class pkgAcquireStatus;
00084 
00093 class pkgAcquire
00094 {   
00095    private:
00097    int LockFD;
00099    void *d;
00100 
00101    public:
00102    
00103    class Item;
00104    class Queue;
00105    class Worker;
00106    struct MethodConfig;
00107    struct ItemDesc;
00108    friend class Item;
00109    friend class Queue;
00110 
00111    typedef std::vector<Item *>::iterator ItemIterator;
00112    typedef std::vector<Item *>::const_iterator ItemCIterator;
00113 
00114    protected:
00115    
00121    std::vector<Item *> Items;
00122    
00128    Queue *Queues;
00129 
00135    Worker *Workers;
00136 
00147    MethodConfig *Configs;
00148 
00150    pkgAcquireStatus *Log;
00151 
00153    unsigned long ToFetch;
00154 
00155    // Configurable parameters for the scheduler
00156 
00158    enum QueueStrategy {
00162      QueueHost,
00166      QueueAccess} QueueMode;
00167 
00169    bool const Debug;
00171    bool Running;
00172 
00174    void Add(Item *Item);
00175 
00177    void Remove(Item *Item);
00178 
00180    void Add(Worker *Work);
00181 
00183    void Remove(Worker *Work);
00184    
00191    void Enqueue(ItemDesc &Item);
00192 
00194    void Dequeue(Item *Item);
00195 
00206    std::string QueueName(std::string URI,MethodConfig const *&Config);
00207 
00222    virtual void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
00223 
00234    virtual void RunFds(fd_set *RSet,fd_set *WSet);   
00235 
00242    void Bump();
00243    
00244    public:
00245 
00252    MethodConfig *GetConfig(std::string Access);
00253 
00255    enum RunResult {
00257      Continue,
00258 
00260      Failed,
00261 
00265      Cancelled};
00266 
00278    RunResult Run(int PulseInterval=500000);
00279 
00283    void Shutdown();
00284    
00289    inline Worker *WorkersBegin() {return Workers;};
00290 
00296    Worker *WorkerStep(Worker *I);
00297 
00299    inline ItemIterator ItemsBegin() {return Items.begin();};
00300 
00302    inline ItemIterator ItemsEnd() {return Items.end();};
00303    
00304    // Iterate over queued Item URIs
00305    class UriIterator;
00311    UriIterator UriBegin();
00313    UriIterator UriEnd();
00314    
00323    bool Clean(std::string Dir);
00324 
00328    unsigned long long TotalNeeded();
00329 
00333    unsigned long long FetchNeeded();
00334 
00338    unsigned long long PartialPresent();
00339 
00351    bool Setup(pkgAcquireStatus *Progress = NULL, std::string const &Lock = "");
00352 
00353    void SetLog(pkgAcquireStatus *Progress) { Log = Progress; }
00354 
00356    pkgAcquire(pkgAcquireStatus *Log) __deprecated;
00357    pkgAcquire();
00358 
00364    virtual ~pkgAcquire();
00365 
00366 };
00367 
00373 struct pkgAcquire::ItemDesc : public WeakPointable
00374 {
00376    std::string URI;
00378    std::string Description;
00380    std::string ShortDesc;
00382    Item *Owner;
00383 };
00384                                                                         /*}}}*/
00389 class pkgAcquire::Queue
00390 {
00391    friend class pkgAcquire;
00392    friend class pkgAcquire::UriIterator;
00393    friend class pkgAcquire::Worker;
00394 
00396    void *d;
00397 
00399    Queue *Next;
00400    
00401    protected:
00402 
00404    struct QItem : pkgAcquire::ItemDesc
00405    {
00407       QItem *Next;
00409       pkgAcquire::Worker *Worker;
00410 
00414       void operator =(pkgAcquire::ItemDesc const &I)
00415       {
00416          URI = I.URI;
00417          Description = I.Description;
00418          ShortDesc = I.ShortDesc;
00419          Owner = I.Owner;
00420       };
00421    };
00422    
00424    std::string Name;
00425 
00430    QItem *Items;
00431 
00441    pkgAcquire::Worker *Workers;
00442 
00444    pkgAcquire *Owner;
00445 
00449    signed long PipeDepth;
00450 
00454    unsigned long MaxPipeDepth;
00455    
00456    public:
00457    
00463    bool Enqueue(ItemDesc &Item);
00464 
00469    bool Dequeue(Item *Owner);
00470 
00479    QItem *FindItem(std::string URI,pkgAcquire::Worker *Owner);
00480 
00485    bool ItemStart(QItem *Itm,unsigned long long Size);
00486 
00497    bool ItemDone(QItem *Itm);
00498    
00506    bool Startup();
00507 
00517    bool Shutdown(bool Final);
00518 
00523    bool Cycle();
00524 
00535    void Bump();
00536    
00542    Queue(std::string Name,pkgAcquire *Owner);
00543 
00547    virtual ~Queue();
00548 };
00549                                                                         /*}}}*/
00551 class pkgAcquire::UriIterator
00552 {
00554    void *d;
00555 
00557    pkgAcquire::Queue *CurQ;
00559    pkgAcquire::Queue::QItem *CurItem;
00560    
00561    public:
00562    
00563    inline void operator ++() {operator ++(0);};
00564 
00565    void operator ++(int)
00566    {
00567       CurItem = CurItem->Next;
00568       while (CurItem == 0 && CurQ != 0)
00569       {
00570          CurItem = CurQ->Items;
00571          CurQ = CurQ->Next;
00572       }
00573    };
00574    
00575    inline pkgAcquire::ItemDesc const *operator ->() const {return CurItem;};
00576    inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;};
00577    inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;};
00578    
00583    UriIterator(pkgAcquire::Queue *Q) : CurQ(Q), CurItem(0)
00584    {
00585       while (CurItem == 0 && CurQ != 0)
00586       {
00587          CurItem = CurQ->Items;
00588          CurQ = CurQ->Next;
00589       }
00590    }   
00591    virtual ~UriIterator() {};
00592 };
00593                                                                         /*}}}*/
00595 struct pkgAcquire::MethodConfig
00596 {
00598    void *d;
00599    
00604    MethodConfig *Next;
00605    
00607    std::string Access;
00608 
00610    std::string Version;
00611 
00615    bool SingleInstance;
00616 
00618    bool Pipeline;
00619 
00624    bool SendConfig;
00625 
00629    bool LocalOnly;
00630 
00637    bool NeedsCleanup;
00638 
00640    bool Removable;
00641    
00647    MethodConfig();
00648 
00649    /* \brief Destructor, empty currently */
00650    virtual ~MethodConfig() {};
00651 };
00652                                                                         /*}}}*/
00657 class pkgAcquireStatus
00658 {
00660    void *d;
00661 
00662    protected:
00663    
00665    struct timeval Time;
00666 
00668    struct timeval StartTime;
00669 
00673    unsigned long long LastBytes;
00674 
00678    unsigned long long CurrentCPS;
00679 
00683    unsigned long long CurrentBytes;
00684 
00690    unsigned long long TotalBytes;
00691 
00695    unsigned long long FetchedBytes;
00696 
00700    unsigned long long ElapsedTime;
00701 
00707    unsigned long TotalItems;
00708 
00710    unsigned long CurrentItems;
00711    
00712    public:
00713 
00717    bool Update;
00718 
00725    bool MorePulses;
00726       
00733    virtual void Fetched(unsigned long long Size,unsigned long long ResumePoint);
00734    
00752    virtual bool MediaChange(std::string Media,std::string Drive) = 0;
00753    
00759    virtual void IMSHit(pkgAcquire::ItemDesc &/*Itm*/) {};
00760 
00762    virtual void Fetch(pkgAcquire::ItemDesc &/*Itm*/) {};
00763 
00765    virtual void Done(pkgAcquire::ItemDesc &/*Itm*/) {};
00766 
00770    virtual void Fail(pkgAcquire::ItemDesc &/*Itm*/) {};
00771 
00782    virtual bool Pulse(pkgAcquire *Owner);
00783 
00785    virtual void Start();
00786 
00788    virtual void Stop();
00789    
00791    pkgAcquireStatus();
00792    virtual ~pkgAcquireStatus() {};
00793 };
00794                                                                         /*}}}*/
00797 #endif