libflute
Public Types | Public Member Functions | Protected Member Functions | Friends | List of all members
LibFlute::Transmitter::FileDescription Class Reference

File Description object. More...

#include <Transmitter.h>

Public Types

enum  CompressionAlgorithm { COMPRESSION_NONE = 0 , COMPRESSION_GZIP , COMPRESSION_DEFLATE }
 
using date_time_type = std::chrono::system_clock::time_point
 

Public Member Functions

 FileDescription ()=delete
 
 FileDescription (const std::string &content_location, const std::string &filename)
 Make a file description using the contents of a local file. More...
 
 FileDescription (const std::string &content_location, const char *data, size_t length)
 Make a file description using the contents of a memory buffer. More...
 
 FileDescription (const std::string &content_location)
 Make a file description without contents. More...
 
 FileDescription (const FileDescription &other)
 Copy constructor. More...
 
 FileDescription (FileDescription &&other)
 Move constructor. More...
 
virtual ~FileDescription ()
 Destructor. More...
 
FileDescriptionoperator= (const FileDescription &other)
 Copy operator. More...
 
FileDescriptionoperator= (FileDescription &&other)
 Move operator. More...
 
bool operator== (const FileDescription &other) const
 Equality operator. More...
 
bool has_tsi () const
 Has a Transmitter associated a TSI with this file? More...
 
uint64_t tsi () const
 Get the associated TSI value. More...
 
uint32_t toi () const
 Get the TOI associated with this file description. More...
 
const FileDeliveryTable::FileEntryfile_entry () const
 Get the FDT file entry. More...
 
const char * data ()
 Get the data to be transmitted. More...
 
size_t data_length ()
 Get the length in bytes of the data to be transmitted. More...
 
FileDescriptionset_compression (CompressionAlgorithm compression)
 Set the compression algorithm. More...
 
FileDescriptionset_content_location (const std::string &location)
 Set Content-Location. More...
 
FileDescriptionset_content (const std::string &filename)
 Change the file contents using a local file. More...
 
FileDescriptionset_content (const char *data, size_t data_length)
 Change the file contents using a memory buffer. More...
 
FileDescriptionset_content_type (const std::string &content_type)
 Change the file content type. More...
 
FileDescriptionset_expiry_time (const date_time_type &expiry_time)
 Change the file expiry time. More...
 
date_time_type get_expiry_time () const
 Get the currently set expiry time. More...
 
FileDescriptionset_etag (const std::string &etag)
 Set the ETag value for the file. More...
 
const std::string & get_etag () const
 Get the current ETag value. More...
 
 FileDescription (const std::string &content_location, const std::vector< char > &data)
 Make a file description using the contents of a vector. More...
 
 FileDescription (const std::string &content_location, const std::vector< unsigned char > &data)
 
FileDescriptionset_content (const std::vector< char > &data)
 Change the file contents using a vector. More...
 
FileDescriptionset_content (const std::vector< unsigned char > &data)
 

Protected Member Functions

FileDescriptiontsi (uint64_t val)
 Set the TSI (used by the Transmitter class) More...
 
FileDescriptiontoi (uint32_t val)
 Set the TOI (used by the Transmitter class) More...
 
uint32_t previous_toi () const
 Get the TOI before reset. More...
 
FileDescriptionreset_previous_toi ()
 Reset the previous TOI value. More...
 
FileDescriptionmerge_fec_oti (const FecOti &fec_oti)
 Merge the FecOti values. More...
 

Friends

class Transmitter
 

Detailed Description

File Description object.

Definition at line 52 of file Transmitter.h.

Member Typedef Documentation

◆ date_time_type

using LibFlute::Transmitter::FileDescription::date_time_type = std::chrono::system_clock::time_point

Definition at line 54 of file Transmitter.h.

Member Enumeration Documentation

◆ CompressionAlgorithm

Enumerator
COMPRESSION_NONE 
COMPRESSION_GZIP 
COMPRESSION_DEFLATE 

Definition at line 56 of file Transmitter.h.

