Browse Source

Redesigned scheduler code logic. Closes #903.

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

106
src/qtlibtorrent/bandwidthscheduler.h

@ -7,6 +7,8 @@
#include "preferences.h" #include "preferences.h"
#include <iostream> #include <iostream>
#define SCHED_INTERVAL 2500
class BandwidthScheduler: public QTimer { class BandwidthScheduler: public QTimer {
Q_OBJECT Q_OBJECT
@ -19,7 +21,7 @@ public:
// Signal shot, we call start() again manually // Signal shot, we call start() again manually
setSingleShot(true); setSingleShot(true);
// Connect Signals/Slots // Connect Signals/Slots
connect(this, SIGNAL(timeout()), this, SLOT(switchMode())); connect(this, SIGNAL(timeout()), this, SLOT(start()));
} }
public slots: public slots:
@ -27,87 +29,45 @@ public slots:
const Preferences pref; const Preferences pref;
Q_ASSERT(pref.isSchedulerEnabled()); Q_ASSERT(pref.isSchedulerEnabled());
QTime startAltSpeeds = pref.getSchedulerStartTime(); QTime start = pref.getSchedulerStartTime();
QTime endAltSpeeds = pref.getSchedulerEndTime(); QTime end = 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 now = QTime::currentTime(); QTime now = QTime::currentTime();
uint time_to_start = secsTo(now, startAltSpeeds); int sched_days = pref.getSchedulerDays();
uint time_to_end = secsTo(now, endAltSpeeds); int day = QDateTime::currentDateTime().toLocalTime().date().dayOfWeek();
if (time_to_end < time_to_start) { bool new_mode = false;
// 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);
}
void switchMode() { if (start < end && start <= now && end >= now) {
const Preferences pref; switch(sched_days) {
// Get the day this mode was started (either today or yesterday) case EVERY_DAY:
QDate current_date = QDateTime::currentDateTime().toLocalTime().date(); new_mode = true;
int day = current_date.dayOfWeek(); break;
if (in_alternative_mode) { case WEEK_ENDS:
// It is possible that starttime was yesterday if (day == 6 || day == 7)
if (QTime::currentTime().secsTo(pref.getSchedulerStartTime()) > 0) { new_mode = true;
current_date.addDays(-1); // Go to yesterday break;
day = current_date.day(); case WEEK_DAYS:
if (day != 6 && day != 7)
new_mode = true;
break;
default:
if (day == sched_days - 2)
new_mode = true;
break;
} }
} }
// Check if the day is in scheduler days
// Notify BTSession only if necessary if (new_mode != in_alternative_mode) {
switch(pref.getSchedulerDays()) { in_alternative_mode = new_mode;
case EVERY_DAY: emit switchToAlternativeMode(new_mode);
emit switchToAlternativeMode(!in_alternative_mode);
break;
case WEEK_ENDS:
if (day == Qt::Saturday || day == Qt::Sunday)
emit switchToAlternativeMode(!in_alternative_mode);
break;
case WEEK_DAYS:
if (day != Qt::Saturday && day != Qt::Sunday)
emit switchToAlternativeMode(!in_alternative_mode);
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);
break;
} }
// Call start again
start(); // Timeout regularly to accomodate for external system clock changes
// eg from the user or from a timesync utility
QTimer::start(SCHED_INTERVAL);
} }
signals: signals:
void switchToAlternativeMode(bool alternative); 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;
}
Q_ASSERT(diff >= 0);
return diff;
}
}; };
#endif // BANDWIDTHSCHEDULER_H #endif // BANDWIDTHSCHEDULER_H

Loading…
Cancel
Save