IpStandardItemWithThread::IpStandardItemWithThread(QString ipAddress, int pingInterval): _ipAddress(ipAddress), _pingInterval(pingInterval), unknownIcon(QIcon(":/icons/ping_unknown.png")), onIcon(QIcon(":/icons/ping_on.png")), offIcon(QIcon(":/icons/ping_off.png")){ this->setIcon(unknownIcon); QFutureWatcher<QByteArray>* fw = new QFutureWatcher<QByteArray>(this); connect(fw, &QFutureWatcher<QString>::finished, this, [=] { qDebug() << fw->result(); }); fw->setFuture(QtConcurrent::run ( [=]() { // YOUR CODE HERE QProcess p; connect(this,&IpStandardItemWithThread::closeProcess,[this,&p](){ if(p.isOpen()) { p.kill(); p.waitForFinished(); qDebug() << p.isOpen(); } }); p.start("C:/Windows/System32/PING.EXE",QStringList() << "-n" << "1" << "-w" << "2" << _ipAddress); qApp->processEvents(); p.waitForFinished(); qApp->processEvents(); return p.readAll().simplified(); }));}IpStandardItemWithThread::~IpStandardItemWithThread(){ //itemPing->setWorkFalse(); emit closeProcess();}
proc->start("C:/Windows/System32/CMD.EXE",QStringList() << "/c" << "C:/Windows/System32/CHCP.COM" << "437" << "&&" << "C:/Windows/System32/PING.EXE" << "-n" << "1" << "-w" << "2" << _ipAddress);
proc->start("C:/Windows/System32/CMD.EXE",QStringList() << "/c" << "C:/Windows/System32/CHCP.COM 437" << "&&" << "C:/Windows/System32/PING.EXE -n 1 -w 2 " + _ipAddress);
proc->start("C:/Windows/System32/PING.EXE",QStringList() << "-n" << "1" << "-w" << "2" << _ipAddress);
qApp->processEvents(); p.waitForFinished(); qApp->processEvents();
class IpStandardItemWithThread : public QStandardItem{public: IpStandardItemWithThread(QString ipAddress,int pingInterval); ~IpStandardItemWithThread();private: QString _ipAddress; int _pingInterval; QIcon unknownIcon, onIcon,offIcon; HANDLE hIcmpFile; // Обработчик unsigned long ipaddr; // Адрес назначения // Количество ответов char SendData[32]; // Буффер отсылаемых данных LPVOID ReplyBuffer; // Буффер ответов DWORD ReplySize; QFuture<void> future; bool work;};
IpStandardItemWithThread::IpStandardItemWithThread(QString ipAddress, int pingInterval): _ipAddress(ipAddress), _pingInterval(pingInterval), unknownIcon(QIcon(":/icons/ping_unknown.png")), onIcon(QIcon(":/icons/ping_on.png")), offIcon(QIcon(":/icons/ping_off.png")), ipaddr(INADDR_NONE), ReplyBuffer(NULL), ReplySize(0), work(true){ this->setIcon(unknownIcon); strcpy(SendData,"Data Buffer"); // Установка ip-адреса ipaddr = inet_addr(_ipAddress.toStdString().c_str()); hIcmpFile = IcmpCreateFile(); // создание обработчика // Выделение памяти под буффер ответов ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData); ReplyBuffer = (VOID*) malloc(ReplySize); future = QtConcurrent::run([this](){ while(work) { if(IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 100)) { this->setIcon(onIcon); qDebug() << _ipAddress << work; } else { this->setIcon(offIcon); qDebug() << _ipAddress << work; } } });}IpStandardItemWithThread::~IpStandardItemWithThread(){ work = false; future.waitForFinished(); if(ReplyBuffer) { free(ReplyBuffer); ReplyBuffer = NULL; }}
#include "segmentmodel.h"#include "segmentwidget.h"SegmentModel::SegmentModel(){ this->setHorizontalHeaderLabels(QStringList() << tr("Д") << tr("IP") << tr("NetBIOS") << tr("MAC") << tr("Тип устройства") << tr("Операционная\nсистема") << tr("Комментарий") << tr("Р"));}void SegmentModel::insertRow(HostInfo ipInf){ IpStandardItemWithThread *item0 = new IpStandardItemWithThread(ipInf.IpAddress,1000); item0->setEditable(false); item0->setSelectable(false); connect(item0,SIGNAL(canRepaint()),this,SIGNAL(canRepaint())); QStandardItem *item1 = new QStandardItem(ipInf.IpAddress); item1->setEditable(false); item1->setSelectable(false); QStandardItem *item2 = new QStandardItem(ipInf.NetBIOS); item2->setEditable(false); item2->setSelectable(false); QStandardItem *item3 = new QStandardItem(ipInf.MAC); item3->setEditable(false); item3->setSelectable(false); QStandardItem *item4 = new QStandardItem(ipInf.DeviceType); item4->setEditable(false); item4->setSelectable(false); QStandardItem *item5 = new QStandardItem(ipInf.OS); item5->setEditable(false); item5->setSelectable(false); QStandardItem *item6 = new QStandardItem(ipInf.comment); item6->setEditable(false); item6->setSelectable(false); QStandardItem *item7 = new QStandardItem(""); item7->setEditable(false); item7->setSelectable(false); this->appendRow(QList<QStandardItem *>() << item0 << item1 << item2 << item3 << item4 << item5 << item6 << item7);}void SegmentModel::setNewSegment(QModelIndex segmentModel){ _segmentModel = segmentModel; _segmentAddress = _segmentModel.data().toString(); _segmentName = _segmentModel.sibling(_segmentModel.row(),1).data().toString(); QStandardItem *segmentItem = ((CentralModel*)(_segmentModel.model()))->itemFromIndex(_segmentModel); for(int i=0;i<segmentItem->rowCount();i++) { IpStandardItem *ipItem = (IpStandardItem *)segmentItem->child(i,0); insertRow(ipItem->getIpInformation()); }}void SegmentModel::insertClearRow(){ if(this->rowCount() < 254) { this->setRowCount(this->rowCount() + 1); }}IpStandardItemWithThread::IpStandardItemWithThread(QString ipAddress, int pingInterval): _ipAddress(ipAddress), _pingInterval(pingInterval), unknownIcon(QIcon(":/icons/ping_unknown.png")), onIcon(QIcon(":/icons/ping_on.png")), offIcon(QIcon(":/icons/ping_off.png")), ipaddr(INADDR_NONE), ReplyBuffer(NULL), ReplySize(0), testTimer(new QTimer()){ this->setIcon(unknownIcon); strcpy(SendData,"Data Buffer"); // Установка ip-адреса ipaddr = inet_addr(_ipAddress.toStdString().c_str()); hIcmpFile = IcmpCreateFile(); // создание обработчика // Выделение памяти под буффер ответов ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData); ReplyBuffer = (VOID*) malloc(ReplySize); connect(testTimer,&QTimer::timeout,[this](){ future = QtConcurrent::run(this,testFunction); }); testTimer->start(_pingInterval); }IpStandardItemWithThread::~IpStandardItemWithThread(){ testTimer->stop(); future.waitForFinished(); if(ReplyBuffer) { free(ReplyBuffer); ReplyBuffer = NULL; } if(testTimer) { delete testTimer; testTimer = NULL; }}void IpStandardItemWithThread::testFunction(){ if(IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 100)) { this->setIcon(onIcon); } else { this->setIcon(offIcon); } emit canRepaint();}
_ipAddress(ipAddress), _pingInterval(pingInterval), unknownIcon(QIcon(":/icons/ping_unknown.png")), onIcon(QIcon(":/icons/ping_on.png")), offIcon(QIcon(":/icons/ping_off.png")), ipaddr(INADDR_NONE), ReplyBuffer(NULL), ReplySize(0), testTimer(new QTimer())
#ifndef SEGMENTMODEL_H#define SEGMENTMODEL_H#include <QObject>#include <QStandardItemModel>#include <QStandardItem>#include <QModelIndex>#include <QStringList>#include <QIcon>#include <QThreadPool>#include <QThread>#include <QMutex>#include "centralmodel.h"class SegmentModel;class IpStandardItemWithThread;class PingThread;class SegmentModel : public QStandardItemModel{ Q_OBJECTpublic: SegmentModel(QObject *parent = nullptr); QString getSegmentAddress();private: QModelIndex _segmentModel; QString _segmentAddress; QString _segmentName;public slots: void setNewSegment(QModelIndex segmentModel); void insertRow(HostInfo ipInf); void removeIp(QString); void clearAll();};class IpStandardItemWithThread : public QObject, public QStandardItem{ Q_OBJECTpublic: IpStandardItemWithThread(QString ipAddress,int pingInterval); ~IpStandardItemWithThread();private: PingThread *_pThread; QIcon unknownIcon, onIcon,offIcon;private slots: void setItemIcon(int);};class PingThread : public QThread{ Q_OBJECTpublic: PingThread(QString ipAd,int ipInterval,QObject * parent = nullptr);private: int _pingInterval; QString _ipAddress; volatile bool _stopped; QMutex mutex; void run();signals: void statusPing(int);public slots: void stop();};#endif // SEGMENTMODEL_H
#include "segmentmodel.h"#include "segmentwidget.h"SegmentModel::SegmentModel(QObject *parent): QStandardItemModel(parent){}QString SegmentModel::getSegmentAddress(){ return _segmentAddress;}void SegmentModel::insertRow(HostInfo ipInf){ if(CommonFunctions::getSegmentFromIp(ipInf.IpAddress) != _segmentAddress) return; this->appendRow(QList<QStandardItem *>() << new IpStandardItemWithThread(ipInf.IpAddress,1000) << new QStandardItem(ipInf.IpAddress) << new QStandardItem(ipInf.NetBIOS) << new QStandardItem(ipInf.MAC) << new QStandardItem(ipInf.DeviceType) << new QStandardItem(ipInf.OS) << new QStandardItem(ipInf.comment) << new QStandardItem("")); for(int i=0;i<this->columnCount();i++) this->item(this->rowCount() - 1,i)->setEditable(false);}void SegmentModel::removeIp(QString ipAddr){ if(CommonFunctions::getSegmentFromIp(ipAddr) != _segmentAddress) return; for(int i=0;i<this->rowCount();i++) { if(this->item(i,1)->text() == ipAddr) { this->removeRow(i); return; } }}void SegmentModel::clearAll(){ this->clear(); _segmentName = "";}void SegmentModel::setNewSegment(QModelIndex segmentModel){ this->setHorizontalHeaderLabels(QStringList() << tr("Д") << tr("IP") << tr("NetBIOS") << tr("MAC") << tr("Тип устройства") << tr("Операционная\nсистема") << tr("Комментарий") << tr("Р")); _segmentModel = segmentModel; _segmentAddress = _segmentModel.data().toString(); _segmentName = _segmentModel.sibling(_segmentModel.row(),1).data().toString(); QStandardItem *segmentItem = ((CentralModel*)(_segmentModel.model()))->itemFromIndex(_segmentModel); int i; for(i=0;i<segmentItem->rowCount();i++) { IpStandardItem *ipItem = (IpStandardItem *)segmentItem->child(i,0); insertRow(ipItem->getIpInformation()); }}IpStandardItemWithThread::IpStandardItemWithThread(QString ipAddress, int pingInterval): unknownIcon(QIcon(":/icons/ping_unknown.png")), onIcon(QIcon(":/icons/ping_on.png")), offIcon(QIcon(":/icons/ping_off.png")){ this->setIcon(unknownIcon); _pThread = new PingThread(ipAddress,pingInterval,this); connect(_pThread,SIGNAL(statusPing(int)),this,SLOT(setItemIcon(int))); _pThread->start();}IpStandardItemWithThread::~IpStandardItemWithThread(){ if(_pThread->isRunning()) { _pThread->stop(); _pThread->terminate(); _pThread->wait(); }}void IpStandardItemWithThread::setItemIcon(int pStatus){ if(pStatus) { this->setIcon(onIcon); } else { this->setIcon(offIcon); }}PingThread::PingThread(QString ipAd, int ipInterval, QObject *parent): QThread(parent), _pingInterval(ipInterval), _ipAddress(ipAd), _stopped(false){}void PingThread::run(){ for(;;) { mutex.lock(); if(_stopped) { _stopped = false; mutex.unlock(); break; } mutex.unlock(); msleep(_pingInterval); emit statusPing(CommonFunctions::isPingActive(_ipAddress)); }}void PingThread::stop(){ mutex.lock(); _stopped = true; mutex.unlock();}