56  {
57  COMPRESSION_NONE = 0, //< No compression
58  COMPRESSION_GZIP, //< Use gzip compression when encoding the file
59  COMPRESSION_DEFLATE //< Use deflate compression when encoding the file
60  };

Constructor & Destructor Documentation

◆ FileDescription() [1/8]

LibFlute::Transmitter::FileDescription::FileDescription ( )
delete

◆ FileDescription() [2/8]

LibFlute::Transmitter::FileDescription::FileDescription ( const std::string &  content_location,
const std::string &  filename 
)

Make a file description using the contents of a local file.

Parameters
content_locationThe URL to use as the content location in the FDT when sending the file
filenameThe filename in the local filesystem of the file contents associated with this file description

Definition at line 63 of file Transmitter.cpp.

64  : _tsi()
65  , _file_entry({ .toi=0, .content_location=content_location})
67  , _filename()
68  , _file_handle(-1)
69  , _data(nullptr)
70  , _data_length(0)
71 {
72  _attach_file(filename);
73  _calculate_file_entry();
74 }

◆ FileDescription() [3/8]

LibFlute::Transmitter::FileDescription::FileDescription ( const std::string &  content_location,
const std::vector< char > &  data 
)

Make a file description using the contents of a vector.

This does not copy the data, it only retains a reference to it. It is up to the application to ensure that the data is retained in memory until it has finished with this file description and the Transmitter has finished sending the file.

Parameters
content_locationThe URL to use as the content location in the FDT when sending the file
dataThe vector containing the file contents

Definition at line 76 of file Transmitter.cpp.

77  : _tsi()
78  , _file_entry({ .toi=0, .content_location=content_location})
80  , _filename()
81  , _file_handle(-1)
82  , _data(data.data())
83  , _data_length(data.size())
84 {
85  _calculate_file_entry();
86 }
const char * data()
Get the data to be transmitted.

◆ FileDescription() [4/8]

LibFlute::Transmitter::FileDescription::FileDescription ( const std::string &  content_location,
const std::vector< unsigned char > &  data 
)

Definition at line 88 of file Transmitter.cpp.

89  : _tsi()
90  , _file_entry({ .toi=0, .content_location=content_location})
92  , _filename()
93  , _file_handle(-1)
94  , _data(reinterpret_cast<const char*>(data.data()))
95  , _data_length(data.size())
96 {
97  _calculate_file_entry();
98 }

◆ FileDescription() [5/8]

LibFlute::Transmitter::FileDescription::FileDescription ( const std::string &  content_location,
const char *  data,
size_t  length 
)

Make a file description using the contents of a memory buffer.

This does not copy the data, it only retains a reference to it. It is up to the application to ensure that the data is retained in memory until it has finished with this file description and the Transmitter has finished sending the file.

Parameters
content_locationThe URL to use as the content location in the FDT when sending the file
dataA pointer to the memory buffer containing the file contents
lengthThe size of the file contents in the memory buffer in bytes

Definition at line 100 of file Transmitter.cpp.

101  : _tsi()
102  , _file_entry({ .toi=0, .content_location=content_location})
104  , _filename()
105  , _file_handle(-1)
106  , _data(data)
107  , _data_length(data?length:0)
108 {
109  _calculate_file_entry();
110 }

◆ FileDescription() [6/8]

LibFlute::Transmitter::FileDescription::FileDescription ( const std::string &  content_location)

Make a file description without contents.

Create a file description with just a URL location and no body content. The content can be added later using the set_content() methods.

Parameters
content_locationThe URL to use as the content location in the FDT when sending the file
See also
set_content()

Definition at line 112 of file Transmitter.cpp.

113  : _tsi()
114  , _file_entry({ .toi=0, .content_location=content_location})
116  , _filename()
117  , _file_handle(-1)
118  , _data(nullptr)
119  , _data_length(0)
120 {
121  _calculate_file_entry();
122 }

◆ FileDescription() [7/8]

LibFlute::Transmitter::FileDescription::FileDescription ( const FileDescription other)

Copy constructor.

This will make a copy of the file description.

