-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageBuilder.cpp
190 lines (144 loc) · 6.38 KB
/
ImageBuilder.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
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
#include "ImageBuilder.hpp"
inline uint32_t compute_sizeVec(const ImageChunkPacket& packet) {
double d = static_cast<double>(packet.sizeImage) / static_cast<double>(IMG_CHUNK_SIZE);
return static_cast<uint32_t>(ceil(d));
}
inline uint32_t compute_indexVec(const ImageChunkPacket& packet) {
double d = static_cast<double>(packet.offset) / static_cast<double>(IMG_CHUNK_SIZE);
return static_cast<uint32_t>(ceil(d));
}
ImageBuilder::ImageBuilder() :_nodeID(0), _timestamp(0), _position(), _sizeImage(0), _sizeVec(0),
_mutex_is_complete(), _image(), _img_building_vec(), _fillstate_vec() {}
ImageBuilder::ImageBuilder(ImageChunkPacket packet) :
_nodeID(packet.nodeID), _timestamp(packet.timestamp), _position(packet.position), _sizeImage(packet.sizeImage),
_sizeVec(compute_sizeVec(packet)),
_mutex_is_complete(), _image(), _img_building_vec(_sizeVec),
_fillstate_vec(_sizeVec) {
this->add_chunk(packet);
}
ImageBuilder::ImageBuilder(ImageBuilder&& other) noexcept :
_nodeID(other._nodeID), _timestamp(other._timestamp), _position(other._position),
_sizeImage(other._sizeImage), _sizeVec(other._sizeVec), _is_complete(other._is_complete),
_mutex_is_complete(), _image(std::move(other._image)),
_img_building_vec(std::move(other._img_building_vec)), _fillstate_vec(std::move(other._fillstate_vec))
{
/*_nodeID = other._nodeID;
_timestamp = other._timestamp;
_position = other._position;
_sizeImage = other._sizeImage;
_sizeVec = other._sizeVec;
_is_complete = other._is_complete;
// _mutex already initialized
_image = std::move(other._image);
_img_building_vec = std::move(other._img_building_vec);
_fillstate_vec = std::move(other._fillstate_vec);*/
}
/*void ImageBuilder::add_chunk(ImageChunkPacket packet) {
{
std::lock_guard<std::mutex> lock(_mutex_is_complete);
if (_is_complete) return;
}
//auto index(static_cast<uint32_t>(ceil(static_cast<double>(packet.offset) / static_cast<double>(IMG_CHUNK_SIZE))));
uint32_t index(compute_indexVec(packet));
if (not _fillstate_vec[index]) {
_img_building_vec[index] = packet.chunk_content;
_fillstate_vec[index] = true;
LOG_F(3, "Added chunk to img_building_vec : %s", packet.repr().c_str());
if ( std::all_of(_fillstate_vec.begin(), _fillstate_vec.end(), [](bool b){return b;}) ){
std::array<char, IMG_CHUNK_SIZE>::iterator it;
uint32_t size_remaining(_sizeImage);
int chunk_treated(0);
{
std::lock_guard<std::mutex> lock(_mutex_is_complete);
_is_complete = true;
_image.nodeID = _nodeID;
_image.timestamp = _timestamp;
_image.position = _position;
for (auto & i : _img_building_vec) {
if (size_remaining >= IMG_CHUNK_SIZE) {
size_remaining -= IMG_CHUNK_SIZE;
it = std::end(i);
}
else {
it = std::begin(i) + size_remaining;
}
(_image.content).insert(_image.content.begin() + (chunk_treated * IMG_CHUNK_SIZE), std::begin(i), it);
chunk_treated += 1;
}
}
// print_md5_sum(&_image.content[0], _image.content.size());
LOG_F(3, "Image is complete from : %s", packet.repr().c_str());
}
}
else {
LOG_F(3, "Ignored chunk to img_building_vec : %s", packet.repr().c_str());
}
}*/
void ImageBuilder::add_chunk(ImageChunkPacket packet) {
{
std::lock_guard<std::mutex> lock(_mutex_is_complete);
if (_is_complete) return;
}
//uint32_t index(compute_indexVec(packet));
auto index(static_cast<uint32_t>(ceil(static_cast<double>(packet.offset) / static_cast<double>(IMG_CHUNK_SIZE))));
if (not _fillstate_vec[index]) {
_img_building_vec[index] = packet.chunk_content;
_fillstate_vec[index] = true;
LOG_F(3, "Added chunk to img_building_vec : %s", packet.repr().c_str());
// if was last chunk, build Image struct and calc MD5
if ( std::all_of(_fillstate_vec.begin(), _fillstate_vec.end(), [](bool b){return b;}) ){
std::array<char, IMG_CHUNK_SIZE>::iterator it;
uint32_t size_remaining(_sizeImage);
int chunk_treated(0);
{
std::lock_guard<std::mutex> lock(_mutex_is_complete);
_is_complete = true;
_image.nodeID = _nodeID;
_image.timestamp = _timestamp;
_image.position = _position;
for (auto & i : _img_building_vec) {
if (size_remaining >= IMG_CHUNK_SIZE) {
size_remaining -= IMG_CHUNK_SIZE;
it = std::end(i);
}
else {
it = std::begin(i) + size_remaining;
}
(_image.content).insert(_image.content.begin() + (chunk_treated * IMG_CHUNK_SIZE), std::begin(i), it);
chunk_treated += 1;
}
}
LOG_F(WARNING, "Image (%d,%d) is complete- MD5: %s", _nodeID, _timestamp, get_md5_string(&_image.content[0], _image.content.size()).c_str());
std::cout << "OK" << std::endl;
}
}
else {
LOG_F(3, "Ignored chunk to img_building_vec : %s", packet.repr().c_str());
}
}
bool ImageBuilder::is_complete() const {
std::lock_guard<std::mutex> lock(_mutex_is_complete);
return _is_complete;
}
Image ImageBuilder::get_image() const {
std::lock_guard<std::mutex> lock(_mutex_is_complete);
if (! _is_complete) {
throw;
}
return _image;
}
uint32_t ImageBuilder::get_timestamp() const {
return _timestamp;
}
uint8_t ImageBuilder::get_nodeid() const {
return _nodeID;
}
uint32_t ImageBuilder::get_loss_percent() const {
long int counterLoss = std::count_if(_fillstate_vec.begin(), _fillstate_vec.end(), [](bool b){return b;});
float res((static_cast<float>(counterLoss) / static_cast<float>(_sizeVec)) * 100);
return static_cast<uint32_t>(res);
}
bool ImageBuilder::is_chunk_already_received(const ImageChunkPacket& packet) const {
auto index(compute_indexVec(packet));
return _fillstate_vec[index];
}