helics  3.5.2
CommsBroker_impl.hpp
1 /*
2 Copyright (c) 2017-2024,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 
8 #pragma once
9 #include "CommsBroker.hpp"
10 #include "CommsInterface.hpp"
12 
13 #include <atomic>
14 #include <memory>
15 #include <mutex>
16 #include <string>
17 #include <thread>
18 #include <utility>
19 namespace helics {
20 template<class COMMS, class BrokerT>
22 {
23  static_assert(std::is_base_of<CommsInterface, COMMS>::value,
24  "COMMS object must be a CommsInterface Object");
25  static_assert(std::is_base_of<BrokerBase, BrokerT>::value,
26  "Broker must be an object with a base of BrokerBase");
27  loadComms();
28 }
29 
30 template<class COMMS, class BrokerT>
31 CommsBroker<COMMS, BrokerT>::CommsBroker(bool arg) noexcept: BrokerT(arg)
32 {
33  static_assert(std::is_base_of<CommsInterface, COMMS>::value,
34  "COMMS object must be a CommsInterface Object");
35  static_assert(std::is_base_of<BrokerBase, BrokerT>::value,
36  "Broker must be an object with a base of BrokerBase");
37  loadComms();
38 }
39 
40 template<class COMMS, class BrokerT>
41 CommsBroker<COMMS, BrokerT>::CommsBroker(std::string_view obj_name): BrokerT(obj_name)
42 {
43  static_assert(std::is_base_of<CommsInterface, COMMS>::value,
44  "COMMS object must be a CommsInterface Object");
45  static_assert(std::is_base_of<BrokerBase, BrokerT>::value,
46  "Broker must be an object with a base of BrokerBase");
47  loadComms();
48 }
49 template<class COMMS, class BrokerT>
51 {
52  comms = std::make_unique<COMMS>();
53  comms->setCallback([this](ActionMessage&& M) { BrokerBase::addActionMessage(std::move(M)); });
54  comms->setLoggingCallback(BrokerBase::getLoggingCallback());
55 }
56 
57 template<class COMMS, class BrokerT>
59 {
61  int exp = 2;
62  while (!disconnectionStage.compare_exchange_weak(exp, 3)) {
63  if (exp == 0) {
64  commDisconnect();
65  exp = 1;
66  } else {
67  std::this_thread::sleep_for(std::chrono::milliseconds(50));
68  }
69  }
70  comms = nullptr; // need to ensure the comms are deleted before the callbacks become invalid
72 }
73 
74 template<class COMMS, class BrokerT>
76 {
77  commDisconnect();
78 }
79 
80 template<class COMMS, class BrokerT>
81 void CommsBroker<COMMS, BrokerT>::commDisconnect()
82 {
83  int exp = 0;
84  if (disconnectionStage.compare_exchange_strong(exp, 1)) {
85  comms->disconnect();
86  disconnectionStage = 2;
87  }
88 }
89 
90 template<class COMMS, class BrokerT>
91 bool CommsBroker<COMMS, BrokerT>::tryReconnect()
92 {
93  return comms->reconnect();
94 }
95 
96 template<class COMMS, class BrokerT>
97 void CommsBroker<COMMS, BrokerT>::transmit(route_id rid, const ActionMessage& cmd)
98 {
99  comms->transmit(rid, cmd);
100 }
101 
102 template<class COMMS, class BrokerT>
103 void CommsBroker<COMMS, BrokerT>::transmit(route_id rid, ActionMessage&& cmd)
104 {
105  comms->transmit(rid, std::move(cmd));
106 }
107 
108 template<class COMMS, class BrokerT>
109 void CommsBroker<COMMS, BrokerT>::addRoute(route_id rid,
110  int /*interfaceId*/,
111  std::string_view routeInfo)
112 {
113  comms->addRoute(rid, routeInfo);
114 }
115 
116 template<class COMMS, class BrokerT>
117 void CommsBroker<COMMS, BrokerT>::removeRoute(route_id rid)
118 {
119  comms->removeRoute(rid);
120 }
121 
122 template<class COMMS, class BrokerT>
124 {
125  return comms.get();
126 }
127 
128 } // namespace helics
Definition: ActionMessage.hpp:30
void joinAllThreads()
Definition: BrokerBase.cpp:93
std::atomic< bool > haltOperations
flag indicating that no further message should be processed
Definition: BrokerBase.hpp:80
std::function< void(int, std::string_view, std::string_view)> getLoggingCallback() const
Definition: BrokerBase.cpp:86
void addActionMessage(const ActionMessage &message)
Definition: BrokerBase.cpp:590
Definition: CommsBroker.hpp:21
~CommsBroker()
Definition: CommsBroker_impl.hpp:58
COMMS * getCommsObjectPointer()
Definition: CommsBroker_impl.hpp:123
CommsBroker() noexcept
Definition: CommsBroker_impl.hpp:21
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14