forked from pt209223/librs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrsmanager.cpp
263 lines (224 loc) · 7.38 KB
/
rsmanager.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
#include "Exception.h"
#include "Time.h"
#include "RS.h"
// C
#include <signal.h>
#include <stdlib.h>
// BOOST
#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace std;
using namespace boost;
// Glowny plik z kolejka zadan
const char *primary_queue = "q/primary.queue";
// Drugi plik z kolejka zadan, do niego dopisujemy nowe zadania
const char *extends_queue = "q/extends.queue";
// Tymczasowy plik na kolejkowanie zadan
const char *temporary_queue = "q/temporary.queue";
// Do tego katalogu pobieramy pliki
const char *download_dir = "./d/";
// Do tego katalogu zapisujemy strony html i naglowki http
const char *sessions_dir = "./s/";
// Do tego pliku RS loguje co robi
const char *log_file = "rs.log";
// Logowanie na ekran co sie dzieje
static void rsprintf(const char *fmt, ...)
{
char stamp[Time::stamp_length];
fprintf(stderr, "%s [RS::Manager] ", Time::stamp(stamp));
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fflush(stderr);
}
void get_url(string &url)
{
RS rs;
RS::Info info;
// Zlecamy pobranie pliku jak w url...
try { rs.download(url.c_str()); }
catch (std::exception &e) {
rsprintf("Nie mozna dodac url'a '%s', analuje...\n", url.c_str());
return;
}
rsprintf("Pobieramy plik : '%s'\n", url.c_str());
// ... i teraz czakamy az plik zostanie pobrany albo wystapi jakis blad
while (true) {
rs.get_info(info);
switch (info.status) {
case RS::Downloaded:
rsprintf("Plik pobrany, %6llu.%.3llu KB w %llu:%.2llu:%.2llu sek (%4llu.%.3llu KB/s) \n",
info.bytes/1000, info.bytes%1000, info.usecs/3600000000ULL,
(info.usecs/60000000ULL)%60ULL, (info.usecs/1000000ULL)%60ULL,
info.usecs ? (1000ULL*info.bytes/info.usecs) : 0ULL,
info.usecs ? (1000000ULL*info.bytes/info.usecs)%1000ULL : 0ULL);
return;
case RS::Canceled:
rsprintf("Nie udalo sie pobrac pliku '%s'...\n", url.c_str());
return;
case RS::NotFound:
rsprintf("Nie znaleziono pliku na serwerze: '%s'...\n", url.c_str());
return;
case RS::Downloading:
{
uint64_t eta = info.bytes ?
((info.all_bytes-info.bytes)*info.usecs/info.bytes/1000000ULL):0ULL;
uint64_t v1 = info.usecs ? (1000ULL*info.bytes/info.usecs):0ULL,
v2 = info.usecs ? ((1000000ULL*info.bytes/info.usecs)%1000ULL):0ULL,
th = info.usecs/3600000000ULL,
tm = (info.usecs/60000000ULL)%60ULL,
ts = (info.usecs/1000000ULL)%60ULL,
eh = eta/3600ULL, em = (eta/60ULL)%60ULL, es = eta%60ULL;
rsprintf("Postep: %6llu.%3llu KB %llu:%.2llu:%.2llu (sr: %4llu.%.3llu KB/s chw: %4llu.%.3llu KB/s roz: %6llu KB ETA: %llu:%.2llu:%.2llu) \r",
info.bytes/1000ULL, info.bytes%1000ULL, th, tm, ts, v1, v2,
((uint64_t)info.tmpspeed)/1000ULL, ((uint64_t)info.tmpspeed)%1000ULL,
info.all_bytes/1000ULL, eh, em, es);
}
break;
case RS::Preparing:
rsprintf("Przygotowywanie sie do pobierania pliku... \r");
break;
case RS::Waiting:
case RS::Later:
case RS::Rivalry:
case RS::Limit:
case RS::Busy:
rsprintf("Status '%s', oczekuje: %u sek \r",
rs.strstatus(info.status), info.waiting);
break;
default:
throw EInternal("nieprawidlowy status?: %d, %s",
info.status, rs.strstatus(info.status));
};
// A tyle odczekujemy pomiedzy kazdym sprawdzeniem statusu pobieranego pliku
usleep(250000);
}
}
bool loop_fn(void)
{
// Wczytanie danych z extends_queue na primary_queue
try {
if (filesystem::exists(extends_queue) &&
filesystem::file_size(extends_queue) > 0) {
filesystem::ifstream in(extends_queue);
if (!in.is_open())
throw EInternal("nie mozna otworzyc pliku: '%s'", extends_queue);
// Zgranie primary na temporary
filesystem::copy_file(primary_queue, temporary_queue);
filesystem::ofstream out(temporary_queue);
if (!out.is_open())
throw EExternal("nie mozna otworzyc pliku: '%s'", temporary_queue);
// Do temporary dopisujemy wpisy z extends
string line;
while (getline(in, line))
out << line << endl;
out.close(); // temporary
in.close(); // extends
// Do temporary wszystkie wpisy sa zgrane, wiec usuwamy primary...
filesystem::remove(primary_queue);
// ... obcinamy plik extends
in.open(extends_queue, ios::out|ios::trunc); // trzeba dac ios::out !!!
in.close();
// ... i zastepujemy primary przez temporary
filesystem::rename(temporary_queue, primary_queue);
}
}
catch (std::exception &e) {
rsprintf("Wyjatek: '%s'\n", e.what());
return false;
}
string url;
// Odczytujemy z primary url
try {
filesystem::ifstream in(primary_queue);
if (!in.is_open())
throw EInternal("nie mozna otworzyc pliku '%s'", primary_queue);
if (!getline(in, url)) {
rsprintf("Brak plikow do pobrania... \r");
sleep(5);
return true;
}
// do temporary zgramy wszystko procz pierwszego, pobranego, wpisu
filesystem::ofstream out(temporary_queue);
if (!out.is_open())
throw EInternal("nie mozna otworzyc pliku '%s'", temporary_queue);
string line;
while (getline(in, line))
out << line << endl;
out.close();
in.close();
}
catch (std::exception &e) {
rsprintf("Wyjatek: '%s'\n", e.what());
return false;
}
// Omijamy nieprawidlowe wpisy
const char *prefix = "http://rapidshare.com";
if (url.compare(0, strlen(prefix), prefix))
rsprintf("Omijam wpis: '%s'\n", url.c_str());
else
get_url(url);
// Na koniec zastepujemy primary przez temporary
try {
filesystem::remove(primary_queue);
filesystem::rename(temporary_queue, primary_queue);
}
catch (std::exception &e) {
rsprintf("Wyjatek: '%s'\n", e.what());
return false;
}
return true;
}
void escape(void)
{
RS rs;
try { rs.close(); }
catch (Exception &e) {
rsprintf("Modul RS rzucil wyjatkiem: %s", e.what());
}
rsprintf("\nKoniec!!\n");
}
void catch_sig(int)
{
exit(0);
}
int main(void)
{
// Przechwytywanie sygnalow
atexit(escape);
signal(SIGPIPE, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
signal(SIGTERM, catch_sig);
signal(SIGQUIT, catch_sig);
signal(SIGINT, catch_sig);
signal(SIGHUP, catch_sig);
rsprintf("Start!!\n");
RS rs;
try {
// Ustawienie odpowiednich sciezek
rs.set_log_file(log_file);
rs.set_download_dir(download_dir);
rs.set_sessions_dir(sessions_dir);
// Wczytanie plikow z kolejka zadan
if (filesystem::exists(primary_queue)) {
if (filesystem::exists(temporary_queue))
filesystem::remove(temporary_queue);
} else {
if (filesystem::exists(temporary_queue))
filesystem::rename(temporary_queue, primary_queue);
else {
filesystem::ofstream f(primary_queue, ios::out|ios::trunc); // trzeba dac ios::out !!!
if (!f.is_open())
throw EInternal("nie mozna utworzyc pliku: '%s'", primary_queue);
}
}
}
catch (std::exception &e) {
rsprintf("Wyjatek: '%s'\n", e.what());
return 1;
}
while (loop_fn());
return 0;
}