FairMQ  1.2.3
C++ Message Passing Framework
DDS.h
1 /********************************************************************************
2  * Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIR_MQ_PLUGINS_DDS
10 #define FAIR_MQ_PLUGINS_DDS
11 
12 #include <fairmq/Plugin.h>
13 
14 #include <dds_intercom.h>
15 
16 #include <condition_variable>
17 #include <mutex>
18 #include <string>
19 #include <queue>
20 #include <thread>
21 #include <vector>
22 #include <unordered_map>
23 #include <set>
24 #include <chrono>
25 #include <functional>
26 
27 namespace fair
28 {
29 namespace mq
30 {
31 namespace plugins
32 {
33 
34 struct DDSConfig
35 {
36  DDSConfig()
37  : fSubChannelAddresses()
38  , fDDSValues()
39  {}
40 
41  // container of sub channel addresses
42  std::vector<std::string> fSubChannelAddresses;
43  // dds values for the channel
44  std::unordered_map<std::string, std::string> fDDSValues;
45 };
46 
47 struct IofN
48 {
49  IofN(int i, int n)
50  : fI(i)
51  , fN(n)
52  , fEntries()
53  {}
54 
55  int fI;
56  int fN;
57  std::vector<std::string> fEntries;
58 
59 };
60 
61 class DDS : public Plugin
62 {
63  public:
64  DDS(const std::string name, const Plugin::Version version, const std::string maintainer, const std::string homepage, PluginServices* pluginServices);
65 
66  ~DDS();
67 
68  private:
69  auto HandleControl() -> void;
70  auto WaitForNextState() -> DeviceState;
71 
72  auto FillChannelContainers() -> void;
73  auto SubscribeForConnectingChannels() -> void;
74  auto PublishBoundChannels() -> void;
75  auto SubscribeForCustomCommands() -> void;
76 
77  auto HeartbeatSender() -> void;
78 
79  dds::intercom_api::CIntercomService fService;
80  dds::intercom_api::CCustomCmd fDDSCustomCmd;
81  dds::intercom_api::CKeyValue fDDSKeyValue;
82 
83  std::unordered_map<std::string, std::vector<std::string>> fBindingChans;
84  std::unordered_map<std::string, DDSConfig> fConnectingChans;
85 
86  std::unordered_map<std::string, int> fI;
87  std::unordered_map<std::string, IofN> fIofN;
88 
89  std::mutex fStopMutex;
90  std::condition_variable fStopCondition;
91 
92  const std::set<std::string> fCommands;
93 
94  std::thread fControllerThread;
95  std::queue<DeviceState> fEvents;
96  std::mutex fEventsMutex;
97  std::condition_variable fNewEvent;
98 
99  std::atomic<bool> fDeviceTerminationRequested;
100 
101  std::set<uint64_t> fHeartbeatSubscribers;
102  std::mutex fHeartbeatSubscriberMutex;
103  std::set<uint64_t> fStateChangeSubscribers;
104  std::mutex fStateChangeSubscriberMutex;
105 
106  std::thread fHeartbeatThread;
107  std::chrono::milliseconds fHeartbeatInterval;
108 };
109 
110 Plugin::ProgOptions DDSProgramOptions()
111 {
112  boost::program_options::options_description options{"DDS Plugin"};
113  options.add_options()
114  ("dds-i", boost::program_options::value<std::vector<std::string>>()->multitoken()->composing(), "Task index for chosing connection target (single channel n to m). When all values come via same update.")
115  ("dds-i-n", boost::program_options::value<std::vector<std::string>>()->multitoken()->composing(), "Task index for chosing connection target (one out of n values to take). When values come as independent updates.");
116 
117  return options;
118 }
119 
120 REGISTER_FAIRMQ_PLUGIN(
121  DDS, // Class name
122  dds, // Plugin name (string, lower case chars only)
123  (Plugin::Version{1,0,0}), // Version
124  "FairRootGroup <fairroot@gsi.de>", // Maintainer
125  "https://github.com/FairRootGroup/FairRoot", // Homepage
126  DDSProgramOptions // custom program options for the plugin
127 )
128 
129 } /* namespace plugins */
130 } /* namespace mq */
131 } /* namespace fair */
132 
133 #endif /* FAIR_MQ_PLUGINS_DDS */
Facilitates communication between devices and plugins.
Definition: PluginServices.h:37
Definition: DDS.h:61
Definition: DDS.h:34
Definition: DDS.h:47
Base class for FairMQ plugins.
Definition: Plugin.h:38
DeviceState
See https://github.com/FairRootGroup/FairRoot/blob/dev/fairmq/docs/Device.md#13-state-machine.
Definition: PluginServices.h:54
Definition: DeviceRunner.h:23
Definition: Version.h:22