-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyawareplugin.cpp
More file actions
79 lines (64 loc) · 2.04 KB
/
pyawareplugin.cpp
File metadata and controls
79 lines (64 loc) · 2.04 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
76
77
78
79
#include "pyawareplugin.h"
PyAwarePlugin::PyAwarePlugin(QString host, int port) :
piaware_host(host), piaware_port(port)
{
this->piaware_socket = new QTcpSocket(this);
this->piaware_alive_timer = new QTimer(this);
QObject::connect(piaware_socket, SIGNAL(connected()),this, SLOT(connected()));
QObject::connect(piaware_socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
QObject::connect(piaware_socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
QObject::connect(this, SIGNAL(doReconnect()),this, SLOT(doConnect()));
QObject::connect(piaware_alive_timer, SIGNAL(timeout()), this, SLOT(checkConnection()));
piaware_alive_timer->start(5000);
}
void PyAwarePlugin::checkConnection()
{
QProcess pingProcess;
QStringList params;
params << "-n" << "1";
params << this->piaware_host;
pingProcess.start("ping",params,QIODevice::ReadOnly);
pingProcess.waitForFinished(-1);
if (pingProcess.exitCode() == 0)
{
if(this->piaware_socket->state() != QTcpSocket::ConnectedState)
emit this->doReconnect();
}
else
{
if(this->piaware_socket->state() == QTcpSocket::ConnectedState)
{
qDebug()<<"Closing socket...";
this->piaware_socket->close();
}
}
}
void PyAwarePlugin::doConnect()
{
qDebug() << "Connecting to "<<this->piaware_host<<":"<<this->piaware_port;
// Non blocking call
piaware_socket->connectToHost(this->piaware_host, this->piaware_port);
if(!this->piaware_socket->waitForConnected(1000))
{
qDebug() << "Error: " << piaware_socket->errorString();
}
}
void PyAwarePlugin::connected()
{
qDebug() << "Socket connected!";
}
void PyAwarePlugin::disconnected()
{
qDebug() << "Socket disconnected!";
}
void PyAwarePlugin::readyRead()
{
QStringList list;
while(this->piaware_socket->canReadLine())
{
QString data = QString(this->piaware_socket->readLine());
data.remove(QRegExp("[\\n\\t\\r]"));
list.append(data);
qDebug()<<data;
}
}