-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.cpp
133 lines (119 loc) · 3.58 KB
/
signature.cpp
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
#include "signature.h"
#include "md5.h"
#include <sstream>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <iostream>
#include <chrono>
Signature::Signature(const string &out_filename, int64_t blocks_count,
int64_t thread_count):out_filename_(out_filename), blocksCount_(blocks_count)
{
in_data_queue_.setMaxSize(thread_count*10);
out_data_queue_.setMaxSize(thread_count*10);
isStart_ = true;
isWriterStart_ = true;
threads_.reserve(thread_count);
for(auto i = 0; i < thread_count; i++) {
threads_.push_back(thread([=] {workerLoop(i+1);}));
}
writerThread_ = thread([=] {writeData();});
}
void Signature::force_stop()
{
in_data_queue_.erase();
out_data_queue_.erase();
join();
}
void Signature::join()
{
isStart_ = false;
cv_data_available_.notify_all();
for(auto &th: threads_) {
if(th.joinable()) {
th.join();
}
}
isWriterStart_ = false;
cv_write_available_.notify_all();
if(writerThread_.joinable())
writerThread_.join();
if(ep) {
rethrow_exception (ep);
}
}
void Signature::appendData(SignatureData data)
{
if(!ep) {
in_data_queue_.push(move(data));
cv_data_available_.notify_one();
} else {
exception_ptr tmp_ep = ep;
ep = nullptr;
rethrow_exception (tmp_ep);
}
}
void Signature::workerLoop(int thread_id)
{
try {
while(isStart_ || in_data_queue_.size() > 0) {
while(SignatureData data = in_data_queue_.take()) {
calcHash(move(data));
}
if(isStart_) {
unique_lock<mutex> lock(cv_data_available_mutex_);
cv_data_available_.wait_for(lock, std::chrono::milliseconds(100));
}
}
} catch (...) {
throwException();
}
}
void Signature::calcHash(SignatureData data)
{
uint8_t result[digets_size_];
md5(reinterpret_cast<uint8_t*>(data->get_data()), data->get_data_size(), result);
stringstream stringStream;
for (int i = 0; i < digets_size_; i++)
stringStream << setfill('0') << setw(2) << hex << static_cast<unsigned int>(result[i]);
string md5sum = stringStream.str();
SignatureData write_data(new block_data(md5sum.length(), data->get_block_num()));
memcpy(write_data->get_data(), md5sum.c_str(), write_data->get_data_size());
out_data_queue_.push(move(write_data));
cv_write_available_.notify_one();
}
void Signature::writeData()
{
try
{
int64_t blocksReady = 0;
ofstream out;
out.open(out_filename_, ios::out | ios::trunc);
if (!out) {
throw runtime_error("Can't open output file");
}
while(isWriterStart_ || out_data_queue_.size() > 0) {
while(SignatureData data = out_data_queue_.take()) {
out.seekp(data->get_block_num() * (data->get_data_size()), ios::beg);
out.write(data->get_data(), data->get_data_size());
blocksReady++;
printf("Block %ld complete (%ld/%ld blocks ready)\n", data->get_block_num() + 1,
blocksReady, blocksCount_);
}
if(isWriterStart_) {
unique_lock<mutex> lock(cv_write_available_mutex_);
cv_write_available_.wait_for(lock, std::chrono::milliseconds(100));
}
}
} catch (...) {
throwException();
}
}
void Signature::throwException()
{
ep = current_exception();
in_data_queue_.erase();
out_data_queue_.erase();
isStart_ = false;
isWriterStart_ = false;
}