2011-03-08 17:34:20 +01:00
|
|
|
#include "portforwarder.h"
|
|
|
|
|
2011-11-28 16:12:23 +01:00
|
|
|
#include <QtConcurrentRun>
|
2011-03-08 17:34:20 +01:00
|
|
|
|
|
|
|
#include "core/boundfuturewatcher.h"
|
|
|
|
|
|
|
|
PortForwarder::PortForwarder(QObject* parent)
|
|
|
|
: QObject(parent) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::Init() {
|
2011-11-28 16:12:23 +01:00
|
|
|
QFuture<bool> future = QtConcurrent::run(this, &PortForwarder::InitSync);
|
2011-03-08 17:34:20 +01:00
|
|
|
QFutureWatcher<bool>* watcher = new QFutureWatcher<bool>(this);
|
|
|
|
watcher->setFuture(future);
|
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(InitFinished()));
|
|
|
|
}
|
|
|
|
|
2011-11-28 16:12:23 +01:00
|
|
|
bool PortForwarder::InitSync() {
|
2011-03-08 17:34:20 +01:00
|
|
|
portfwd_.init(10000);
|
2011-11-28 16:12:23 +01:00
|
|
|
return true;
|
2011-03-08 17:34:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::InitFinished() {
|
|
|
|
QFutureWatcher<bool>* watcher = dynamic_cast<QFutureWatcher<bool>*>(sender());
|
|
|
|
Q_ASSERT(watcher);
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
|
|
|
emit(InitFinished(watcher->result()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::AddPortMapping(quint16 port) {
|
|
|
|
QFuture<bool> future = QtConcurrent::run(
|
|
|
|
this, &PortForwarder::AddPortMappingSync, port);
|
|
|
|
BoundFutureWatcher<bool, quint16>* watcher =
|
2011-11-28 16:12:23 +01:00
|
|
|
new BoundFutureWatcher<bool, quint16>(port, this);
|
2011-03-08 17:34:20 +01:00
|
|
|
watcher->setFuture(future);
|
2011-11-28 16:12:23 +01:00
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(AddPortMappingFinished()));
|
2011-03-08 17:34:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PortForwarder::AddPortMappingSync(quint16 port) {
|
|
|
|
return portfwd_.add(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::AddPortMappingFinished() {
|
|
|
|
BoundFutureWatcher<bool, quint16>* watcher =
|
|
|
|
dynamic_cast<BoundFutureWatcher<bool, quint16>*>(sender());
|
|
|
|
Q_ASSERT(watcher);
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
|
|
|
if (watcher->result())
|
|
|
|
emit(PortMappingAdded(watcher->data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::RemovePortMapping(quint16 port) {
|
|
|
|
QFuture<bool> future = QtConcurrent::run(
|
|
|
|
this, &PortForwarder::RemovePortMappingSync, port);
|
|
|
|
BoundFutureWatcher<bool, quint16>* watcher =
|
2011-11-28 16:12:23 +01:00
|
|
|
new BoundFutureWatcher<bool, quint16>(port, this);
|
2011-03-08 17:34:20 +01:00
|
|
|
watcher->setFuture(future);
|
2011-11-28 16:12:23 +01:00
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(RemovePortMappingFinished()));
|
2011-03-08 17:34:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PortForwarder::RemovePortMappingSync(quint16 port) {
|
|
|
|
return portfwd_.remove(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::RemovePortMappingFinished() {
|
|
|
|
BoundFutureWatcher<bool, quint16>* watcher =
|
|
|
|
dynamic_cast<BoundFutureWatcher<bool, quint16>*>(sender());
|
|
|
|
Q_ASSERT(watcher);
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
|
|
|
if (watcher->result())
|
2011-11-28 16:12:23 +01:00
|
|
|
emit(PortMappingRemoved(watcher->data()));
|
2011-03-08 17:34:20 +01:00
|
|
|
}
|