summaryrefslogtreecommitdiff
path: root/plugins/CopyEngine/Ultracopier/ReadThread.h
blob: f817e35e7645426de5ef58f98bb5b0484b819fdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/** \file ReadThread.h
\brief Thread changed to open/close and read the source file
\author alpha_one_x86
\licence GPL3, see the file COPYING */

#ifndef READTHREAD_H
#define READTHREAD_H

#include <QThread>
#include <QByteArray>
#include <QSemaphore>
#include <QDateTime>
#include <QFileInfo>
#include <QCryptographicHash>

#include "WriteThread.h"
#include "Environment.h"
#include "StructEnumDefinition_CopyEngine.h"
#include "AvancedQFile.h"

/// \brief Thread changed to open/close and read the source file
class ReadThread : public QThread
{
    Q_OBJECT
public:
    explicit ReadThread();
    ~ReadThread();
protected:
    void run();
public:
    /// \brief open with the name and copy mode
    void open(const QFileInfo &file, const Ultracopier::CopyMode &mode);
    /// \brief return the error string
    std::string errorString() const;
    //QByteArray read(qint64 position,qint64 maxSize);
    /// \brief stop the copy
    void stop();
    /// \brief put the copy in pause
    void pause();
    /// \brief resume the copy
    void resume();
    /// \brief get the size of the source file
    int64_t size() const;
    /// \brief get the last good position
    int64_t getLastGoodPosition() const;
    /// \brief start the reading of the source file
    void startRead();
    #ifdef ULTRACOPIER_PLUGIN_SPEED_SUPPORT
    /// \brief set the current max speed in KB/s
    void setMultiForBigSpeed(const int &multiForBigSpeed);
    #endif
    /// \brief set block size in KB
    bool setBlockSize(const int blockSize);
    /// \brief reopen after an error
    void reopen();
    /// \brief set the write thread
    void setWriteThread(WriteThread * writeThread);
    #ifdef ULTRACOPIER_PLUGIN_DEBUG
    /// \brief to set the id
    void setId(int id);
    /// \brief stat
    enum ReadStat
    {
        Idle=0,
        InodeOperation=1,
        Read=2,
        WaitWritePipe=3,
        Checksum=4
    };
    ReadStat stat;
    #endif
    /// \brief return if it's reading
    bool isReading() const;
    #ifdef ULTRACOPIER_PLUGIN_SPEED_SUPPORT
    /// \brief executed at regular interval to do a speed throling
    void timeOfTheBlockCopyFinished();
    #endif
    /// \brief do the fake open
    void fakeOpen();
    /// \brief do the fake readIsStarted
    void fakeReadIsStarted();
    /// \brief do the fake readIsStopped
    void fakeReadIsStopped();
    /// do the checksum
    void startCheckSum();
public slots:
    /// \brief to reset the copy, and put at the same state when it just open
    void seekToZeroAndWait();
    void postOperation();
    /// do the checksum
    void checkSum();
signals:
    void error() const;
    void opened() const;
    void readIsStarted() const;
    void readIsStopped() const;
    void closed() const;
    void isSeekToZeroAndWait() const;
    void checkIfIsWait() const;
    void resumeAfterErrorByRestartAll() const;
    void resumeAfterErrorByRestartAtTheLastPosition() const;
    void checksumFinish(const QByteArray&) const;
    // internal signals
    void internalStartOpen() const;
    void internalStartChecksum() const;
    void internalStartReopen() const;
    void internalStartRead() const;
    void internalStartClose() const;
    /// \brief To debug source
    void debugInformation(const Ultracopier::DebugLevel &level,std::string fonction,std::string text,std::string file,int ligne) const;

private:
    std::string         errorString_internal;
    AvancedQFile	file;
    volatile bool	stopIt;
    Ultracopier::CopyMode	mode;
    int64_t          lastGoodPosition;
    volatile int	blockSize;//in Bytes
    #ifdef ULTRACOPIER_PLUGIN_SPEED_SUPPORT
    QSemaphore      waitNewClockForSpeed;
    volatile int	numberOfBlockCopied;		///< Multiple for count the number of block copied
    volatile int	multiForBigSpeed;           ///< Multiple for count the number of block needed
    #endif
    WriteThread*	writeThread;
    int		id;
    QSemaphore      isOpen;
    QSemaphore      pauseMutex;
    volatile bool	putInPause;
    volatile bool	isInReadLoop;
    volatile bool	seekToZero;
    volatile bool	tryStartRead;
    int64_t          size_at_open;
    uint64_t       mtime_at_open;
    bool            fakeMode;
    //internal function
    bool seek(const int64_t &position);/// \todo search if is use full
private slots:
    bool internalOpen(bool resetLastGoodPosition=true);
    bool internalOpenSlot();
    bool internalReopen();
    void internalRead();
    void internalClose(bool callByTheDestructor=false);
    void internalCloseSlot();
    void isInWait();
};

#endif // READTHREAD_H