parent
1071d8950c
commit
4c9a88c214
@ -0,0 +1,87 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "devicemanage.h"
|
||||
#include "server.h"
|
||||
|
||||
#define DM_MAX_DEVICES_NUM 16
|
||||
|
||||
DeviceManage::DeviceManage(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
DeviceManage::~DeviceManage()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool DeviceManage::connectDevice(Device::DeviceParams params)
|
||||
{
|
||||
if (params.serial.trimmed().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (m_devices.contains(params.serial)) {
|
||||
return false;
|
||||
}
|
||||
if (DM_MAX_DEVICES_NUM < m_devices.size()) {
|
||||
qInfo("over the maximum number of connections");
|
||||
return false;
|
||||
}
|
||||
quint16 port = 0;
|
||||
if (params.useReverse) {
|
||||
port = getFreePort();
|
||||
if (0 == port) {
|
||||
qInfo("no port available, automatically switch to forward");
|
||||
params.useReverse = false;
|
||||
} else {
|
||||
qInfo("free port %d", port);
|
||||
}
|
||||
}
|
||||
Device *device = new Device(params);
|
||||
connect(device, &Device::deviceDisconnect, this, &DeviceManage::onDeviceDisconnect);
|
||||
m_devices[params.serial] = device;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeviceManage::disconnectDevice(const QString &serial)
|
||||
{
|
||||
bool ret = false;
|
||||
if (!serial.isEmpty() && m_devices.contains(serial)) {
|
||||
auto it = m_devices.find(serial);
|
||||
if (it->data()) {
|
||||
it->data()->deleteLater();
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void DeviceManage::onDeviceDisconnect(QString serial)
|
||||
{
|
||||
if (!serial.isEmpty() && m_devices.contains(serial)) {
|
||||
m_devices.remove(serial);
|
||||
}
|
||||
}
|
||||
|
||||
quint16 DeviceManage::getFreePort()
|
||||
{
|
||||
quint16 port = m_localPortStart;
|
||||
QMapIterator<QString, QPointer<Device>> i(m_devices);
|
||||
while (port < m_localPortStart + DM_MAX_DEVICES_NUM) {
|
||||
bool used = false;
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
auto device = i.value();
|
||||
if (device && device->getServer()
|
||||
&& device->getServer()->isReverse()
|
||||
&& port == device->getServer()->getParams().localPort) {
|
||||
used = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!used) {
|
||||
return port;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
#ifndef DEVICEMANAGE_H
|
||||
#define DEVICEMANAGE_H
|
||||
|
||||
#include <QPointer>
|
||||
#include <QMap>
|
||||
|
||||
#include "device.h"
|
||||
|
||||
class DeviceManage : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit DeviceManage(QObject *parent = nullptr);
|
||||
virtual ~DeviceManage();
|
||||
|
||||
bool connectDevice(Device::DeviceParams params);
|
||||
bool disconnectDevice(const QString &serial);
|
||||
|
||||
protected slots:
|
||||
void onDeviceDisconnect(QString serial);
|
||||
|
||||
private:
|
||||
quint16 getFreePort();
|
||||
|
||||
private:
|
||||
QMap<QString, QPointer<Device>> m_devices;
|
||||
quint16 m_localPortStart = 27183;
|
||||
};
|
||||
|
||||
#endif // DEVICEMANAGE_H
|
||||
@ -0,0 +1,5 @@
|
||||
HEADERS += \
|
||||
$$PWD/devicemanage.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/devicemanage.cpp
|
||||
Loading…
Reference in new issue