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...
 
FileDescriptionmerge_fec_oti (const FecOti &fec_oti)
 Merge the FecOti values. More...
 

Friends

class Transmitter
 

Detailed Description

File Description object.

Definition at line 51 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 53 of file Transmitter.h.

Member Enumeration Documentation

◆ CompressionAlgorithm

Enumerator
COMPRESSION_NONE 
COMPRESSION_GZIP 
COMPRESSION_DEFLATE 

Definition at line 55 of file Transmitter.h.

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

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 61 of file Transmitter.cpp.

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

◆ 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 74 of file Transmitter.cpp.

75  : _tsi()
76  , _file_entry({ .toi=0, .content_location=content_location})
78  , _filename()
79  , _file_handle(-1)
80  , _data(data.data())
81  , _data_length(data.size())
82 {
83  _calculate_file_entry();
84 }
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 86 of file Transmitter.cpp.

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

◆ 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 98 of file Transmitter.cpp.

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

◆ 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 110 of file Transmitter.cpp.

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

◆ 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 122 of file Transmitter.cpp.

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

◆ 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 147 of file Transmitter.cpp.

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

◆ ~FileDescription()

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

Destructor.

Free all resources associated with this file description.

Definition at line 161 of file Transmitter.cpp.

162 {
163  _free_file_data();
164 }

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 227 of file Transmitter.cpp.

228 {
229  return _data;
230 }

◆ 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 232 of file Transmitter.cpp.

233 {
234  return _data_length;
235 }

◆ 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 192 of file Transmitter.h.

192  {
193  return _file_entry;
194  };

◆ 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 368 of file Transmitter.cpp.

369 {
370  return _file_entry.etag;
371 }

◆ 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 356 of file Transmitter.cpp.

357 {
358  auto durn = std::chrono::duration_cast<date_time_type::duration>(std::chrono::seconds(_file_entry.expires));
359  return _get_ntp_epoch() + durn;
360 }
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 168 of file Transmitter.h.

168 { 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 373 of file Transmitter.cpp.

374 {
375  if (static_cast<unsigned>(_file_entry.fec_oti.encoding_id) == 0) {
376  _file_entry.fec_oti.encoding_id = fec_oti.encoding_id;
377  }
378  if (!_file_entry.fec_oti.instance_id) {
379  _file_entry.fec_oti.instance_id = fec_oti.instance_id;
380  }
381  if (!_file_entry.fec_oti.transfer_length) {
382  _file_entry.fec_oti.transfer_length = fec_oti.transfer_length;
383  }
384  if (!_file_entry.fec_oti.encoding_symbol_length) {
385  _file_entry.fec_oti.encoding_symbol_length = fec_oti.encoding_symbol_length;
386  }
387  if (!_file_entry.fec_oti.max_source_block_length) {
388  _file_entry.fec_oti.max_source_block_length = fec_oti.max_source_block_length;
389  }
390  if (!_file_entry.fec_oti.max_number_of_encoding_symbols) {
391  _file_entry.fec_oti.max_number_of_encoding_symbols = fec_oti.max_number_of_encoding_symbols;
392  }
393  return *this;
394 }
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 166 of file Transmitter.cpp.

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

◆ 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 194 of file Transmitter.cpp.

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

◆ operator==()

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

Equality operator.

Check if two file descriptions are equivalent

Definition at line 211 of file Transmitter.cpp.

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

◆ 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 237 of file Transmitter.cpp.

239 {
240  if (compression != _compression_type) {
241  _compression_type = compression;
242  switch (_compression_type) {
243  case COMPRESSION_GZIP:
244  _file_entry.content_encoding = "gzip";
245  break;
246  case COMPRESSION_DEFLATE:
247  _file_entry.content_encoding = "deflate";
248  break;
249  default:
250  _file_entry.content_encoding.clear();
251  break;
252  }
253  /* change in compression will change transmitted data, reset the TOI */
254  _file_entry.toi = 0;
255  _calculate_file_entry();
256  }
257 
258  return *this;
259 }

◆ 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 281 of file Transmitter.cpp.

282 {
283  if (!data) data_length=0;
284  if (data != _data || _data_length != data_length) {
285  /* data area has changed in some way, do we need to reset the TOI? */
286  if (_data_length != data_length) {
287  /* data length has changed, reset the TOI */
288  _file_entry.toi = 0;
289  } else if (data) {
290  if (!_data) {
291  if (data_length) {
292  /* data being added, reset the TOI */
293  _file_entry.toi = 0;
294  }
295  } else if (data_length) {
296  /* had data before and have new data now, but are they the same? */
297  unsigned char md5[MD5_DIGEST_LENGTH];
298  MD5(reinterpret_cast<const unsigned char*>(data), data_length, md5);
299  if (_file_entry.content_md5 != base64_encode(md5, sizeof(md5))) {
300  /* data contents are different, reset TOI */
301  _file_entry.toi = 0;
302  }
303  }
304  } else if (_data) {
305  /* data being removed, reset the TOI */
306  _file_entry.toi = 0;
307  }
308 
309  _free_file_data();
310  _data = data;
311  _data_length = data_length;
312  _calculate_file_entry();
313  }
314 
315  return *this;
316 }
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 268 of file Transmitter.cpp.

269 {
270  if (filename != _filename) {
271  _free_file_data();
272  _attach_file(filename);
273  /* Assume a change of filename changes the contents too and zero the TOI */
274  _file_entry.toi = 0;
275  _calculate_file_entry();
276  }
277 
278  return *this;
279 }

◆ 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 318 of file Transmitter.cpp.

319 {
320  return set_content(data.data(), data.size());
321 }
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 323 of file Transmitter.cpp.

324 {
325  return set_content(reinterpret_cast<const char*>(data.data()), data.size());
326 }

◆ 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 261 of file Transmitter.cpp.

262 {
263  _file_entry.content_location = location;
264 
265  return *this;
266 }

◆ 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 328 of file Transmitter.cpp.

329 {
330  _file_entry.content_type = content_type;
331  return *this;
332 }

◆ 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 362 of file Transmitter.cpp.

363 {
364  _file_entry.etag = etag;
365  return *this;
366 }

◆ 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 346 of file Transmitter.cpp.

348 {
349  auto diff = std::chrono::duration_cast<std::chrono::seconds>(expiry_time - _get_ntp_epoch());
350  _file_entry.expires = diff.count();
351  _file_entry.cache_control.cache_expires = _file_entry.expires;
352 
353  return *this;
354 }
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 185 of file Transmitter.h.

185 { 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 334 of file Transmitter.h.

334 { _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 176 of file Transmitter.h.

176 { 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 327 of file Transmitter.h.

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

Friends And Related Function Documentation

◆ Transmitter

friend class Transmitter
friend

Definition at line 320 of file Transmitter.h.


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