Update reversessh plugin

This commit is contained in:
Michael Zanetti 2019-09-19 19:58:02 +02:00
parent bf71132faa
commit ae5ebcbc58
2 changed files with 28 additions and 23 deletions

View File

@ -38,19 +38,22 @@ void DevicePluginRemoteSsh::init()
connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginRemoteSsh::onPluginTimeout); connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginRemoteSsh::onPluginTimeout);
} }
Device::DeviceSetupStatus DevicePluginRemoteSsh::setupDevice(Device *device) void DevicePluginRemoteSsh::setupDevice(DeviceSetupInfo *info)
{ {
Device *device = info->device();
qCDebug(dcRemoteSsh()) << "Setup" << device->name() << device->params(); qCDebug(dcRemoteSsh()) << "Setup" << device->name() << device->params();
if (device->deviceClassId() == reverseSshDeviceClassId) { if (device->deviceClassId() == reverseSshDeviceClassId) {
m_identityFilePath = QString("%1/.ssh/id_rsa_guh").arg(QDir::homePath()); m_identityFilePath = QString("%1/.ssh/id_rsa_guh").arg(QDir::homePath());
return Device::DeviceSetupStatusSuccess; return info->finish(Device::DeviceErrorNoError);
} }
return Device::DeviceSetupStatusFailure;
} }
Device::DeviceError DevicePluginRemoteSsh::executeAction(Device *device, const Action &action) void DevicePluginRemoteSsh::executeAction(DeviceActionInfo *info)
{ {
Device *device = info->device();
Action action = info->action();
if (device->deviceClassId() == reverseSshDeviceClassId ) { if (device->deviceClassId() == reverseSshDeviceClassId ) {
if (action.actionTypeId() == reverseSshConnectedActionTypeId) { if (action.actionTypeId() == reverseSshConnectedActionTypeId) {
@ -58,27 +61,32 @@ Device::DeviceError DevicePluginRemoteSsh::executeAction(Device *device, const A
if (action.param(reverseSshConnectedActionConnectedParamTypeId).value().toBool() == true) { if (action.param(reverseSshConnectedActionConnectedParamTypeId).value().toBool() == true) {
QProcess *process = startReverseSSHProcess(device); QProcess *process = startReverseSSHProcess(device);
m_reverseSSHProcess.insert(process, device); m_reverseSSHProcess.insert(process, device);
m_startingProcess.insert(process, action.id()); m_startingProcess.insert(process, info);
return Device::DeviceErrorAsync; // in case action call is cancelled, detach result reporting
connect(info, &DeviceActionInfo::destroyed, process, [this, process]{
m_startingProcess.remove(process);
});
return;
} else { } else {
QProcess *process = m_reverseSSHProcess.key(device); QProcess *process = m_reverseSSHProcess.key(device);
// Check if the application is running... // Check if the application is running...
if (!process) if (!process)
return Device::DeviceErrorNoError; return info->finish(Device::DeviceErrorNoError);
if (process->state() == QProcess::NotRunning) if (process->state() == QProcess::NotRunning)
return Device::DeviceErrorNoError; return info->finish(Device::DeviceErrorNoError);
process->kill(); process->kill();
m_killingProcess.insert(process, action.id()); m_killingProcess.insert(process, info);
return Device::DeviceErrorAsync; // in case action call is cancelled, detach result reporting
connect(info, &DeviceActionInfo::destroyed, process, [this, process]{
m_killingProcess.remove(process);
});
return;
} }
return Device::DeviceErrorNoError;
} }
return Device::DeviceErrorActionTypeNotFound;
} }
return Device::DeviceErrorDeviceClassNotFound;
} }
@ -179,8 +187,7 @@ void DevicePluginRemoteSsh::processStateChanged(QProcess::ProcessState state)
case QProcess::Running: case QProcess::Running:
device->setStateValue(reverseSshConnectedStateTypeId, true); device->setStateValue(reverseSshConnectedStateTypeId, true);
if (m_startingProcess.contains(process)) { if (m_startingProcess.contains(process)) {
emit actionExecutionFinished(m_startingProcess.value(process), Device::DeviceErrorNoError); m_startingProcess.take(process)->finish(Device::DeviceErrorNoError);
m_startingProcess.remove(process);
} }
break; break;
@ -189,14 +196,12 @@ void DevicePluginRemoteSsh::processStateChanged(QProcess::ProcessState state)
device->setStateValue(reverseSshConnectedStateTypeId, false); device->setStateValue(reverseSshConnectedStateTypeId, false);
if (m_startingProcess.contains(process)) { if (m_startingProcess.contains(process)) {
emit actionExecutionFinished(m_startingProcess.value(process), Device::DeviceErrorInvalidParameter); m_startingProcess.take(process)->finish(Device::DeviceErrorInvalidParameter);
m_startingProcess.remove(process);
} }
if (m_killingProcess.contains(process)) { if (m_killingProcess.contains(process)) {
emit actionExecutionFinished(m_killingProcess.value(process), Device::DeviceErrorNoError); m_killingProcess.take(process)->finish(Device::DeviceErrorNoError);
m_reverseSSHProcess.remove(process); m_reverseSSHProcess.remove(process);
m_killingProcess.remove(process);
} }
break; break;
default: default:

View File

@ -40,16 +40,16 @@ public:
explicit DevicePluginRemoteSsh(); explicit DevicePluginRemoteSsh();
void init() override; void init() override;
Device::DeviceSetupStatus setupDevice(Device *device) override; void setupDevice(DeviceSetupInfo *info) override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
Device::DeviceError executeAction(Device *device, const Action &action) override; void executeAction(DeviceActionInfo *info) override;
private: private:
QHash<QProcess *, Device *> m_reverseSSHProcess; QHash<QProcess *, Device *> m_reverseSSHProcess;
QHash<QProcess *, Device *> m_sshKeyGenProcess; QHash<QProcess *, Device *> m_sshKeyGenProcess;
QHash<QProcess *, ActionId> m_startingProcess; QHash<QProcess *, DeviceActionInfo*> m_startingProcess;
QHash<QProcess *, ActionId> m_killingProcess; QHash<QProcess *, DeviceActionInfo*> m_killingProcess;
PluginTimer *m_pluginTimer = nullptr; PluginTimer *m_pluginTimer = nullptr;