-
Notifications
You must be signed in to change notification settings - Fork 1
/
Torrent.cpp
156 lines (150 loc) · 4.31 KB
/
Torrent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "Torrent.h"
#include <iostream>
#include <boost/variant/get.hpp>
#include <boost/variant/get.hpp>
#include <boost/bind.hpp>
namespace Bittorrent
{
//add tracker processing
Torrent::Torrent()
: clientRank{0},generalData(), piecesData(), hashesData(),
statusData(piecesData),
sig_addPeer{
std::make_shared<boost::signals2::signal<void(
peer, std::shared_ptr<Torrent>)>>()},
sig_pieceVerified{
std::make_shared<boost::signals2::signal<void(Torrent*, int)>>()}
{
}
valueDictionary Torrent::filesToDictionary(valueDictionary& dict)
{
if (fileList.empty())
{
throw std::invalid_argument("Error: no files in torrent!");
}
else if (fileList.size() == 1)
{
dict.emplace("name", fileList.at(0).filePath);
dict.emplace("length", fileList.at(0).fileSize);
}
else
{
valueList multiFilesList;
std::string majorPath;
for (size_t i = 0; i < fileList.size(); ++i)
{
valueDictionary fileDataDict;
valueList subPathList;
//add file size
fileDataDict.emplace("length", fileList.at(i).fileSize);
//format file paths (recursive folders check)
majorPath = fileList.at(i).filePath;
auto found = majorPath.find_first_of("/\\");
if (found == std::string::npos)
{
subPathList.push_back(majorPath);
}
else
{
std::string subPath;
while (found != std::string::npos)
{
subPath = majorPath.substr(0, found);
majorPath = majorPath.substr(found + 1);
subPathList.push_back(subPath);
found = majorPath.find_first_of("/\\");
if (found == std::string::npos)
{
subPathList.push_back(majorPath);
break;
}
}
}
//add path to subdict
fileDataDict.emplace("path", subPathList);
//add subdict to file list
multiFilesList.push_back(fileDataDict);
}
//add file list to parent files dict
dict.emplace("files", multiFilesList);
}
return dict;
}
void Torrent::setFileList(const valueDictionary& torrent)
{
valueDictionary info = boost::get<valueDictionary>(torrent.at("info"));
fileObj resObj;
//if torrent contains only one file
if (info.count("name") && info.count("length"))
{
resObj.filePath = boost::get<std::string>(info.at("name"));
resObj.fileSize = static_cast<long long>(boost::get<long long>(info.at("length")));
fileList.push_back(resObj);
}
//if torrent contains multiple files
else if (info.count("files"))
{
long long runningOffset = 0;
valueList list = boost::get<valueList>(info.at("files"));
for (size_t i = 0; i < list.size(); ++i)
{
valueDictionary fileData =
boost::get<valueDictionary>(list.at(i));
if (fileData.empty() || !fileData.count("length") || !fileData.count("path"))
{
throw std::invalid_argument("Error: incorrect file specification!");
}
//get file size and offset
if (fileData.count("length"))
{
long long size = static_cast<long>(boost::get<long long>(fileData.at("length")));
resObj.fileSize = size;
resObj.setReadableFileSize();
resObj.fileOffset = runningOffset;
//add to running size total
runningOffset += size;
}
//get file path
if (fileData.count("path"))
{
valueList pathList = boost::get<valueList>(fileData.at("path"));
std::string fullPath;
for (auto path : pathList)
{
fullPath += boost::get<std::string>(path);
fullPath += "/";
}
fullPath.erase(fullPath.find_last_of("/"));
resObj.filePath = fullPath;
}
fileList.push_back(resObj);
}
}
else
{
throw std::invalid_argument("Error: no files specified in torrent!");
}
//std::string test = boost::get<std::string>(decodedTorrent.at("info"));
//std::cout << test << std::endl;
}
void Torrent::handlePeerListUpdated()
{
for (auto& tracker : generalData.trackerList)
{
for (auto singlePeer : tracker.peerList)
{
if (generalData.uniquePeerList.insert(singlePeer).second == true)
{
if (!sig_addPeer->empty())
{
sig_addPeer->operator()(singlePeer, getPtr());
}
}
}
}
}
std::shared_ptr<Torrent> Torrent::getPtr()
{
return shared_from_this();
}
}