-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathntptest.cpp
More file actions
75 lines (62 loc) · 2.22 KB
/
ntptest.cpp
File metadata and controls
75 lines (62 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <QDebug>
#include <QCoreApplication>
#include <QStringList>
#include "ntptest.h"
namespace my_np{
const std::string ntp_server{"0.ua.pool.ntp.org"};
const quint16 port{123};
const quint16 interval{14}; // in ntpd start from 64sec and then correct this value. I use constant interval
}
NtpTest::NtpTest(int timeZone, QObject *parent)
: QObject(parent)
, m_timeZone(timeZone*3600)
, m_timer(nullptr)
, mInterval(my_np::interval)
{
m_client = new NtpClient(QString(my_np::ntp_server.c_str()),my_np::port,this);
connect(m_client, SIGNAL(replyReceived(QHostAddress,quint16,NtpReply)), this, SLOT(onReplyReceived(QHostAddress,quint16,NtpReply)));
m_timer = new QTimer();
m_timer->setInterval(mInterval*1000);
connect(m_timer, SIGNAL(timeout()), this, SLOT(timeEnd()));
}
void NtpTest::timeEnd() {
// the interval has ended, we repeat the time comparison
this->run();
}
void NtpTest::run()
{
emit sendMsg( "send request to ntp-server: " + QString(my_np::ntp_server.c_str())+", to its first ip: " + m_client->getIPofNTPserver());
if ( m_client->sendRequest() ) {
qDebug()<<"sendRequest works";
m_timer->start();
return;
}
qDebug()<<"sendRequest doen't work";
}
void NtpTest::onReplyReceived( QHostAddress host, quint16 port, NtpReply reply )
{
Q_UNUSED(host)
Q_UNUSED(port)
QDateTime Arrival{ QDateTime::currentDateTime() };
QDateTime Receive{ reply.receiveTime() };
QDateTime Originate{ reply.originTime() };
QDateTime Transmit{ reply.transmitTime() };
// difference = Receive — Originate — roundTripDelay/2
qint64 diff = Receive.msecsTo( Originate ) - reply.roundTripDelay()/2;
emit differenceSignal( static_cast<int>( diff ) );
}
void NtpTest::setTimerInterval( QString interval ) {
m_timer->stop();
const int new_interval = interval.toInt();
if (new_interval != 0 && new_interval > 0 ) {
mInterval = new_interval;
m_timer->setInterval( interval.toInt()*1000 );
run();
return;
}
emit wrongIntervalChangingSignal( mInterval ); // send messsage to user - wrong interval - look in qml code
run();
}
quint16 NtpTest::getTimerInterval() {
return mInterval;
}