Skip to content

Commit

Permalink
Add a limit on the total data transferred. Fixes #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
mavit committed May 19, 2013
1 parent f295eed commit d260f16
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 69 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ TrafficLimits can be found at [http://github.com/mavit/deluge-trafficlimits](htt

## Configuration:

As well as setting the limits through the preferences (GTK UI only, for now), you can also create a file called `~/.config/deluge/trafficlimits` containing a label, the upload limit, and the download limit (in bytes), each on a line by themselves. For example:
As well as setting the limits through the preferences (GTK UI only, for now), you can also create a file called `~/.config/deluge/trafficlimits` containing a label, the upload limit, the download limit, and the combined limit (in bytes), each on a line by themselves. For example:

January
-1
21474836480
-1

This is intended to be used by a cron job for automatic scheduling, e.g.,

* 00-15,21-23 * * * /bin/echo -e "Unlimited\n-1\n-1" > ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits.tmp && mv ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits.tmp ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits
* 16-20 * * * /bin/echo -e "Evening\n400000000\n750000000" > ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits.tmp && mv ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits.tmp ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits
* 00-15,21-23 * * * /bin/echo -e "Unlimited\n-1\n-1\n-1" > ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits.tmp && mv ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits.tmp ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits
* 16-20 * * * /bin/echo -e "Evening\n400000000\n750000000\n-1" > ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits.tmp && mv ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits.tmp ${XDG_CONFIG_HOME:-~/.config}/deluge/trafficlimits


## See also:
Expand Down
2 changes: 1 addition & 1 deletion TrafficLimits.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 1.0
Name: TrafficLimits
Version: 0.2
Version: 0.3
Summary: Restrict the maximum total upload/download during certain time periods.
Home-page: http://github.com/mavit/deluge-trafficlimits
Author: Peter Oliver
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
__plugin_name__ = "TrafficLimits"
__author__ = "Peter Oliver"
__author_email__ = "[email protected]"
__version__ = "0.2"
__version__ = "0.3"
__url__ = "http://github.com/mavit/deluge-trafficlimits"
__license__ = "GPLv3"
__description__ = "Restrict the maximum total upload/download during certain time periods."
Expand Down
105 changes: 83 additions & 22 deletions trafficlimits/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@
from twisted.internet.task import LoopingCall
import time

init_time = time.time()
DEFAULT_PREFS = {
"maximum_upload": 0,
"maximum_download": 0,
"maximum_upload": -1,
"maximum_download": -1,
"maximum_total": -1,
"previous_upload": 0,
"previous_download": 0,
"previous_total": 0,
"reset_time_upload": init_time,
"reset_time_download": init_time,
"reset_time_total": init_time,
"label": ""
}

Expand All @@ -68,6 +74,8 @@ def enable(self):

self.update_timer = LoopingCall(self.update_traffic)
self.update_timer.start(10)
log.debug("TrafficLimits: Enabled.")


def disable(self):
log.debug("TrafficLimits: Disabling...")
Expand All @@ -77,12 +85,17 @@ def disable(self):
+= self.session_upload - self.initial_upload
self.config["previous_download"] \
+= self.session_download - self.initial_download
self.config["previous_total"] \
+= self.session_total - self.initial_total
self.config.save()
if self.paused:
component.get("Core").session.resume()
log.debug("TrafficLimits: Disabled.")


def update_traffic(self):
log.debug("TrafficLimits: Updating...")

try:
if os.stat(deluge.configmanager.get_config_dir("trafficlimits")) \
.st_mtime != self.limits_mtime:
Expand All @@ -96,11 +109,14 @@ def update_traffic(self):
"total_download"])
self.session_upload = status["total_upload"]
self.session_download = status["total_download"]
self.session_total = status["total_upload"] + status["total_download"]

self.upload = ( self.config["previous_upload"]
+ self.session_upload - self.initial_upload )
self.download = ( self.config["previous_download"]
+ self.session_download - self.initial_download )
self.total = ( self.config["previous_total"]
+ self.session_total - self.initial_total )

if ( self.config["maximum_upload"] >= 0
and self.upload > self.config["maximum_upload"] ):
Expand All @@ -120,11 +136,29 @@ def update_traffic(self):
self.config["previous_download"] = 0
self.config["reset_time_download"] = time.time()

