UI: RPC: Wait result.

This commit is contained in:
Nodir Temirkhodjaev 2021-05-03 17:18:52 +03:00
parent 2e53990ce4
commit 22677b1217
6 changed files with 52 additions and 5 deletions

View File

@ -9,6 +9,8 @@ enum Command : qint8 {
CommandNone = 0, CommandNone = 0,
Conf, Conf,
Prog, Prog,
Rpc_Result_Ok,
Rpc_Result_Error,
Rpc_RpcManager_initClient, Rpc_RpcManager_initClient,
Rpc_AppInfoManager_lookupAppInfo, Rpc_AppInfoManager_lookupAppInfo,
Rpc_AppInfoManager_checkLookupFinished, Rpc_AppInfoManager_checkLookupFinished,

View File

@ -99,6 +99,8 @@ bool ControlWorker::sendCommand(Control::Command command, const QVariantList &ar
socket()->write(buffer); socket()->write(buffer);
} }
socket()->flush();
return true; return true;
} }
@ -107,6 +109,11 @@ bool ControlWorker::waitForSent(int msecs) const
return socket()->waitForBytesWritten(msecs); return socket()->waitForBytesWritten(msecs);
} }
bool ControlWorker::waitForRead(int msecs) const
{
return socket()->waitForReadyRead(msecs);
}
void ControlWorker::processRequest() void ControlWorker::processRequest()
{ {
if (!readRequest()) { if (!readRequest()) {

View File

@ -25,9 +25,10 @@ public:
void setupForAsync(); void setupForAsync();
bool sendCommand(Control::Command command, const QVariantList &args); bool sendCommand(Control::Command command, const QVariantList &args = {});
bool waitForSent(int msecs = 1000) const; bool waitForSent(int msecs = 1000) const;
bool waitForRead(int msecs = 1000) const;
static QVariantList buildArgs(const QStringList &list); static QVariantList buildArgs(const QStringList &list);

View File

@ -33,8 +33,17 @@ void ConfManagerRpc::setupAppEndTimer() { }
bool ConfManagerRpc::saveToDbIni(FirewallConf &newConf, bool onlyFlags) bool ConfManagerRpc::saveToDbIni(FirewallConf &newConf, bool onlyFlags)
{ {
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_save, rpcManager()->invokeOnServer(Control::Rpc_ConfManager_save,
{ newConf.toVariant(onlyFlags), onlyFlags, confVersion() }); { newConf.toVariant(onlyFlags), confVersion(), onlyFlags });
// TODO: get result
// showErrorMessage("Save Conf: Service error."); if (!rpcManager()->waitResult()) {
showErrorMessage("Save Conf: Service isn't responding.");
return false;
}
if (rpcManager()->resultCommand() != Control::Rpc_Result_Ok) {
showErrorMessage("Save Conf: Service error.");
return false;
}
return true; return true;
} }

View File

@ -117,6 +117,23 @@ void RpcManager::invokeOnServer(Control::Command cmd, const QVariantList &args)
client()->sendCommand(cmd, args); client()->sendCommand(cmd, args);
} }
bool RpcManager::waitResult()
{
m_resultCommand = Control::CommandNone;
do {
if (!client()->waitForRead())
return false;
} while (m_resultCommand == Control::CommandNone);
return true;
}
void RpcManager::sendResult(ControlWorker *w, bool ok)
{
w->sendCommand(ok ? Control::Rpc_Result_Ok : Control::Rpc_Result_Error);
}
void RpcManager::invokeOnClients(Control::Command cmd, const QVariantList &args) void RpcManager::invokeOnClients(Control::Command cmd, const QVariantList &args)
{ {
const auto clients = controlManager()->clients(); const auto clients = controlManager()->clients();
@ -149,6 +166,10 @@ bool RpcManager::processCommandRpc(
ControlWorker *w, Control::Command cmd, const QVariantList &args, QString &errorMessage) ControlWorker *w, Control::Command cmd, const QVariantList &args, QString &errorMessage)
{ {
switch (cmd) { switch (cmd) {
case Control::Rpc_Result_Ok:
case Control::Rpc_Result_Error:
m_resultCommand = cmd;
return true;
case Control::Rpc_RpcManager_initClient: case Control::Rpc_RpcManager_initClient:
initClientOnServer(w); initClientOnServer(w);
return true; return true;
@ -166,7 +187,7 @@ bool RpcManager::processCommandRpc(
case Control::Rpc_ConfManager_save: { case Control::Rpc_ConfManager_save: {
const bool ok = confManager()->saveVariant( const bool ok = confManager()->saveVariant(
args.value(0), args.value(1).toInt(), args.value(2).toBool()); args.value(0), args.value(1).toInt(), args.value(2).toBool());
w->sendCommand(Control::Rpc_ConfManager_saveResult, { ok }); sendResult(w, ok);
return true; return true;
} }
case Control::Rpc_DriverManager_updateState: case Control::Rpc_DriverManager_updateState:

View File

@ -24,6 +24,8 @@ class RpcManager : public QObject
public: public:
explicit RpcManager(FortManager *fortManager, QObject *parent = nullptr); explicit RpcManager(FortManager *fortManager, QObject *parent = nullptr);
Control::Command resultCommand() const { return m_resultCommand; }
FortManager *fortManager() const { return m_fortManager; } FortManager *fortManager() const { return m_fortManager; }
FortSettings *settings() const; FortSettings *settings() const;
ControlManager *controlManager() const; ControlManager *controlManager() const;
@ -39,6 +41,9 @@ public:
void invokeOnServer(Control::Command cmd, const QVariantList &args = {}); void invokeOnServer(Control::Command cmd, const QVariantList &args = {});
bool waitResult();
void sendResult(ControlWorker *w, bool ok);
bool processCommandRpc(ControlWorker *w, Control::Command cmd, const QVariantList &args, bool processCommandRpc(ControlWorker *w, Control::Command cmd, const QVariantList &args,
QString &errorMessage); QString &errorMessage);
@ -59,6 +64,8 @@ private:
QVariantList driverManager_updateState_args() const; QVariantList driverManager_updateState_args() const;
private: private:
Control::Command m_resultCommand = Control::CommandNone;
FortManager *m_fortManager = nullptr; FortManager *m_fortManager = nullptr;
ControlWorker *m_client = nullptr; ControlWorker *m_client = nullptr;