summarylogtreecommitdiffstats
path: root/backport_rt_all_02-display_throttle_speed.patch
blob: 9e964350a9ba1697450fc45fdfb9244fff10ff8e (plain)
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
--- a/src/command_ui.cc	2017-04-30 20:45:24.094335223 +0100
+++ a/src/command_ui.cc	2017-04-30 20:56:18.000000000 +0100
@@ -568,6 +568,26 @@ apply_elapsed_greater(const torrent::Obj
   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");
@@ -608,6 +628,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");
 
--- a/src/core/manager.cc	2017-04-30 20:37:29.000000000 +0100
+++ a/src/core/manager.cc	2017-04-30 20:56:18.000000000 +0100
@@ -146,6 +146,35 @@ Manager::get_address_throttle(const sock
   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() {
--- a/src/core/manager.h	2016-10-23 05:33:00.000000000 +0100
+++ a/src/core/manager.h	2017-04-30 20:56:18.000000000 +0100
@@ -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"
@@ -91,6 +92,8 @@ public:
   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);
--- a/src/display/utils.cc	2017-04-30 20:37:29.000000000 +0100
+++ a/src/display/utils.cc	2017-04-30 20:56:18.000000000 +0100
@@ -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"
@@ -323,20 +324,100 @@ print_client_version(char* first, char*
 }
 
 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());
 
--- a/src/display/utils.h	2016-10-23 05:33:00.000000000 +0100
+++ a/src/display/utils.h	2017-04-30 20:56:18.000000000 +0100
@@ -80,6 +80,9 @@ char*       print_client_version(char* f
 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);
 
--- a/src/ui/root.h	2016-10-23 05:33:00.000000000 +0100
+++ a/src/ui/root.h	2019-07-26 20:01:18.000000000 +0100
@@ -59,6 +59,8 @@ namespace ui {
 
 class DownloadList;
 
+typedef std::vector<std::string> ThrottleNameList;
+
 class Root {
 public:
   typedef display::WindowTitle     WTitle;
@@ -93,6 +95,12 @@ public:
 
   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, ui::DownloadList::Input type);
   void                disable_input();
 
@@ -119,6 +127,9 @@ private:
 
   input::Bindings     m_bindings;
 
+  ThrottleNameList    m_throttle_up_names;
+  ThrottleNameList    m_throttle_down_names;
+
   int                   m_input_history_length;
   std::string           m_input_history_last_input;
   int                   m_input_history_pointer_get;