component.get("EventManager").emit(TrafficLimitUpdate(
self.label, self.upload, self.download,
self.config["maximum_upload"], self.config["maximum_download"],
if ( self.config["maximum_total"] >= 0
and self.total > self.config["maximum_total"] ):
log.info("TrafficLimits: Session paused due to excessive throughput.")
self.paused = True
component.get("Core").session.pause()
self.initial_total = self.session_total
self.config["previous_total"] = 0
self.config["reset_time_total"] = time.time()

component.get("EventManager").emit(
TrafficLimitUpdate(
self.label, self.upload, self.download, self.total,
self.config["maximum_upload"],
self.config["maximum_download"],
self.config["maximum_total"],
self.config["reset_time_upload"],
self.config["reset_time_download"]))
self.config["reset_time_download"],
self.config["reset_time_total"]
)
)

log.debug("TrafficLimits: Updated.")


def load_limits(self):
log.debug("TrafficLimits: Loading limits...")
Expand All @@ -135,6 +169,10 @@ def load_limits(self):
label = limits.readline().rstrip(os.linesep)
maximum_upload = int(limits.readline().rstrip(os.linesep))
maximum_download = int(limits.readline().rstrip(os.linesep))

line = limits.readline().rstrip(os.linesep)
maximum_total = -1 if line == '' else int(line)

except (IOError, OSError, ValueError) as error:
log.error("TrafficLimits: "
+ deluge.configmanager.get_config_dir("trafficlimits")
Expand All @@ -145,6 +183,7 @@ def load_limits(self):
self.label = label
self.config["maximum_upload"] = maximum_upload
self.config["maximum_download"] = maximum_download
self.config["maximum_total"] = maximum_total

if self.label != self.config["label"]:
self.config["label"] = self.label
Expand All @@ -153,19 +192,27 @@ def load_limits(self):
self.paused = False
component.get("Core").session.resume()

log.debug("TrafficLimits: Loaded limits.")

@export
def reset_initial(self):
self.config["previous_upload"] = 0
self.config["previous_download"] = 0
self.config["previous_total"] = 0
self.config["reset_time_upload"] = time.time()
self.config["reset_time_download"] = self.config["reset_time_upload"]
self.config["reset_time_total"] = self.config["reset_time_upload"]
self.set_initial()


def set_initial(self):
status = component.get("Core").get_session_status(["total_download",
"total_upload"])
status = component.get("Core").get_session_status(
["total_download", "total_upload"]
)
self.initial_upload = status["total_upload"]
self.initial_download = status["total_download"]
self.initial_total = status["total_upload"] + status["total_download"]


@export
def set_config(self, config):
Expand All @@ -174,32 +221,46 @@ def set_config(self, config):
self.config[key] = config[key]
self.config.save()


@export
def get_config(self):
"""Returns the config dictionary"""
return self.config.config


@export
def get_state(self):
state = [ self.label, self.upload, self.download,
self.config["maximum_upload"],
self.config["maximum_download"],
self.config["reset_time_upload"],
self.config["reset_time_download"] ]
state = [
self.label, self.upload, self.download, self.total,
self.config["maximum_upload"],
self.config["maximum_download"],
self.config["maximum_total"],
self.config["reset_time_upload"],
self.config["reset_time_download"],
self.config["reset_time_total"]
]
return state


class TrafficLimitUpdate (DelugeEvent):
"""
Emitted when the ammount of transferred data changes.
"""
def __init__(self, label, upload, download, maximum_upload,
maximum_download, reset_time_upload, reset_time_download):
def __init__(self, label, upload, download, total, maximum_upload,
maximum_download, maximum_total, reset_time_upload,
reset_time_download, reset_time_total):
"""
:param FIXME label: str, a description of the current period
:param upload: str, bytes uploaded during the current period
:param download: str, bytes downloaded during the current period
:param maximum_upload: str, upper bound for bytes transmitted
:param maximum_download: str, upper bound for bytes received
:param label: str, a description of the current period
:param upload: int, bytes uploaded during the current period
:param download: int, bytes downloaded during the current period
:param total: int, bytes up/downloaded during the current period
:param maximum_upload: int, upper bound for bytes transmitted
:param maximum_download: int, upper bound for bytes received
:param maximum_total: int, upper bound for bytes transferred
:param reset_time_upload: float, secs since epoch that upload was reset
:param reset_time_download: float, secs since epoch download was reset
:param reset_time_total: float, secs since epoch total was reset
"""
self._args = [label, upload, download, maximum_upload, maximum_download,
reset_time_upload, reset_time_download]
self._args = [label, upload, download, total, maximum_upload,
maximum_download, maximum_total, reset_time_upload,
reset_time_download, reset_time_total]
Loading

0 comments on commit d260f16

Please sign in to comment.