1 // rTorrent - BitTorrent client
2 // Copyright (C) 2005-2011, Jari Sundell
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // In addition, as a special exception, the copyright holders give
19 // permission to link the code of portions of this program with the
20 // OpenSSL library under certain conditions as described in each
21 // individual source file, and distribute linked combinations
22 // including the two.
23 //
24 // You must obey the GNU General Public License in all respects for
25 // all of the code used other than OpenSSL. If you modify file(s)
26 // with this exception, you may extend this exception to your version
27 // of the file(s), but you are not obligated to do so. If you do not
28 // wish to do so, delete this exception statement from your version.
29 // If you delete this exception statement from all source files in the
30 // program, then also delete it here.
31 //
32 // Contact: Jari Sundell <jaris@ifi.uio.no>
33 //
34 // Skomakerveien 33
35 // 3185 Skoppum, NORWAY
36
37 #include "config.h"
38
39 #include <cstring>
40 #include <cstdio>
41 #include <sstream>
42 #include <iomanip>
43 #include <rak/socket_address.h>
44 #include <rak/timer.h>
45 #include <torrent/exceptions.h>
46 #include <torrent/connection_manager.h>
47 #include <torrent/rate.h>
48 #include <torrent/throttle.h>
49 #include <torrent/torrent.h>
50 #include <torrent/tracker.h>
51 #include <torrent/tracker_list.h>
52 #include <torrent/data/file_list.h>
53 #include <torrent/data/file_manager.h>
54 #include <torrent/download/resource_manager.h>
55 #include <torrent/peer/client_info.h>
56
57 #include "core/download.h"
58 #include "core/manager.h"
59 #include "rpc/parse_commands.h"
60
61 #include "control.h"
62 #include "globals.h"
63 #include "utils.h"
64
65 namespace display {
66
67 char*
print_string(char * first,char * last,char * str)68 print_string(char* first, char* last, char* str) {
69 if (first == last)
70 return first;
71
72 // We don't have any nice simple functions for copying strings that
73 // return the end address.
74 while (first + 1 != last && *str != '\0')
75 *(first++) = *(str++);
76
77 *first = '\0';
78
79 return first;
80 }
81
82 char*
print_hhmmss(char * first,char * last,time_t t)83 print_hhmmss(char* first, char* last, time_t t) {
84 return print_buffer(first, last, "%2d:%02d:%02d", (int)t / 3600, ((int)t / 60) % 60, (int)t % 60);
85 }
86
87 char*
print_hhmmss_local(char * first,char * last,time_t t)88 print_hhmmss_local(char* first, char* last, time_t t) {
89 std::tm *u = std::localtime(&t);
90
91 if (u == NULL)
92 //return "inv_time";
93 throw torrent::internal_error("print_hhmmss_local(...) failed.");
94
95 return print_buffer(first, last, "%2u:%02u:%02u", u->tm_hour, u->tm_min, u->tm_sec);
96 }
97
98 char*
print_ddhhmm(char * first,char * last,time_t t)99 print_ddhhmm(char* first, char* last, time_t t) {
100 if (t / (24 * 3600) < 100)
101 return print_buffer(first, last, "%2id %2i:%02i", (int)t / (24 * 3600), ((int)t / 3600) % 24, ((int)t / 60) % 60);
102 else
103 return print_buffer(first, last, "--d --:--");
104 }
105
106 char*
print_ddmmyyyy(char * first,char * last,time_t t)107 print_ddmmyyyy(char* first, char* last, time_t t) {
108 std::tm *u = std::gmtime(&t);
109
110 if (u == NULL)
111 //return "inv_time";
112 throw torrent::internal_error("print_ddmmyyyy(...) failed.");
113
114 return print_buffer(first, last, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year));
115 }
116
117 char*
print_address(char * first,char * last,const rak::socket_address * sa)118 print_address(char* first, char* last, const rak::socket_address* sa) {
119 if (!sa->address_c_str(first, last - first))
120 return first;
121
122 return std::find(first, last, '\0');
123 }
124
125 inline char*
print_address(char * first,char * last,const sockaddr * sa)126 print_address(char* first, char* last, const sockaddr* sa) {
127 return print_address(first, last, rak::socket_address::cast_from(sa));
128 }
129
130 char*
print_download_title(char * first,char * last,core::Download * d)131 print_download_title(char* first, char* last, core::Download* d) {
132 return print_buffer(first, last, " %s", d->info()->name().c_str());
133 }
134
135 char*
print_download_info_full(char * first,char * last,core::Download * d)136 print_download_info_full(char* first, char* last, core::Download* d) {
137 if (!d->download()->info()->is_open())
138 first = print_buffer(first, last, "[CLOSED] ");
139 else if (!d->download()->info()->is_active())
140 first = print_buffer(first, last, "[OPEN] ");
141 else
142 first = print_buffer(first, last, " ");
143
144 if (d->is_done())
145 first = print_buffer(first, last, "done %10.1f MB", (double)d->download()->file_list()->size_bytes() / (double)(1 << 20));
146 else
147 first = print_buffer(first, last, "%6.1f / %6.1f MB",
148 (double)d->download()->bytes_done() / (double)(1 << 20),
149 (double)d->download()->file_list()->size_bytes() / (double)(1 << 20));
150
151 first = print_buffer(first, last, " Rate: %5.1f / %5.1f KB Uploaded: %7.1f MB",
152 (double)d->info()->up_rate()->rate() / (1 << 10),
153 (double)d->info()->down_rate()->rate() / (1 << 10),
154 (double)d->info()->up_rate()->total() / (1 << 20));
155
156 if (d->download()->info()->is_active() && !d->is_done()) {
157 first = print_buffer(first, last, " ");
158 first = print_download_percentage_done(first, last, d);
159
160 first = print_buffer(first, last, " ");
161 first = print_download_time_left(first, last, d);
162 } else {
163 first = print_buffer(first, last, " ");
164 }
165
166 first = print_buffer(first, last, " [%c%c R: %4.2f",
167 rpc::call_command_string("d.tied_to_file", rpc::make_target(d)).empty() ? ' ' : 'T',
168 rpc::call_command_value("d.ignore_commands", rpc::make_target(d)) == 0 ? ' ' : 'I',
169 (double)rpc::call_command_value("d.ratio", rpc::make_target(d)) / 1000.0);
170
171 if (d->priority() != 2)
172 first = print_buffer(first, last, " %s", rpc::call_command_string("d.priority_str", rpc::make_target(d)).c_str());
173
174 if (!d->bencode()->get_key("rtorrent").get_key_string("throttle_name").empty())
175 first = print_buffer(first, last , " %s", rpc::call_command_string("d.throttle_name", rpc::make_target(d)).c_str());
176
177 first = print_buffer(first, last , "]");
178
179 if (first > last)
180 throw torrent::internal_error("print_download_info_full(...) wrote past end of the buffer.");
181
182 return first;
183 }
184
185 char*
print_download_status(char * first,char * last,core::Download * d)186 print_download_status(char* first, char* last, core::Download* d) {
187 if (d->is_active())
188 ;
189 else if (rpc::call_command_value("d.hashing", rpc::make_target(d)) != 0)
190 first = print_buffer(first, last, "Hashing: ");
191 else if (!d->is_active())
192 first = print_buffer(first, last, "Inactive: ");
193
194 if (d->is_hash_checking()) {
195 first = print_buffer(first, last, "Checking hash [%2i%%]",
196 (d->download()->chunks_hashed() * 100) / d->download()->file_list()->size_chunks());
197
198 } else if (d->tracker_list()->has_active_not_scrape()) {
199 torrent::TrackerList::iterator itr =
200 std::find_if(d->tracker_list()->begin(), d->tracker_list()->end(),
201 std::mem_fun(&torrent::Tracker::is_busy_not_scrape));
202 char status[128];
203
204 (*itr)->get_status(status, sizeof(status));
205 first = print_buffer(first, last, "Tracker[%i:%i]: Connecting to %s %s",
206 (*itr)->group(), std::distance(d->tracker_list()->begin(), itr), (*itr)->url().c_str(), status);
207
208 } else if (!d->message().empty()) {
209 first = print_buffer(first, last, "%s", d->message().c_str());
210
211 } else {
212 *first = '\0';
213 }
214
215 if (first > last)
216 throw torrent::internal_error("print_download_status(...) wrote past end of the buffer.");
217
218 return first;
219 }
220
221 char*
print_download_column_compact(char * first,char * last)222 print_download_column_compact(char* first, char* last) {
223 first = print_buffer(first, last, " %-64.64s", "Name");
224 first = print_buffer(first, last, "| Status | Downloaded | Size | Done | Up Rate | Down Rate | Uploaded | ETA | Ratio| Misc ");
225
226 if (first > last)
227 throw torrent::internal_error("print_download_column_compact(...) wrote past end of the buffer.");
228
229 return first;
230 }
231
232 char*
print_download_info_compact(char * first,char * last,core::Download * d)233 print_download_info_compact(char* first, char* last, core::Download* d) {
234 first = print_buffer(first, last, " %-64.64s", d->info()->name().c_str());
235 first = print_buffer(first, last, "|");
236
237 if (!d->download()->info()->is_open())
238 first = print_buffer(first, last, " CLOSED ");
239 else if (!d->download()->info()->is_active())
240 first = print_buffer(first, last, " OPEN ");
241 else
242 first = print_buffer(first, last, " ");
243
244 first = print_buffer(first, last, "| %7.1f MB ", (double)d->download()->bytes_done() / (double)(1 << 20));
245 first = print_buffer(first, last, "| %7.1f MB ", (double)d->download()->file_list()->size_bytes() / (double)(1 << 20));
246 first = print_buffer(first, last, "|");
247
248 if (d->is_done())
249 first = print_buffer(first, last, " 100%% ");
250 else if (d->is_open())
251 first = print_buffer(first, last, " %2u%% ",(d->download()->file_list()->completed_chunks() * 100) / d->download()->file_list()->size_chunks());
252 else
253 first = print_buffer(first, last, " ");
254
255 first = print_buffer(first, last, "| %6.1f KB ", (double)d->info()->up_rate()->rate() / (1 << 10));
256 first = print_buffer(first, last, "| %6.1f KB ", (double)d->info()->down_rate()->rate() / (1 << 10));
257 first = print_buffer(first, last, "| %7.1f MB ", (double)d->info()->up_rate()->total() / (1 << 20));
258 first = print_buffer(first, last, "| ");
259
260 if (d->download()->info()->is_active() && !d->is_done())
261 first = print_download_time_left(first, last, d);
262 else
263 first = print_buffer(first, last, " ");
264
265 first = print_buffer(first, last, "| %4.2f ", (double)rpc::call_command_value("d.ratio", rpc::make_target(d)) / 1000.0);
266 first = print_buffer(first, last, "| %c%c",
267 rpc::call_command_string("d.tied_to_file", rpc::make_target(d)).empty() ? ' ' : 'T',
268 rpc::call_command_value("d.ignore_commands", rpc::make_target(d)) == 0 ? ' ' : 'I',
269 (double)rpc::call_command_value("d.ratio", rpc::make_target(d)) / 1000.0);
270
271 if (d->priority() != 2)
272 first = print_buffer(first, last, " %s", rpc::call_command_string("d.priority_str", rpc::make_target(d)).c_str());
273
274 if (!d->bencode()->get_key("rtorrent").get_key_string("throttle_name").empty())
275 first = print_buffer(first, last , " %s", rpc::call_command_string("d.throttle_name", rpc::make_target(d)).c_str());
276
277 if (first > last)
278 throw torrent::internal_error("print_download_info_compact(...) wrote past end of the buffer.");
279
280 return first;
281 }
282
283 char*
print_download_time_left(char * first,char * last,core::Download * d)284 print_download_time_left(char* first, char* last, core::Download* d) {
285 uint32_t rate = d->info()->down_rate()->rate();
286
287 if (rate < 512)
288 return print_buffer(first, last, "--d --:--");
289
290 time_t remaining = (d->download()->file_list()->size_bytes() - d->download()->bytes_done()) / (rate & ~(uint32_t)(512 - 1));
291
292 return print_ddhhmm(first, last, remaining);
293 }
294
295 char*
print_download_percentage_done(char * first,char * last,core::Download * d)296 print_download_percentage_done(char* first, char* last, core::Download* d) {
297 if (!d->is_open() || d->is_done())
298 //return print_buffer(first, last, "[--%%]");
299 return print_buffer(first, last, " ");
300 else
301 return print_buffer(first, last, "[%2u%%]", (d->download()->file_list()->completed_chunks() * 100) / d->download()->file_list()->size_chunks());
302 }
303
304 char*
print_client_version(char * first,char * last,const torrent::ClientInfo & clientInfo)305 print_client_version(char* first, char* last, const torrent::ClientInfo& clientInfo) {
306 switch (torrent::ClientInfo::version_size(clientInfo.type())) {
307 case 4:
308 return print_buffer(first, last, "%s %hhu.%hhu.%hhu.%hhu",
309 clientInfo.short_description(),
310 clientInfo.version()[0], clientInfo.version()[1],
311 clientInfo.version()[2], clientInfo.version()[3]);
312 case 3:
313 return print_buffer(first, last, "%s %hhu.%hhu.%hhu",
314 clientInfo.short_description(),
315 clientInfo.version()[0], clientInfo.version()[1],
316 clientInfo.version()[2]);
317 default:
318 return print_buffer(first, last, "%s", clientInfo.short_description());
319 }
320 }
321
322 char*
print_status_info(char * first,char * last)323 print_status_info(char* first, char* last) {
324 if (!torrent::up_throttle_global()->is_throttled())
325 first = print_buffer(first, last, "[Throttle off");
326 else
327 first = print_buffer(first, last, "[Throttle %3i", torrent::up_throttle_global()->max_rate() / 1024);
328
329 if (!torrent::down_throttle_global()->is_throttled())
330 first = print_buffer(first, last, "/off KB]");
331 else
332 first = print_buffer(first, last, "/%3i KB]", torrent::down_throttle_global()->max_rate() / 1024);
333
334 first = print_buffer(first, last, " [Rate %5.1f/%5.1f KB]",
335 (double)torrent::up_rate()->rate() / 1024.0,
336 (double)torrent::down_rate()->rate() / 1024.0);
337
338 first = print_buffer(first, last, " [Port: %i]", (unsigned int)torrent::connection_manager()->listen_port());
339
340 if (!rak::socket_address::cast_from(torrent::connection_manager()->local_address())->is_address_any()) {
341 first = print_buffer(first, last, " [Local ");
342 first = print_address(first, last, torrent::connection_manager()->local_address());
343 first = print_buffer(first, last, "]");
344 }
345
346 if (first > last)
347 throw torrent::internal_error("print_status_info(...) wrote past end of the buffer.");
348
349 if (!rak::socket_address::cast_from(torrent::connection_manager()->bind_address())->is_address_any()) {
350 first = print_buffer(first, last, " [Bind ");
351 first = print_address(first, last, torrent::connection_manager()->bind_address());
352 first = print_buffer(first, last, "]");
353 }
354
355 return first;
356 }
357
358 char*
print_status_extra(char * first,char * last)359 print_status_extra(char* first, char* last) {
360 first = print_buffer(first, last, " [U %i/%i]",
361 torrent::resource_manager()->currently_upload_unchoked(),
362 torrent::resource_manager()->max_upload_unchoked());
363
364 first = print_buffer(first, last, " [D %i/%i]",
365 torrent::resource_manager()->currently_download_unchoked(),
366 torrent::resource_manager()->max_download_unchoked());
367
368 first = print_buffer(first, last, " [H %u/%u]",
369 control->core()->http_stack()->active(),
370 control->core()->http_stack()->max_active());
371
372 first = print_buffer(first, last, " [S %i/%i/%i]",
373 torrent::total_handshakes(),
374 torrent::connection_manager()->size(),
375 torrent::connection_manager()->max_size());
376
377 first = print_buffer(first, last, " [F %i/%i]",
378 torrent::file_manager()->open_files(),
379 torrent::file_manager()->max_open_files());
380
381 return first;
382 }
383
384 }
385