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 "RiaGrpcServer.h"
|
|
|
|
|
|
|
|
#include "RiaApplication.h"
|
|
|
|
#include "RiaDefines.h"
|
|
|
|
|
|
|
|
#include "RiaGrpcCallbacks.h"
|
|
|
|
#include "RiaGrpcServiceInterface.h"
|
2019-06-03 07:33:16 -05:00
|
|
|
#include "RiaGrpcCaseService.h"
|
2019-05-20 06:21:02 -05:00
|
|
|
|
|
|
|
#include "RigCaseCellResultsData.h"
|
|
|
|
#include "RigMainGrid.h"
|
|
|
|
#include "RimEclipseCase.h"
|
|
|
|
#include "RimProject.h"
|
|
|
|
|
|
|
|
#include "cafAssert.h"
|
2019-05-20 07:32:29 -05:00
|
|
|
#include "cafProgressInfo.h"
|
2019-05-20 06:21:02 -05:00
|
|
|
|
|
|
|
#include <grpc/support/log.h>
|
|
|
|
#include <grpcpp/grpcpp.h>
|
|
|
|
|
|
|
|
#include <QTcpServer>
|
|
|
|
|
|
|
|
using grpc::CompletionQueue;
|
|
|
|
using grpc::Server;
|
|
|
|
using grpc::ServerAsyncResponseWriter;
|
|
|
|
using grpc::ServerBuilder;
|
|
|
|
using grpc::ServerCompletionQueue;
|
|
|
|
using grpc::ServerContext;
|
|
|
|
using grpc::Status;
|
|
|
|
|
|
|
|
//==================================================================================================
|
|
|
|
//
|
|
|
|
// The GRPC server implementation
|
|
|
|
//
|
|
|
|
//==================================================================================================
|
|
|
|
class RiaGrpcServerImpl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RiaGrpcServerImpl(int portNumber);
|
|
|
|
~RiaGrpcServerImpl();
|
|
|
|
int portNumber() const;
|
|
|
|
bool isRunning() const;
|
|
|
|
void run();
|
|
|
|
void runInThread();
|
|
|
|
void initialize();
|
2019-05-23 02:58:04 -05:00
|
|
|
void processAllQueuedRequests();
|
2019-05-20 06:21:02 -05:00
|
|
|
void quit();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void waitForNextRequest();
|
2019-05-27 00:29:20 -05:00
|
|
|
void process(RiaGrpcCallbackInterface* method);
|
2019-05-20 06:21:02 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_portNumber;
|
|
|
|
std::unique_ptr<grpc::ServerCompletionQueue> m_completionQueue;
|
|
|
|
std::unique_ptr<grpc::Server> m_server;
|
|
|
|
std::list<std::shared_ptr<RiaGrpcServiceInterface>> m_services;
|
2019-05-27 00:29:20 -05:00
|
|
|
std::list<RiaGrpcCallbackInterface*> m_unprocessedRequests;
|
2019-05-20 06:21:02 -05:00
|
|
|
std::mutex m_requestMutex;
|
|
|
|
std::thread m_thread;
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RiaGrpcServerImpl::RiaGrpcServerImpl(int portNumber)
|
|
|
|
: m_portNumber(portNumber)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RiaGrpcServerImpl::~RiaGrpcServerImpl()
|
|
|
|
{
|
|
|
|
quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
int RiaGrpcServerImpl::portNumber() const
|
|
|
|
{
|
|
|
|
return m_portNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RiaGrpcServerImpl::isRunning() const
|
|
|
|
{
|
|
|
|
return m_server != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServerImpl::run()
|
|
|
|
{
|
|
|
|
initialize();
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
waitForNextRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServerImpl::runInThread()
|
|
|
|
{
|
|
|
|
initialize();
|
|
|
|
m_thread = std::thread(&RiaGrpcServerImpl::waitForNextRequest, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServerImpl::initialize()
|
|
|
|
{
|
|
|
|
CAF_ASSERT(m_portNumber > 0 && m_portNumber <= (int) std::numeric_limits<quint16>::max());
|
|
|
|
|
|
|
|
QString serverAddress = QString("localhost:%1").arg(m_portNumber);
|
|
|
|
|
|
|
|
ServerBuilder builder;
|
|
|
|
builder.AddListeningPort(serverAddress.toStdString(), grpc::InsecureServerCredentials());
|
|
|
|
|
|
|
|
for (auto key : RiaGrpcServiceFactory::instance()->allKeys())
|
|
|
|
{
|
|
|
|
std::shared_ptr<RiaGrpcServiceInterface> service(RiaGrpcServiceFactory::instance()->create(key));
|
|
|
|
builder.RegisterService(dynamic_cast<grpc::Service*>(service.get()));
|
|
|
|
m_services.push_back(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_completionQueue = builder.AddCompletionQueue();
|
|
|
|
m_server = builder.BuildAndStart();
|
|
|
|
|
|
|
|
CVF_ASSERT(m_server);
|
|
|
|
RiaLogging::info(QString("Server listening on %1").arg(serverAddress));
|
|
|
|
|
|
|
|
// Spawn new CallData instances to serve new clients.
|
|
|
|
for (auto service : m_services)
|
|
|
|
{
|
|
|
|
for (auto callback : service->createCallbacks())
|
|
|
|
{
|
|
|
|
process(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-05-23 02:58:04 -05:00
|
|
|
void RiaGrpcServerImpl::processAllQueuedRequests()
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-05-27 00:29:20 -05:00
|
|
|
std::list<RiaGrpcCallbackInterface*> waitingRequests;
|
|
|
|
{
|
|
|
|
// Block only while transferring the unprocessed requests to a local function list
|
|
|
|
std::lock_guard<std::mutex> requestLock(m_requestMutex);
|
|
|
|
waitingRequests.swap(m_unprocessedRequests);
|
|
|
|
}
|
|
|
|
// Now free to receive new requests from client while processing the current ones.
|
|
|
|
while (!waitingRequests.empty())
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-05-27 00:29:20 -05:00
|
|
|
RiaGrpcCallbackInterface* method = waitingRequests.front();
|
|
|
|
waitingRequests.pop_front();
|
2019-05-20 06:21:02 -05:00
|
|
|
process(method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-05-23 02:58:04 -05:00
|
|
|
/// Gracefully shut down the GRPC server.
|
|
|
|
/// BE VERY CAREFUL ABOUT CHANGING THE ORDER IN THIS METHOD. IT IS IMPORTANT!
|
2019-05-20 06:21:02 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServerImpl::quit()
|
|
|
|
{
|
|
|
|
if (m_server)
|
|
|
|
{
|
|
|
|
RiaLogging::info("Shutting down gRPC server");
|
|
|
|
// Clear unhandled requests
|
|
|
|
while (!m_unprocessedRequests.empty())
|
|
|
|
{
|
2019-05-27 00:29:20 -05:00
|
|
|
RiaGrpcCallbackInterface* method = m_unprocessedRequests.front();
|
2019-05-20 06:21:02 -05:00
|
|
|
m_unprocessedRequests.pop_front();
|
|
|
|
delete method;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown server and queue
|
|
|
|
m_server->Shutdown();
|
|
|
|
m_completionQueue->Shutdown();
|
|
|
|
|
|
|
|
// Wait for thread to join after handling the shutdown call
|
|
|
|
m_thread.join();
|
|
|
|
|
|
|
|
// Must destroy server before services
|
|
|
|
m_server.reset();
|
|
|
|
m_completionQueue.reset();
|
|
|
|
|
|
|
|
// Finally clear services
|
|
|
|
m_services.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-05-23 02:58:04 -05:00
|
|
|
/// Block and wait for requests from the client from the command queue.
|
|
|
|
/// The requests are pushed onto the Unprocessed Request queue which are handled in processRequests
|
2019-05-20 06:21:02 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServerImpl::waitForNextRequest()
|
|
|
|
{
|
|
|
|
void* tag;
|
|
|
|
bool ok = false;
|
|
|
|
|
|
|
|
while (m_completionQueue->Next(&tag, &ok))
|
|
|
|
{
|
2019-05-30 11:52:16 -05:00
|
|
|
std::lock_guard<std::mutex> requestLock(m_requestMutex);
|
2019-05-27 00:29:20 -05:00
|
|
|
RiaGrpcCallbackInterface* method = static_cast<RiaGrpcCallbackInterface*>(tag);
|
2019-05-30 11:52:16 -05:00
|
|
|
if (!ok)
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-05-30 11:52:16 -05:00
|
|
|
method->setNextCallState(RiaGrpcCallbackInterface::FINISH_REQUEST);
|
2019-05-20 06:21:02 -05:00
|
|
|
}
|
2019-05-30 11:52:16 -05:00
|
|
|
m_unprocessedRequests.push_back(method);
|
2019-05-20 06:21:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-05-23 02:58:04 -05:00
|
|
|
/// The handling of calls pushed onto the command queue. We only get one queued callback per client request.
|
|
|
|
/// The gRPC calls triggered in the callback will see each callback pushed back onto the command queue.
|
|
|
|
/// The call state will then determine what the callback should do next.
|
2019-05-20 06:21:02 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-05-27 00:29:20 -05:00
|
|
|
void RiaGrpcServerImpl::process(RiaGrpcCallbackInterface* method)
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-05-27 00:29:20 -05:00
|
|
|
if (method->callState() == RiaGrpcCallbackInterface::CREATE_HANDLER)
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
|
|
|
method->createRequestHandler(m_completionQueue.get());
|
|
|
|
}
|
2019-05-27 00:29:20 -05:00
|
|
|
else if (method->callState() == RiaGrpcCallbackInterface::INIT_REQUEST_STARTED)
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-05-22 08:30:09 -05:00
|
|
|
method->onInitRequestStarted();
|
|
|
|
}
|
2019-05-27 00:29:20 -05:00
|
|
|
else if (method->callState() == RiaGrpcCallbackInterface::INIT_REQUEST_COMPLETED)
|
2019-05-22 08:30:09 -05:00
|
|
|
{
|
|
|
|
method->onInitRequestCompleted();
|
2019-05-20 06:21:02 -05:00
|
|
|
}
|
2019-05-27 00:29:20 -05:00
|
|
|
else if (method->callState() == RiaGrpcCallbackInterface::PROCESS_REQUEST)
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-05-22 08:30:09 -05:00
|
|
|
method->onProcessRequest();
|
2019-05-20 06:21:02 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-22 08:30:09 -05:00
|
|
|
method->onFinishRequest();
|
2019-05-20 06:21:02 -05:00
|
|
|
process(method->createNewFromThis());
|
|
|
|
delete method;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RiaGrpcServer::RiaGrpcServer(int portNumber)
|
|
|
|
{
|
|
|
|
m_serverImpl = new RiaGrpcServerImpl(portNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RiaGrpcServer::~RiaGrpcServer()
|
|
|
|
{
|
|
|
|
delete m_serverImpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
int RiaGrpcServer::portNumber() const
|
|
|
|
{
|
|
|
|
if (m_serverImpl) return m_serverImpl->portNumber();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RiaGrpcServer::isRunning() const
|
|
|
|
{
|
|
|
|
if (m_serverImpl) return m_serverImpl->isRunning();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServer::run()
|
|
|
|
{
|
|
|
|
m_serverImpl->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServer::runInThread()
|
|
|
|
{
|
|
|
|
m_serverImpl->runInThread();
|
|
|
|
}
|
|
|
|
|
2019-06-04 05:59:06 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RiaGrpcServer::s_receivedExitRequest = false;
|
|
|
|
|
2019-05-20 06:21:02 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServer::initialize()
|
|
|
|
{
|
|
|
|
m_serverImpl->initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-05-23 02:58:04 -05:00
|
|
|
void RiaGrpcServer::processAllQueuedRequests()
|
2019-05-20 06:21:02 -05:00
|
|
|
{
|
2019-05-23 02:58:04 -05:00
|
|
|
m_serverImpl->processAllQueuedRequests();
|
2019-05-20 06:21:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServer::quit()
|
|
|
|
{
|
|
|
|
if (m_serverImpl)
|
|
|
|
m_serverImpl->quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
int RiaGrpcServer::findAvailablePortNumber(int defaultPortNumber)
|
|
|
|
{
|
|
|
|
int startPort = 50051;
|
|
|
|
|
|
|
|
if (defaultPortNumber > 0 && defaultPortNumber < (int)std::numeric_limits<quint16>::max())
|
|
|
|
{
|
|
|
|
startPort = defaultPortNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
int endPort = std::min(startPort + 100, (int)std::numeric_limits<quint16>::max());
|
|
|
|
|
|
|
|
QTcpServer serverTest;
|
|
|
|
quint16 port = static_cast<quint16>(startPort);
|
|
|
|
for (; port <= static_cast<quint16>(endPort); ++port)
|
|
|
|
{
|
|
|
|
if (serverTest.listen(QHostAddress::LocalHost, port))
|
|
|
|
{
|
|
|
|
return static_cast<int>(port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2019-06-04 05:59:06 -05:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RiaGrpcServer::setReceivedExitRequest()
|
|
|
|
{
|
|
|
|
RiaLogging::info("Received Exit Request");
|
|
|
|
s_receivedExitRequest = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RiaGrpcServer::receivedExitRequest()
|
|
|
|
{
|
|
|
|
return s_receivedExitRequest;
|
|
|
|
}
|