blob: 0536c17b9999af968e172f83dcbafa0771897695 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2016 Regents of the University of California.
*
* This file is part of the nTorrent codebase.
*
* nTorrent is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS for complete list of nTorrent authors and contributors.
*/
#ifndef TORRENT_FILE_HPP
#define TORRENT_FILE_HPP
#include "file-manifest.hpp"
#include <ndn-cxx/name.hpp>
#include <ndn-cxx/encoding/block.hpp>
#include <ndn-cxx/data.hpp>
#include <memory>
namespace ndn {
namespace ntorrent {
class TorrentFile : public Data {
public:
class Error : public Data::Error
{
public:
explicit
Error(const std::string& what)
: Data::Error(what)
{
}
};
/**
* @brief Create a new empty TorrentFile.
*/
TorrentFile() = default;
/**
* @brief Create a new TorrentFile.
* @param torrentFileName The name of the torrent-file
* @param torrentFilePtr A pointer (name) to the next segment of the torrent-file
* @param commonPrefix The common name prefix of the manifest file names included in the catalog
* @param catalog The catalog containing the name of each file manifest
*/
TorrentFile(const Name& torrentFileName,
const Name& torrentFilePtr,
const Name& commonPrefix,
const std::vector<ndn::Name>& catalog);
/**
* @brief Create a new TorrentFile.
* @param torrentFileName The name of the torrent-file
* @param commonPrefix The common name prefix of the manifest file names included in the catalog
* @param catalog The catalog containing the name of each file manifest
*/
TorrentFile(const Name& torrentFileName,
const Name& commonPrefix,
const std::vector<ndn::Name>& catalog);
/**
* @brief Create a new TorrentFile
* @param block The block format of the torrent-file
*/
explicit
TorrentFile(const Block& block);
/**
* @brief Get the name of the TorrentFile
*/
const Name&
getName() const;
/**
* @brief Get the common prefix of the file manifest names of this torrent-file
*/
const Name&
getCommonPrefix() const;
/**
* @brief Get a shared pointer to the name of the next segment of the torrent-file.
*
* If there is no next segment, it returns a nullptr
*/
shared_ptr<Name>
getTorrentFilePtr() const;
/**
* @brief Get the catalog of names of the file manifests
*/
const std::vector<Name>&
getCatalog() const;
/**
* @brief Get the segment number for this torrent file
*/
size_t
getSegmentNumber() const;
/**
* @brief Get the directory name for this torrent file
*/
std::string
getTorrentFilePath() const;
/**
* @brief Decode from wire format
*/
void
wireDecode(const Block& wire);
/**
* @brief Finalize torrent-file before signing the data packet
*
* This method has to be called (every time) right before signing or encoding
* the torrent-file
*/
void
finalize();
/**
* @brief Insert a name to the catalog of file manifest names
*/
void
insert(const Name& name);
/**
* @brief Erase a name from the catalog of file manifest names
*/
bool
erase(const Name& name);
/**
* @brief Get the size of the catalog of file manifest names
*/
size_t
catalogSize() const;
/**
* @brief Given a directory path for the torrent file, it generates the torrent file
*
* @param directoryPath The path to the directory for which we are to create a torrent-file
* @param torrentFilePrefix The prefix to be used for the name of this torrent-file
* @param namesPerSegment The number of manifest names to be included in each segment of the
* torrent-file
* @param returnData Determines whether the data would be returned in memory or it will be
* stored on disk without being returned
*
* Generates the torrent-file for the directory at the specified 'directoryPath',
* splitting the torrent-file into multiple segments, each one of which contains
* at most 'namesPerSegment' number of manifest names
*
**/
static std::pair<std::vector<TorrentFile>,
std::vector<std::pair<std::vector<FileManifest>,
std::vector<Data>>>>
generate(const std::string& directoryPath,
size_t namesPerSegment,
size_t subManifestSize,
size_t dataPacketSize,
bool returnData = false);
protected:
/**
* @brief prepend torrent file as a Content block to the encoder
*/
template<encoding::Tag TAG>
size_t
encodeContent(EncodingImpl<TAG>& encoder) const;
void
encodeContent();
void
decodeContent();
private:
/**
* @brief Check whether the torrent-file has a pointer to the next segment
*/
bool
hasTorrentFilePtr() const;
/**
* @brief Create a catalog of suffixes for the names of the file manifests
*
* To optimize encoding and decoding, we encode the name of the file manifests
* as suffixes along with their common prefix.
*
*/
void
createSuffixCatalog();
/**
* @brief Construct the catalog of long names from a catalog of suffixes for the file
* manifests' name
*
* After decoding a torrent-file from its wire format, we construct the catalog of
* long names from the decoded common prefix and suffixes
*
*/
void
constructLongNames();
/**
* @brief Insert a suffix to the suffix catalog
*/
void
insertToSuffixCatalog(const PartialName& suffix);
/**
* @brief Set the pointer of the current torrent-file segment to the next segment
*/
void
setTorrentFilePtr(const Name& ptrName);
private:
Name m_commonPrefix;
Name m_torrentFilePtr;
std::vector<ndn::Name> m_suffixCatalog;
std::vector<ndn::Name> m_catalog;
};
inline bool
TorrentFile::hasTorrentFilePtr() const
{
return !m_torrentFilePtr.empty();
}
inline const std::vector<Name>&
TorrentFile::getCatalog() const
{
return m_catalog;
}
inline size_t
TorrentFile::catalogSize() const
{
return m_catalog.size();
}
inline void
TorrentFile::insert(const Name& name)
{
m_catalog.push_back(name);
}
inline void
TorrentFile::insertToSuffixCatalog(const PartialName& suffix)
{
m_suffixCatalog.push_back(suffix);
}
inline void
TorrentFile::setTorrentFilePtr(const Name& ptrName)
{
m_torrentFilePtr = ptrName;
}
inline const Name&
TorrentFile::getName() const
{
return Data::getName();
}
inline const Name&
TorrentFile::getCommonPrefix() const
{
return m_commonPrefix;
}
inline std::string
TorrentFile::getTorrentFilePath() const
{
return (0 == getSegmentNumber() ? getFullName().get(-3) : getFullName().get(-4)).toUri();
}
inline size_t
TorrentFile::getSegmentNumber() const
{
const auto& lastComponent = getName().get(getName().size() - 1);
return lastComponent.isSequenceNumber() ? lastComponent.toSequenceNumber() : 0;
}
} // namespace ntorrent
} // namespace ndn
#endif // TORRENT_FILE_HPP