Commit f233c4d7 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

Better calculation of the bandidth

parent 8e011be5
...@@ -13,9 +13,10 @@ class Connection: ...@@ -13,9 +13,10 @@ class Connection:
os.O_WRONLY|os.O_CREAT|os.O_TRUNC) ) os.O_WRONLY|os.O_CREAT|os.O_TRUNC) )
self.iface = iface self.iface = iface
self._lastTrafic = self._getTrafic()
self._bandwidth = None
self._prefix = prefix self._prefix = prefix
self._creation_date = time.time()
self._bandwidth = None
self._last_trafic = None
# TODO : update the stats # TODO : update the stats
def refresh(self): def refresh(self):
...@@ -25,23 +26,35 @@ class Connection: ...@@ -25,23 +26,35 @@ class Connection:
% (self._prefix, self.process.returncode), 3) % (self._prefix, self.process.returncode), 3)
return False return False
trafic = self._getTrafic() self._updateBandwidth()
if self._bandwidth == None:
self._bandwidth = trafic - self._lastTrafic
else:
self._bandwidth = (1-smooth)*self._bandwidth + smooth*trafic
self._lastTrafic = trafic
utils.log('New bandwidth calculated on iface %s : %sb' % self._bandwidth, 4)
return True return True
def _getTrafic(self): def _updateBandwidth(self):
try: try:
f_rx = open('/sys/class/net/%s/statistics/rx_bytes' % self.iface, 'r') f_rx = open('/sys/class/net/%s/statistics/rx_bytes' %
f_tx = open('/sys/class/net/%s/statistics/tx_bytes' % self.iface, 'r') self.iface, 'r')
return int(f_rx.read()) + int(f_tx.read()) f_tx = open('/sys/class/net/%s/statistics/tx_bytes' %
except Exception: self.iface, 'r')
return 0
trafic = int(f_rx.read()) + int(f_tx.read())
t = time.time()
if bool(self._last_trafic):
bw = (trafic - self._last_trafic)/(t -
self._last_trafic_update)
if bool(self._bandwidth):
self._bandwidth = (1-smooth)*self._bandwidth + smooth*bw
else:
self._bandwidth = bw
utils.log('New bandwidth calculated on iface %s : %s' %
(self.iface, self._bandwidth), 4)
self._last_trafic_update = t
self._last_trafic = trafic
except IOError: # This just means that the interface is downs
utils.log('Unable to calculate bandwidth on iface %s' %
self.iface, 4)
class TunnelManager: class TunnelManager:
......
...@@ -61,8 +61,8 @@ def getConfig(): ...@@ -61,8 +61,8 @@ def getConfig():
# args to be removed ? # args to be removed ?
_('--proto', default='udp', _('--proto', default='udp',
help='The protocol used by other peers to connect') help='The protocol used by other peers to connect')
_('--connection-count', default=30, type=int, _('--connection-count', default=20, type=int,
help='Number of client connections') help='Number of tunnels')
_('--refresh-rate', default=0.05, type=float, _('--refresh-rate', default=0.05, type=float,
help='The ratio of connections to drop when refreshing the connections') help='The ratio of connections to drop when refreshing the connections')
# Openvpn options # Openvpn options
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment