Skip to content

Commit

Permalink
Modify throttle status bar mod to be able to display multiple values …
Browse files Browse the repository at this point in the history
…(See #6)
  • Loading branch information
chros authored and chros committed Apr 19, 2017
1 parent 226e670 commit 3f047ec
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 10 deletions.
23 changes: 23 additions & 0 deletions src/command_ui.cc
Expand Up @@ -519,6 +519,26 @@ apply_elapsed_greater(const torrent::Object::list_type& args) {
return (int64_t)(start_time != 0 && rak::timer::current_seconds() - start_time > rpc::convert_to_value(args.back()));
}

torrent::Object
cmd_status_throttle_names(bool up, const torrent::Object::list_type& args) {
if (args.size() == 0)
return torrent::Object();

std::vector<std::string> throttle_name_list;

for (torrent::Object::list_const_iterator itr = args.begin(), last = args.end(); itr != last; itr++) {
if (itr->is_string())
throttle_name_list.push_back(itr->as_string());
}

if (up)
control->ui()->set_status_throttle_up_names(throttle_name_list);
else
control->ui()->set_status_throttle_down_names(throttle_name_list);

return torrent::Object();
}

void
initialize_command_ui() {
CMD2_VAR_STRING("keys.layout", "qwerty");
Expand Down Expand Up @@ -555,6 +575,9 @@ initialize_command_ui() {
CMD2_ANY ("ui.current_view", std::bind(&cmd_ui_current_view));
CMD2_ANY_STRING("ui.current_view.set", std::bind(&cmd_ui_set_view, std::placeholders::_2));

CMD2_ANY_LIST ("ui.status.throttle.up.set", std::bind(&cmd_status_throttle_names, true, std::placeholders::_2));
CMD2_ANY_LIST ("ui.status.throttle.down.set", std::bind(&cmd_status_throttle_names, false, std::placeholders::_2));

// TODO: Add 'option_string' for rtorrent-specific options.
CMD2_VAR_STRING("ui.torrent_list.layout", "full");

Expand Down
29 changes: 29 additions & 0 deletions src/core/manager.cc
Expand Up @@ -146,6 +146,35 @@ Manager::get_address_throttle(const sockaddr* addr) {
return m_addressThrottles.get(rak::socket_address::cast_from(addr)->sa_inet()->address_h(), torrent::ThrottlePair(NULL, NULL));
}

int64_t
Manager::retrieve_throttle_value(const torrent::Object::string_type& name, bool rate, bool up) {
ThrottleMap::iterator itr = throttles().find(name);

if (itr == throttles().end()) {
return (int64_t)-1;
} else {
torrent::Throttle* throttle = up ? itr->second.first : itr->second.second;

// check whether the actual up/down throttle exist (one of the pair can be missing)
if (throttle == NULL)
return (int64_t)-1;

int64_t throttle_max = (int64_t)throttle->max_rate();

if (rate) {

if (throttle_max > 0)
return (int64_t)throttle->rate()->rate();
else
return (int64_t)-1;

} else {
return throttle_max;
}

}
}

// Most of this should be possible to move out.
void
Manager::initialize_second() {
Expand Down
3 changes: 3 additions & 0 deletions src/core/manager.h
Expand Up @@ -42,6 +42,7 @@

#include <torrent/utils/log_buffer.h>
#include <torrent/connection_manager.h>
#include <torrent/object.h>

#include "download_list.h"
#include "poll_manager.h"
Expand Down Expand Up @@ -91,6 +92,8 @@ class Manager {
ThrottleMap& throttles() { return m_throttles; }
torrent::ThrottlePair get_throttle(const std::string& name);

int64_t retrieve_throttle_value(const torrent::Object::string_type& name, bool rate, bool up);

// Use custom throttle for the given range of IP addresses.
void set_address_throttle(uint32_t begin, uint32_t end, torrent::ThrottlePair throttles);
torrent::ThrottlePair get_address_throttle(const sockaddr* addr);
Expand Down
101 changes: 91 additions & 10 deletions src/display/utils.cc
Expand Up @@ -57,6 +57,7 @@
#include "core/download.h"
#include "core/manager.h"
#include "rpc/parse_commands.h"
#include "ui/root.h"

#include "control.h"
#include "globals.h"
Expand Down Expand Up @@ -319,21 +320,101 @@ print_client_version(char* first, char* last, const torrent::ClientInfo& clientI
}
}

char*
print_status_throttle_limit(char* first, char* last, bool up, const ui::ThrottleNameList& throttle_names) {
char throttle_str[40];
throttle_str[0] = 0;
char* firstc = throttle_str;
char* lastc = throttle_str + 40 - 1;

for (ui::ThrottleNameList::const_iterator itr = throttle_names.begin(), laste = throttle_names.end(); itr != laste; itr++) {

if (!(*itr).empty()) {
int64_t throttle_max = control->core()->retrieve_throttle_value(*itr, false, up);

if (throttle_max > 0)
firstc = print_buffer(firstc, lastc, "|%1.0f", (double)throttle_max / 1024.0);
}

}

// Add temp buffer (chop first char first) into main buffer if temp buffer isn't empty
if (throttle_str[0] != 0)
first = print_buffer(first, last, "(%s)", &throttle_str[1]);

return first;
}

char*
print_status_throttle_rate(char* first, char* last, bool up, const ui::ThrottleNameList& throttle_names, const double& global_rate) {
double main_rate = global_rate;
char throttle_str[50];
throttle_str[0] = 0;
char* firstc = throttle_str;
char* lastc = throttle_str + 50 - 1;

for (ui::ThrottleNameList::const_iterator itr = throttle_names.begin(), laste = throttle_names.end(); itr != laste; itr++) {

if (!(*itr).empty() && (up ? torrent::up_throttle_global()->is_throttled() : torrent::down_throttle_global()->is_throttled())) {
int64_t throttle_rate_value = control->core()->retrieve_throttle_value(*itr, true, up);

if (throttle_rate_value > -1) {
double throttle_rate = (double)throttle_rate_value / 1024.0;
main_rate = main_rate - throttle_rate;

firstc = print_buffer(firstc, lastc, "|%3.1f", throttle_rate);
}
}

}

// Add temp buffer into main buffer if temp buffer isn't empty
if (throttle_str[0] != 0)
first = print_buffer(first, last, "(%3.1f%s)",
main_rate < 0.0 ? 0.0 : main_rate,
throttle_str);

return first;
}

char*
print_status_info(char* first, char* last) {
if (!torrent::up_throttle_global()->is_throttled())
ui::ThrottleNameList& throttle_up_names = control->ui()->get_status_throttle_up_names();
ui::ThrottleNameList& throttle_down_names = control->ui()->get_status_throttle_down_names();

if (!torrent::up_throttle_global()->is_throttled()) {
first = print_buffer(first, last, "[Throttle off");
else
} else {
first = print_buffer(first, last, "[Throttle %3i", torrent::up_throttle_global()->max_rate() / 1024);

if (!torrent::down_throttle_global()->is_throttled())
first = print_buffer(first, last, "/off KB]");
else
first = print_buffer(first, last, "/%3i KB]", torrent::down_throttle_global()->max_rate() / 1024);

first = print_buffer(first, last, " [Rate %5.1f/%5.1f KB]",
(double)torrent::up_rate()->rate() / 1024.0,
(double)torrent::down_rate()->rate() / 1024.0);
if (!throttle_up_names.empty())
first = print_status_throttle_limit(first, last, true, throttle_up_names);
}

if (!torrent::down_throttle_global()->is_throttled()) {
first = print_buffer(first, last, " / off KB]");
} else {
first = print_buffer(first, last, " / %3i", torrent::down_throttle_global()->max_rate() / 1024);

if (!throttle_down_names.empty())
first = print_status_throttle_limit(first, last, false, throttle_down_names);

first = print_buffer(first, last, " KB]");
}

double global_uprate = (double)torrent::up_rate()->rate() / 1024.0;
first = print_buffer(first, last, " [Rate %5.1f", global_uprate);

if (!throttle_up_names.empty())
first = print_status_throttle_rate(first, last, true, throttle_up_names, global_uprate);

double global_downrate = (double)torrent::down_rate()->rate() / 1024.0;
first = print_buffer(first, last, " / %5.1f", global_downrate);

if (!throttle_down_names.empty())
first = print_status_throttle_rate(first, last, false, throttle_down_names, global_downrate);

first = print_buffer(first, last, " KB]");

first = print_buffer(first, last, " [Port: %i]", (unsigned int)torrent::connection_manager()->listen_port());

Expand Down
3 changes: 3 additions & 0 deletions src/display/utils.h
Expand Up @@ -80,6 +80,9 @@ char* print_client_version(char* first, char* last, const torrent::ClientI
char* print_entry_tags(char* first, char* last);
char* print_entry_file(char* first, char* last, const torrent::Entry& entry);

char* print_status_throttle_limit(char* first, char* last, bool up, const std::vector<std::string>& throttle_names);
char* print_status_throttle_rate(char* first, char* last, bool up, const std::vector<std::string>& throttle_names, const double& global_rate);

char* print_status_info(char* first, char* last);
char* print_status_extra(char* first, char* last);

Expand Down
11 changes: 11 additions & 0 deletions src/ui/root.h
Expand Up @@ -58,6 +58,8 @@ namespace ui {

class DownloadList;

typedef std::vector<std::string> ThrottleNameList;

class Root {
public:
typedef display::WindowTitle WTitle;
Expand Down Expand Up @@ -88,6 +90,12 @@ class Root {

const char* get_throttle_keys();

ThrottleNameList& get_status_throttle_up_names() { return m_throttle_up_names; }
ThrottleNameList& get_status_throttle_down_names() { return m_throttle_down_names; }

void set_status_throttle_up_names(const ThrottleNameList& throttle_list) { m_throttle_up_names = throttle_list; }
void set_status_throttle_down_names(const ThrottleNameList& throttle_list) { m_throttle_down_names = throttle_list; }

void enable_input(const std::string& title, input::TextInput* input);
void disable_input();

Expand All @@ -105,6 +113,9 @@ class Root {
WStatusbar* m_windowStatusbar;

input::Bindings m_bindings;

ThrottleNameList m_throttle_up_names;
ThrottleNameList m_throttle_down_names;
};

}
Expand Down

0 comments on commit 3f047ec

Please sign in to comment.