Represents a file being transmitted or received.
More...
#include <File.h>
Represents a file being transmitted or received.
Definition at line 30 of file File.h.
◆ File() [1/3]
Create a file from an FDT entry (used for reception)
- Parameters
-
Definition at line 38 of file File.cpp.
39 : _meta( std::move(entry) )
40 , _received_at( time(
nullptr) )
43 spdlog::debug(
"Creating File from FileEntry");
45 spdlog::debug(
"Allocating buffer");
47 if (_buffer ==
nullptr)
49 throw std::runtime_error(
"Failed to allocate file buffer");
53 this->calculate_partitioning();
54 this->create_blocks();
◆ File() [2/3]
Create a file from a Transmitter::FileDescription (used for transmission)
- Parameters
-
Definition at line 57 of file File.cpp.
59 , _file_description(file_description)
61 spdlog::debug(
"Creating File from FileDescription");
63 auto length = _file_description->data_length();
64 _buffer = (
char*)malloc(
length);
65 if (_buffer ==
nullptr)
67 throw std::runtime_error(
"No data allocated");
70 memcpy(_buffer, _file_description->data(),
length);
71 _meta = _file_description->file_entry();
77 throw std::runtime_error(
"Unsupported FEC scheme");
82 calculate_partitioning();
size_t length() const
Get the data buffer length.
void encode()
Encode the buffer using the Content-Encoding.
◆ File() [3/3]
| LibFlute::File::File |
( |
uint32_t |
toi, |
|
|
FecOti |
fec_oti, |
|
|
std::string |
content_location, |
|
|
std::string |
content_type, |
|
|
uint64_t |
expires, |
|
|
char * |
data, |
|
|
size_t |
length, |
|
|
bool |
copy_data = false |
|
) |
| |
Create a file from the given parameters (used for transmission)
- Parameters
-
| toi | TOI of the file |
| content_location | Content location URI to use |
| content_type | MIME type |
| expires | Expiry value (in seconds since the NTP epoch) |
| data | Pointer to the data buffer |
| length | Length of the buffer |
| copy_data | Copy the buffer. If false (the default), the caller must ensure the buffer remains valid while the file is being transmitted. |
Definition at line 86 of file File.cpp.
98 spdlog::debug(
"Creating File from data");
100 spdlog::debug(
"Allocating buffer");
101 _buffer = (
char*)malloc(
length);
102 if (_buffer ==
nullptr)
104 throw std::runtime_error(
"Failed to allocate file buffer");
106 memcpy(_buffer, data,
length);
112 unsigned char md5[MD5_DIGEST_LENGTH];
113 MD5((
const unsigned char*)data,
length, md5);
119 _meta.
content_md5 = base64_encode(md5, MD5_DIGEST_LENGTH);
127 throw std::runtime_error(
"Unsupported FEC scheme");
130 this->calculate_partitioning();
131 this->create_blocks();
const FecOti & fec_oti() const
Get the FEC OTI values.
std::string content_location
◆ ~File()
| LibFlute::File::~File |
( |
| ) |
|
|
virtual |
Default destructor.
Definition at line 134 of file File.cpp.
136 spdlog::debug(
"Destroying File");
137 if (_own_buffer && _buffer !=
nullptr)
139 spdlog::debug(
"Freeing buffer");
◆ access_count()
| unsigned LibFlute::File::access_count |
( |
| ) |
const |
|
inline |
Get the access counter value.
Definition at line 132 of file File.h.
132 {
return _access_count; };
◆ buffer()
| char* LibFlute::File::buffer |
( |
| ) |
const |
|
inline |
Get the data buffer.
Definition at line 85 of file File.h.
◆ complete()
| bool LibFlute::File::complete |
( |
| ) |
const |
|
inline |
Check if the file is complete.
Definition at line 80 of file File.h.
80 {
return _complete; };
◆ decode()
| auto LibFlute::File::decode |
( |
| ) |
|
Decode the buffer using the Content-Encoding.
Will check the MD5 sum after decoding, if present.
Definition at line 340 of file File.cpp.
344 auto comp_buffer = _buffer;
345 bool own_comp = _own_buffer;
346 std::shared_ptr<unsigned char[]> decomp_buffer(
new unsigned char[16384]);
348 .next_in =
reinterpret_cast<unsigned char*
>(comp_buffer),
350 .next_out = decomp_buffer.get(),
357 auto zstate = inflate(&zs, Z_FINISH);
359 while (zstate == Z_OK) {
360 spdlog::debug(
"Part decompressed: {} bytes", 16384-zs.avail_out);
361 _buffer =
reinterpret_cast<char*
>(realloc(_buffer, zs.total_out));
362 memcpy(_buffer+last_out, decomp_buffer.get(), 16384-zs.avail_out);
363 last_out = zs.total_out;
365 zs.avail_out = 16384;
366 zs.next_out = decomp_buffer.get();
367 zstate = inflate(&zs, Z_FINISH);
369 if (zstate==Z_STREAM_END) {
370 if (last_out != zs.total_out) {
371 spdlog::debug(
"Finish decompress, last block is {} bytes. Total {} bytes", 16384-zs.avail_out, zs.total_out);
372 _buffer =
reinterpret_cast<char*
>(realloc(_buffer, zs.total_out));
373 memcpy(_buffer+last_out, decomp_buffer.get(), 16384-zs.avail_out);
379 spdlog::error(
"Decompressed length does not match expected Content-Length ({} != {})", _meta.
content_length, zs.total_out);
382 spdlog::error(
"Error decompressing file {}: {}", _meta.
toi, zs.msg);
386 if (own_comp) free(comp_buffer);
389 throw std::runtime_error(
"Content-Encoding not known");
392 _been_decoded =
true;
393 _been_encoded =
false;
397 unsigned char md5[MD5_DIGEST_LENGTH];
400 auto content_md5 = base64_decode(_meta.
content_md5);
401 if (memcmp(md5, content_md5.c_str(), MD5_DIGEST_LENGTH) != 0) {
402 spdlog::debug(
"MD5 mismatch for TOI {}, discarding", _meta.
toi);
405 for (
auto& block : _source_blocks) {
406 for (
auto& symbol : block.second.symbols) {
407 symbol.second.complete =
false;
409 block.second.complete =
false;
char * buffer() const
Get the data buffer.
std::string content_encoding
◆ encode()
| auto LibFlute::File::encode |
( |
| ) |
|
Encode the buffer using the Content-Encoding.
Definition at line 285 of file File.cpp.
289 auto decomp_buffer = _buffer;
290 bool own_decomp = _own_buffer;
291 std::shared_ptr<unsigned char[]> comp_buffer(
new unsigned char[16384]);
293 .next_in =
reinterpret_cast<unsigned char*
>(decomp_buffer),
295 .next_out = comp_buffer.get(),
300 if (deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY) == Z_OK) {
302 auto zstate = deflate(&zs, Z_FINISH);
304 while (zstate == Z_OK) {
305 spdlog::debug(
"Part compressed: {} bytes", 16384-zs.avail_out);
306 _buffer =
reinterpret_cast<char*
>(realloc(_buffer, zs.total_out));
307 memcpy(_buffer+last_out, comp_buffer.get(), 16384-zs.avail_out);
308 last_out = zs.total_out;
310 zs.avail_out = 16384;
311 zs.next_out = comp_buffer.get();
312 zstate = deflate(&zs, Z_FINISH);
314 if (zstate==Z_STREAM_END) {
315 if (last_out != zs.total_out) {
316 spdlog::debug(
"Finish compress, last block is {} bytes. Total {} bytes", 16384-zs.avail_out, zs.total_out);
317 _buffer =
reinterpret_cast<char*
>(realloc(_buffer, zs.total_out));
318 memcpy(_buffer+last_out, comp_buffer.get(), 16384-zs.avail_out);
323 spdlog::error(
"Error compressing file {}: {}", _meta.
toi, zs.msg);
328 if (own_decomp) free(decomp_buffer);
332 throw std::runtime_error(
"Content-Encoding not known");
335 _been_encoded =
true;
336 _been_decoded =
false;
◆ fdt_instance_id()
| uint16_t LibFlute::File::fdt_instance_id |
( |
| ) |
|
|
inline |
Get the FDT instance ID.
Definition at line 152 of file File.h.
152 {
return _fdt_instance_id; };
◆ fec_oti()
| const FecOti& LibFlute::File::fec_oti |
( |
| ) |
const |
|
inline |
Get the FEC OTI values.
Definition at line 112 of file File.h.
◆ get_next_symbols()
| auto LibFlute::File::get_next_symbols |
( |
size_t |
max_size | ) |
|
Get the next encoding symbols that fit in max_size bytes.
Definition at line 244 of file File.cpp.
248 std::vector<EncodingSymbol> symbols;
250 for (
auto& block : _source_blocks) {
251 if (cnt >= nof_symbols)
break;
253 if (!block.second.complete) {
254 for (
auto& symbol : block.second.symbols) {
255 if (cnt >= nof_symbols)
break;
257 if (!symbol.second.complete && !symbol.second.queued) {
258 symbols.emplace_back(symbol.first, block.first, symbol.second.data, symbol.second.length, _meta.
fec_oti.
encoding_id);
259 symbol.second.queued =
true;
uint32_t encoding_symbol_length
◆ is_encoded()
| bool LibFlute::File::is_encoded |
( |
| ) |
const |
|
inline |
Check if the buffer is content encoded.
Definition at line 107 of file File.h.
107 {
return _been_encoded || !_been_decoded; };
◆ length()
| size_t LibFlute::File::length |
( |
| ) |
const |
|
inline |
Get the data buffer length.
Definition at line 90 of file File.h.
◆ log_access()
| void LibFlute::File::log_access |
( |
| ) |
|
|
inline |
Log access to the file by incrementing a counter.
Definition at line 127 of file File.h.
127 { _access_count++; };
◆ mark_completed()
| auto LibFlute::File::mark_completed |
( |
const std::vector< EncodingSymbol > & |
symbols, |
|
|
bool |
success |
|
) |
| |
Mark encoding symbols as completed.
Definition at line 269 of file File.cpp.
271 for (
auto& symbol : symbols) {
272 auto block = _source_blocks.find(symbol.source_block_number());
273 if (block != _source_blocks.end()) {
274 auto sym = block->second.symbols.find(symbol.id());
275 if (sym != block->second.symbols.end()) {
276 sym->second.queued =
false;
277 sym->second.complete = success;
279 check_source_block_completion(block->second);
280 check_file_completion();
◆ meta()
Get the file metadata from its FDT entry.
Definition at line 117 of file File.h.
◆ put_symbol()
Write the data from an encoding symbol into the appropriate place in the buffer.
Definition at line 144 of file File.cpp.
153 if (symbol.source_block_number() >= _source_blocks.size()) {
154 throw std::runtime_error(fmt::format(
"FLUTE: source block number {} out of range (have {} blocks)",
155 symbol.source_block_number(), _source_blocks.size()));
158 SourceBlock& source_block = _source_blocks[ symbol.source_block_number() ];
160 if (symbol.id() >= source_block.symbols.size()) {
161 throw std::runtime_error(fmt::format(
"FLUTE: encoding symbol id {} out of range (block {} has {} symbols)",
162 symbol.id(), symbol.source_block_number(), source_block.symbols.size()));
165 SourceBlock::Symbol& target_symbol = source_block.symbols[symbol.id()];
167 if (!target_symbol.complete) {
168 symbol.decode_to(target_symbol.data, target_symbol.length);
169 target_symbol.complete =
true;
171 check_source_block_completion(source_block);
172 check_file_completion();
◆ received_at()
| unsigned long LibFlute::File::received_at |
( |
| ) |
const |
|
inline |
Timestamp of file reception.
Definition at line 122 of file File.h.
122 {
return _received_at; };
◆ set_fdt_instance_id()
| void LibFlute::File::set_fdt_instance_id |
( |
uint16_t |
id | ) |
|
|
inline |
Set the FDT instance ID.
Definition at line 147 of file File.h.
147 { _fdt_instance_id = id; };
The documentation for this class was generated from the following files: