libflute
Classes | Public Member Functions | List of all members
LibFlute::File Class Reference

Represents a file being transmitted or received. More...

#include <File.h>

Public Member Functions

 File (LibFlute::FileDeliveryTable::FileEntry entry)
 Create a file from an FDT entry (used for reception) More...
 
 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) More...
 
virtual ~File ()
 Default destructor. More...
 
void put_symbol (const EncodingSymbol &symbol)
 Write the data from an encoding symbol into the appropriate place in the buffer. More...
 
bool complete () const
 Check if the file is complete. More...
 
char * buffer () const
 Get the data buffer. More...
 
size_t length () const
 Get the data buffer length. More...
 
const FecOtifec_oti () const
 Get the FEC OTI values. More...
 
const LibFlute::FileDeliveryTable::FileEntrymeta () const
 Get the file metadata from its FDT entry. More...
 
unsigned long received_at () const
 Timestamp of file reception. More...
 
void log_access ()
 Log access to the file by incrementing a counter. More...
 
unsigned access_count () const
 Get the access counter value. More...
 
std::vector< EncodingSymbolget_next_symbols (size_t max_size)
 Get the next encoding symbols that fit in max_size bytes. More...
 
void mark_completed (const std::vector< EncodingSymbol > &symbols, bool success)
 Mark encoding symbols as completed. More...
 
void set_fdt_instance_id (uint16_t id)
 Set the FDT instance ID. More...
 
uint16_t fdt_instance_id ()
 Get the FDT instance ID. More...
 

Detailed Description

Represents a file being transmitted or received.

Definition at line 31 of file File.h.

Constructor & Destructor Documentation

◆ File() [1/2]

LibFlute::File::File ( LibFlute::FileDeliveryTable::FileEntry  entry)

Create a file from an FDT entry (used for reception)

Parameters
entryFDT entry

Definition at line 31 of file File.cpp.

32  : _meta( std::move(entry) )
33  , _received_at( time(nullptr) )
34 {
35  // Allocate a data buffer
36  _buffer = (char*)malloc(_meta.fec_oti.transfer_length);
37  if (_buffer == nullptr)
38  {
39  throw "Failed to allocate file buffer";
40  }
41  _own_buffer = true;
42 
43  calculate_partitioning();
44  create_blocks();
45 }
uint64_t transfer_length
Definition: flute_types.h:55

◆ File() [2/2]

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
toiTOI of the file
content_locationContent location URI to use
content_typeMIME type
expiresExpiry value (in seconds since the NTP epoch)
dataPointer to the data buffer
lengthLength of the buffer
copy_dataCopy the buffer. If false (the default), the caller must ensure the buffer remains valid while the file is being transmitted.

Definition at line 47 of file File.cpp.

55 {
56  if (copy_data) {
57  _buffer = (char*)malloc(length);
58  if (_buffer == nullptr)
59  {
60  throw "Failed to allocate file buffer";
61  }
62  memcpy(_buffer, data, length);
63  _own_buffer = true;
64  } else {
65  _buffer = data;
66  }
67 
68  unsigned char md5[MD5_DIGEST_LENGTH];
69  MD5((const unsigned char*)data, length, md5);
70 
71  _meta.toi = toi;
72  _meta.content_location = std::move(content_location);
73  _meta.content_type = std::move(content_type);
74  _meta.content_length = length;
75  _meta.content_md5 = base64_encode(md5, MD5_DIGEST_LENGTH);
76  _meta.expires = expires;
77  _meta.fec_oti = fec_oti;
78 
79  // for no-code
82  } else {
83  throw "Unsupported FEC scheme";
84  }
85 
86  calculate_partitioning();
87  create_blocks();
88 }
const FecOti & fec_oti() const
Get the FEC OTI values.
Definition: File.h:89
size_t length() const
Get the data buffer length.
Definition: File.h:84
FecScheme encoding_id
Definition: flute_types.h:54

◆ ~File()

LibFlute::File::~File ( )
virtual

Default destructor.

Definition at line 90 of file File.cpp.

91 {
92  if (_own_buffer && _buffer != nullptr)
93  {
94  free(_buffer);
95  }
96 }

Member Function Documentation

◆ access_count()

unsigned LibFlute::File::access_count ( ) const
inline

Get the access counter value.

Definition at line 109 of file File.h.

109 { return _access_count; };

◆ buffer()

char* LibFlute::File::buffer ( ) const
inline

Get the data buffer.

Definition at line 79 of file File.h.

79 { return _buffer; };

◆ complete()

bool LibFlute::File::complete ( ) const
inline

Check if the file is complete.

Definition at line 74 of file File.h.

74 { return _complete; };

◆ fdt_instance_id()

uint16_t LibFlute::File::fdt_instance_id ( )
inline

Get the FDT instance ID.

Definition at line 129 of file File.h.

129 { return _fdt_instance_id; };

◆ fec_oti()

const FecOti& LibFlute::File::fec_oti ( ) const
inline

Get the FEC OTI values.

Definition at line 89 of file File.h.

89 { return _meta.fec_oti; };

◆ 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 189 of file File.cpp.

190 {
191  auto block = _source_blocks.begin();
192  int nof_symbols = std::ceil((float)(max_size - 4) / (float)_meta.fec_oti.encoding_symbol_length);
193  auto cnt = 0;
194  std::vector<EncodingSymbol> symbols;
195 
196  for (auto& block : _source_blocks) {
197  if (cnt >= nof_symbols) break;
198 
199  if (!block.second.complete) {
200  for (auto& symbol : block.second.symbols) {
201  if (cnt >= nof_symbols) break;
202 
203  if (!symbol.second.complete && !symbol.second.queued) {
204  symbols.emplace_back(symbol.first, block.first, symbol.second.data, symbol.second.length, _meta.fec_oti.encoding_id);
205  symbol.second.queued = true;
206  cnt++;
207  }
208  }
209  }
210  }
211  return symbols;
212 
213 }
uint32_t encoding_symbol_length
Definition: flute_types.h:56

◆ length()

size_t LibFlute::File::length ( ) const
inline

Get the data buffer length.

Definition at line 84 of file File.h.

84 { return _meta.fec_oti.transfer_length; };

◆ log_access()

void LibFlute::File::log_access ( )
inline

Log access to the file by incrementing a counter.

Definition at line 104 of file File.h.

104 { _access_count++; };

◆ mark_completed()

auto LibFlute::File::mark_completed ( const std::vector< EncodingSymbol > &  symbols,
bool  success 
)

Mark encoding symbols as completed.

Definition at line 215 of file File.cpp.

216 {
217  for (auto& symbol : symbols) {
218  auto block = _source_blocks.find(symbol.source_block_number());
219  if (block != _source_blocks.end()) {
220  auto sym = block->second.symbols.find(symbol.id());
221  if (sym != block->second.symbols.end()) {
222  sym->second.queued = false;
223  sym->second.complete = success;
224  }
225  check_source_block_completion(block->second);
226  check_file_completion();
227  }
228  }
229 }

◆ meta()

const LibFlute::FileDeliveryTable::FileEntry& LibFlute::File::meta ( ) const
inline

Get the file metadata from its FDT entry.

Definition at line 94 of file File.h.

94 { return _meta; };

◆ put_symbol()

auto LibFlute::File::put_symbol ( const EncodingSymbol symbol)

Write the data from an encoding symbol into the appropriate place in the buffer.

Definition at line 98 of file File.cpp.

99 {
100  if (symbol.source_block_number() > _source_blocks.size()) {
101  throw "Source Block number too high";
102  }
103 
104  SourceBlock& source_block = _source_blocks[ symbol.source_block_number() ];
105 
106  if (symbol.id() > source_block.symbols.size()) {
107  throw "Encoding Symbol ID too high";
108  }
109 
110  SourceBlock::Symbol& target_symbol = source_block.symbols[symbol.id()];
111 
112  if (!target_symbol.complete) {
113  symbol.decode_to(target_symbol.data, target_symbol.length);
114  target_symbol.complete = true;
115 
116  check_source_block_completion(source_block);
117  check_file_completion();
118  }
119 
120 }

◆ received_at()

unsigned long LibFlute::File::received_at ( ) const
inline

Timestamp of file reception.

Definition at line 99 of file File.h.

99 { 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 124 of file File.h.

124 { _fdt_instance_id = id; };

The documentation for this class was generated from the following files: