NRP Core  1.4.1
nrp_core_server.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef NRP_CORE_SERVER_H
23 #define NRP_CORE_SERVER_H
24 
25 #include <thread>
26 #include <mutex>
27 #include <condition_variable>
28 #include <grpcpp/grpcpp.h>
29 
30 #include "nrp_protobuf/nrp_server.grpc.pb.h"
33 
51 class NrpCoreServer : public NrpCore::NrpCore::Service
52 {
53 public:
56 
62  NrpCoreServer(const std::string & address, std::shared_ptr<SimulationManager> && manager);
63 
67  void runServerLoop();
68 
72  void stopServerLoop();
73 
74 private:
75 
77 
84  void waitForRequest();
85 
87  bool isRequestPending() const;
88 
90 
92  RequestType getRequestType() const;
93 
95  unsigned getRequestNumIterations() const;
96 
98 
100  void resetRequest();
101 
111  void markRequestAsProcessed(const SimulationManager::RequestResult & result);
112 
114 
125  grpc::Status requestHelper(std::unique_lock<std::mutex> & lock, RequestType requestType, NrpCore::SimStateMessage * returnMessage);
126 
139  void prepareTrajectory(NrpCore::Trajectory * trajectory);
140 
144  void setReturnMessageContent(const SimulationManager::RequestResult& res, NrpCore::SimStateMessage * returnMessage);
145 
146  std::vector<std::shared_ptr<const DataPackInterface>> extractExternalDataPacks(const NrpCore::DataPacks & message);
147 
151  grpc::Status initialize(grpc::ServerContext * , const NrpCore::EmptyMessage * message, NrpCore::InitializeResponse * returnMessage) override;
152 
156  grpc::Status runLoop(grpc::ServerContext * , const NrpCore::RunLoopMessage * message, NrpCore::RunLoopResponse * status) override;
157 
161  grpc::Status runUntilTimeout(grpc::ServerContext * , const NrpCore::EmptyMessage * , NrpCore::RunLoopResponse * returnMessage) override;
162 
166  grpc::Status stopLoop(grpc::ServerContext * , const NrpCore::EmptyMessage * , NrpCore::Response * returnMessage) override;
167 
171  grpc::Status reset(grpc::ServerContext * , const NrpCore::ResetMessage * message, NrpCore::ResetResponse * returnMessage) override;
172 
176  grpc::Status shutdown(grpc::ServerContext * , const NrpCore::EmptyMessage * , NrpCore::Response * returnMessage) override;
177 
179 
181  std::mutex _mutex;
182 
184  std::condition_variable _consumerConditionalVar;
185 
187  std::condition_variable _producerConditionalVar;
188 
190  std::unique_lock<std::mutex> _lock;
191 
193 
195  std::unique_ptr<grpc::Server> _server;
196 
198  RequestType _requestType = RequestType::None;
199 
201  unsigned _numIterations = 0;
202 
204  SimulationManager::RequestResult _requestResult;
205 
207  std::shared_ptr<SimulationManager> _manager;
208 
210  std::vector<std::unique_ptr<protobuf_ops::NRPProtobufOpsIface>> _protoOps;
211 
212 };
213 
214 #endif // NRP_CORE_SERVER_H
215 
216 // EOF
NrpCoreServer::RequestType::RunUntilTimeout
@ RunUntilTimeout
NrpCoreServer::RequestType::None
@ None
NrpCoreServer::RequestType
RequestType
enum with possible request types to be processed by SimulationManager
Definition: nrp_core_server.h:55
NrpCoreServer::stopServerLoop
void stopServerLoop()
Stops server loop.
Definition: nrp_core_server.cpp:128
protobuf_ops.h
NrpCoreServer::NrpCoreServer
NrpCoreServer(const std::string &address, std::shared_ptr< SimulationManager > &&manager)
Constructor. Spawns an instance of gRPC server with given address.
Definition: nrp_core_server.cpp:28
SimulationManager::RequestResult
Helper structure for storing request result and error messages.
Definition: simulation_manager.h:58
NrpCoreServer
NRP Server class, responsible for handling simulation control requests coming from the client applica...
Definition: nrp_core_server.h:51
NrpCoreServer::runServerLoop
void runServerLoop()
Enters server loop in which requests are processed sequentially.
Definition: nrp_core_server.cpp:69
NrpCoreServer::RequestType::Reset
@ Reset
NrpCoreServer::RequestType::StopLoop
@ StopLoop
simulation_manager.h
NrpCoreServer::RequestType::RunLoop
@ RunLoop
NrpCoreServer::RequestType::Shutdown
@ Shutdown
NrpCoreServer::RequestType::Initialize
@ Initialize