Electroneum
network_throttle.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 basic interface */
35 /* rfree: also includes the manager for singeton/global such objects */
36 
37 
38 #ifndef INCLUDED_p2p_network_throttle_hpp
39 #define INCLUDED_p2p_network_throttle_hpp
40 
41 #include <boost/asio.hpp>
42 #include <string>
43 #include <vector>
44 #include <boost/noncopyable.hpp>
45 #include <boost/shared_ptr.hpp>
46 #include <atomic>
47 
48 #include <boost/asio.hpp>
49 #include <boost/array.hpp>
50 #include <boost/noncopyable.hpp>
51 #include <boost/shared_ptr.hpp>
52 #include <boost/enable_shared_from_this.hpp>
53 #include <boost/interprocess/detail/atomic.hpp>
54 #include <boost/thread/thread.hpp>
55 
56 #include "syncobj.h"
57 
58 #include "net/net_utils_base.h"
59 #include "misc_log_ex.h"
60 #include <boost/lambda/bind.hpp>
61 #include <boost/lambda/lambda.hpp>
62 #include <boost/uuid/random_generator.hpp>
63 #include <boost/chrono.hpp>
64 #include <boost/utility/value_init.hpp>
65 #include <boost/asio/deadline_timer.hpp>
66 #include <boost/date_time/posix_time/posix_time.hpp>
67 #include <boost/thread/thread.hpp>
68 #include "misc_language.h"
69 #include "pragma_comp_defs.h"
70 #include <sstream>
71 #include <iomanip>
72 #include <algorithm>
73 
74 #include <memory>
75 #include <mutex>
76 #include <fstream>
77 
78 namespace epee
79 {
80 namespace net_utils
81 {
82 
83 // just typedefs to in code define the units used. TODO later it will be enforced that casts to other numericals are only explicit to avoid mistakes? use boost::chrono?
84 typedef double network_speed_kbps;
85 typedef double network_time_seconds;
86 typedef double network_MB;
87 
88 class i_network_throttle;
89 
90 /***
91 @brief All information about given throttle - speed calculations
92 */
94  double average;
95  double window;
96  double delay;
98 };
100 
101 
102 namespace cryptonote { class cryptonote_protocol_handler_base; } // a friend class // TODO friend not working
103 
104 /***
105 @brief Access to simple throttles, with singlton to access global network limits
106 */
108  // provides global (singleton) in/inreq/out throttle access
109 
110  // [[note1]] see also http://www.nuonsoft.com/blog/2012/10/21/implementing-a-thread-safe-singleton-with-c11/
111  // [[note2]] _inreq is the requested in traffic - we anticipate we will get in-bound traffic soon as result of what we do (e.g. that we sent network downloads requests)
112 
113  //protected:
114  public: // XXX
115 
116  static boost::mutex m_lock_get_global_throttle_in;
117  static boost::mutex m_lock_get_global_throttle_inreq;
118  static boost::mutex m_lock_get_global_throttle_out;
119 
120  friend class cryptonote::cryptonote_protocol_handler_base; // FRIEND - to directly access global throttle-s. !! REMEMBER TO USE LOCKS!
121  friend class connection_basic; // FRIEND - to directly access global throttle-s. !! REMEMBER TO USE LOCKS!
122  friend class connection_basic_pimpl; // ditto
123 
124  static int xxx;
125 
126  public:
130 };
131 
132 
133 
134 /***
135 @brief interface for the throttle, see the derivated class
136 */
138  public:
139  virtual void set_name(const std::string &name)=0;
140  virtual void set_target_speed( network_speed_kbps target )=0;
141  virtual void set_real_target_speed(network_speed_kbps real_target)=0;
143 
144  virtual void handle_trafic_exact(size_t packet_size) =0; // count the new traffic/packet; the size is exact considering all network costs
145  virtual void handle_trafic_tcp(size_t packet_size) =0; // count the new traffic/packet; the size is as TCP, we will consider MTU etc
146  virtual void tick() =0; // poke and update timers/history
147 
148  // time calculations:
149 
150  virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const =0; // assuming sending new package (or 0), calculate:
151  // Average, Window, Delay, Recommended data size ; also gets dbg=debug flag, and forced widnow size if >0 or -1 for not forcing window size
152 
153  // Average speed, Window size, recommended Delay to sleep now, Recommended size of data to send now
154 
155  virtual network_time_seconds get_sleep_time(size_t packet_size) const =0; // gets the D (recommended Delay time) from calc
156  virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size) =0; // ditto, but first tick the timer
157 
158  virtual size_t get_recommended_size_of_planned_transport() const =0; // what should be the recommended limit of data size that we can transport over current network_throttle in near future
159 
160  virtual double get_time_seconds() const =0; // a timer
161  virtual void logger_handle_net(const std::string &filename, double time, size_t size)=0;
162 
163 
164 };
165 
166 
167 // ... more in the -advanced.h file
168 
169 
170 } // namespace net_utils
171 } // namespace epee
172 
173 
174 #endif
175 
176 
177 
virtual void set_real_target_speed(network_speed_kbps real_target)=0
double average
Definition: network_throttle.hpp:94
double window
Definition: network_throttle.hpp:95
static i_network_throttle & get_global_throttle_in()
singleton ; for friend class ; caller MUST use proper locks! like m_lock_get_global_throttle_in ...
Definition: network_throttle.cpp:80
virtual network_speed_kbps get_target_speed()=0
virtual void handle_trafic_tcp(size_t packet_size)=0
double network_time_seconds
Definition: network_throttle.hpp:85
static boost::mutex m_lock_get_global_throttle_out
Definition: network_throttle.hpp:118
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
time_t time
Definition: blockchain.cpp:89
double network_MB
Definition: network_throttle.hpp:86
static int xxx
Definition: network_throttle.hpp:124
double network_speed_kbps
Definition: network_throttle.hpp:84
virtual network_time_seconds get_sleep_time(size_t packet_size) const =0
static boost::mutex m_lock_get_global_throttle_in
Definition: network_throttle.hpp:116
Definition: connection_basic.cpp:111
virtual void set_target_speed(network_speed_kbps target)=0
virtual double get_time_seconds() const =0
double recomendetDataSize
Definition: network_throttle.hpp:97
Definition: network_throttle.hpp:107
static i_network_throttle & get_global_throttle_inreq()
ditto ; use lock ... use m_lock_get_global_throttle_inreq obviously
Definition: network_throttle.cpp:87
Definition: cryptonote_protocol_handler.h:64
double delay
Definition: network_throttle.hpp:96
Definition: network_throttle.hpp:137
Definition: connection_basic.cpp:91
static boost::mutex m_lock_get_global_throttle_inreq
Definition: network_throttle.hpp:117
Definition: network_throttle.hpp:93
static i_network_throttle & get_global_throttle_out()
ditto ; use lock ... use m_lock_get_global_throttle_out obviously
Definition: network_throttle.cpp:93
virtual void set_name(const std::string &name)=0
const char * name
Definition: simplewallet.cpp:180
virtual void handle_trafic_exact(size_t packet_size)=0
virtual void logger_handle_net(const std::string &filename, double time, size_t size)=0
virtual size_t get_recommended_size_of_planned_transport() const =0
virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const =0
calculate_times_struct calculate_times_struct
Definition: network_throttle.hpp:99
virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size)=0
Definition: connection_basic.hpp:86