UI: RpcManager: Check connection.

This commit is contained in:
Nodir Temirkhodjaev 2021-05-15 17:16:07 +03:00
parent 00f0da4293
commit 7eeb67184b
10 changed files with 50 additions and 30 deletions

View File

@ -78,6 +78,11 @@ int ControlWorker::id() const
return int(socket()->socketDescriptor());
}
bool ControlWorker::isConnected() const
{
return socket()->state() == QLocalSocket::ConnectedState;
}
QString ControlWorker::errorString() const
{
return socket()->errorString();
@ -124,7 +129,11 @@ bool ControlWorker::sendCommandData(const QByteArray &commandData)
const int bytesSent = socket()->write(commandData);
if (bytesSent != commandData.size()) {
qWarning() << "Sent partial:" << id() << bytesSent << commandData.size();
if (bytesSent < 0) {
qWarning() << "Send error:" << id() << errorString();
} else {
qWarning() << "Sent partial:" << id() << bytesSent << commandData.size();
}
return false;
}

View File

@ -24,7 +24,7 @@ public:
QLocalSocket *socket() const { return m_socket; }
int id() const;
bool isConnected() const;
QString errorString() const;
void setupForAsync();

View File

@ -116,22 +116,22 @@ bool DriverManager::writeData(quint32 code, QByteArray &buf, int size)
return res;
}
void DriverManager::reinstallDriver()
bool DriverManager::reinstallDriver()
{
executeCommand("reinstall.lnk");
return executeCommand("reinstall.lnk");
}
void DriverManager::uninstallDriver()
bool DriverManager::uninstallDriver()
{
executeCommand("uninstall.lnk");
return executeCommand("uninstall.lnk");
}
void DriverManager::executeCommand(const QString &fileName)
bool DriverManager::executeCommand(const QString &fileName)
{
const QString binPath = FileUtil::toNativeSeparators(FileUtil::appBinLocation());
const QString cmdPath = qEnvironmentVariable("COMSPEC");
const QString scriptPath = binPath + R"(\driver\scripts\execute-cmd.bat)";
QProcess::execute(cmdPath, QStringList() << "/C" << scriptPath << fileName);
return QProcess::execute(cmdPath, QStringList() << "/C" << scriptPath << fileName) == 0;
}

View File

@ -31,8 +31,8 @@ public:
virtual void initialize();
virtual void reinstallDriver();
virtual void uninstallDriver();
virtual bool reinstallDriver();
virtual bool uninstallDriver();
signals:
void errorCodeChanged();
@ -58,7 +58,7 @@ private:
bool writeData(quint32 code, QByteArray &buf, int size);
static void executeCommand(const QString &fileName);
static bool executeCommand(const QString &fileName);
private:
quint32 m_errorCode = 0;

View File

@ -161,7 +161,7 @@ void FortManager::setupRpcManager()
}
}
void FortManager::installDriver()
bool FortManager::installDriver()
{
closeDriver();
@ -170,13 +170,17 @@ void FortManager::installDriver()
if (setupDriver()) {
updateDriverConf();
}
return true;
}
void FortManager::removeDriver()
bool FortManager::removeDriver()
{
closeDriver();
driverManager()->uninstallDriver();
return true;
}
bool FortManager::setupDriver()

View File

@ -86,8 +86,8 @@ signals:
void graphWindowChanged(bool visible);
public slots:
void installDriver();
void removeDriver();
bool installDriver();
bool removeDriver();
void show();

View File

