2018-06-24 22:13:37 +00:00
|
|
|
#pragma once
|
2018-09-29 09:19:44 +00:00
|
|
|
|
|
|
|
#include "nvcomputer.h"
|
2018-07-06 06:12:55 +00:00
|
|
|
#include "nvpairingmanager.h"
|
2018-06-24 22:13:37 +00:00
|
|
|
|
2018-07-01 06:07:31 +00:00
|
|
|
#include <qmdnsengine/server.h>
|
|
|
|
#include <qmdnsengine/cache.h>
|
|
|
|
#include <qmdnsengine/browser.h>
|
|
|
|
#include <qmdnsengine/service.h>
|
|
|
|
#include <qmdnsengine/resolver.h>
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
#include <QThread>
|
|
|
|
#include <QReadWriteLock>
|
2018-06-27 07:43:46 +00:00
|
|
|
#include <QSettings>
|
2018-07-06 06:12:55 +00:00
|
|
|
#include <QRunnable>
|
2018-06-24 22:13:37 +00:00
|
|
|
|
2018-07-01 06:07:31 +00:00
|
|
|
class MdnsPendingComputer : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MdnsPendingComputer(QMdnsEngine::Server* server,
|
|
|
|
QMdnsEngine::Cache* cache,
|
|
|
|
const QMdnsEngine::Service& service)
|
|
|
|
: m_Hostname(service.hostname()),
|
|
|
|
m_Resolver(server, m_Hostname, cache)
|
|
|
|
{
|
|
|
|
connect(&m_Resolver, SIGNAL(resolved(QHostAddress)),
|
|
|
|
this, SLOT(handleResolved(QHostAddress)));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString hostname()
|
|
|
|
{
|
|
|
|
return m_Hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void handleResolved(const QHostAddress& address)
|
|
|
|
{
|
|
|
|
if (address.protocol() == QAbstractSocket::IPv4Protocol) {
|
|
|
|
m_Resolver.disconnect();
|
|
|
|
emit resolvedv4(this, address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void resolvedv4(MdnsPendingComputer*, const QHostAddress&);
|
|
|
|
|
|
|
|
private:
|
|
|
|
QByteArray m_Hostname;
|
|
|
|
QMdnsEngine::Resolver m_Resolver;
|
|
|
|
};
|
|
|
|
|
2019-01-20 03:16:09 +00:00
|
|
|
class ComputerPollingEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ComputerPollingEntry()
|
|
|
|
: m_ActiveThread(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ComputerPollingEntry()
|
|
|
|
{
|
|
|
|
interrupt();
|
|
|
|
|
|
|
|
// interrupt() should have taken care of this
|
|
|
|
Q_ASSERT(m_ActiveThread == nullptr);
|
|
|
|
|
|
|
|
for (QThread* thread : m_InactiveList) {
|
|
|
|
thread->wait();
|
|
|
|
delete thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isActive()
|
|
|
|
{
|
|
|
|
cleanInactiveList();
|
|
|
|
|
|
|
|
return m_ActiveThread != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setActiveThread(QThread* thread)
|
|
|
|
{
|
|
|
|
cleanInactiveList();
|
|
|
|
|
|
|
|
Q_ASSERT(!isActive());
|
|
|
|
m_ActiveThread = thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void interrupt()
|
|
|
|
{
|
|
|
|
cleanInactiveList();
|
|
|
|
|
|
|
|
if (m_ActiveThread != nullptr) {
|
|
|
|
// Interrupt the active thread
|
|
|
|
m_ActiveThread->requestInterruption();
|
|
|
|
|
|
|
|
// Place it on the inactive list awaiting death
|
|
|
|
m_InactiveList.append(m_ActiveThread);
|
|
|
|
|
|
|
|
m_ActiveThread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void cleanInactiveList()
|
|
|
|
{
|
|
|
|
QMutableListIterator<QThread*> i(m_InactiveList);
|
|
|
|
|
|
|
|
// Reap any threads that have finished
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
|
|
|
|
QThread* thread = i.value();
|
|
|
|
if (thread->isFinished()) {
|
|
|
|
delete thread;
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QThread* m_ActiveThread;
|
|
|
|
QList<QThread*> m_InactiveList;
|
|
|
|
};
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
class ComputerManager : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2018-07-06 06:12:55 +00:00
|
|
|
friend class DeferredHostDeletionTask;
|
2018-07-06 07:34:16 +00:00
|
|
|
friend class PendingAddTask;
|
2018-07-06 06:12:55 +00:00
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
public:
|
2018-06-28 02:55:44 +00:00
|
|
|
explicit ComputerManager(QObject *parent = nullptr);
|
2018-06-27 04:47:01 +00:00
|
|
|
|
2018-07-19 04:27:43 +00:00
|
|
|
virtual ~ComputerManager();
|
|
|
|
|
2018-07-06 05:08:55 +00:00
|
|
|
Q_INVOKABLE void startPolling();
|
2018-06-27 04:47:01 +00:00
|
|
|
|
2018-07-06 05:08:55 +00:00
|
|
|
Q_INVOKABLE void stopPollingAsync();
|
2018-06-27 04:47:01 +00:00
|
|
|
|
2018-07-06 07:34:16 +00:00
|
|
|
Q_INVOKABLE void addNewHost(QString address, bool mdns);
|
2018-06-27 04:47:01 +00:00
|
|
|
|
2018-07-06 06:12:55 +00:00
|
|
|
void pairHost(NvComputer* computer, QString pin);
|
|
|
|
|
2018-08-01 05:21:39 +00:00
|
|
|
void quitRunningApp(NvComputer* computer);
|
|
|
|
|
2018-06-28 05:16:57 +00:00
|
|
|
QVector<NvComputer*> getComputers();
|
|
|
|
|
|
|
|
// computer is deleted inside this call
|
|
|
|
void deleteHost(NvComputer* computer);
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
signals:
|
|
|
|
void computerStateChanged(NvComputer* computer);
|
|
|
|
|
2018-07-06 06:12:55 +00:00
|
|
|
void pairingCompleted(NvComputer* computer, QString error);
|
|
|
|
|
2018-07-06 07:34:16 +00:00
|
|
|
void computerAddCompleted(QVariant success);
|
|
|
|
|
2018-08-02 05:32:21 +00:00
|
|
|
void quitAppCompleted(QVariant error);
|
2018-08-01 05:21:39 +00:00
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
private slots:
|
|
|
|
void handleComputerStateChanged(NvComputer* computer);
|
|
|
|
|
2018-07-01 06:07:31 +00:00
|
|
|
void handleMdnsServiceResolved(MdnsPendingComputer* computer, const QHostAddress& address);
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
private:
|
2018-06-27 07:43:46 +00:00
|
|
|
void saveHosts();
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
void startPollingComputer(NvComputer* computer);
|
|
|
|
|
2018-07-09 05:07:20 +00:00
|
|
|
int m_PollingRef;
|
2018-06-27 04:47:01 +00:00
|
|
|
QReadWriteLock m_Lock;
|
|
|
|
QMap<QString, NvComputer*> m_KnownHosts;
|
2019-01-20 03:16:09 +00:00
|
|
|
QMap<QString, ComputerPollingEntry*> m_PollEntries;
|
2018-07-01 06:07:31 +00:00
|
|
|
QMdnsEngine::Server m_MdnsServer;
|
|
|
|
QMdnsEngine::Browser* m_MdnsBrowser;
|
|
|
|
QMdnsEngine::Cache m_MdnsCache;
|
|
|
|
QVector<MdnsPendingComputer*> m_PendingResolution;
|
2018-06-24 22:13:37 +00:00
|
|
|
};
|