DynExp
Highly flexible laboratory automation for dynamically changing experiments.
InterModuleCommunicator.h
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
9 #pragma once
10 
11 #include "stdafx.h"
12 #include "DynExpCore.h"
13 
14 namespace DynExpInstr
15 {
16  class InterModuleCommunicator;
17 
21  namespace InterModuleCommunicatorTasks
22  {
27  {
29 
34  };
35 
40  {
42 
47  };
48 
53  {
55 
60  };
61  }
62 
67  {
68  public:
70  virtual ~InterModuleCommunicatorData() = default;
71 
72  private:
75  };
76 
81  {
82  public:
88 
89  virtual ~InterModuleCommunicatorParams() = default;
90 
91  virtual const char* GetParamClassTag() const noexcept override { return "InterModuleCommunicatorParams"; }
92 
93  private:
96 
97  DummyParam Dummy = { *this };
98  };
99 
104  {
105  public:
108 
111 
112  private:
113  virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore& Core) const override { return DynExp::MakeParams<InterModuleCommunicatorConfigurator>(ID, Core); }
114  };
115 
124  {
125  public:
129 
130  constexpr static auto Name() noexcept { return "Inter-Module Communicator"; }
131 
136 
138 
139  virtual std::string GetName() const override { return Name(); }
140 
149  template <typename DerivedEvent,
150  std::enable_if_t<std::is_base_of_v<DynExp::InterModuleEventBase, DerivedEvent>, int> = 0>
151  void PostEvent(const DynExp::ModuleBase& Caller, const DerivedEvent& InterModuleEvent) const
152  {
153  auto& ModuleMgr = Core.GetModuleManager();
154  const auto UserIDs = GetUserIDs();
155 
156  for (auto ID : UserIDs)
157  {
158  // Do not send the event back to the caller.
159  if (Caller.GetID() == ID)
160  continue;
161 
162  auto Resource = ModuleMgr.GetResource(ID);
163 
164  // Retry in case of Util::TimeoutException a couple of times to not lose events.
165  for (int NumTries = 0; true; NumTries++)
166  {
167  try
168  {
169  if (Resource->IsReady())
170  Resource->EnqueueEvent(std::make_unique<DerivedEvent>(InterModuleEvent));
171  }
172  catch ([[maybe_unused]] const Util::TimeoutException& e)
173  {
174  if (NumTries >= 100)
175  throw;
176  else
177  {
178  std::this_thread::yield();
179 
180  continue;
181  }
182  }
183 
184  break;
185  }
186  }
187  }
188 
189  private:
190  void ResetImpl(dispatch_tag<DynExp::InstrumentBase>) override final;
192 
193  // Do not override MakeInitTask(), MakeExitTask(), and MakeUpdateTask() since respective tasks don't do anything.
194 
196  };
197 }
Defines DynExp's core module as an interface between the UI and DynExp objects.
Configurator class for InterModuleCommunicator.
virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core) const override
Override to make derived classes call DynExp::MakeParams with the correct configurator type derived f...
Data class for InterModuleCommunicator.
virtual void ResetImpl(dispatch_tag< InterModuleCommunicatorData >)
void ResetImpl(dispatch_tag< DynExp::InstrumentDataBase >) override final
Refer to DynExp::InstrumentDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl()...
Parameter class for InterModuleCommunicator.
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...
InterModuleCommunicatorParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core)
Constructs the parameters for a InterModuleCommunicator instance.
DummyParam Dummy
Dummy parameter which is to be owned once by parameter classes that do not contain any other paramete...
virtual void ConfigureParamsImpl(dispatch_tag< InterModuleCommunicatorParams >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
void ConfigureParamsImpl(dispatch_tag< DynExp::InstrumentParamsBase >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Defines a task for deinitializing an instrument within an instrument inheritance hierarchy....
virtual void ExitFuncImpl(dispatch_tag< ExitTask >, DynExp::InstrumentInstance &Instance)
Deinitializes the respective instrument within the instrument inheritance hierarchy....
void ExitFuncImpl(dispatch_tag< DynExp::ExitTaskBase >, DynExp::InstrumentInstance &Instance) override final
Deinitializes the respective instrument within the instrument inheritance hierarchy....
Defines a task for initializing an instrument within an instrument inheritance hierarchy....
void InitFuncImpl(dispatch_tag< DynExp::InitTaskBase >, DynExp::InstrumentInstance &Instance) override final
Initializes the respective instrument within the instrument inheritance hierarchy....
virtual void InitFuncImpl(dispatch_tag< InitTask >, DynExp::InstrumentInstance &Instance)
Initializes the respective instrument within the instrument inheritance hierarchy....
Defines a task for updating an instrument within an instrument inheritance hierarchy....
void UpdateFuncImpl(dispatch_tag< DynExp::UpdateTaskBase >, DynExp::InstrumentInstance &Instance) override final
Updates the respective instrument within the instrument inheritance hierarchy. Call UpdateFuncImpl() ...
virtual void UpdateFuncImpl(dispatch_tag< UpdateTask >, DynExp::InstrumentInstance &Instance)
Updates the respective instrument within the instrument inheritance hierarchy. Call UpdateFuncImpl() ...
Defines an inter-module communicator instrument to exchange events in between DynExp modules....
void PostEvent(const DynExp::ModuleBase &Caller, const DerivedEvent &InterModuleEvent) const
Inserts the event passed to the function into the event queues of the modules making use of this Inte...
virtual void ResetImpl(dispatch_tag< InterModuleCommunicator >)
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
constexpr static auto Name() noexcept
Every derived class has to redefine this function.
virtual std::string GetName() const override
Returns the name of this Object type.
const DynExp::DynExpCore & Core
Reference to DynExp's core.
InterModuleCommunicator(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
Constructs an instrument instance.
void ResetImpl(dispatch_tag< DynExp::InstrumentBase >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition: DynExpCore.h:127
auto & GetModuleManager() noexcept
Getter for the module manager.
Definition: DynExpCore.h:293
Defines a task for deinitializing an instrument within an instrument inheritance hierarchy....
Definition: Instrument.h:1144
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1151
Defines a task for initializing an instrument within an instrument inheritance hierarchy....
Definition: Instrument.h:1113
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1120
Base class for instruments. Instruments comprise virtual devices (meta instruments) and physial devic...
Definition: Instrument.h:451
Configurator class for InstrumentBase.
Definition: Instrument.h:435
Data structure to contain data which is synchronized in between different threads....
Definition: Instrument.h:135
Refer to ParamsBase::dispatch_tag.
Definition: Instrument.h:146
Defines data for a thread belonging to a InstrumentBase instance. Refer to RunnableInstance.
Definition: Instrument.h:772
Parameter class for InstrumentBase.
Definition: Instrument.h:401
InstrumentParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a InstrumentBase instance.
Definition: Instrument.h:412
Base class for modules. Modules implement programs on their own (e.g. measurement protocols or server...
Definition: Module.h:392
auto GetUserIDs(const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const
Returns a list of the IDs of the registered users in a thread-safe way.
Definition: Object.h:2228
ItemIDType GetID() const noexcept
Returns the ID of this Object instance. Thread-safe since ID is const.
Definition: Object.h:2143
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
Definition: Object.h:2302
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
Definition: Object.h:2303
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
Dummy parameter which is to be owned once by parameter classes that do not contain any other paramete...
Definition: Object.h:522
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
Definition: Object.h:1779
const DynExpCore & Core
Reference to DynExp's core.
Definition: Object.h:1780
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Definition: Object.h:349
Defines a task for updating an instrument within an instrument inheritance hierarchy....
Definition: Instrument.h:1175
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1182
Thrown when an operation timed out before it could be completed, especially used for locking shared d...
Definition: Exception.h:261
DynExp's instrument namespace contains the implementation of DynExp instruments which extend DynExp's...
Definition: Instrument.h:1254
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition: Object.h:1807
size_t ItemIDType
ID type of objects/items managed by DynExp.
Accumulates include statements to provide a precompiled header.