@ -21,14 +21,14 @@ RpcManager *DriverManagerRpc::rpcManager() const
return fortManager()->rpcManager();
}
void DriverManagerRpc::reinstallDriver()
bool DriverManagerRpc::reinstallDriver()
{
rpcManager()->invokeOnServer(Control::Rpc_DriverManager_reinstallDriver);
return rpcManager()->doOnServer(Control::Rpc_DriverManager_reinstallDriver);
}
void DriverManagerRpc::uninstallDriver()
bool DriverManagerRpc::uninstallDriver()
{
rpcManager()->invokeOnServer(Control::Rpc_DriverManager_uninstallDriver);
return rpcManager()->doOnServer(Control::Rpc_DriverManager_uninstallDriver);
}
void DriverManagerRpc::updateState(quint32 errorCode, bool isDeviceOpened)

View File

@ -21,8 +21,8 @@ public:
void initialize() override { }
void reinstallDriver() override;
void uninstallDriver() override;
bool reinstallDriver() override;
bool uninstallDriver() override;
void updateState(quint32 errorCode, bool isDeviceOpened);

View File

@ -184,14 +184,20 @@ void RpcManager::sendResult(ControlWorker *w, bool ok, const QVariantList &args)
w->sendCommand(ok ? Control::Rpc_Result_Ok : Control::Rpc_Result_Error, args);
}
void RpcManager::invokeOnServer(Control::Command cmd, const QVariantList &args)
bool RpcManager::invokeOnServer(Control::Command cmd, const QVariantList &args)
{
client()->sendCommand(cmd, args);
return client()->sendCommand(cmd, args);
}
bool RpcManager::doOnServer(Control::Command cmd, const QVariantList &args)
{
invokeOnServer(cmd, args);
if (!client()->isConnected()) {
fortManager()->showErrorBox("Service isn't available.");
return false;
}
if (!invokeOnServer(cmd, args))
return false;
if (!waitResult()) {
fortManager()->showErrorBox("Service isn't responding.");
@ -280,7 +286,7 @@ bool RpcManager::processManagerRpc(
return processConfManagerRpc(w, cmd, args);
case Control::Rpc_DriverManager:
return processDriverManagerRpc(cmd, args);
return processDriverManagerRpc(w, cmd, args);
case Control::Rpc_QuotaManager:
return processQuotaManagerRpc(cmd, args);
@ -398,14 +404,15 @@ bool RpcManager::processConfManagerRpc(
}
}
bool RpcManager::processDriverManagerRpc(Control::Command cmd, const QVariantList &args)
bool RpcManager::processDriverManagerRpc(
ControlWorker *w, Control::Command cmd, const QVariantList &args)
{
switch (cmd) {
case Control::Rpc_DriverManager_reinstallDriver:
fortManager()->installDriver();
sendResult(w, fortManager()->installDriver());
return true;
case Control::Rpc_DriverManager_uninstallDriver:
fortManager()->removeDriver();
sendResult(w, fortManager()->removeDriver());
return true;
case Control::Rpc_DriverManager_updateState:
if (auto dm = qobject_cast<DriverManagerRpc *>(driverManager())) {

View File

@ -43,7 +43,7 @@ public:
bool waitResult();
void sendResult(ControlWorker *w, bool ok, const QVariantList &args = {});
void invokeOnServer(Control::Command cmd, const QVariantList &args = {});
bool invokeOnServer(Control::Command cmd, const QVariantList &args = {});
bool doOnServer(Control::Command cmd, const QVariantList &args = {});
bool processCommandRpc(ControlWorker *w, Control::Command cmd, const QVariantList &args,
@ -72,7 +72,7 @@ private:
bool processAppInfoManagerRpc(Control::Command cmd, const QVariantList &args);
bool processConfManagerRpc(ControlWorker *w, Control::Command cmd, const QVariantList &args);
bool processDriverManagerRpc(Control::Command cmd, const QVariantList &args);
bool processDriverManagerRpc(ControlWorker *w, Control::Command cmd, const QVariantList &args);
bool processQuotaManagerRpc(Control::Command cmd, const QVariantList &args);
bool processStatManagerRpc(ControlWorker *w, Control::Command cmd, const QVariantList &args);
bool processTaskManagerRpc(Control::Command cmd, const QVariantList &args);