UI: RpcManager: Add doOnServer().

This commit is contained in:
Nodir Temirkhodjaev 2021-05-12 18:02:06 +03:00
parent f6dbfba287
commit 527d5eaaa3
9 changed files with 73 additions and 59 deletions

View File

@ -43,6 +43,7 @@ enum Command : qint8 {
Rpc_QuotaManager_alert,
Rpc_StatManager_clear,
Rpc_StatManager_appCreated,
Rpc_StatManager_trafficAdded,

View File

@ -21,37 +21,32 @@ bool ConfManagerRpc::addApp(const QString &appPath, const QString &appName,
const QDateTime &endTime, qint64 groupId, int groupIndex, bool useGroupPerm, bool blocked,
bool alerted)
{
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_addApp,
return rpcManager()->doOnServer(Control::Rpc_ConfManager_addApp,
{ appPath, appName, endTime, groupId, groupIndex, useGroupPerm, blocked, alerted });
return checkResult();
}
bool ConfManagerRpc::deleteApp(qint64 appId, const QString &appPath)
{
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_deleteApp, { appId, appPath });
return checkResult();
return rpcManager()->doOnServer(Control::Rpc_ConfManager_deleteApp, { appId, appPath });
}
bool ConfManagerRpc::updateApp(qint64 appId, const QString &appPath, const QString &appName,
const QDateTime &endTime, qint64 groupId, int groupIndex, bool useGroupPerm, bool blocked)
{
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_updateApp,
return rpcManager()->doOnServer(Control::Rpc_ConfManager_updateApp,
{ appId, appPath, appName, endTime, groupId, groupIndex, useGroupPerm, blocked });
return checkResult();
}
bool ConfManagerRpc::updateAppName(qint64 appId, const QString &appName)
{
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_updateAppName, { appId, appName });
return checkResult();
return rpcManager()->doOnServer(Control::Rpc_ConfManager_updateAppName, { appId, appName });
}
bool ConfManagerRpc::addZone(const QString &zoneName, const QString &sourceCode, const QString &url,
const QString &formData, bool enabled, bool customUrl, int &zoneId)
{
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_addZone,
{ zoneName, sourceCode, url, formData, enabled, customUrl });
if (!checkResult())
if (!rpcManager()->doOnServer(Control::Rpc_ConfManager_addZone,
{ zoneName, sourceCode, url, formData, enabled, customUrl }))
return false;
zoneId = rpcManager()->resultArgs().value(0).toInt();
@ -61,35 +56,32 @@ bool ConfManagerRpc::addZone(const QString &zoneName, const QString &sourceCode,
bool ConfManagerRpc::deleteZone(int zoneId)
{
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_deleteZone, { zoneId });
return checkResult();
return rpcManager()->doOnServer(Control::Rpc_ConfManager_deleteZone, { zoneId });
}
bool ConfManagerRpc::updateZone(int zoneId, const QString &zoneName, const QString &sourceCode,
const QString &url, const QString &formData, bool enabled, bool customUrl)
{
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_updateZone,
return rpcManager()->doOnServer(Control::Rpc_ConfManager_updateZone,
{ zoneId, zoneName, sourceCode, url, formData, enabled, customUrl });
return checkResult();
}
bool ConfManagerRpc::updateZoneName(int zoneId, const QString &zoneName)
{
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_updateZoneName, { zoneId, zoneName });
return checkResult();
return rpcManager()->doOnServer(Control::Rpc_ConfManager_updateZoneName, { zoneId, zoneName });
}
bool ConfManagerRpc::updateZoneEnabled(int zoneId, bool enabled)
{
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_updateZoneEnabled, { zoneId, enabled });
return checkResult();
return rpcManager()->doOnServer(
Control::Rpc_ConfManager_updateZoneEnabled, { zoneId, enabled });
}
bool ConfManagerRpc::saveConf(FirewallConf &newConf)
{
setSaving(true);
rpcManager()->invokeOnServer(Control::Rpc_ConfManager_save, { newConf.toVariant() });
const bool ok = checkResult();
const bool ok =
rpcManager()->doOnServer(Control::Rpc_ConfManager_save, { newConf.toVariant() });
setSaving(false);
if (!ok)
@ -109,21 +101,6 @@ bool ConfManagerRpc::saveConf(FirewallConf &newConf)
return true;
}
bool ConfManagerRpc::checkResult()
{
if (rpcManager()->waitResult()) {
showErrorMessage("Service isn't responding.");
return false;
}
if (rpcManager()->resultCommand() != Control::Rpc_Result_Ok) {
showErrorMessage("Service error.");
return false;
}
return true;
}
void ConfManagerRpc::onConfChanged(const QVariant &confVar)
{
settings()->clearCache();

View File

@ -42,8 +42,6 @@ protected:
bool saveConf(FirewallConf &newConf) override;
private:
bool checkResult();
bool saving() const { return m_saving; }
void setSaving(bool v) { m_saving = v; }

View File

@ -155,11 +155,6 @@ void RpcManager::setupClient()
invokeOnServer(Control::Rpc_RpcManager_initClient);
}
void RpcManager::invokeOnServer(Control::Command cmd, const QVariantList &args)
{
client()->sendCommand(cmd, args);
}
bool RpcManager::waitResult()
{
m_resultCommand = Control::CommandNone;
@ -177,6 +172,28 @@ 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)
{
client()->sendCommand(cmd, args);
}
bool RpcManager::doOnServer(Control::Command cmd, const QVariantList &args)
{
invokeOnServer(cmd, args);
if (!waitResult()) {
fortManager()->showErrorBox("Service isn't responding.");
return false;
}
if (resultCommand() != Control::Rpc_Result_Ok) {
fortManager()->showErrorBox("Service error.");
return false;
}
return true;
}
void RpcManager::invokeOnClients(Control::Command cmd, const QVariantList &args)
{
const QByteArray buffer = ControlWorker::buildCommandData(cmd, args);
@ -259,9 +276,13 @@ bool RpcManager::processCommandRpc(
case Control::Rpc_QuotaManager_alert:
return processQuotaManagerRpc(cmd, args);
case Control::Rpc_StatManager_clear:
if (!checkClientValidated(w))
return false;
Q_FALLTHROUGH();
case Control::Rpc_StatManager_appCreated:
case Control::Rpc_StatManager_trafficAdded:
return processStatManagerRpc(cmd, args);
return processStatManagerRpc(w, cmd, args);
case Control::Rpc_TaskManager_runTask:
case Control::Rpc_TaskManager_abortTask:
@ -410,19 +431,19 @@ bool RpcManager::processQuotaManagerRpc(Control::Command cmd, const QVariantList
}
}
bool RpcManager::processStatManagerRpc(Control::Command cmd, const QVariantList &args)
bool RpcManager::processStatManagerRpc(
ControlWorker *w, Control::Command cmd, const QVariantList &args)
{
switch (cmd) {
case Control::Rpc_StatManager_clear:
sendResult(w, statManager()->clear());
return true;
case Control::Rpc_StatManager_appCreated:
if (auto sm = qobject_cast<StatManagerRpc *>(statManager())) {
emit sm->appCreated(args.value(0).toLongLong(), args.value(1).toString());
}
emit statManager()->appCreated(args.value(0).toLongLong(), args.value(1).toString());
return true;
case Control::Rpc_StatManager_trafficAdded:
if (auto sm = qobject_cast<StatManagerRpc *>(statManager())) {
emit sm->trafficAdded(
args.value(0).toLongLong(), args.value(1).toUInt(), args.value(2).toUInt());
}
emit statManager()->trafficAdded(
args.value(0).toLongLong(), args.value(1).toUInt(), args.value(2).toUInt());
return true;
default:
return false;

View File

@ -40,11 +40,12 @@ public:
void initialize();
void invokeOnServer(Control::Command cmd, const QVariantList &args = {});
bool waitResult();
void sendResult(ControlWorker *w, bool ok, const QVariantList &args = {});
void 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,
QString &errorMessage);
@ -70,7 +71,7 @@ private:
bool processConfManagerRpc(ControlWorker *w, Control::Command cmd, const QVariantList &args);
bool processDriverManagerRpc(Control::Command cmd, const QVariantList &args);
bool processQuotaManagerRpc(Control::Command cmd, const QVariantList &args);
bool processStatManagerRpc(Control::Command cmd, const QVariantList &args);
bool processStatManagerRpc(ControlWorker *w, Control::Command cmd, const QVariantList &args);
bool processTaskManagerRpc(Control::Command cmd, const QVariantList &args);
private:

View File

@ -14,3 +14,13 @@ RpcManager *StatManagerRpc::rpcManager() const
{
return fortManager()->rpcManager();
}
bool StatManagerRpc::clear()
{
if (!rpcManager()->doOnServer(Control::Rpc_StatManager_clear))
return false;
// TODO: re-open the DB
return true;
}

View File

@ -19,6 +19,9 @@ public:
void setConf(const FirewallConf * /*conf*/) override { }
public slots:
bool clear() override;
private:
FortManager *m_fortManager = nullptr;
};

View File

@ -204,7 +204,7 @@ bool StatManager::updateTrafDay(qint64 unixTime)
return isNewDay;
}
void StatManager::clear()
bool StatManager::clear()
{
clearAppIdCache();
clearStmts();
@ -216,6 +216,8 @@ void StatManager::clear()
initialize();
quotaManager()->clear();
return true;
}
void StatManager::clearStmts()

View File

@ -69,13 +69,14 @@ signals:
void trafficAdded(qint64 unixTime, quint32 inBytes, quint32 outBytes);
public slots:
void clear();
virtual bool clear();
protected:
void setupConnBlockId();
private:
using QStmtList = QList<SqliteStmt *>;
void setupConnBlockId();
void setupByConf();
void setupActivePeriod();