Browse Source

Redesigned scheduler code logic. Closes #903.

adaptive-webui-19844
sledgehammer999 11 years ago
parent
commit
774d749eef
  1. 94
      src/qtlibtorrent/bandwidthscheduler.h

94
src/qtlibtorrent/bandwidthscheduler.h

@ -7,6 +7,8 @@ @@ -7,6 +7,8 @@
#include "preferences.h"
#include <iostream>
#define SCHED_INTERVAL 2500
class BandwidthScheduler: public QTimer {
Q_OBJECT
@ -19,7 +21,7 @@ public: @@ -19,7 +21,7 @@ public:
// Signal shot, we call start() again manually
setSingleShot(true);
// Connect Signals/Slots
connect(this, SIGNAL(timeout()), this, SLOT(switchMode()));
connect(this, SIGNAL(timeout()), this, SLOT(start()));
}
public slots:
@ -27,87 +29,45 @@ public slots: @@ -27,87 +29,45 @@ public slots:
const Preferences pref;
Q_ASSERT(pref.isSchedulerEnabled());
QTime startAltSpeeds = pref.getSchedulerStartTime();
QTime endAltSpeeds = pref.getSchedulerEndTime();
if (startAltSpeeds == endAltSpeeds) {
std::cerr << "Error: bandwidth scheduler have the same start time and end time." << std::endl;
std::cerr << "The bandwidth scheduler will be disabled" << std::endl;
stop();
emit switchToAlternativeMode(false);
return;
}
// Determine what the closest QTime is
QTime start = pref.getSchedulerStartTime();
QTime end = pref.getSchedulerEndTime();
QTime now = QTime::currentTime();
uint time_to_start = secsTo(now, startAltSpeeds);
uint time_to_end = secsTo(now, endAltSpeeds);
if (time_to_end < time_to_start) {
// We should be in alternative mode
in_alternative_mode = true;
// Start counting
QTimer::start(time_to_end*1000);
} else {
// We should be in normal mode
in_alternative_mode = false;
// Start counting
QTimer::start(time_to_start*1000);
}
// Send signal to notify BTSession
emit switchToAlternativeMode(in_alternative_mode);
}
int sched_days = pref.getSchedulerDays();
int day = QDateTime::currentDateTime().toLocalTime().date().dayOfWeek();
bool new_mode = false;
void switchMode() {
const Preferences pref;
// Get the day this mode was started (either today or yesterday)
QDate current_date = QDateTime::currentDateTime().toLocalTime().date();
int day = current_date.dayOfWeek();
if (in_alternative_mode) {
// It is possible that starttime was yesterday
if (QTime::currentTime().secsTo(pref.getSchedulerStartTime()) > 0) {
current_date.addDays(-1); // Go to yesterday
day = current_date.day();
}
}
// Check if the day is in scheduler days
// Notify BTSession only if necessary
switch(pref.getSchedulerDays()) {
if (start < end && start <= now && end >= now) {
switch(sched_days) {
case EVERY_DAY:
emit switchToAlternativeMode(!in_alternative_mode);
new_mode = true;
break;
case WEEK_ENDS:
if (day == Qt::Saturday || day == Qt::Sunday)
emit switchToAlternativeMode(!in_alternative_mode);
if (day == 6 || day == 7)
new_mode = true;
break;
case WEEK_DAYS:
if (day != Qt::Saturday && day != Qt::Sunday)
emit switchToAlternativeMode(!in_alternative_mode);
if (day != 6 && day != 7)
new_mode = true;
break;
default:
// Convert our enum index to Qt enum index
int scheduler_day = ((int)pref.getSchedulerDays()) - 2;
if (day == scheduler_day)
emit switchToAlternativeMode(!in_alternative_mode);
if (day == sched_days - 2)
new_mode = true;
break;
}
// Call start again
start();
}
signals:
void switchToAlternativeMode(bool alternative);
private:
// Qt function can return negative values and we
// don't want that
uint secsTo(QTime now, QTime t) {
int diff = now.secsTo(t);
if (diff < 0) {
// 86400 seconds in a day
diff += 86400;
if (new_mode != in_alternative_mode) {
in_alternative_mode = new_mode;
emit switchToAlternativeMode(new_mode);
}
Q_ASSERT(diff >= 0);
return diff;
// Timeout regularly to accomodate for external system clock changes
// eg from the user or from a timesync utility
QTimer::start(SCHED_INTERVAL);
}
signals:
void switchToAlternativeMode(bool alternative);
};
#endif // BANDWIDTHSCHEDULER_H

Loading…
Cancel
Save