-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexchangedispatcher.cpp
146 lines (128 loc) · 4.54 KB
/
exchangedispatcher.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright (C) 2019 Alessandro Arrabito
*/
/*
* This file is part of QtExchangeDemo.
*
* QtExchangeDemo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QtExchangeDemo 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 QtExchangeDemo. If not, see <http://www.gnu.org/licenses/>.
*/
#include "exchangedispatcher.h"
#include <QJsonDocument>
#include <QJsonParseError>
#include <QErrorMessage>
#include <QTimer>
#include "exchangemessages.h"
#include "config.h"
static int current_refresh_interval;
static QString provider_url;
static QString provider_field;
DataFetcher::DataFetcher()
{
connect(&qnam, &QNetworkAccessManager::finished, this, &DataFetcher::response);
}
void DataFetcher::onTimeout()
{
QUrl url(provider_url);
QNetworkRequest req(url);
qnam.get(req);
}
void DataFetcher::response(QNetworkReply* reply)
{
if (reply->error() != QNetworkReply::NoError)
{
qDebug() << reply->error();
return;
}
QJsonDocument json_doc = QJsonDocument::fromJson(reply->readAll());
emit data_fetched(json_doc[provider_field].toString().toDouble());
}
ExchangeDispatcher::ExchangeDispatcher():socket(nullptr)
{
current_refresh_interval = default_refresh_interval;
provider_url = default_provider_url;
provider_field = default_provider_field;
}
void ExchangeDispatcher::run()
{
// Setup of DataFetcher loop timer to get data
timer.reset(new QTimer());
DataFetcher data_fetcher;
connect(timer.get(), SIGNAL(timeout()), &data_fetcher, SLOT(onTimeout()));
// Connect by direct connection to be sure that slot will be executed in this thread
connect(&data_fetcher ,
&DataFetcher::data_fetched,
this,
[this](double val){
ExchangeMessageData msg(val);
socket->write(msg.toJsonDoc().toJson());
},
Qt::DirectConnection);
connect(this,SIGNAL(runTimer(int)),timer.get(), SLOT(start(int)));
connect(this,SIGNAL(stopTimer()),timer.get(), SLOT(stop()));
qDebug() << "ExchangeDispatcher ThreadId:" << QThread::currentThreadId();
// start tpc server to handle client connection
// this version handle just one client.
QTcpServer server;
server.setParent(nullptr);
server.moveToThread(this);
connect(&server,
&QTcpServer::newConnection,
this,
[&server, this]()
{
socket = server.nextPendingConnection();
connect(socket, &QTcpSocket::readyRead, this, &ExchangeDispatcher::handleReadyRead);
},
Qt::DirectConnection);
server.setMaxPendingConnections(1);
if(!server.listen(QHostAddress::Any, 9999))
{
throw std::runtime_error("Server could not start");
}
emit server_is_listening();
qDebug() << "Server started";
exec();
timer.release();
qDebug() << "end thread";
}
void ExchangeDispatcher::handleReadyRead()
{
std::unique_ptr<ExchangeMessage> gmsg_ptr = ExchangeMessage::createFromJson(socket->readAll());
qDebug() << "handleReadyRead type:" << static_cast<int>(gmsg_ptr->type());
switch(gmsg_ptr->type())
{
case ExchangeMessage::MessageType::Parameters:
{
ExchangeMessageParameters* msg_ptr = static_cast<ExchangeMessageParameters*>(gmsg_ptr.get());
int new_refresh_interval = msg_ptr->refresh_interval;
if(new_refresh_interval != current_refresh_interval)
current_refresh_interval = new_refresh_interval;
provider_field = msg_ptr->provider_field;
provider_url = msg_ptr->provider_url;
break;
}
case ExchangeMessage::MessageType::SetActiveStatus:
{
ExchangeMessageStatus* msg_ptr = static_cast<ExchangeMessageStatus*>(gmsg_ptr.get());
qDebug() << "SetActiveStatus:" << msg_ptr->is_active;
if(msg_ptr->is_active && !timer->isActive())
emit runTimer(current_refresh_interval);
else if(!msg_ptr->is_active && timer->isActive())
emit stopTimer();
break;
}
default:
break;
}
}