Skip to content

Instantly share code, notes, and snippets.

@ttldtor
Created August 1, 2015 14:08
Show Gist options
  • Save ttldtor/d440dec432bb20b3b963 to your computer and use it in GitHub Desktop.
Save ttldtor/d440dec432bb20b3b963 to your computer and use it in GitHub Desktop.
#ifndef MUPINGER_HPP
#define MUPINGER_HPP
#include <QObject>
#include <QString>
#include <QSet>
#include <QHash>
#include <QTimer>
#include <QtConcurrent/QtConcurrent>
#include <QQmlEngine>
#include <QReadWriteLock>
#include <utility>
#include <tuple>
class MuPinger : public QObject
{
Q_OBJECT
Q_ENUMS(Status)
public:
enum Status {
None = -127,
InvalidIP = -10,
InvalidHandle,
DestHostUnreachable,
DestNetUnreachable,
RequestTimeOut,
ReplyBufferSizeTooSmall,
OK = 0
};
explicit MuPinger(QObject *parent = 0);
~ MuPinger();
Q_INVOKABLE void registerIP(const QString& ip);
Q_INVOKABLE void unregisterIP(const QString& ip);
Q_INVOKABLE void setPingTimeout(int timeout);
Q_INVOKABLE void start(int timeout = -1);
Q_INVOKABLE void stop();
signals:
void pinged(const QString& ip, Status status, long roundtripTime);
void started();
void stopped();
public slots:
private:
const int defaultPingTimeout = 30000;
int pingTimeout_;
QTimer pingTimer_;
QSet<QString> ips_;
bool initialized_;
QReadWriteLock lock_;
};
struct PingImpl
{
static bool init();
static std::tuple<QString, MuPinger::Status, long> ping(const QString& ip);
static void done();
};
#endif // MUPINGER_HPP
import QtQuick 2.0
import Mu 1.0
Item {
id: pingPoint
property string address: "127.0.0.1"
property var status: Pinger.None
property int roundtripTime: -1
implicitWidth: gray.width
implicitHeight: gray.height
signal removeMe()
MouseArea {
anchors.fill: pingPoint
drag.target: pingPoint
onDoubleClicked: {
removeMe();
}
}
Image {
id: gray
source: "img/gray2x32.svg"
opacity: 1;
}
Image {
id: darkgreen
source: "img/darkgreen2x32.svg"
opacity: 0;
anchors.fill: gray
}
Image {
id: green
source: "img/lightgreen2x32.svg"
opacity: 0;
anchors.fill: gray
}
Image {
id: darkred
source: "img/darkred2x32.svg"
opacity: 0;
anchors.fill: gray
}
Image {
id: red
source: "img/lightred2x32.svg"
opacity: 0;
anchors.fill: gray
}
SequentialAnimation {
id: animation
ParallelAnimation {
NumberAnimation {
id: from1
target: gray
properties: "opacity"
from: 1
to: 0
duration: 500
}
NumberAnimation {
id: to1
target: green;
properties: "opacity";
from: 0
to: 1
duration: 500;
}
}
ParallelAnimation {
NumberAnimation {
id: from2
target: green
properties: "opacity"
from: 1
to: 0
duration: 500
}
NumberAnimation {
id: to2
target: darkgreen;
properties: "opacity";
from: 0
to: 1
duration: 500;
}
}
}
Connections {
target: Pinger
onPinged: {
if (address !== ip)
{
return;
}
if (pingPoint.status === Pinger.None)
{
from1.target = gray
}
else if (pingPoint.status === Pinger.OK)
{
from1.target = darkgreen
}
else
{
from1.target = darkred
}
if (status === Pinger.OK)
{
to1.target = green
from2.target = green
to2.target = darkgreen
}
else
{
to1.target = red
from2.target = red
to2.target = darkred
}
pingPoint.status = status;
pingPoint.roundtripTime = roundtripTime;
animation.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment