Electroneum
network_throttle-detail.hpp
Go to the documentation of this file.
1 
5 // Copyrights(c) 2017-2019, The Electroneum Project
6 // Copyrights(c) 2014-2017, The Monero Project
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without modification, are
11 // permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this list of
14 // conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
17 // of conditions and the following disclaimer in the documentation and/or other
18 // materials provided with the distribution.
19 //
20 // 3. Neither the name of the copyright holder nor the names of its contributors may be
21 // used to endorse or promote products derived from this software without specific
22 // prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
32 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 /* rfree: throttle details, implementing rate limiting */
35 
36 
37 #ifndef INCLUDED_src_p2p_throttle_detail_hpp
38 #define INCLUDED_src_p2p_throttle_detail_hpp
39 
40 #include "network_throttle.hpp"
41 
42 namespace epee
43 {
44 namespace net_utils
45 {
46 
47 
49  private:
50  struct packet_info {
51  size_t m_size; // octets sent. Summary for given small-window (e.g. for all packaged in 1 second)
52  packet_info();
53  };
54 
55 
58  size_t m_network_add_cost; // estimated add cost of headers
59  size_t m_network_minimal_segment; // estimated minimal cost of sending 1 byte to round up to
60  size_t m_network_max_segment; // recommended max size of 1 TCP transmission
61 
62  const size_t m_window_size; // the number of samples to average over
63  network_time_seconds m_slot_size; // the size of one slot. TODO: now hardcoded for 1 second e.g. in time_to_slot()
64  // TODO for big window size, for performance better the substract on change of m_last_sample_time instead of recalculating average of eg >100 elements
65 
66  std::vector< packet_info > m_history; // the history of bw usage
67  network_time_seconds m_last_sample_time; // time of last history[0] - so we know when to rotate the buffer
68  network_time_seconds m_start_time; // when we were created
69  bool m_any_packet_yet; // did we yet got any packet to count
70 
71  std::string m_name; // my name for debug and logs
72  std::string m_nameshort; // my name for debug and logs (used in log file name)
73 
74  // each sample is now 1 second
75  public:
76  network_throttle(const std::string &nameshort, const std::string &name, int window_size=-1);
77  virtual ~network_throttle();
78  virtual void set_name(const std::string &name);
79  virtual void set_target_speed( network_speed_kbps target );
80  virtual void set_real_target_speed( network_speed_kbps real_target ); // only for throttle_out
82 
83  // add information about events:
84  virtual void handle_trafic_exact(size_t packet_size);
85  virtual void handle_trafic_tcp(size_t packet_size);
86 
87  virtual void tick();
88 
89  virtual double get_time_seconds() const ;
90 
91  // time calculations:
92  virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const;
93 
94  virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size);
95  virtual network_time_seconds get_sleep_time(size_t packet_size) const;
96 
97  virtual size_t get_recommended_size_of_planned_transport() const;
98  virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const;
99  virtual double get_current_speed() const;
100 
101  private:
102  virtual network_time_seconds time_to_slot(network_time_seconds t) const { return std::floor( t ); } // convert exact time eg 13.7 to rounded time for slot number in history 13
103  virtual void _handle_trafic_exact(size_t packet_size, size_t orginal_size);
104  virtual void logger_handle_net(const std::string &filename, double time, size_t size);
105 };
106 
107 /***
108  * The complete set of traffic throttle for one typical connection
109 */
111  public:
115 
116  public:
117  network_throttle_bw(const std::string &name1);
118 };
119 
120 
121 
122 } // namespace net_utils
123 } // namespace epee
124 
125 
126 #endif
127 
128 
size_t m_size
Definition: network_throttle-detail.hpp:51
virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const
ditto, but for given windows time frame
Definition: network_throttle-detail.cpp:338
network_speed_kbps m_real_target_speed
Definition: network_throttle-detail.hpp:57
size_t m_network_add_cost
Definition: network_throttle-detail.hpp:58
virtual ~network_throttle()
Definition: network_throttle-detail.cpp:135
network_time_seconds m_slot_size
Definition: network_throttle-detail.hpp:63
packet_info()
Definition: network_throttle-detail.cpp:137
network_throttle(const std::string &nameshort, const std::string &name, int window_size=-1)
Definition: network_throttle-detail.cpp:142
network_time_seconds m_last_sample_time
Definition: network_throttle-detail.hpp:67
virtual void _handle_trafic_exact(size_t packet_size, size_t orginal_size)
Definition: network_throttle-detail.cpp:209
virtual network_speed_kbps get_target_speed()
Definition: network_throttle-detail.cpp:172
virtual void logger_handle_net(const std::string &filename, double time, size_t size)
Definition: network_throttle-detail.cpp:240
double network_time_seconds
Definition: network_throttle.hpp:85
virtual double get_current_speed() const
Definition: network_throttle-detail.cpp:359
virtual void set_real_target_speed(network_speed_kbps real_target)
Definition: network_throttle-detail.cpp:167
virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const
MAIN LOGIC (see base class for info)
Definition: network_throttle-detail.cpp:263
network_time_seconds m_start_time
Definition: network_throttle-detail.hpp:68
time_t time
Definition: blockchain.cpp:89
Definition: network_throttle-detail.hpp:50
virtual size_t get_recommended_size_of_planned_transport() const
what should be the size (bytes) of next data block to be transported
Definition: network_throttle-detail.cpp:348
network_throttle m_inreq
for requesting incomming traffic (this is exact usually)
Definition: network_throttle-detail.hpp:113
virtual network_time_seconds time_to_slot(network_time_seconds t) const
Definition: network_throttle-detail.hpp:102
virtual void set_target_speed(network_speed_kbps target)
Definition: network_throttle-detail.cpp:160
double network_speed_kbps
Definition: network_throttle.hpp:84
network_throttle_bw(const std::string &name1)
Definition: network_throttle.cpp:101
const size_t m_window_size
Definition: network_throttle-detail.hpp:62
interface for throttling of connection (count and rate-limit speed etc)
virtual void handle_trafic_tcp(size_t packet_size)
count the new traffic/packet; the size is as TCP, we will consider MTU etc
Definition: network_throttle-detail.cpp:228
virtual void handle_trafic_exact(size_t packet_size)
count the new traffic/packet; the size is exact considering all network costs
Definition: network_throttle-detail.cpp:204
network_throttle m_out
for outgoing traffic that we just sent (this is exact usually)
Definition: network_throttle-detail.hpp:114
virtual void tick()
poke and update timers/history (recalculates, moves the history if needed, checks the real clock etc)...
Definition: network_throttle-detail.cpp:177
bool m_any_packet_yet
Definition: network_throttle-detail.hpp:69
virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size)
increase the timer if needed, and get the package size
Definition: network_throttle-detail.cpp:235
Definition: network_throttle.hpp:137
std::vector< packet_info > m_history
Definition: network_throttle-detail.hpp:66
Definition: network_throttle-detail.hpp:110
Definition: connection_basic.cpp:91
virtual network_time_seconds get_sleep_time(size_t packet_size) const
gets the Delay (recommended Delay time) from calc. (not safe: only if time didnt change?) TODO
Definition: network_throttle-detail.cpp:254
std::string m_nameshort
Definition: network_throttle-detail.hpp:72
Definition: network_throttle.hpp:93
Definition: network_throttle-detail.hpp:48
std::string m_name
Definition: network_throttle-detail.hpp:71
size_t m_network_minimal_segment
Definition: network_throttle-detail.hpp:59
const char * name
Definition: simplewallet.cpp:180
network_speed_kbps m_target_speed
Definition: network_throttle-detail.hpp:56
size_t m_network_max_segment
Definition: network_throttle-detail.hpp:60
virtual double get_time_seconds() const
timer that we use, time in seconds, monotionic
Definition: network_throttle-detail.cpp:326
network_throttle m_in
for incomming traffic (this we can not controll directly as it depends of what others send to us - us...
Definition: network_throttle-detail.hpp:112
virtual void set_name(const std::string &name)
Definition: network_throttle-detail.cpp:155