helics  2.8.1
CommsBroker_impl.hpp
1 /*
2 Copyright (c) 2017-2021,
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(const std::string& 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 {
60  BrokerBase::haltOperations = true;
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
71  BrokerBase::joinAllThreads();
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  const std::string& 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
helics::ActionMessage
Definition: ActionMessage.hpp:29
helics::CommsBroker
Definition: CommsBroker.hpp:21
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::CommsBroker::CommsBroker
CommsBroker() noexcept
Definition: CommsBroker_impl.hpp:21
BrokerBase.hpp