File indexing completed on 2024-05-12 05:26:00

0001 /*
0002  * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 #include "inspector.h"
0021 
0022 #include "resourcecontext.h"
0023 #include "inspection_generated.h"
0024 #include "bufferutils.h"
0025 
0026 #include <QDataStream>
0027 
0028 using namespace Sink;
0029 
0030 Inspector::Inspector(const ResourceContext &context)
0031     : QObject(),
0032     mResourceContext(context)
0033 {
0034 }
0035 
0036 Inspector::~Inspector()
0037 {
0038 
0039 }
0040 
0041 void Inspector::setSecret(const QString &s)
0042 {
0043     mSecret = s;
0044 }
0045 
0046 QString Inspector::secret() const
0047 {
0048     return mSecret;
0049 }
0050 
0051 KAsync::Job<void> Inspector::processCommand(void const *command, size_t size)
0052 {
0053     flatbuffers::Verifier verifier((const uint8_t *)command, size);
0054     if (Sink::Commands::VerifyInspectionBuffer(verifier)) {
0055         auto buffer = Sink::Commands::GetInspection(command);
0056         int inspectionType = buffer->type();
0057 
0058         QByteArray inspectionId = BufferUtils::extractBuffer(buffer->id());
0059         QByteArray entityId = BufferUtils::extractBuffer(buffer->entityId());
0060         QByteArray domainType = BufferUtils::extractBuffer(buffer->domainType());
0061         QByteArray property = BufferUtils::extractBuffer(buffer->property());
0062         QByteArray expectedValueString = BufferUtils::extractBuffer(buffer->expectedValue());
0063         QDataStream s(expectedValueString);
0064         QVariant expectedValue;
0065         s >> expectedValue;
0066         inspect(inspectionType, inspectionId, domainType, entityId, property, expectedValue)
0067             .then<void>(
0068                 [=](const KAsync::Error &error) {
0069                     Sink::Notification n;
0070                     n.type = Sink::Notification::Inspection;
0071                     n.id = inspectionId;
0072                     if (error) {
0073                         Warning_area("resource.inspection") << "Inspection failed: " << inspectionType << inspectionId << entityId << error.errorMessage;
0074                         n.code = Sink::Notification::Failure;
0075                     } else {
0076                         Log_area("resource.inspection") << "Inspection was successful: " << inspectionType << inspectionId << entityId;
0077                         n.code = Sink::Notification::Success;
0078                     }
0079                     emit notify(n);
0080                     return KAsync::null();
0081                 })
0082             .exec();
0083         return KAsync::null<void>();
0084     }
0085     return KAsync::error<void>(-1, "Invalid inspection command.");
0086 }
0087 
0088 KAsync::Job<void> Inspector::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue)
0089 {
0090     return KAsync::error(-1, "Inspection not implemented.");
0091 }
0092