forked from pt209223/librs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RS.cpp
527 lines (436 loc) · 14.6 KB
/
RS.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "RS.h"
#include "Time.h"
#include "Http.h"
// C
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
// C++
#include <cerrno>
#include <cstdlib>
#include <iostream>
// BOOST
#include <boost/regex.hpp>
#include <boost/thread.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
// regexy
#include "RS_regex.h"
using namespace std;
using namespace boost;
// @brief: do sciagania plikow
static Http http;
// @brief: oczekiwanie z roznych powodow:
static const size_t rs_waiting_try_later = 60;
static const size_t rs_waiting_busy = 120;
static const size_t rs_waiting_rivalry = 60;
static const size_t rs_waiting_limit = 120;
// @brief: lokalizacja katalogow na pliki:
static filesystem::path rs_download_dir_path = "./d/"; // !TODO!boost::filesystem!
static filesystem::path rs_sessions_dir_path = "./s/"; // !TODO!boost::filesystem!
// @brief: strumien, jak z C, do zapisu logow
static FILE *rs_log = NULL;
// @brief: zapis po logow, jak printf z C
static void rsprintf(const char *fmt, ...)
{
if (!rs_log) return;
char stamp[Time::stamp_length];
fprintf(rs_log, "%s [RS] ", Time::stamp(stamp));
va_list args;
va_start(args, fmt);
vfprintf(rs_log, fmt, args);
va_end(args);
fprintf(rs_log, "\n");
fflush(rs_log);
}
// @brief: generowanie sciezki do pliku
static const filesystem::path &rs_sessions_get_path(const string &url, const char *suffix)
{
static filesystem::path path;
path = rs_sessions_dir_path / (url.substr(url.rfind('/')) + "-" + Time::stamp() + suffix);
return path;
}
// @brief: generowanie sciezki do pliku
static const filesystem::path &rs_download_get_path(const string &url)
{
static filesystem::path path;
path = rs_download_dir_path / url.substr(url.rfind('/'));
return path;
}
// @brief: informacje o aktualnie pobieranym pliku
static RS::Info rs_info;
// @brief: ochrona danych
static boost::mutex rs_mutex;
// @brief: zmienna warunekowa
static boost::condition_variable rs_cond;
// @brief: czy jest co sciagac
static bool rs_is_ready = false;
struct DExc { };
struct DAgain : public DExc { };
struct DBreak : public DExc { };
struct DAbort : public DExc {
DAbort(RS::Status s) : status(s) { }
const RS::Status status;
};
static void rs_wait(RS::Status pre, RS::Status post, size_t secs)
{
{
unique_lock<mutex> lock(rs_mutex);
rs_info.status = pre;
rs_info.waiting = secs + 1;
}
while (--rs_info.waiting) sleep(1);
unique_lock<mutex> lock(rs_mutex);
rs_info.status = post;
}
static uint64_t rs_progress_start = 0;
static bool rs_progress_callback(const char *, size_t len, void *)
{
uint64_t now = Time::in_usec(),
dif = now - rs_progress_start;
rs_info.bytes += len;
rs_info.usecs += dif;
rs_info.tmpspeed = ((long double)len) / ((long double)dif) * 1.0e6;
rs_progress_start = now;
return true;
}
struct rs_loop {
void operator()(void)
{
while (true) {
rsprintf("Oczekiwanie na nowe zadanie...");
{
unique_lock<mutex> lock(rs_mutex);
while (!rs_is_ready)
rs_cond.wait(lock);
if (rs_info.url == "") {
rsprintf("Pusty utr, watek pobierajacy konczy dzialanie");
return;
}
}
rsprintf("Nowy plik do pobrania: '%s'", rs_info.url.c_str());
uint32_t tries = 0;
while (true) {
try {
string url = rs_info.url;
stage_1(url);
stage_2(url);
stage_3(url);
rsprintf("Plik pobrany: '%s', %lluB w %llu.%.3llu sek (%.3f KB/s)",
rs_info.url.c_str(), rs_info.bytes, rs_info.usecs/100000, (rs_info.usecs/1000)%1000,
1.0e3*(((double)rs_info.bytes)/((double)rs_info.usecs)));
unique_lock<mutex> lock(rs_mutex);
rs_info.status = RS::Downloaded;
rs_is_ready = false;
break; // wychodzimy z 'while'
}
catch (DBreak) { // nastepna proba pobrania
rsprintf("Nie udalo sie pobrac pliku: '%s' (proba %u)", rs_info.url.c_str(), tries);
++tries;
if (tries < 5) continue; // wracamy na poczatek 'while'
}
catch (DAgain) { // od nowa jeszcze raz
rsprintf("Nie udalo sie pobrac pliku: '%s', (proba %u i od nowa)", rs_info.url.c_str(), tries);
tries = 0;
continue; // wracamy na poczatek 'while'
}
catch (DAbort &e) { // odrzucono
rsprintf("Nie udalo sie pobrac pliku: '%s', odrzucono przez rapidshare.com", rs_info.url.c_str());
unique_lock<mutex> lock(rs_mutex);
rs_info.status = e.status; //RS::Canceled;
rs_is_ready = false;
break; // wychodzimu z 'while'
}
rsprintf("Nie udalo sie pobrac pliku: '%s', osiagniety limit prob", rs_info.url.c_str());
unique_lock<mutex> lock(rs_mutex);
rs_info.status = RS::Canceled;
rs_is_ready = false;
break; // wyjscie z petli
}
}
}
void stage_1(string &url)
{
rsprintf("Lacze z '%s'... (1)", url.c_str());
char *buffer = NULL;
size_t buflen = 0;
try {
int code = http.get(buffer, buflen, url.c_str());
filesystem::ofstream head(rs_sessions_get_path(url, "-head1.http"));
head.write(http.get_recv_header(), strlen(http.get_recv_header()));
if (code == HTTP_OK) // Oczekujemy kodu 200
rsprintf("Polaczono, code = %d, page = %p,%zd", code, buffer, buflen);
else {
rsprintf("Polaczono, brak strony? code = %d, page = %p,%zd, sprobuje za chwile...", code, buffer, buflen);
if (buffer) delete[] buffer;
throw DBreak();
}
filesystem::ofstream page(rs_sessions_get_path(url, "-page1.html"));
page.write(buffer, buflen);
}
catch (Exception &e) {
rsprintf("Nastapil wyjatek: %s", e.what());
throw DBreak();
}
// Sprawdzamy czy plik jest dostepny...
if (regex_search(buffer, rs_regex_illegal_file) ||
regex_search(buffer, rs_regex_illegal_file2) ||
regex_search(buffer, rs_regex_not_available) ||
regex_search(buffer, rs_regex_not_found)) {
rsprintf("Plik '%s' jest niedostepny", url.c_str());
delete[] buffer;
//rs_info.status = RS::NotFound;
throw DAbort(RS::NotFound);
}
// Szukamy nastepnego url'a...
cmatch what;
if (!regex_search(buffer, what, rs_regex_url)) {
rsprintf("Brak odnosnika do nastepnej strony, sprobuje jeszcze raz...");
delete[] buffer;
throw DBreak();
}
url = what[1]; // ustawiamy nowy url
delete[] buffer;
}
void stage_2(string &url)
{
rsprintf("Lacze z '%s' (2)", url.c_str());
char *buffer = NULL;
size_t buflen = 0;
try {
int code = http.get(buffer, buflen, url.c_str(), "dl.start=Free");
filesystem::ofstream head(rs_sessions_get_path(url, "-head2.http"));
head.write(http.get_recv_header(), strlen(http.get_recv_header()));
if (code == HTTP_OK) // Oczekujemy kodu 200
rsprintf("Polaczono, code = %d, page = %p,%zd", code, buffer, buflen);
else {
rsprintf("Polaczono, brak strony? code = %d, page = %p,%zd, try again", code, buffer, buflen);
if (buffer) delete[] buffer;
throw DBreak();
}
filesystem::ofstream page(rs_sessions_get_path(url, "-page2.html"));
page.write(buffer, buflen);
}
catch (Exception &e) {
rsprintf("Nastapil wyjatek: %s", e.what());
throw DBreak();
}
if (regex_search(buffer, rs_regex_try_later)) {
rsprintf("Nie mozna teraz pobierac pliku, sprobuje za chwile, czekam...");
rs_wait(RS::Waiting, RS::Preparing, rs_waiting_try_later);
delete[] buffer;
throw DAgain();
}
if (regex_search(buffer, rs_regex_reached_limit)) {
rsprintf("Wyczerpal sie limit pobran, czekam...");
rs_wait(RS::Limit, RS::Preparing, rs_waiting_limit);
delete[] buffer;
throw DAgain();
}
if (regex_search(buffer, rs_regex_server_busy)) {
rsprintf("Serwery sa przeciazone, czekam...");
rs_wait(RS::Busy, RS::Preparing, rs_waiting_busy);
delete[] buffer;
throw DAgain();
}
if (regex_search(buffer, rs_regex_server_busy)) {
rsprintf("Brak wolnych slotow, czekam...");
rs_wait(RS::Busy, RS::Preparing, rs_waiting_busy);
delete[] buffer;
throw DAgain();
}
if (regex_search(buffer, rs_regex_already_downloading)) {
rsprintf("Ktos o tym samym IP pobiera juz jakis plik, czekam...");
rs_wait(RS::Rivalry, RS::Preparing, rs_waiting_rivalry);
delete[] buffer;
throw DAgain();
}
size_t wait_for = 0;
string ssize;
cmatch what;
if (regex_search(buffer, what, rs_regex_time)) {
wait_for = strtol(what[1].str().c_str(), 0, 10) + 5;
rsprintf("Czekam %zd sek...", wait_for);
} else {
wait_for = 5;
rsprintf("Nieznany czas oczekiwania, sprobuje %zd sek...", wait_for);
}
if (!regex_search(buffer, what, rs_regex_size)) {
rsprintf("Nieznany rozmiar pliku, jeszcze raz...");
delete[] buffer;
throw DBreak();
}
ssize = what[1];
rs_info.all_bytes = 1000*strtol(ssize.c_str(), 0, 10);
try {
vector<pair<string, string> > srvs;
for (cregex_iterator it(buffer, buffer+buflen, rs_regex_server), end; it != end; ++it)
srvs.push_back(make_pair<string, string>((*it)[2], (*it)[1]));
if (!srvs.size()) {
rsprintf("Brak serwerow, przerywam...");
throw DBreak();
}
size_t i = 0;
while (rs_favorites_servers[i]) {
size_t found = 0, end = srvs.size();
while (found < end) {
if (srvs[found].first == rs_favorites_servers[i]) break;
++found;
}
if (found == end) {
rsprintf("Brak serwera '%s', szukam dalej...", rs_favorites_servers[i]);
++i;
continue;
}
rsprintf("Wybieram serwer '%s'...", rs_favorites_servers[i]);
url = srvs[found].second;
break;
}
if (!rs_favorites_servers[i]) {
url = srvs[0].second;
rsprintf("Brak jakiegokolwiek ulubionego serwera, wybieram pierwszy z brzegu '%s'",
srvs[0].first.c_str());
}
}
catch (DBreak) { delete[] buffer; throw; }
catch (DExc &e) { delete[] buffer; throw e; }
rs_wait(RS::Waiting, RS::Preparing, wait_for);
delete[] buffer;
}
void stage_3(string &url)
{
rsprintf("Lacze z '%s' (3)", url.c_str());
rs_progress_start = Time::in_usec();
rs_info.status = RS::Downloading;
rs_info.bytes = 0;
rs_info.tmpspeed = 0;
off_t length = 0;
try { // TODO
int code = http.get(rs_download_get_path(rs_info.url.c_str()).string().c_str(), length, url.c_str(),
"mirror=", NULL, rs_progress_callback, NULL);
filesystem::ofstream head(rs_sessions_get_path(url, "-head3.http"));
head.write(http.get_recv_header(), strlen(http.get_recv_header()));
if (code == HTTP_OK) // Oczekujemy kodu 200
rsprintf("Polaczono, code = %d, length = %lld", code, length);
else {
rsprintf("Polaczono, brak strony? code = %d, length = %lld, try again", code, length);
throw DBreak();
}
//string clen = http.get_recv_header("content-length");
//rsprintf("");
}
catch (Exception &e) {
rsprintf("Nastapil wyjatek: %s", e.what());
throw DBreak();
}
}
} rs_loop_object;
// @brief: watek w petli sciagajacy pliki z rapidshare.com
static boost::thread rs_thread(rs_loop_object);
// @brief: kontruktor - nic nie robi
RS::RS(void) throw() { }
// @brief: destruktor - nic nie robi
RS::~RS(void) throw() { }
void RS::download(const char *url) throw(Exception)
{
unique_lock<mutex> lock(rs_mutex);
// jak status nie jest taki jak nizej to ok
if (rs_info.status != None && rs_info.status != Downloaded &&
rs_info.status != Canceled && rs_info.status != NotFound)
throw EExternal("teraz sie pobierany plik: '%s' (status: '%s')",
rs_info.url.c_str(), strstatus(rs_info.status));
if (!boost::regex_match(url, rs_regex_correct_url)) {
rsprintf("Nieprawidlowy url: '%s'", url);
throw EExternal("nieprawidlowy url '%s'", url);
}
rs_info.status = Preparing;
rs_info.url = url;
rs_info.bytes = 0;
rs_info.all_bytes = 0;
rs_info.usecs = 0;
rs_info.tmpspeed = 0.;
rs_info.waiting = 0;
rs_is_ready = true;
rs_cond.notify_one();
rsprintf("Dodano plik do pobrania: '%s'", url);
}
const char *RS::strstatus(Status s)
{
switch (s) {
case None:
return "nic do roboty";
case Downloaded:
return "plik zostal pobrany";
case Canceled:
return "pobieranie zostalo anulowane";
case NotFound:
return "nie znaleziono pliku";
case Preparing:
return "przygotowanie do pobierania";
case Downloading:
return "plik jest pobierany";
case Waiting:
return "oczekiwanie na pobranie";
case Later:
return "sprobuje za chwile";
case Rivalry:
return "ktos rowniez korzysta z rapidshare.com";
case Limit:
return "wyczerpany limit";
case Busy:
return "serwery sa przeciezone";
default:
return "nieznany status";
};
}
void RS::set_download_dir(const char *path) throw(Exception)
{
if (!filesystem::is_directory(path))
throw EExternal("'%s' nie istnieje lub nie jest katalogiem", path);
rs_download_dir_path = path;
}
void RS::set_sessions_dir(const char *path) throw(Exception)
{
if (!filesystem::is_directory(path))
throw EExternal("'%s' nie istnieje lub nie jest katalogiem", path);
rs_sessions_dir_path = path;
}
void RS::set_log_file(const char *path) throw(Exception)
{
if (!(rs_log = fopen(path, "a")))
throw EExternal("nie mozna otworzyc pliku: '%s': %d, %s",
path, errno, strerror(errno));
rsprintf("Loguje do pliku: '%s'", path);
}
void RS::set_log_stderr(void) throw(Exception)
{
rs_log = stderr;
rsprintf("Loguje na stderr");
}
void RS::get_info(Info &info) throw()
{
unique_lock<mutex> lock(rs_mutex);
info = rs_info;
}
void RS::close(void) throw(Exception)
{
{
unique_lock<mutex> lock(rs_mutex);
// jak status nie jest taki jak nizej to ok
if (rs_info.status != None && rs_info.status != Downloaded &&
rs_info.status != Canceled && rs_info.status != NotFound)
throw EExternal("nie mozna zamknac, plik '%s' jest pobierany (status: '%s')",
rs_info.url.c_str(), strstatus(rs_info.status));
rs_info.status = Preparing;
rs_info.url = "";
rs_info.bytes = 0;
rs_info.all_bytes = 0;
rs_info.usecs = 0;
rs_info.tmpspeed = 0;
rs_info.waiting = 0;
rs_is_ready = true;
rs_cond.notify_one();
}
rs_thread.join();
rsprintf("Zamknieto watek pobierajacy");
}