-
Notifications
You must be signed in to change notification settings - Fork 5
/
TrackerFileReading.cpp
executable file
·310 lines (235 loc) · 6.35 KB
/
TrackerFileReading.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* TrackerFileReading.cpp
*
* Created on: Nov 11, 2015
* Author: irti
*/
#include <iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<algorithm>
#include"FileMaking.cpp"
using namespace std;
// template end
// template to conert String to Integer
template<typename T>
T StringToNumber(const string &Text) //Text not by const reference so that the function can be used with a
{ //character array as argument
stringstream ss(Text);
T result;
return ss >> result ? result : 0;
}
// template end
int StringToInt(string s) {
int integer;
std::istringstream ss(s);
ss >> integer;
return integer;
}
struct TrackerFile {
// attributes
string peer;
string ip;
int port;
string File_name;
string chunks[20];
};
// strcut end
class TrackerFileReader { // TrackerFileReader class
string filename;
TrackerFile *tracker;
public:
TrackerFileReader() {
filename = "Tracker";
tracker = NULL;
}
int No_of_lines_in_File() {
int _no_of_lines = 0;
string line;
fstream no_of_lines;
no_of_lines.open("Tracker", ios::out | ios::in);
while (getline(no_of_lines, line)) {
_no_of_lines++;
}
return _no_of_lines;
} // function ends
void Load_file() {
fstream Tracker_file;
string line;
tracker = new TrackerFile[No_of_lines_in_File()];
Tracker_file.open(&filename[0], ios::out | ios::in);
if (Tracker_file.is_open()) {
//cout << "file is Open" << endl;
// while (getline (Tracker_file,line,'\t')) { // loop to get particular values
for (int i = 0; getline(Tracker_file, line, '\t'); i++) {
if (i > 0) {
tracker[i - 1].peer = line;
}
getline(Tracker_file, line, '\t');
// cout<<line<<endl;
if (i > 0) {
tracker[i - 1].ip = line;
// cout<<tracker[i-1].ip<<endl;
}
getline(Tracker_file, line, '\t');
if (i > 0) {
tracker[i - 1].port = StringToInt(line);
}
getline(Tracker_file, line, '\t');
if (i > 0) {
tracker[i - 1].File_name = line;
}
getline(Tracker_file, line, '\n');
if (i > 0) {
stringstream ss(line);
for (int indexs = 0;
getline(ss, tracker[i - 1].chunks[indexs], ',');
indexs++) {
//cout<<tracker[i].chunks[indexs]<<endl;
} // internal for loop end
}
} // outer for end
} // if condition end to check file is open
else {
//cout << "Cannot open" << endl;
}
} // function ends
void Display_data() {
for (int i = 0; i < No_of_lines_in_File() - 1; i++) {
cout << "Tracker : " << i << endl << endl;
cout << tracker[i].peer << endl;
cout << "IP " << tracker[i].ip << endl;
cout << tracker[i].port << endl;
cout << tracker[i].File_name << endl;
for (int j = 0; tracker[i].chunks[j] != "\0"; j++) {
cout << tracker[i].chunks[j] << endl;
} // inner for
} // outer for
} // function ends
vector<int> get_chunks(string s) {
vector<int> chunks;
vector<int>::iterator it;
for (int i = 0; i < No_of_lines_in_File() - 1; i++) {
if (tracker[i].File_name == s) {
for (int j = 0; tracker[i].chunks[j] != "\0"; j++) {
int str = StringToInt(tracker[i].chunks[j]);
it = find(chunks.begin(), chunks.end(), str);
if (it != chunks.end()) {
} else {
chunks.push_back(str);
}
} // inner for
} // if end
} // for end
return chunks;
}
int get_no_of_chunks(string s) {
vector<int> chunks;
vector<int>::iterator it;
for (int i = 0; i < No_of_lines_in_File() - 1; i++) {
if (tracker[i].File_name == s) {
for (int j = 0; tracker[i].chunks[j] != "\0"; j++) {
int str = StringToInt(tracker[i].chunks[j]);
it = find(chunks.begin(), chunks.end(), str);
if (it != chunks.end()) {
} else {
chunks.push_back(str);
}
} // inner for
} // if end
} // for end
return chunks.size();
} // get chunks end
vector<int> getUniquePorts() {
TrackerFileReader *Tfile1 = new TrackerFileReader();
Tfile1[0].Load_file();
vector<string> files = Tfile1[0].filenames();
vector<int> ports;
// int count =0;
for (int i = 0; i < files.size(); i++) {
vector<int> chunks = Tfile1[0].get_chunks(files[i]);
for (int j = 0; j < chunks.size(); j++) {
int port = Tfile1[0].find_port(files[i], chunks[j]);
//port = Tfile1[0].find_port(files[i], chunks[j]);
vector<int>::iterator it;
it = find(ports.begin(), ports.end(), port);
if (it != ports.end()) {
} else if (i != files.size() - 1) {
ports.push_back(port);
//cout<<port<<endl;
}
}
}
return ports;
}
vector<string> filenames() {
vector<string> arr;
string str;
for (int i = 0; i < No_of_lines_in_File() - 1; i++) {
str = tracker[i].File_name;
vector<string>::iterator it;
it = find(arr.begin(), arr.end(), str);
if (it != arr.end()) {
} else {
arr.push_back(str);
//get_chunks(str);
}
}
for (unsigned int i = 0; i < arr.size(); i++) {
//cout << arr[i] << endl;
}
return arr;
} // function end
string getFileName(int port) {
for (int i = 0; i < No_of_lines_in_File() - 1; i++) {
if (tracker[i].port == port) {
return tracker[i].File_name;
// inner for
}
} // outer for
string filename;
return filename;
}
void makeFilesAccordingly() {
for (int i = 0; i < No_of_lines_in_File()-2; i++) {
string filename = tracker[i].File_name;
int port = tracker[i].port;
int size=0;
for (int j = 0; tracker[i].chunks[j] != "\0"; j++)
{
size++;
}
char* destinationName=new char[filename.length() + 1];
strcpy(destinationName, filename.c_str());
//cout<<"Destination:"<<destinationName<<endl;
string folder="Port"+NumberToString(port);
//cout<<"Folder:"<<folder<<endl;
//cout<<"Size:"<<size<<endl;
// vector<string> arr = filenames() ;
// for (unsigned int i = 0; i < arr.size(); i++) {
if(port!=0)
{
cout<<"Chunking : "<<filename << endl;
chunkFile(&filename[0], destinationName , size-1,folder); // is mein path file ka path ha
}
// }
}
}
int find_port(string filename, int chunk) {
int return_val;
for (int i = 0; i < No_of_lines_in_File() - 1; i++) {
if (tracker[i].File_name == filename) {
for (int j = 0; tracker[i].chunks[j] != "\0"; j++) {
int str = StringToInt(tracker[i].chunks[j]);
if (str == chunk) {
return_val = tracker[i].port;
break;
}
} // inner for
}
} // outer for
return return_val;
}
};