Parameters
otherThe other file description to copy

Definition at line 124 of file Transmitter.cpp.

125  : _tsi(other._tsi)
126  , _file_entry(other._file_entry)
127  , _compression_type(other._compression_type)
128  , _filename(other._filename)
129  , _file_handle(-1)
130  , _data(other._data)
131  , _data_length(other._data_length)
132 {
133  if (!_filename.empty()) {
134  if (other._file_handle >= 0) {
135  _file_handle = dup(other._file_handle);
136  }
137 #if HAVE_MMAP
138  // Map the file contents into memory
139  _data = reinterpret_cast<char*>(mmap(nullptr, _data_length, PROT_READ, MAP_SHARED, _file_handle, 0));
140 #else
141  // copy the file contents into a new memory block
142  char *data = new char[_data_length];
143  _data = data;
144  memcpy(data, other._data, _data_length);
145 #endif
146  }
147 }

◆ FileDescription() [8/8]

LibFlute::Transmitter::FileDescription::FileDescription ( Transmitter::FileDescription &&  other)

Move constructor.

This will move the resources of the other file description into a new file description.

Parameters
otherThe other file description to move

Definition at line 149 of file Transmitter.cpp.

150  : _tsi(std::move(other._tsi))
151  , _file_entry(other._file_entry)
152  , _compression_type(other._compression_type)
153  , _filename(std::move(other._filename))
154  , _file_handle(other._file_handle)
155  , _data(other._data)
156  , _data_length(other._data_length)
157 {
158  other._data = nullptr;
159  other._data_length = 0;
160  other._file_handle = -1;
161 }

◆ ~FileDescription()

LibFlute::Transmitter::FileDescription::~FileDescription ( )
virtual

Destructor.

Free all resources associated with this file description.

Definition at line 163 of file Transmitter.cpp.

164 {
165  _free_file_data();
166 }

Member Function Documentation

◆ data()

const char * LibFlute::Transmitter::FileDescription::data ( )

Get the data to be transmitted.

This will apply the compression standard currently set to the contents and provide a transfer buffer.

Returns
The transfer data buffer pointer for the file contents. Use data_length() to get the transfer buffer size
See also
set_compression()
data_length()

Definition at line 229 of file Transmitter.cpp.

230 {
231  return _data;
232 }

◆ data_length()

size_t LibFlute::Transmitter::FileDescription::data_length ( )

Get the length in bytes of the data to be transmitted.

This will apply the compression standard currently set to the contents and provide the resulting transfer buffer length.

Returns
The transfer data buffer length in bytes. Use data() to get the transfer buffer pointer
See also
set_compression()
data()

Definition at line 234 of file Transmitter.cpp.

235 {
236  return _data_length;
237 }

◆ file_entry()

const FileDeliveryTable::FileEntry& LibFlute::Transmitter::FileDescription::file_entry ( ) const
inline

Get the FDT file entry.

Returns
The current FDT File entry

Definition at line 193 of file Transmitter.h.

193  {
194  return _file_entry;
195  };

◆ get_etag()

const std::string & LibFlute::Transmitter::FileDescription::get_etag ( ) const

Get the current ETag value.

Returns
The current ETag value for the file

Definition at line 381 of file Transmitter.cpp.

382 {
383  return _file_entry.etag;
384 }

◆ get_expiry_time()

Transmitter::FileDescription::date_time_type LibFlute::Transmitter::FileDescription::get_expiry_time ( ) const

Get the currently set expiry time.

Returns
the expiry time of this file.

Definition at line 369 of file Transmitter.cpp.

370 {
371  auto durn = std::chrono::duration_cast<date_time_type::duration>(std::chrono::seconds(_file_entry.expires));
372  return _get_ntp_epoch() + durn;
373 }
static const Transmitter::FileDescription::date_time_type & _get_ntp_epoch()

◆ has_tsi()

bool LibFlute::Transmitter::FileDescription::has_tsi ( ) const
inline

Has a Transmitter associated a TSI with this file?

Returns
true if the TSI has been set

