Back to index

nordugrid-arc-nox  1.1.0~rc6
DataBuffer.h
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifndef __ARC_DATABUFFER_H__
00004 #define __ARC_DATABUFFER_H__
00005 
00006 #include <arc/Thread.h>
00007 #include <arc/data/DataSpeed.h>
00008 
00009 namespace Arc {
00010 
00011   class CheckSum;
00012 
00014 
00015   class DataBuffer {
00016   private:
00017     /* used to check if configuration changed */
00018     int set_counter;
00019     /* general purpose mutex and condition used to
00020        achieve thread safety */
00021     Glib::Mutex lock;
00022     Glib::Cond cond;
00023     /* structure do describe status of every buffer */
00024     typedef struct {
00025       /* buffer address in memory */
00026       char *start;
00027       /* true if taken by application for filling */
00028       bool taken_for_read;
00029       /* true if taken by application for emptying */
00030       bool taken_for_write;
00031       /* size of buffer */
00032       unsigned int size;
00033       /* amount of information stored */
00034       unsigned int used;
00035       /* offset in file or similar, has meaning only for application */
00036       unsigned long long int offset;
00037     } buf_desc;
00038     /* amount of data passed through buffer (including current stored).
00039        computed using offset and size. gaps are ignored. */
00040     unsigned long long int eof_pos;
00041     /* list of controlled buffers */
00042     buf_desc *bufs;
00043     /* amount of controlled buffers */
00044     int bufs_n;
00045     /* set to true if application's reading(filling) part
00046        won't use buffer anymore */
00047     bool eof_read_flag;
00048     /* same for writing(emptying) part */
00049     bool eof_write_flag;
00050     /* reading part of application experienced error */
00051     bool error_read_flag;
00052     /* same for writing part */
00053     bool error_write_flag;
00054     /* error was originated in DataBuffer itself */
00055     bool error_transfer_flag;
00056     /* wait for any change of buffers' status */
00057     bool cond_wait();
00058     /* pointer to object to compute checksum */
00059     class checksum_desc {
00060      public:
00061       checksum_desc(CheckSum *sum)
00062         : sum(sum),
00063           offset(0),
00064           ready(true) {}
00065       CheckSum *sum;
00066       unsigned long long int offset;
00067       bool ready;
00068     };
00069 
00070     std::list<checksum_desc> checksums;
00071 
00072   public:
00074     DataSpeed speed;
00076     operator bool() const {
00077       return (bufs != 0);
00078     }
00082     DataBuffer(unsigned int size = 65536, int blocks = 3);
00088     DataBuffer(CheckSum *cksum, unsigned int size = 65536, int blocks = 3);
00090     ~DataBuffer();
00096     bool set(CheckSum *cksum = NULL, unsigned int size = 65536,
00097              int blocks = 3);
00102     int add(CheckSum *cksum);
00104     char* operator[](int n);
00111     bool for_read(int& handle, unsigned int& length, bool wait);
00115     bool for_read();
00120     bool is_read(int handle, unsigned int length,
00121                  unsigned long long int offset);
00126     bool is_read(char *buf, unsigned int length,
00127                  unsigned long long int offset);
00133     bool for_write(int& handle, unsigned int& length,
00134                    unsigned long long int& offset, bool wait);
00138     bool for_write();
00141     bool is_written(int handle);
00144     bool is_written(char *buf);
00148     bool is_notwritten(int handle);
00152     bool is_notwritten(char *buf);
00155     void eof_read(bool v);
00158     void eof_write(bool v);
00161     void error_read(bool v);
00164     void error_write(bool v);
00167     bool eof_read();
00170     bool eof_write();
00172     bool error_read();
00174     bool error_write();
00176     bool error_transfer();
00179     bool error();
00182     bool wait_any();
00184     bool wait_used();
00188     bool checksum_valid() const;
00189     bool checksum_valid(int index) const;
00193     const CheckSum* checksum_object() const;
00194     const CheckSum* checksum_object(int index) const;
00196     bool wait_eof_read();
00198     bool wait_read();
00200     bool wait_eof_write();
00202     bool wait_write();
00204     bool wait_eof();
00206     unsigned long long int eof_position() const {
00207       return eof_pos;
00208     }
00211     unsigned int buffer_size() const;
00212   };
00213 
00214 } // namespace Arc
00215 
00216 #endif // __ARC_DATABUFFER_H__