mirror of
https://github.com/ElvishArtisan/rivendell.git
synced 2025-09-16 16:30:26 +02:00
2021-07-14 Fred Gleason <fredg@paravelsystems.com>
* Refactored the 'RDTimeEngine' class. * Added a 'timeengine_test' test harness. Signed-off-by: Fred Gleason <fredg@paravelsystems.com>
This commit is contained in:
parent
2db1d6dda7
commit
600aef26dd
1
.gitignore
vendored
1
.gitignore
vendored
@ -145,6 +145,7 @@ tests/sendmail_test
|
||||
tests/stringcode_test
|
||||
tests/test_hash
|
||||
tests/test_pam
|
||||
tests/timeengine_test
|
||||
tests/timer_test
|
||||
tests/upload_test
|
||||
tests/wav_chunk_test
|
||||
|
@ -22042,3 +22042,6 @@
|
||||
2021-07-13 Fred Gleason <fredg@paravelsystems.com>
|
||||
* Fixed a bug in 'RDCheckExitCode()' that caused spurrious warnings
|
||||
to be sent to syslog(3).
|
||||
2021-07-14 Fred Gleason <fredg@paravelsystems.com>
|
||||
* Refactored the 'RDTimeEngine' class.
|
||||
* Added a 'timeengine_test' test harness.
|
||||
|
@ -18,159 +18,78 @@
|
||||
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <rdtimeengine.h>
|
||||
#include "rdtimeengine.h"
|
||||
|
||||
RDTimeEngine::RDTimeEngine(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
engine_pending_id=-1;
|
||||
engine_timer=new QTimer(this);
|
||||
engine_timer->setSingleShot(true);
|
||||
engine_time_offset=0;
|
||||
connect(engine_timer,SIGNAL(timeout()),this,SLOT(timerData()));
|
||||
d_mapper=new QSignalMapper(this);
|
||||
connect(d_mapper,SIGNAL(mapped(int)),this,SLOT(timerData(int)));
|
||||
}
|
||||
|
||||
|
||||
RDTimeEngine::~RDTimeEngine()
|
||||
{
|
||||
clear();
|
||||
delete engine_timer;
|
||||
delete d_mapper;
|
||||
}
|
||||
|
||||
|
||||
void RDTimeEngine::clear()
|
||||
{
|
||||
engine_time_offset=0;
|
||||
engine_events.clear();
|
||||
SetTimer();
|
||||
for(QMap<int,QTimer *>::const_iterator it=d_timers.begin();
|
||||
it!=d_timers.end();it++) {
|
||||
d_mapper->removeMappings(it.value());
|
||||
delete it.value();
|
||||
}
|
||||
d_timers.clear();
|
||||
d_times.clear();
|
||||
}
|
||||
|
||||
|
||||
QTime RDTimeEngine::event(int id) const
|
||||
{
|
||||
for(unsigned i=0;i<engine_events.size();i++) {
|
||||
for(int j=0;j<engine_events[i].size();j++) {
|
||||
if(id==engine_events[i].id(j)) {
|
||||
return engine_events[i].time();
|
||||
}
|
||||
}
|
||||
}
|
||||
return QTime();
|
||||
return d_times.value(id);
|
||||
}
|
||||
|
||||
|
||||
int RDTimeEngine::timeOffset() const
|
||||
void RDTimeEngine::addEvent(int id,const QTime &time)
|
||||
{
|
||||
return engine_time_offset;
|
||||
}
|
||||
|
||||
|
||||
void RDTimeEngine::setTimeOffset(int msecs)
|
||||
{
|
||||
engine_time_offset=msecs;
|
||||
SetTimer();
|
||||
}
|
||||
|
||||
|
||||
void RDTimeEngine::addEvent(int id,QTime time)
|
||||
{
|
||||
for(unsigned i=0;i<engine_events.size();i++) {
|
||||
if(time==engine_events[i].time()) {
|
||||
engine_events[i].addId(id);
|
||||
SetTimer();
|
||||
return;
|
||||
}
|
||||
}
|
||||
engine_events.push_back(RDTimeEvent());
|
||||
engine_events.back().setTime(time);
|
||||
engine_events.back().addId(id);
|
||||
SetTimer();
|
||||
d_times[id]=time;
|
||||
d_timers[id]=new QTimer(this);
|
||||
d_timers.value(id)->setTimerType(Qt::PreciseTimer);
|
||||
d_timers.value(id)->setSingleShot(true);
|
||||
d_mapper->setMapping(d_timers.value(id),id);
|
||||
connect(d_timers.value(id),SIGNAL(timeout()),d_mapper,SLOT(map()));
|
||||
StartEvent(id);
|
||||
}
|
||||
|
||||
|
||||
void RDTimeEngine::removeEvent(int id)
|
||||
{
|
||||
for(unsigned i=0;i<engine_events.size();i++) {
|
||||
for(int j=0;j<engine_events[i].size();j++) {
|
||||
if(id==engine_events[i].id(j)) {
|
||||
if(engine_events[i].size()==1) {
|
||||
std::vector<RDTimeEvent>::iterator it=engine_events.begin()+i;
|
||||
engine_events.erase(it,it+1);
|
||||
}
|
||||
else {
|
||||
engine_events[i].removeId(j);
|
||||
}
|
||||
SetTimer();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
d_timers.value(id)->stop();
|
||||
d_mapper->removeMappings(d_timers.value(id));
|
||||
delete d_timers.value(id);
|
||||
d_timers.remove(id);
|
||||
d_times.remove(id);
|
||||
}
|
||||
|
||||
|
||||
int RDTimeEngine::next() const
|
||||
void RDTimeEngine::timerData(int id)
|
||||
{
|
||||
return engine_pending_id;
|
||||
emit timeout(id);
|
||||
StartEvent(id);
|
||||
}
|
||||
|
||||
|
||||
void RDTimeEngine::timerData()
|
||||
void RDTimeEngine::StartEvent(int id)
|
||||
{
|
||||
for(unsigned i=0;i<engine_events.size();i++) {
|
||||
for(int j=0;j<engine_events[i].size();j++) {
|
||||
if(engine_pending_id==engine_events[i].id(j)) {
|
||||
EmitEvents(i);
|
||||
SetTimer();
|
||||
return;
|
||||
}
|
||||
}
|
||||
QTime now=QTime::currentTime();
|
||||
int interval=now.msecsTo(d_times.value(id));
|
||||
|
||||
if(interval<0) { // Crosses midnight
|
||||
interval=
|
||||
now.msecsTo(QTime(23,59,59))+1000+QTime(0,0,0).msecsTo(d_times.value(id));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void RDTimeEngine::EmitEvents(int offset)
|
||||
{
|
||||
// for(int i=0;i<engine_events[offset].size();i++) {
|
||||
for(int i=engine_events[offset].size()-1;i>=0;i--) {
|
||||
emit timeout(engine_events[offset].id(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void RDTimeEngine::SetTimer()
|
||||
{
|
||||
engine_timer->stop();
|
||||
if(engine_events.size()==0) {
|
||||
return;
|
||||
}
|
||||
QTime current_time=QTime::currentTime().addMSecs(engine_time_offset);
|
||||
int diff=GetNextDiff(current_time,&engine_pending_id);
|
||||
if(diff!=86400001) {
|
||||
engine_timer->start(diff);
|
||||
return;
|
||||
}
|
||||
diff=GetNextDiff(QTime(0,0,0),&engine_pending_id);
|
||||
if(diff!=86400001) {
|
||||
diff+=(current_time.msecsTo(QTime(23,59,59))+1000);
|
||||
engine_timer->start(diff);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int RDTimeEngine::GetNextDiff(QTime time,int *pending_id)
|
||||
{
|
||||
int diff=86400001;
|
||||
*pending_id=-1;
|
||||
|
||||
for(unsigned i=0;i<engine_events.size();i++) {
|
||||
if(((time.msecsTo(engine_events[i].time()))>=0)&&
|
||||
(time.msecsTo(engine_events[i].time())<diff)) {
|
||||
diff=time.msecsTo(engine_events[i].time());
|
||||
*pending_id=engine_events[i].id(0);
|
||||
}
|
||||
}
|
||||
return diff;
|
||||
d_timers.value(id)->start(1+interval);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// An event timer engine.
|
||||
//
|
||||
// (C) Copyright 2002,2016 Fred Gleason <fredg@paravelsystems.com>
|
||||
// (C) Copyright 2002-2021 Fred Gleason <fredg@paravelsystems.com>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Library General Public License
|
||||
@ -21,11 +21,11 @@
|
||||
#ifndef RDTIMEENGINE_H
|
||||
#define RDTIMEENGINE_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <qwidget.h>
|
||||
#include <qdatetime.h>
|
||||
#include <qtimer.h>
|
||||
#include <QDateTime>
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
#include <QSignalMapper>
|
||||
#include <QTimer>
|
||||
|
||||
#include <rdtimeevent.h>
|
||||
|
||||
@ -37,26 +37,20 @@ class RDTimeEngine : public QObject
|
||||
~RDTimeEngine();
|
||||
void clear();
|
||||
QTime event(int id) const;
|
||||
int timeOffset() const;
|
||||
void setTimeOffset(int msecs);
|
||||
void addEvent(int id,QTime time);
|
||||
void addEvent(int id,const QTime &time);
|
||||
void removeEvent(int id);
|
||||
int next() const;
|
||||
|
||||
signals:
|
||||
void timeout(int id);
|
||||
|
||||
private slots:
|
||||
void timerData();
|
||||
void timerData(int id);
|
||||
|
||||
private:
|
||||
void EmitEvents(int offset);
|
||||
void SetTimer();
|
||||
int GetNextDiff(QTime time,int *pending_id);
|
||||
QTimer *engine_timer;
|
||||
std::vector<RDTimeEvent> engine_events;
|
||||
int engine_pending_id;
|
||||
int engine_time_offset;
|
||||
void StartEvent(int id);
|
||||
QMap<int,QTime> d_times;
|
||||
QMap<int,QTimer *> d_timers;
|
||||
QSignalMapper *d_mapper;
|
||||
};
|
||||
|
||||
|
||||
|
@ -52,6 +52,7 @@ noinst_PROGRAMS = audio_convert_test\
|
||||
test_hash\
|
||||
test_pam\
|
||||
timer_test\
|
||||
timeengine_test\
|
||||
upload_test\
|
||||
wav_chunk_test\
|
||||
wavefactory_test\
|
||||
@ -142,6 +143,10 @@ dist_timer_test_SOURCES = timer_test.cpp timer_test.h
|
||||
nodist_timer_test_SOURCES = moc_timer_test.cpp
|
||||
timer_test_LDADD = @QT5_LIBS@ @MUSICBRAINZ_LIBS@
|
||||
|
||||
dist_timeengine_test_SOURCES = timeengine_test.cpp timeengine_test.h
|
||||
nodist_timeengine_test_SOURCES = moc_timeengine_test.cpp
|
||||
timeengine_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT5_LIBS@ @MUSICBRAINZ_LIBS@
|
||||
|
||||
dist_upload_test_SOURCES = upload_test.cpp upload_test.h
|
||||
upload_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT5_LIBS@ @MUSICBRAINZ_LIBS@
|
||||
|
||||
|
121
tests/timeengine_test.cpp
Normal file
121
tests/timeengine_test.cpp
Normal file
@ -0,0 +1,121 @@
|
||||
// timeengine_test.cpp
|
||||
//
|
||||
// Test the RDTimeEngine class
|
||||
//
|
||||
// (C) Copyright 2021 Fred Gleason <fredg@paravelsystems.com>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License version 2 as
|
||||
// published by the Free Software Foundation.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public
|
||||
// License along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QMultiMap>
|
||||
#include <QStringList>
|
||||
|
||||
#include <rdapplication.h>
|
||||
|
||||
#include "timeengine_test.h"
|
||||
|
||||
MainObject::MainObject(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
QString err_msg;
|
||||
RDApplication::ErrorType err_type;
|
||||
QMultiMap<QTime,int> time_ids;
|
||||
|
||||
//
|
||||
// Open the Database
|
||||
//
|
||||
rda=static_cast<RDApplication *>(new RDCoreApplication("timeengine_test",
|
||||
"timeengine_test",
|
||||
TIMEENGINE_TEST_USAGE,
|
||||
this));
|
||||
if(!rda->open(&err_msg,&err_type,false)) {
|
||||
fprintf(stderr,"timeengine_test: %s\n",err_msg.toUtf8().constData());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//
|
||||
// Read Command Options
|
||||
//
|
||||
for(unsigned i=0;i<rda->cmdSwitch()->keys();i++) {
|
||||
if(rda->cmdSwitch()->key(i)=="--time-event") {
|
||||
bool ok=false;
|
||||
int id;
|
||||
QTime time;
|
||||
QStringList f0=
|
||||
rda->cmdSwitch()->value(i).split(":",QString::SkipEmptyParts);
|
||||
if(f0.size()!=4) {
|
||||
fprintf(stderr,"timeengine_test: invalid argument format\n");
|
||||
exit(1);
|
||||
}
|
||||
id=f0.at(0).toUInt(&ok);
|
||||
if(!ok) {
|
||||
fprintf(stderr,"timeengine_test: invalid ID value\n");
|
||||
exit(1);
|
||||
}
|
||||
f0.removeFirst();
|
||||
time=QTime::fromString(f0.join(":"),"hh:mm:ss");
|
||||
if(time.isNull()) {
|
||||
fprintf(stderr,"timeengine_test: invalid time \"%s\" specified\n",
|
||||
f0.join(":").toUtf8().constData());
|
||||
exit(1);
|
||||
}
|
||||
time_ids.insert(time,id);
|
||||
rda->cmdSwitch()->setProcessed(i,true);
|
||||
}
|
||||
if(!rda->cmdSwitch()->processed(i)) {
|
||||
fprintf(stderr,"timeengine_test: invalid command\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Sanity Checks
|
||||
//
|
||||
if(time_ids.size()==0) {
|
||||
fprintf(stderr,"timeengine_test: nothing to do!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
d_time_engine=new RDTimeEngine(this);
|
||||
connect(d_time_engine,SIGNAL(timeout(int)),this,SLOT(timeoutData(int)));
|
||||
for(QMultiMap<QTime,int>::const_iterator it=time_ids.begin();
|
||||
it!=time_ids.end();it++) {
|
||||
d_time_engine->addEvent(it.value(),it.key());
|
||||
printf("Added ID %d at %s\n",it.value(),
|
||||
it.key().toString().toUtf8().constData());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void MainObject::timeoutData(int id)
|
||||
{
|
||||
QTime now=QTime::currentTime();
|
||||
|
||||
printf("ID %d fired [scheduled: %s, actual: %s difference: %d mS\n",id,
|
||||
d_time_engine->event(id).toString("hh:mm:ss.zzz").toUtf8().constData(),
|
||||
now.toString("hh:mm:ss.zzz").toUtf8().constData(),
|
||||
d_time_engine->event(id).msecsTo(now));
|
||||
}
|
||||
|
||||
|
||||
int main(int argc,char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc,argv);
|
||||
new MainObject();
|
||||
return a.exec();
|
||||
}
|
44
tests/timeengine_test.h
Normal file
44
tests/timeengine_test.h
Normal file
@ -0,0 +1,44 @@
|
||||
// timeengine_test.h
|
||||
//
|
||||
// Test the RDTimeEngine class
|
||||
//
|
||||
// (C) Copyright 2021 Fred Gleason <fredg@paravelsystems.com>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License version 2 as
|
||||
// published by the Free Software Foundation.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public
|
||||
// License along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
|
||||
#ifndef TIMEENGINE_TEST_H
|
||||
#define TIMEENGINE_TEST_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include <rdtimeengine.h>
|
||||
|
||||
#define TIMEENGINE_TEST_USAGE "--time-event=<id>:<hh:mm:ss> [...]\n"
|
||||
|
||||
class MainObject : public QObject
|
||||
{
|
||||
Q_OBJECT;
|
||||
public:
|
||||
MainObject(QObject *parent=0);
|
||||
|
||||
private slots:
|
||||
void timeoutData(int id);
|
||||
|
||||
private:
|
||||
RDTimeEngine *d_time_engine;
|
||||
};
|
||||
|
||||
|
||||
#endif // TIMEENGINE_TEST_H
|
Loading…
x
Reference in New Issue
Block a user