Definition at line 169 of file Transmitter.h.

169 { return _tsi.has_value(); };

◆ merge_fec_oti()

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::merge_fec_oti ( const FecOti fec_oti)
protected

Merge the FecOti values.

Takes any values that are unset in _file_entry.fec_oti from fec_oti.

Parameters
fec_otiThe FecOti to merge values from.

Definition at line 386 of file Transmitter.cpp.

387 {
388  if (static_cast<unsigned>(_file_entry.fec_oti.encoding_id) == 0) {
389  _file_entry.fec_oti.encoding_id = fec_oti.encoding_id;
390  }
391  if (!_file_entry.fec_oti.instance_id) {
392  _file_entry.fec_oti.instance_id = fec_oti.instance_id;
393  }
394  if (!_file_entry.fec_oti.transfer_length) {
395  _file_entry.fec_oti.transfer_length = fec_oti.transfer_length;
396  }
397  if (!_file_entry.fec_oti.encoding_symbol_length) {
398  _file_entry.fec_oti.encoding_symbol_length = fec_oti.encoding_symbol_length;
399  }
400  if (!_file_entry.fec_oti.max_source_block_length) {
401  _file_entry.fec_oti.max_source_block_length = fec_oti.max_source_block_length;
402  }
403  if (!_file_entry.fec_oti.max_number_of_encoding_symbols) {
404  _file_entry.fec_oti.max_number_of_encoding_symbols = fec_oti.max_number_of_encoding_symbols;
405  }
406  return *this;
407 }
uint64_t transfer_length
Definition: flute_types.h:54
uint32_t instance_id
Definition: flute_types.h:53
FecScheme encoding_id
Definition: flute_types.h:52
uint32_t max_source_block_length
Definition: flute_types.h:56
uint32_t max_number_of_encoding_symbols
Definition: flute_types.h:57
uint32_t encoding_symbol_length
Definition: flute_types.h:55

◆ operator=() [1/2]

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::operator= ( const FileDescription other)

Copy operator.

This will make a copy of the other file description into this one.

Parameters
otherThe other file description to copy
Returns
this file description

Definition at line 168 of file Transmitter.cpp.

169 {
170  _tsi = other._tsi;
171  _file_entry = other._file_entry;
172  _compression_type = other._compression_type;
173  _filename = other._filename;
174  _file_handle = -1;
175  _data = other._data;
176  _data_length = other._data_length;
177 
178  if (!_filename.empty()) {
179  if (other._file_handle >= 0) {
180  _file_handle = dup(other._file_handle);
181  }
182 #if HAVE_MMAP
183  // Map the file contents into memory
184  _data = reinterpret_cast<char*>(mmap(nullptr, _data_length, PROT_READ, MAP_SHARED, _file_handle, 0));
185 #else
186  // copy the file contents into a new memory block
187  char *data = new char[_data_length];
188  _data = data;
189  memcpy(data, other._data, _data_length);
190 #endif
191  }
192 
193  return *this;
194 }

◆ operator=() [2/2]

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::operator= ( Transmitter::FileDescription &&  other)

Move operator.

This will move the resources of the other file description into this one.

Parameters
otherThe other file description to move into this
Returns
this file description

Definition at line 196 of file Transmitter.cpp.

197 {
198  _tsi = std::move(other._tsi);
199  _file_entry = other._file_entry;
200  _compression_type = other._compression_type;
201  _filename = std::move(other._filename);
202  _file_handle = other._file_handle;
203  other._file_handle = -1;
204 
205  _data = other._data;
206  other._data = nullptr;
207  _data_length = other._data_length;
208  other._data_length = 0;
209 
210  return *this;
211 }

◆ operator==()

bool LibFlute::Transmitter::FileDescription::operator== ( const FileDescription other) const

Equality operator.

Check if two file descriptions are equivalent

Definition at line 213 of file Transmitter.cpp.

