-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComputerThread.h
211 lines (178 loc) · 6.4 KB
/
ComputerThread.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
File: ComputerThread.h
Created on: 06/06/2016
Author: Felix de las Pozas Alvarez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef COMPUTERTHREAD_H_
#define COMPUTERTHREAD_H_
// Project
#include <Hash.h>
// Qt
#include <QThread>
#include <QMap>
#include <QMutex>
#include <QWaitCondition>
// C++
#include <atomic>
#include <iostream>
/** \class HashChecker
* \brief Thread for computing an individual hash.
*
*/
class HashChecker
: public QThread
{
Q_OBJECT
public:
/** \brief HashChecker class constructor.
* \param[in] hash hash object to update.
* \param[in] file opened QFile object.
* \param[in] parent raw pointer of the object parent of this one.
*
*/
HashChecker(std::shared_ptr<Hash> hash, QFile *file, QObject *parent = nullptr)
: QThread {parent}
, m_hash {hash}
, m_file {file}
, m_progress{0}
, m_abort {false}
{};
/** \brief HashChecker class virtual destructor.
*
*/
~HashChecker()
{
m_file->close();
delete m_file;
}
/** \brief Returns the progress of the hash computation in [0,100].
*
*/
const int progress() const
{ return m_progress; }
/** \brief Aborts the current hash computation.
*
*/
void abort()
{ if(isRunning()) m_abort = true; }
/** \brief Returns true if aborted and false otherwise.
*
*/
const bool aborted() const
{ return m_abort; }
signals:
void hashComputed(const QString &filename, const Hash *hash);
void hashUpdated(const QString &filename, const Hash *hash, const int value);
void progressed();
protected:
void run()
{
m_progress = 0;
m_file->seek(0);
unsigned long long message_length = 0;
const unsigned long long fileSize = m_file->size();
m_hash->reset();
while(fileSize != message_length && !m_abort)
{
int currentProgress = (message_length*100.)/fileSize;
if(currentProgress != m_progress)
{
m_progress = currentProgress;
emit progressed();
emit hashUpdated(m_file->fileName(), m_hash.get(), m_progress);
}
auto block = m_file->read(m_hash->blockSize());
message_length += block.length();
m_hash->update(block, message_length * m_hash->bitsPerUnit());
// last block needs to be processed
if((fileSize == message_length) && (block.size() == m_hash->blockSize()))
{
m_hash->update(QByteArray(), message_length * m_hash->bitsPerUnit());
}
}
m_file->close();
if(!m_abort) emit hashComputed(m_file->fileName(), m_hash.get());
}
private:
std::shared_ptr<Hash> m_hash; /** hash object to update. */
QFile *m_file; /** opened QFile object. */
int m_progress; /** computation progress value in [0,100]. */
bool m_abort; /** true if aborted, false otherwise. */
};
/** \class ComputerThread
* \brief Class to compute the hashes in a separate thread.
*
*/
class ComputerThread
: public QThread
{
Q_OBJECT
public:
/** \brief ComputeThread class constructor.
* \param[in] computations maps files to hashes to be computed.
* \param[in] threadsNum number of simultaneous threads or -1 for system's maximum value.
*
*/
ComputerThread(QMap<QString, HashList> computations, const int threadsNum, QObject *parent = nullptr);
/** \brief ComputeThread class virtual destructor.
*
*/
virtual ~ComputerThread()
{};
/** \brief Returns the map of file hashes.
*
*/
QMap<QString, HashList> getResults() const;
/** \brief Stops computation and returns.
*
*/
void abort();
/** \brief Returns true if the process has been aborted.
*
*/
bool isAborted() const
{ return m_abort; }
signals:
void progress(int value);
void hashComputed(const QString &filename, const Hash *hash);
void hashUpdated(const QString &filename, const Hash *hash, const int value);
private slots:
/** \brief Computes progress and emits progress and hashComputed signals.
* \param[in] filename filename of the computed hash.
* \param[in] hash hash object pointer.
*/
void onHashComputed(const QString &filename, const Hash *hash);
/** \brief Updates the total progress of the computations and reports it.
*
*/
void onProgressSignaled();
/** \brief Removes the finished thread from the thread pool.
*
*/
void onThreadFinished();
protected:
virtual void run();
private:
QMap<QString, HashList> m_computations; /** maps the files with the hashes to be computed. */
bool m_abort; /** set to true to stop computing and return ASAP. */
int m_hashNumber; /** total number of hashes to compute. */
std::atomic<int> m_progress; /** progress accumulator. */
QMutex m_progressMutex; /** protects the progress variable. */
QMutex m_mutex; /** mutex for the wait condition. */
QWaitCondition m_condition; /** wait condition for the main thread. */
int m_maxThreads; /** max number of threads in the system. */
std::atomic<int> m_threadsNum; /** number of threads currently running. */
QMap<QString, HashList> m_results; /** computed hashes. */
QList<std::shared_ptr<HashChecker>> m_threads; /** list of running threads. */
};
#endif // COMPUTERTHREAD_H_