blob: 89d3173d1bd295929a5b6bcb0fbfb00f11329a61 [file] [log] [blame]
spirosmastorakisa6057f52016-01-28 13:34:41 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3* Copyright (c) 2016 Regents of the University of California.
4*
5* This file is part of the nTorrent codebase.
6*
7* nTorrent is free software: you can redistribute it and/or modify it under the
8* terms of the GNU Lesser General Public License as published by the Free Software
9* Foundation, either version 3 of the License, or (at your option) any later version.
10*
11* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
12* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14*
15* You should have received copies of the GNU General Public License and GNU Lesser
16* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
17* <http://www.gnu.org/licenses/>.
18*
19* See AUTHORS for complete list of nTorrent authors and contributors.
20*/
21
22#ifndef TORRENT_FILE_HPP
23#define TORRENT_FILE_HPP
24
25#include <ndn-cxx/name.hpp>
26#include <ndn-cxx/encoding/block.hpp>
27#include <ndn-cxx/data.hpp>
28
29#include <memory>
30
31namespace ndn {
32
33namespace ntorrent {
34
35class TorrentFile : public Data {
36public:
37 class Error : public Data::Error
38 {
39 public:
40 explicit
41 Error(const std::string& what)
42 : Data::Error(what)
43 {
44 }
45 };
46
47 /**
48 * @brief Create a new empty .TorrentFile.
49 */
50 TorrentFile() = default;
51
52 /**
53 * @brief Create a new .TorrentFile.
54 * @param torrentFileName The name of the .torrent-file
55 * @param torrentFilePtr A pointer (name) to the next segment of the .torrent-file
56 * @param commonPrefix The common name prefix of the manifest file names included in the catalog
57 * @param catalog The catalog containing the name of each file manifest
58 */
59 TorrentFile(const Name& torrentFileName,
60 const Name& torrentFilePtr,
61 const Name& commonPrefix,
62 const std::vector<ndn::Name>& catalog);
63
64 /**
65 * @brief Create a new .TorrentFile.
66 * @param torrentFileName The name of the .torrent-file
67 * @param commonPrefix The common name prefix of the manifest file names included in the catalog
68 * @param catalog The catalog containing the name of each file manifest
69 */
70 TorrentFile(const Name& torrentFileName,
71 const Name& commonPrefix,
72 const std::vector<ndn::Name>& catalog);
73
74 /**
75 * @brief Create a new .TorrentFile
76 * @param block The block format of the .torrent-file
77 */
78 explicit
79 TorrentFile(const Block& block);
80
81 /**
82 * @brief Get the name of the .TorrentFile
83 */
84 const Name&
85 getName() const;
86
87 /**
88 * @brief Get the common prefix of the file manifest names of this .torrent-file
89 */
90 const Name&
91 getCommonPrefix() const;
92
93 /**
94 * @brief Get a shared pointer to the name of the next segment of the .torrent-file.
95 *
96 * If there is no next segment, it returns a nullptr
97 */
98 shared_ptr<Name>
99 getTorrentFilePtr() const;
100
101 /**
102 * @brief Get the catalog of names of the file manifests
103 */
104 const std::vector<Name>&
105 getCatalog() const;
106
107 /**
108 * @brief Decode from wire format
109 */
110 void
111 wireDecode(const Block& wire);
112
113 /**
114 * @brief Encode from wire format
115 */
116 const Block&
117 wireEncode();
118
119 /**
120 * @brief Insert a name to the catalog of file manifest names
121 */
122 void
123 insert(const Name& name);
124
125 /**
126 * @brief Erase a name from the catalog of file manifest names
127 */
128 bool
129 erase(const Name& name);
130
131 /**
132 * @brief Get the size of the catalog of file manifest names
133 */
134 size_t
135 catalogSize() const;
136
137protected:
138 /**
139 * @brief prepend .torrent file as a Content block to the encoder
140 */
141 template<encoding::Tag TAG>
142 size_t
143 encodeContent(EncodingImpl<TAG>& encoder) const;
144
145 void
146 encodeContent();
147
148 void
149 decodeContent();
150
151private:
152 /**
153 * @brief Check whether the .torrent-file has a pointer to the next segment
154 */
155 bool
156 hasTorrentFilePtr() const;
157
158 /**
159 * @brief Create a catalog of suffixes for the names of the file manifests
160 *
161 * To optimize encoding and decoding, we encode the name of the file manifests
162 * as suffixes along with their common prefix.
163 *
164 */
165 void
166 createSuffixCatalog();
167
168 /**
169 * @brief Construct the catalog of long names from a catalog of suffixes for the file
170 * manifests' name
171 *
172 * After decoding a .torrent-file from its wire format, we construct the catalog of
173 * long names from the decoded common prefix and suffixes
174 *
175 */
176 void
177 constructLongNames();
178
179 /**
180 * @brief Insert a suffix to the suffix catalog
181 */
182 void
183 insertToSuffixCatalog(const PartialName& suffix);
184
185private:
186 Name m_commonPrefix;
187 Name m_torrentFilePtr;
188 std::vector<ndn::Name> m_suffixCatalog;
189 std::vector<ndn::Name> m_catalog;
190};
191
192inline bool
193TorrentFile::hasTorrentFilePtr() const
194{
195 return !m_torrentFilePtr.empty();
196}
197
198inline const std::vector<Name>&
199TorrentFile::getCatalog() const
200{
201 return m_catalog;
202}
203
204inline size_t
205TorrentFile::catalogSize() const
206{
207 return m_catalog.size();
208}
209
210inline void
211TorrentFile::insert(const Name& name)
212{
213 m_catalog.push_back(name);
214}
215
216inline void
217TorrentFile::insertToSuffixCatalog(const PartialName& suffix)
218{
219 m_suffixCatalog.push_back(suffix);
220}
221
222} // namespace ntorrent
223
224} // namespace ndn
225
226#endif // TORRENT_FILE_HPP