214 {
215  if (_tsi != other._tsi) return false;
216  if (_compression_type != other._compression_type) return false;
217 
218  // _file_entry
219  if (_file_entry != other._file_entry) return false;
220 
221  //if (_filename != other._filename) return false;
222 
223  if (_data_length != other._data_length) return false;
224 
225  if (_data == other._data) return true;
226  return memcmp(_data, other._data, _data_length) == 0;
227 }

◆ previous_toi()

uint32_t LibFlute::Transmitter::FileDescription::previous_toi ( ) const
inlineprotected

Get the TOI before reset.

Returns
The TOI as it was before the TOI was reset for file changes. 0 means no previous TOI.

Definition at line 342 of file Transmitter.h.

342 { return _previous_toi; };

◆ reset_previous_toi()

FileDescription& LibFlute::Transmitter::FileDescription::reset_previous_toi ( )
inlineprotected

Reset the previous TOI value.

Returns
this file description

Definition at line 349 of file Transmitter.h.

349 { _previous_toi = 0; return *this; };

◆ set_compression()

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::set_compression ( CompressionAlgorithm  compression)

Set the compression algorithm.

This sets the compression algorithm that will be used to compress the file contents before sending. This will reset the TOI if the compression algorithm is changed.

Parameters
compressionThe compression algorithm to set
Returns
this file description

Definition at line 250 of file Transmitter.cpp.

252 {
253  if (compression != _compression_type) {
254  _compression_type = compression;
255  switch (_compression_type) {
256  case COMPRESSION_GZIP:
257  _file_entry.content_encoding = "gzip";
258  break;
259  case COMPRESSION_DEFLATE:
260  _file_entry.content_encoding = "deflate";
261  break;
262  default:
263  _file_entry.content_encoding.clear();
264  break;
265  }
266  /* change in compression will change transmitted data, reset the TOI */
267  _reset_toi();
268  _calculate_file_entry();
269  }
270 
271  return *this;
272 }

◆ set_content() [1/4]

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::set_content ( const char *  data,
size_t  data_length 
)

Change the file contents using a memory buffer.

This will alter the contents associated with this file description and replace them with a reference to the memory buffer. It is up to the application to ensure that the data is retained in memory until it has finished with this file description and the Transmitter has finished sending the file. This will reset the TOI if the contents have changed.

Parameters
dataThe in memory buffer to use for the new file contents
data_lengthThe length of the contents in the memory buffer
Returns
this file description

Definition at line 294 of file Transmitter.cpp.

295 {
296  if (!data) data_length=0;
297  if (data != _data || _data_length != data_length) {
298  /* data area has changed in some way, do we need to reset the TOI? */
299  if (_data_length != data_length) {
300  /* data length has changed, reset the TOI */
301  _reset_toi();
302  } else if (data) {
303  if (!_data) {
304  if (data_length) {
305  /* data being added, reset the TOI */
306  _reset_toi();
307  }
308  } else if (data_length) {
309  /* had data before and have new data now, but are they the same? */
310  unsigned char md5[MD5_DIGEST_LENGTH];
311  MD5(reinterpret_cast<const unsigned char*>(data), data_length, md5);
312  if (_file_entry.content_md5 != base64_encode(md5, sizeof(md5))) {
313  /* data contents are different, reset TOI */
314  _reset_toi();
315  }
316  }
317  } else if (_data) {
318  /* data being removed, reset the TOI */
319  _reset_toi();
320  }
321 
322  _free_file_data();
323  _data = data;
324  _data_length = data_length;
325  _calculate_file_entry();
326  }
327 
328  return *this;
329 }
size_t data_length()
Get the length in bytes of the data to be transmitted.

◆ set_content() [2/4]

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::set_content ( const std::string &  filename)

Change the file contents using a local file.

This will alter the contents associated with this file description and replace them with the contents of the local file. This will reset the TOI if the contents have changed.

Parameters
filenameThe local file path for the new contents
Returns
this file description

Definition at line 281 of file Transmitter.cpp.

282 {
283  if (filename != _filename) {
284  _free_file_data();
285  _attach_file(filename);
286  /* Assume a change of filename changes the contents too and zero the TOI */
287  _reset_toi();
288  _calculate_file_entry();
289  }
290 
291  return *this;
292 }

