2019-05-20 06:21:02 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (C) 2019- Equinor ASA
|
|
|
|
//
|
|
|
|
// ResInsight is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
//
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "RiaGrpcCommandService.h"
|
|
|
|
|
|
|
|
#include "RiaGrpcCallbacks.h"
|
|
|
|
|
2019-07-16 10:12:08 -05:00
|
|
|
#include "RicfReplaceCase.h"
|
2019-05-20 06:21:02 -05:00
|
|
|
#include "RicfSetTimeStep.h"
|
|
|
|
|
|
|
|
#include "cafAssert.h"
|
|
|
|
#include "cafPdmDefaultObjectFactory.h"
|
2019-05-24 05:19:44 -05:00
|
|
|
#include "cafPdmDataValueField.h"
|
2019-05-20 06:21:02 -05:00
|
|
|
#include "cafPdmValueField.h"
|
2019-05-24 05:19:44 -05:00
|
|
|
#include <google/protobuf/reflection.h>
|
2019-05-20 06:21:02 -05:00
|
|
|
|
|
|
|
using namespace rips;
|
|
|
|
using namespace google::protobuf;
|
|
|
|
|
2019-05-27 00:29:20 -05:00
|
|
|
// Windows may define GetMessage as a Macro and this is in direct conflict with the gRPC GetMessage calls.
|
2019-05-20 06:21:02 -05:00
|
|
|
#ifdef WIN32
|
|
|
|
#ifdef GetMessage
|
|
|
|
#undef GetMessage
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-05-23 09:46:50 -05:00
|
|
|
grpc::Status RiaGrpcCommandService::Execute(grpc::ServerContext* context, const CommandParams* request, CommandReply* reply)
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
|
|
|
auto requestDescriptor = request->GetDescriptor();
|
|
|
|
|
|
|
|
CommandParams::ParamsCase paramsCase = request->params_case();
|
|
|
|
if (paramsCase != CommandParams::PARAMS_NOT_SET)
|
|
|
|
{
|
|
|
|
auto grpcOneOfMessage = requestDescriptor->FindFieldByNumber((int)paramsCase);
|
|
|
|
CAF_ASSERT(grpcOneOfMessage->type() == FieldDescriptor::TYPE_MESSAGE);
|
|
|
|
|
|
|
|
const Message& params = request->GetReflection()->GetMessage(*request, grpcOneOfMessage);
|
|
|
|
QString grpcOneOfMessageName = QString::fromStdString(grpcOneOfMessage->name());
|
2019-07-16 10:12:08 -05:00
|
|
|
auto pdmObjectHandle = caf::PdmDefaultObjectFactory::instance()->create(grpcOneOfMessageName);
|
|
|
|
auto commandHandle = dynamic_cast<RicfCommandObject*>(pdmObjectHandle);
|
|
|
|
|
2019-05-20 06:21:02 -05:00
|
|
|
if (commandHandle)
|
|
|
|
{
|
2019-07-16 10:12:08 -05:00
|
|
|
RicfMultiCaseReplace* multiCaseReplaceCommand = dynamic_cast<RicfMultiCaseReplace*>(commandHandle);
|
|
|
|
if (multiCaseReplaceCommand)
|
|
|
|
{
|
|
|
|
CAF_ASSERT(request->has_replacemultiplecases());
|
|
|
|
auto replaceMultipleCasesRequest = request->replacemultiplecases();
|
|
|
|
std::map<int, QString> caseIdFileMap;
|
|
|
|
for (auto caseGridFilePair : replaceMultipleCasesRequest.casepairs())
|
|
|
|
{
|
|
|
|
caseIdFileMap.insert(std::make_pair(caseGridFilePair.caseid(),
|
|
|
|
QString::fromStdString(caseGridFilePair.newgridfile())));
|
|
|
|
}
|
|
|
|
multiCaseReplaceCommand->setCaseReplacePairs(caseIdFileMap);
|
|
|
|
}
|
|
|
|
else
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-07-16 10:12:08 -05:00
|
|
|
auto subMessageDescriptor = grpcOneOfMessage->message_type();
|
|
|
|
int numParameters = subMessageDescriptor->field_count();
|
|
|
|
for (int i = 0; i < numParameters; ++i)
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-07-16 10:12:08 -05:00
|
|
|
auto parameter = subMessageDescriptor->field(i);
|
|
|
|
if (parameter)
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-07-16 10:12:08 -05:00
|
|
|
QString parameterName = QString::fromStdString(parameter->name());
|
|
|
|
auto pdmValueFieldHandle = dynamic_cast<caf::PdmValueField*>(pdmObjectHandle->findField(parameterName));
|
|
|
|
if (pdmValueFieldHandle)
|
|
|
|
{
|
|
|
|
assignPdmFieldValue(pdmValueFieldHandle, params, parameter);
|
|
|
|
}
|
2019-05-20 06:21:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 06:59:19 -05:00
|
|
|
RicfCommandResponse response = commandHandle->execute();
|
|
|
|
if (response.status() == RicfCommandResponse::COMMAND_ERROR)
|
|
|
|
{
|
2019-05-23 09:46:50 -05:00
|
|
|
return grpc::Status(grpc::FAILED_PRECONDITION, response.message().toStdString());
|
2019-05-23 06:59:19 -05:00
|
|
|
}
|
|
|
|
else if (response.status() == RicfCommandResponse::COMMAND_WARNING)
|
|
|
|
{
|
|
|
|
context->AddInitialMetadata("warning", response.message().toStdString());
|
|
|
|
}
|
2019-05-23 09:46:50 -05:00
|
|
|
|
|
|
|
assignResultToReply(response.result(), reply);
|
|
|
|
|
2019-05-20 06:21:02 -05:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return grpc::Status(grpc::NOT_FOUND, "Command not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-05-27 00:29:20 -05:00
|
|
|
std::vector<RiaGrpcCallbackInterface*> RiaGrpcCommandService::createCallbacks()
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
|
|
|
typedef RiaGrpcCommandService Self;
|
|
|
|
|
2019-05-27 00:29:20 -05:00
|
|
|
return {new RiaGrpcUnaryCallback<Self, CommandParams, CommandReply>(this, &Self::Execute, &Self::RequestExecute)};
|
2019-05-20 06:21:02 -05:00
|
|
|
}
|
|
|
|
|
2019-05-24 05:19:44 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
|
|
caf::PdmField<T>* RiaGrpcCommandService::dataValueField(caf::PdmValueField* valueField)
|
|
|
|
{
|
|
|
|
caf::PdmField<T>* dataValField = dynamic_cast<caf::PdmField<T>*>(valueField);
|
|
|
|
CAF_ASSERT(dataValField);
|
|
|
|
return dataValField;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
|
|
const caf::PdmField<T>* RiaGrpcCommandService::constDataValueField(const caf::PdmValueField* valueField)
|
|
|
|
{
|
|
|
|
const caf::PdmField<T>* dataValField = dynamic_cast<const caf::PdmField<T>*>(valueField);
|
|
|
|
CAF_ASSERT(dataValField);
|
|
|
|
return dataValField;
|
|
|
|
}
|
|
|
|
|
2019-05-20 06:21:02 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-05-23 09:46:50 -05:00
|
|
|
void RiaGrpcCommandService::assignPdmFieldValue(caf::PdmValueField* pdmValueField,
|
|
|
|
const Message& params,
|
|
|
|
const FieldDescriptor* paramDescriptor)
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
|
|
|
FieldDescriptor::Type fieldDataType = paramDescriptor->type();
|
2019-05-24 05:19:44 -05:00
|
|
|
const Reflection* reflection = params.GetReflection();
|
|
|
|
|
2019-05-20 06:21:02 -05:00
|
|
|
switch (fieldDataType)
|
|
|
|
{
|
|
|
|
case FieldDescriptor::TYPE_BOOL: {
|
2019-05-24 05:19:44 -05:00
|
|
|
auto value = reflection->GetBool(params, paramDescriptor);
|
|
|
|
auto dataField = dataValueField<bool>(pdmValueField);
|
|
|
|
dataField->setValue(value);
|
2019-05-20 06:21:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_INT32: {
|
2019-05-24 05:19:44 -05:00
|
|
|
if (paramDescriptor->is_repeated())
|
|
|
|
{
|
|
|
|
RepeatedFieldRef<int> repeatedField =
|
|
|
|
reflection->GetRepeatedFieldRef<int>(params, paramDescriptor);
|
|
|
|
auto dataField = dataValueField<std::vector<int>>(pdmValueField);
|
|
|
|
dataField->setValue(std::vector<int>(repeatedField.begin(), repeatedField.end()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int value = reflection->GetInt32(params, paramDescriptor);
|
|
|
|
auto dataField = dataValueField<int>(pdmValueField);
|
|
|
|
dataField->setValue(value);
|
|
|
|
}
|
2019-05-20 06:21:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_UINT32: {
|
2019-05-24 05:19:44 -05:00
|
|
|
uint value = reflection->GetUInt32(params, paramDescriptor);
|
|
|
|
auto dataField = dataValueField<uint>(pdmValueField);
|
|
|
|
dataField->setValue(value);
|
2019-05-20 06:21:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_STRING: {
|
2019-05-24 05:19:44 -05:00
|
|
|
if (paramDescriptor->is_repeated())
|
|
|
|
{
|
|
|
|
RepeatedFieldRef<std::string> repeatedField =
|
|
|
|
reflection->GetRepeatedFieldRef<std::string>(params, paramDescriptor);
|
|
|
|
std::vector<QString> stringVector;
|
|
|
|
for (const std::string& string : repeatedField)
|
|
|
|
{
|
|
|
|
stringVector.push_back(QString::fromStdString(string));
|
|
|
|
}
|
|
|
|
auto dataField = dataValueField<std::vector<QString>>(pdmValueField);
|
|
|
|
dataField->setValue(stringVector);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto value = QString::fromStdString(reflection->GetString(params, paramDescriptor));
|
|
|
|
auto dataField = dataValueField<QString>(pdmValueField);
|
|
|
|
dataField->setValue(value);
|
|
|
|
}
|
2019-05-20 06:21:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_FLOAT: {
|
2019-05-24 05:19:44 -05:00
|
|
|
auto value = reflection->GetFloat(params, paramDescriptor);
|
|
|
|
auto dataField = dataValueField<float>(pdmValueField);
|
|
|
|
dataField->setValue(value);
|
2019-05-20 06:21:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_DOUBLE: {
|
2019-05-24 05:19:44 -05:00
|
|
|
auto value = reflection->GetDouble(params, paramDescriptor);
|
|
|
|
auto dataField = dataValueField<double>(pdmValueField);
|
|
|
|
dataField->setValue(value);
|
2019-05-20 06:21:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_ENUM: {
|
2019-05-24 05:19:44 -05:00
|
|
|
auto value = reflection->GetEnumValue(params, paramDescriptor);
|
|
|
|
pdmValueField->setFromQVariant(QVariant(value));
|
2019-05-20 06:21:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 09:46:50 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcCommandService::assignGrpcFieldValue(google::protobuf::Message* reply,
|
|
|
|
const google::protobuf::FieldDescriptor* fieldDescriptor,
|
|
|
|
const caf::PdmValueField* pdmValueField)
|
|
|
|
{
|
|
|
|
FieldDescriptor::Type fieldDataType = fieldDescriptor->type();
|
|
|
|
QVariant qValue = pdmValueField->toQVariant();
|
2019-05-24 05:19:44 -05:00
|
|
|
|
|
|
|
auto reflection = reply->GetReflection();
|
2019-05-23 09:46:50 -05:00
|
|
|
switch (fieldDataType)
|
|
|
|
{
|
|
|
|
case FieldDescriptor::TYPE_BOOL: {
|
2019-05-24 05:19:44 -05:00
|
|
|
reflection->SetBool(reply, fieldDescriptor, qValue.toBool());
|
2019-05-23 09:46:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_INT32: {
|
2019-05-24 05:19:44 -05:00
|
|
|
reflection->SetInt32(reply, fieldDescriptor, qValue.toInt());
|
2019-05-23 09:46:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_UINT32: {
|
2019-05-24 05:19:44 -05:00
|
|
|
reflection->SetUInt32(reply, fieldDescriptor, qValue.toUInt());
|
2019-05-23 09:46:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_INT64: {
|
2019-05-24 05:19:44 -05:00
|
|
|
reflection->SetInt64(reply, fieldDescriptor, qValue.toLongLong());
|
2019-05-23 09:46:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_UINT64: {
|
2019-05-24 05:19:44 -05:00
|
|
|
reflection->SetUInt64(reply, fieldDescriptor, qValue.toULongLong());
|
2019-05-23 09:46:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_STRING: {
|
2019-05-24 05:19:44 -05:00
|
|
|
reflection->SetString(reply, fieldDescriptor, qValue.toString().toStdString());
|
2019-05-23 09:46:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_FLOAT: {
|
2019-05-24 05:19:44 -05:00
|
|
|
reflection->SetFloat(reply, fieldDescriptor, qValue.toFloat());
|
2019-05-23 09:46:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_DOUBLE: {
|
2019-05-24 05:19:44 -05:00
|
|
|
reflection->SetDouble(reply, fieldDescriptor, qValue.toDouble());
|
2019-05-23 09:46:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FieldDescriptor::TYPE_ENUM: {
|
2019-05-24 05:19:44 -05:00
|
|
|
reflection->SetEnumValue(reply, fieldDescriptor, qValue.toInt());
|
2019-05-23 09:46:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcCommandService::assignResultToReply(const caf::PdmObject* result, CommandReply* reply)
|
|
|
|
{
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
reply->set_allocated_emptyresult(new Empty);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString resultType = result->classKeyword();
|
|
|
|
|
|
|
|
auto replyDescriptor = reply->GetDescriptor();
|
|
|
|
auto oneofDescriptor = replyDescriptor->FindOneofByName("result");
|
|
|
|
const FieldDescriptor* matchingOneOf = nullptr;
|
|
|
|
for (int fieldIndex = 0; fieldIndex < oneofDescriptor->field_count(); ++fieldIndex)
|
|
|
|
{
|
|
|
|
auto fieldDescriptor = oneofDescriptor->field(fieldIndex);
|
|
|
|
if (fieldDescriptor->name() == resultType.toStdString())
|
|
|
|
{
|
|
|
|
matchingOneOf = fieldDescriptor;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CAF_ASSERT(matchingOneOf);
|
|
|
|
Message* message = reply->GetReflection()->MutableMessage(reply, matchingOneOf);
|
|
|
|
CAF_ASSERT(message);
|
|
|
|
auto resultDescriptor = message->GetDescriptor();
|
|
|
|
|
|
|
|
for (int fieldIndex = 0; fieldIndex < resultDescriptor->field_count(); ++fieldIndex)
|
|
|
|
{
|
|
|
|
auto fieldDescriptor = resultDescriptor->field(fieldIndex);
|
|
|
|
const auto pdmField =
|
|
|
|
dynamic_cast<const caf::PdmValueField*>(result->findField(QString::fromStdString(fieldDescriptor->name())));
|
|
|
|
assignGrpcFieldValue(message, fieldDescriptor, pdmField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 06:21:02 -05:00
|
|
|
static bool RiaGrpcCommandService_init =
|
|
|
|
RiaGrpcServiceFactory::instance()->registerCreator<RiaGrpcCommandService>(typeid(RiaGrpcCommandService).hash_code());
|