summaryrefslogtreecommitdiff
path: root/example/ntp.cpp
Unidiff
Diffstat (limited to 'example/ntp.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--example/ntp.cpp130
1 files changed, 130 insertions, 0 deletions
diff --git a/example/ntp.cpp b/example/ntp.cpp
new file mode 100644
index 0000000..5357cae
--- a/dev/null
+++ b/example/ntp.cpp
@@ -0,0 +1,130 @@
1#include "ntp.h"
2#include <qpushbutton.h>
3#include <qregexp.h>
4#include <qlabel.h>
5#include <qlineedit.h>
6#include <qmultilineedit.h>
7#include <opie/oprocess.h>
8#include <qpe/config.h>
9#include <qpe/global.h>
10#include <qpe/timeconversion.h>
11#include <qpe/tzselect.h>
12#include <qpe/timestring.h>
13#include <qpe/qpedialog.h>
14#include <sys/time.h>
15#include <time.h>
16#include <stdlib.h>
17
18
19Ntp::Ntp( QWidget* parent, const char* name, WFlags fl )
20 : NtpBase( parent, name, fl )
21{
22 Config cfg("ntp",Config::User);
23 cfg.setGroup("settings");
24 _maxOffset = cfg.readNumEntry("maxOffset",5);
25 _minLookupDiff = cfg.readNumEntry("minLookupDiff",10);
26 LineEditNtpServer->setText( cfg.readEntry("ntpServer") );
27 ntpProcess = new OProcess( );
28 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)),
29 this, SLOT(getNtpOutput(OProcess*,char*,int)));
30 connect ( ntpProcess, SIGNAL(processExited(OProcess*)),
31 this, SLOT(ntpFinished(OProcess*)));
32 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp()));
33 _nextCorrection = new QTimer( this );
34 connect( _nextCorrection, SIGNAL(timeout()), SLOT(correctClock()) );
35 slotRunNtp();
36}
37
38Ntp::~Ntp()
39{
40 Config cfg("ntp",Config::User);
41 cfg.setGroup("settings");
42 cfg.writeEntry("ntpServer", LineEditNtpServer->text());
43}
44
45
46void Ntp::slotRunNtp()
47{
48 TextLabelStartTime->setText(QDateTime::currentDateTime().toString());
49 ntpProcess->clearArguments();
50 *ntpProcess << "ntpdate" << LineEditNtpServer->text();
51 bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput);
52 if ( !ret ) {
53 qDebug("Error while executing ntp");
54 outPut->append("\nError while executing\n\n");
55 }
56}
57
58void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen)
59{
60 QString lineStr, lineStrOld;
61 lineStr = buffer;
62 lineStr=lineStr.left(buflen);
63 if (lineStr!=lineStrOld)
64 {
65 outPut->append(lineStr);
66 _ntpOutput += lineStr;
67 }
68 lineStrOld = lineStr;
69}
70
71void Ntp::ntpFinished(OProcess*)
72{
73 Config cfg("ntp",Config::User);
74 cfg.setGroup("lookups");
75 int lastLookup = cfg.readNumEntry("time",0);
76 int lookupCount = cfg.readNumEntry("count",-1);
77 int time = TimeConversion::toUTC( QDateTime::currentDateTime() );
78 cfg.writeEntry("time", time);
79
80 float timeShift = getTimeShift();
81 int secsSinceLast = time - lastLookup;
82 TextLabelNewTime->setText(QDateTime::currentDateTime().toString());
83 if ( lastLookup > 0 && secsSinceLast > 60*_minLookupDiff)
84 {
85 lookupCount++;
86 cfg.writeEntry("count",lookupCount);
87 cfg.setGroup("lookup_"+QString::number(lookupCount));
88 _shiftPerSec = timeShift / secsSinceLast;
89 float nextCorr = _maxOffset / _shiftPerSec;
90 qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec);qDebug("in %f secs the time will be shifted by %i", nextCorr,_maxOffset);
91 cfg.writeEntry("secsSinceLast",secsSinceLast);
92 cfg.writeEntry("timeShift",QString::number(timeShift));
93 }
94}
95
96void Ntp::correctClock()
97{
98 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
99 Config cfg("ntp",Config::User);
100 cfg.setGroup("correction");
101 int lastTime = cfg.readNumEntry("time",0);
102 int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
103 int corr = int((now - lastTime) * _shiftPerSec);
104 outPut->append( "time will be shifted by "+QString::number(corr)+ "secs");
105 struct timeval myTv;
106 myTv.tv_sec = TimeConversion::toUTC( QDateTime::currentDateTime().addSecs(corr) );
107 myTv.tv_usec = 0;
108
109 if ( myTv.tv_sec != -1 )
110 ::settimeofday( &myTv, 0 );
111 Global::writeHWClock();
112 cfg.writeEntry("time",now);
113 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
114}
115
116float Ntp::getTimeShift()
117{
118 QString _offset = "offset";
119 QString _sec = "sec";
120 QRegExp _reOffset = QRegExp(_offset);
121 QRegExp _reEndOffset = QRegExp(_sec);
122 int posOffset = _reOffset.match( _ntpOutput );
123 int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset );
124 posOffset += _offset.length() + 1;
125 QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1);
126 qDebug("%s", _ntpOutput.latin1());
127 qDebug("diff = >%s<",diff.latin1());
128 TextLabelTimeShift->setText(diff);
129 return diff.toFloat();
130} \ No newline at end of file