◆ set_content() [3/4]

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::set_content ( const std::vector< char > &  data)

Change the file contents using a vector.

This will alter the contents associated with this file description and replace them with a reference to the contents of the vector. It is up to the application to ensure that the data is retained in memory until it has finished with this file description and the Transmitter has finished sending the file. This will reset the TOI if the contents have changed.

Parameters
dataThe vector to use for the new file contents
Returns
this file description

Definition at line 331 of file Transmitter.cpp.

332 {
333  return set_content(data.data(), data.size());
334 }
FileDescription & set_content(const std::string &filename)
Change the file contents using a local file.

◆ set_content() [4/4]

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::set_content ( const std::vector< unsigned char > &  data)

Definition at line 336 of file Transmitter.cpp.

337 {
338  return set_content(reinterpret_cast<const char*>(data.data()), data.size());
339 }

◆ set_content_location()

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::set_content_location ( const std::string &  location)

Set Content-Location.

Parameters
locationThe location URL or filename.

Definition at line 274 of file Transmitter.cpp.

275 {
276  _file_entry.content_location = location;
277 
278  return *this;
279 }

◆ set_content_type()

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::set_content_type ( const std::string &  content_type)

Change the file content type.

This will set the Content-Type that is associated with this file.

Parameters
content_typeThe content type to set.
Returns
this file description

Definition at line 341 of file Transmitter.cpp.

342 {
343  _file_entry.content_type = content_type;
344  return *this;
345 }

◆ set_etag()

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::set_etag ( const std::string &  etag)

Set the ETag value for the file.

Set to the empty string to remove the ETag.

Parameters
etagThe ETag to set
Returns
this file description

Definition at line 375 of file Transmitter.cpp.

376 {
377  _file_entry.etag = etag;
378  return *this;
379 }

◆ set_expiry_time()

Transmitter::FileDescription & LibFlute::Transmitter::FileDescription::set_expiry_time ( const date_time_type expiry_time)

Change the file expiry time.

Parameters
expiry_timeThe expiry time of the file in the FLUTE session.
Returns
this file description

Definition at line 359 of file Transmitter.cpp.

361 {
362  auto diff = std::chrono::duration_cast<std::chrono::seconds>(expiry_time - _get_ntp_epoch());
363  _file_entry.expires = diff.count();
364  _file_entry.cache_control.cache_expires = _file_entry.expires;
365 
366  return *this;
367 }
std::optional< uint64_t > cache_expires
struct LibFlute::FileDeliveryTable::FileEntry::@0 cache_control

◆ toi() [1/2]

uint32_t LibFlute::Transmitter::FileDescription::toi ( ) const
inline

Get the TOI associated with this file description.

This is meaningless if has_tsi() is false.

Returns
the TOI associated with this file description

Definition at line 186 of file Transmitter.h.

186 { return _file_entry.toi; };

◆ toi() [2/2]

FileDescription& LibFlute::Transmitter::FileDescription::toi ( uint32_t  val)
inlineprotected

Set the TOI (used by the Transmitter class)

Parameters
valThe new TOI value
Returns
this file description

Definition at line 335 of file Transmitter.h.

335 { _file_entry.toi = val; return *this; };

◆ tsi() [1/2]

uint64_t LibFlute::Transmitter::FileDescription::tsi ( ) const
inline

Get the associated TSI value.

Returns
the TSI value associated with this file description or 0 if not set
See also
has_tsi()

Definition at line 177 of file Transmitter.h.

177 { return _tsi?_tsi.value():0; };

◆ tsi() [2/2]

FileDescription& LibFlute::Transmitter::FileDescription::tsi ( uint64_t  val)
inlineprotected

Set the TSI (used by the Transmitter class)

Parameters
valThe new TSI value
Returns
this file description

Definition at line 328 of file Transmitter.h.

328 { _tsi = val; return *this; };

Friends And Related Function Documentation

◆ Transmitter

friend class Transmitter
friend

Definition at line 321 of file Transmitter.h.


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