Further cleanups in dcc_glist()

This now calculates a 'pcomplete' value for proportion of transfer complete (0.0 to 1.0).
From this we can calculate most other things easily - percent complete, ETA, and the size
of the completion bars.

User-visible change is that the DCC_BAR_TYPE 1 bar now matches the % complete for the RESUMEd
file case.
This commit is contained in:
Kevin Easton
2016-10-27 00:23:35 +11:00
parent ed12420755
commit 3cba15f6b4
2 changed files with 43 additions and 36 deletions

View File

@@ -1,7 +1,7 @@
[Changes 1.2.2] [Changes 1.2.2]
* Set completion bar for /SET DCC_BAR_TYPE 1 to empty rather than full for * Improve completion bar for /SET DCC_BAR_TYPE 1 so that it always matches
zero filesize files. (caf) the completion percentage. (caf)
* Correctly align formatting of /DCC LIST line for /SET DCC_BAR_TYPE 1. (caf) * Correctly align formatting of /DCC LIST line for /SET DCC_BAR_TYPE 1. (caf)

View File

@@ -2465,7 +2465,7 @@ int blocksize = get_int_var(DCC_BLOCK_SIZE_VAR);
doing_multi = 0; doing_multi = 0;
} }
static const char *get_bar_percent(int percent) static const char *get_bar_percent(double pcomplete)
{ {
#ifdef ONLY_STD_CHARS #ifdef ONLY_STD_CHARS
static const char * const _dcc_offer[] = { static const char * const _dcc_offer[] = {
@@ -2494,7 +2494,7 @@ static const char * const _dcc_offer[] = {
"%K<><4B><EFBFBD>%1%K<><4B><EFBFBD><EFBFBD>%R<><52>%0%K<>%n", /* 90 */ "%K<><4B><EFBFBD>%1%K<><4B><EFBFBD><EFBFBD>%R<><52>%0%K<>%n", /* 90 */
"%K<><4B><EFBFBD>%1%K<><4B><EFBFBD><EFBFBD>%R<><52><EFBFBD>%n"}; /* 100 */ "%K<><4B><EFBFBD>%1%K<><4B><EFBFBD><EFBFBD>%R<><52><EFBFBD>%n"}; /* 100 */
#endif #endif
const int idx = percent / 10; const int idx = pcomplete * 10;
if (idx >= 0 && idx < (sizeof _dcc_offer / sizeof _dcc_offer[0])) if (idx >= 0 && idx < (sizeof _dcc_offer / sizeof _dcc_offer[0]))
return _dcc_offer[idx]; return _dcc_offer[idx];
@@ -2577,14 +2577,8 @@ void dcc_glist(char *command, char *args)
} }
for (i = 0; i < get_max_fd() + 1; i++, count++) for (i = 0; i < get_max_fd() + 1; i++, count++)
{ {
double perc = 0.0;
double bytes = 0.0;
char local_type[30]; char local_type[30];
time_t xtime; time_t xtime;
int seconds = 0, minutes = 0;
char percent[20];
char eta[20];
char kilobytes[20];
if (!check_dcc_socket(i)) if (!check_dcc_socket(i))
continue; continue;
@@ -2630,9 +2624,7 @@ void dcc_glist(char *command, char *args)
"N/A", "N/A",
strip_path(filename))); strip_path(filename)));
} }
else else if (!(s->flags & DCC_ACTIVE))
{
if (!(s->flags & DCC_ACTIVE))
{ {
if (do_hook(DCC_STAT_LIST, "%d %s %s %s %s %s %s", if (do_hook(DCC_STAT_LIST, "%d %s %s %s %s %s %s",
n->dccnum, local_type, s->server, n->dccnum, local_type, s->server,
@@ -2641,6 +2633,7 @@ void dcc_glist(char *command, char *args)
s->flags & DCC_ACTIVE ?"Active" : s->flags & DCC_ACTIVE ?"Active" :
"Unknown", "Unknown",
"N/A", strip_path(filename), n->encrypt?"E":empty_string)) "N/A", strip_path(filename), n->encrypt?"E":empty_string))
{
put_it("%s", convert_output_format(DCC_FORMAT_STAT_PENDING, "%d %s %s %s %s %s %s", put_it("%s", convert_output_format(DCC_FORMAT_STAT_PENDING, "%d %s %s %s %s %s %s",
n->dccnum, n->dccnum,
local_type, local_type,
@@ -2652,24 +2645,37 @@ void dcc_glist(char *command, char *args)
"Unknown", "Unknown",
"N/A", "N/A",
strip_path(filename))); strip_path(filename)));
continue;
} }
}
bytes = n->bytes_read + n->bytes_sent; else
{
double bytes = n->bytes_read + n->bytes_sent;
double pcomplete; /* proportion of transfer completed, 0.0 to 1.0 */
char percent[20];
char eta[20];
char kilobytes[20];
int seconds = 0, minutes = 0;
type = s->flags & DCC_TYPES; type = s->flags & DCC_TYPES;
tdcc = s->flags & DCC_TDCC; tdcc = s->flags & DCC_TDCC;
status = s->flags & DCC_OFFER ? "Offer":s->flags & DCC_ACTIVE ? "Active": s->flags&DCC_WAIT?"Wait":"Unknown"; status = s->flags & DCC_OFFER ? "Offer":s->flags & DCC_ACTIVE ? "Active": s->flags&DCC_WAIT?"Wait":"Unknown";
if (bytes && (n->filesize - n->transfer_orders.byteoffset) >= bytes) /* Calculate proportion of transfer completed */
if (n->filesize > 0)
{ {
perc = 100.0 * (bytes + n->transfer_orders.byteoffset) / n->filesize; pcomplete = (bytes + n->transfer_orders.byteoffset) / n->filesize;
if (perc > 100.0) if (pcomplete > 1.0)
perc = 100.0; pcomplete = 1.0;
else if (perc < 0.0) else if (pcomplete < 0.0)
perc = 0.0; pcomplete = 0.0;
}
else
pcomplete = 0.0;
seconds = (int) (((n->filesize - n->transfer_orders.byteoffset - bytes) / (bytes / xtime)) + 0.5); /* Calculate ETA */
if (pcomplete > 0.0)
{
seconds = (int)((1.0 / pcomplete - 1.0) * xtime + 0.5);
minutes = seconds / 60; minutes = seconds / 60;
seconds = seconds % 60; seconds = seconds % 60;
if (minutes > 999) { if (minutes > 999) {
@@ -2677,11 +2683,12 @@ void dcc_glist(char *command, char *args)
seconds = 59; seconds = 59;
} }
if (seconds < 0) seconds = 0; if (seconds < 0) seconds = 0;
} else }
seconds = minutes = perc = 0; else
seconds = minutes = pcomplete = 0;
strcpy(spec, convert_output_format(get_bar_percent(perc), NULL, NULL)); strcpy(spec, convert_output_format(get_bar_percent(pcomplete), NULL, NULL));
snprintf(percent, sizeof percent, "%4.1f%%", perc); snprintf(percent, sizeof percent, "%4.1f%%", pcomplete * 100.0);
snprintf(eta, sizeof eta, "%02d:%02d", minutes, seconds); snprintf(eta, sizeof eta, "%02d:%02d", minutes, seconds);
snprintf(kilobytes, sizeof kilobytes, "%2.4g", bytes / 1024.0 / xtime); snprintf(kilobytes, sizeof kilobytes, "%2.4g", bytes / 1024.0 / xtime);
@@ -2703,7 +2710,7 @@ void dcc_glist(char *command, char *args)
} }
/* This prints the second DCC stat line, if DCC_BAR_TYPE is non-zero. */ /* This prints the second DCC stat line, if DCC_BAR_TYPE is non-zero. */
if (do_hook(DCC_STATF1_LIST, "%4.1f %lu %lu %d %d", perc, (unsigned long)bytes, (unsigned long)n->filesize, minutes, seconds)) if (do_hook(DCC_STATF1_LIST, "%4.1f %lu %lu %d %d", pcomplete * 100.0, (unsigned long)bytes, (unsigned long)n->filesize, minutes, seconds))
{ {
char stats[80]; char stats[80];
char *stat_ptr, *spec_ptr; char *stat_ptr, *spec_ptr;
@@ -2713,10 +2720,10 @@ void dcc_glist(char *command, char *args)
continue; continue;
if (n->filesize > 0) if (n->filesize > 0)
size = (int)(BAR_LENGTH * bytes / n->filesize); size = (int)(BAR_LENGTH * pcomplete);
snprintf(stats, sizeof stats, "%4.1f%% (%lu of %lu bytes)", snprintf(stats, sizeof stats, "%4.1f%% (%lu of %lu bytes)",
perc, (unsigned long)bytes, (unsigned long)n->filesize); pcomplete * 100.0, (unsigned long)bytes, (unsigned long)n->filesize);
snprintf(spec, sizeof spec, BOLD_TOG_STR "[" snprintf(spec, sizeof spec, BOLD_TOG_STR "["
REV_TOG_STR "%*.*s" REV_TOG_STR "%*.*s]" BOLD_TOG_STR REV_TOG_STR "%*.*s" REV_TOG_STR "%*.*s]" BOLD_TOG_STR
" ETA " BOLD_TOG_STR "%02d:%02d", " ETA " BOLD_TOG_STR "%02d:%02d",