12 #include "../HardwareAdapters/HardwareAdaptergRPC.h"
13 #include "../MetaInstruments/gRPCInstrument.h"
17 template <
typename... gRPCServices>
24 template <
typename... gRPCServices>
53 template <
typename... gRPCServices>
69 virtual const char*
GetParamClassTag() const noexcept
override {
return "gRPCModuleParams"; }
94 template <
typename... gRPCServices>
114 template <
typename... gRPCServices>
121 template <
typename gRPCService>
129 constexpr
static auto Name() noexcept {
return "gRPC Module"; }
130 constexpr
static auto Category() noexcept {
return "Network Modules (Servers)"; }
155 std::chrono::milliseconds
GetMainLoopDelay() const override final {
return std::chrono::milliseconds(1); }
169 template <
size_t Index>
179 template <
typename T>
326 template <
typename DerivedType,
typename gRPCService,
typename RequestMessageType,
typename ResponseMessageType,
353 using RequestFuncType = std::function<void(
typename gRPCService::AsyncService*, grpc::ServerContext*,
354 RequestMessageType*,
ResponseWriterType*, grpc::CompletionQueue*, grpc::ServerCompletionQueue*,
void*)>;
363 :
CallDataBase(OwningModule), RequestFunc(RequestFunc), ResponseWriter(this->GetServerContext()) {}
373 RequestFunc(&this->GetOwningModule()->
template GetService<gRPCService>(), this->GetServerContext(),
382 MakeCall(this->GetOwningModule(), Instance);
384 ProcessChildImpl(Instance);
386 ResponseWriter.Finish(ResponseMessage, grpc::Status::OK,
this);
423 auto Result =
ServerQueue->AsyncNext(&Tag, &IsOK, std::chrono::system_clock::now() + std::chrono::milliseconds(80));
425 if (Result == grpc::CompletionQueue::NextStatus::GOT_EVENT && Tag && IsOK)
497 auto ModuleParams = DynExp::dynamic_Params_cast<gRPCModule>(Instance->ParamsGetter());
498 Address = ModuleParams->NetworkParams.MakeAddress();
499 ObjName = ModuleParams->ObjectName;
502 grpc::ServerBuilder ServerBuilder;
503 ServerBuilder.AddListeningPort(Address, grpc::InsecureServerCredentials());
504 std::apply([&ServerBuilder](
auto&... ServicePtr) { (ServerBuilder.RegisterService(ServicePtr.get()), ...); },
ServicePtrs);
506 Server = ServerBuilder.BuildAndStart();
579 mutable std::unique_ptr<grpc::Server>
Server;
Defines DynExp's core module as an interface between the UI and DynExp objects.
Configurator class for gRPCModule.
gRPCModuleConfigurator()=default
virtual ~gRPCModuleConfigurator()=default
Data class for gRPCModule.
virtual ~gRPCModuleData()=default
void ResetImpl(dispatch_tag< ModuleDataBase >) override final
Refer to DynExp::ModuleDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of ...
void Init()
Called by ResetImpl(dispatch_tag<DynExp::ModuleDataBase>) overridden by this class to initialize the ...
virtual void ResetImpl(dispatch_tag< gRPCModuleData >)
Refer to DynExp::ModuleDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of ...
Parameter class for gRPCModule.
void ConfigureParamsImpl(dispatch_tag< ModuleParamsBase >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
DynExp::NetworkParamsExtension NetworkParams
Network address the gRPC server listens on.
gRPCModuleParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core)
Constructs the parameters for a gRPCModule instance.
virtual void ConfigureParamsImpl(dispatch_tag< gRPCModuleParams >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
virtual ~gRPCModuleParams()=default
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
virtual const DynExp::NetworkParamsExtension * GetNetworkAddressParamsChild() const noexcept override
Returns the network address parameters of a derived gRPC instrument. Override GetNetworkAddressParams...
Base class for all TypedCallDataBase classes. Instances of this class manage the state of a single re...
virtual void InitChild(DynExp::ModuleInstance &Instance)=0
Tells gRPC that this CallDataBase instance is ready to handle a respective (as determined by TypedCal...
static constexpr auto ProcessState
State machine state for the StateType::Process state.
StateType InitStateFunc(DynExp::ModuleInstance &Instance)
State function for the CallDataBase::InitState state. Calls InitChild().
Util::StateMachine< StateMachineStateType > StateMachine
State machine based on the states listed in StateType to manage this remote procedure call's state.
StateType ExitStateFunc(DynExp::ModuleInstance &Instance)
State function for the CallDataBase::ExitState state. Deletes this instance.
StateType
Type defining the possible states of remote procedure calls as used by CallDataBase::StateMachine.
@ Init
The remote procedure call's waiting state directly after construction.
@ Process
The remote procedure call's state when it was invoked by a client.
@ Exit
The remote procedure call's state after it has been handled by the server.
CallDataBase(const gRPCModule *const OwningModule) noexcept
Constructs a CallDataBase instance.
static constexpr auto InitState
State machine state for the StateType::Init state.
void Proceed(DynExp::ModuleInstance &Instance)
Calls the state function of the current state of CallDataBase::StateMachine by a call to Util::StateM...
virtual void ProcessChild(DynExp::ModuleInstance &Instance)=0
Creates a new TypedCallDataBase instance of the same type to handle a further remote procedure call,...
static constexpr auto ExitState
State machine state for the StateType::Exit state.
grpc::ServerContext ServerContext
Information about the remote procedure call. Refer to gRPC documentation.
const gRPCModule *const OwningModule
gRPC server this remote procedure call belongs to
StateType ProcessStateFunc(DynExp::ModuleInstance &Instance)
State function for the CallDataBase::ProcessState state. Calls ProcessChild().
auto * GetServerContext() noexcept
Getter for the gRPC server context.
auto GetOwningModule() const noexcept
Getter for the gRPC server this remote procedure call belongs to.
Derive from this class to implement a single remote procedure call handled by this gRPC server gRPCMo...
virtual void ProcessChildImpl(DynExp::ModuleInstance &Instance)=0
Override to implement the server's action to handle this remote procedure call. Particularly,...
grpc::ServerAsyncResponseWriter< ResponseMessageType > ResponseWriterType
Alias for the gRPC response writer which sends a message of type ResponseMessageType back to the clie...
const RequestFuncType RequestFunc
Request function to register the remote procedure call derived from TypedCallDataBase with gRPC.
ResponseWriterType ResponseWriter
gRPC response writer to send gRPCModule::ResponseMessage back to the client. Refer to gRPC documentat...
static void MakeCall(const gRPCModule *const OwningModule, DynExp::ModuleInstance &Instance)
Creates a new remote procedure call of this type which awaits requests from the client.
TypedCallDataBase(const gRPCModule *const OwningModule, const RequestFuncType RequestFunc) noexcept
Constructs a TypedCallDataBase instance.
RequestMessageType RequestMessage
Client's message sent along with its invocation of this remote procedure call.
void InitChild(DynExp::ModuleInstance &Instance) override final
Tells gRPC that this CallDataBase instance is ready to handle a respective (as determined by TypedCal...
ResponseMessageType ResponseMessage
Response the server sends back to the client by finishing the remote procedure call.
virtual ~TypedCallDataBase()=default
std::function< void(typename gRPCService::AsyncService *, grpc::ServerContext *, RequestMessageType *, ResponseWriterType *, grpc::CompletionQueue *, grpc::ServerCompletionQueue *, void *)> RequestFuncType
Alias for the remote procedure call function implemented by this class as part of an asynchronous gRP...
void ProcessChild(DynExp::ModuleInstance &Instance) override final
Creates a new TypedCallDataBase instance of the same type to handle a further remote procedure call,...
Module template for building gRPC servers listening on TCP sockets for network instruments to connect...
void OnInit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread starts. Override it to lock instruments this m...
virtual void CreateInitialCallDataObjectsImpl(DynExp::Object::dispatch_tag< gRPCModule >, DynExp::ModuleInstance &Instance) const =0
Override by derived classes to let them call TypedCallDataBase::MakeCall of the TypedCallDataBase typ...
auto MakeServicePtrTuple()
Constructs the gRPC services (gRPCServices) this gRPC server implements and packs them as a tuple.
grpc::ServerCompletionQueue * GetServerQueue() const noexcept
Getter for the gRPC server's request queue.
virtual void OnInitChild(DynExp::ModuleInstance *Instance) const
Allows derived classes to lock instruments they are controlling (e.g. with calls to DynExp::RunnableI...
std::chrono::milliseconds GetMainLoopDelay() const override final
Specifies in which time intervals the module's event queue runs to handle pending events.
void OnErrorChild(DynExp::ModuleInstance &Instance) const override final
This handler gets called just before the module thread terminates due to an exception....
void Shutdown() const
Shuts down the gRPC server Server and its request queue ServerQueue. Then, it calls DrainServerQueue(...
std::unique_ptr< typename gRPCService::AsyncService > ServicePtrType
Alias for a pointer to a gRPC service.
auto & GetService() const noexcept
Returns a reference to a service this gRPC server implements selected by the service index in the gRP...
bool TreatModuleExceptionsAsWarnings() const override
Determines whether this module should be terminated if an exception leaves the module's main loop or ...
Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(DynExp::ModuleInstance &Instance) override final
Module main loop. The function is executed periodically by the module thread. Also refer to GetMainLo...
virtual void OnExitChild(DynExp::ModuleInstance *Instance) const
Allows derived classes to unlock instruments they are controlling (e.g. with calls to DynExp::Runnabl...
gRPCModule(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
Constructs a ModuleBase instance.
constexpr static auto Name() noexcept
Every derived class has to redefine this function.
constexpr static auto Category() noexcept
Every derived class has to redefine this function.
std::unique_ptr< grpc::Server > Server
Pointer to the actual gRPC server.
std::tuple< ServicePtrType< gRPCServices >... > ServicePtrs
Tuple of pointers to all the services this gRPC server implements.
void ResetImpl(dispatch_tag< ModuleBase >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
virtual void ResetImpl(DynExp::Object::dispatch_tag< gRPCModule >)
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
virtual ~gRPCModule()=default
std::atomic< bool > ServerRunning
Indicates whether Server is running.
virtual std::string GetName() const override
Returns the name of this Object type.
virtual std::string GetCategory() const override
Returns the category of this Object type.
void OnExit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread terminates (not due to an exception,...
std::unique_ptr< grpc::ServerCompletionQueue > ServerQueue
Queue holding the pending requests made to the gRPC server Server.
void DrainServerQueue() const
Empties ServerQueue removing every request and deleting associated CallDataBase instances.
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Base class for modules. Modules implement programs on their own (e.g. measurement protocols or server...
ModuleBase(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs a ModuleBase instance.
Configurator class for ModuleBase.
Data structure to contain data which is synchronized in between different threads....
Refer to ParamsBase::dispatch_tag.
Defines data for a thread belonging to a ModuleBase instance. Refer to RunnableInstance.
Parameter class for ModuleBase.
ModuleParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a ModuleBase instance.
Bundles several parameters to describe a network connection. Use in parameter classes.
std::string GetCategoryAndName() const
Builds a string from an Object's category and name to allow the user to identify an Object's type.
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
auto GetObjectName(const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
Returns the name of this Object instance.
Refer to ParamsBase::dispatch_tag.
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
const DynExpCore & Core
Reference to DynExp's core.
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
void Log(const std::string &Message, const ErrorType Type=ErrorType::Info, const size_t Line=0, const std::string &Function="", const std::string &File="", const int ErrorCode=0, const std::stacktrace &Trace={}) noexcept
Logs an event from information specified manually.
State machine state as used by class StateMachine. A state mainly wraps a state function of the membe...
This class models a state machine. It keeps track of the current state and allows to invoke its assoc...
DynExp's module namespace contains the implementation of DynExp modules which extend DynExp's core fu...
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
size_t ItemIDType
ID type of objects/items managed by DynExp.
DynExpErrorCodes
DynExp's error codes
constexpr bool is_contained_in_v
Value type of is_contained_in.
EventLogger & EventLog()
This function holds a static EventLogger instance and returns a reference to it. DynExp uses only one...
Accumulates include statements to provide a precompiled header.