| helics 3.6.1

application_api/BrokerApp.hpp
1 /*
2 Copyright (c) 2017-2025,
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 #pragma once
8
9 #include "../core/CoreTypes.hpp"
10 #include "../core/helicsTime.hpp"
11 #include "helics_cxx_export.h"
12
13 #include <chrono>
14 #include <memory>
15 #include <string>
16 #include <utility>
17 #include <vector>
18
19 namespace helics {
20 class Broker;
21 class helicsCLI11App;
22
26 class HELICS_CXX_EXPORT BrokerApp {
27 public:
29 BrokerApp() = default;
33 explicit BrokerApp(std::vector<std::string>&& args);
38 BrokerApp(CoreType ctype, std::vector<std::string>&& args);
44 BrokerApp(CoreType ctype, const std::string& broker_name, std::vector<std::string>&& args);
48 explicit BrokerApp(std::vector<std::string>& args);
53 BrokerApp(CoreType ctype, std::vector<std::string>& args);
59 BrokerApp(CoreType ctype, const std::string& broker_name, std::vector<std::string>& args);
64 BrokerApp(int argc, char* argv[]);
70 BrokerApp(CoreType ctype, int argc, char* argv[]);
77 BrokerApp(CoreType ctype, std::string_view brokerName, int argc, char* argv[]);
81 explicit BrokerApp(std::string_view argString);
86 explicit BrokerApp(CoreType ctype, std::string_view argString = std::string_view{});
92 BrokerApp(CoreType ctype, std::string_view brokerName, std::string_view argString);
93
95 explicit BrokerApp(std::shared_ptr<Broker> brk);
96
98 bool isConnected() const;
100 bool connect();
103 bool isOpenToNewFederates() const;
104
106 void forceTerminate();
108 bool waitForDisconnect(std::chrono::milliseconds waitTime = std::chrono::milliseconds(0));
110 void linkEndpoints(std::string_view source, std::string_view target);
112 void dataLink(std::string_view source, std::string_view target);
114 void addSourceFilterToEndpoint(std::string_view filter, std::string_view endpoint);
116 void addDestinationFilterToEndpoint(std::string_view filter, std::string_view endpoint);
118 void addAlias(std::string_view interfaceName, std::string_view alias);
120 void makeConnections(const std::string& file);
122 const std::string& getIdentifier() const;
124 const std::string& getAddress() const;
133 std::string query(std::string_view target,
134 std::string_view queryStr,
135 HelicsSequencingModes mode = HELICS_SEQUENCING_MODE_FAST);
143 void setGlobal(std::string_view valueName, std::string_view value);
144
153 void sendCommand(std::string_view target,
154 std::string_view commandStr,
155 HelicsSequencingModes mode = HELICS_SEQUENCING_MODE_FAST);
156
158 void setLogFile(std::string_view logFile);
160 void setLoggingLevel(int loglevel);
162 void reset();
163 #ifdef HELICS_CXX_STATIC_DEFINE
166 auto* operator->() const { return broker.operator->(); }
167 #else
168 BrokerApp* operator->() { return this; }
169 const BrokerApp* operator->() const { return this; }
170 #endif
172 std::shared_ptr<Broker> getCopyofBrokerPointer() const { return broker; }
173
175 void setTimeBarrier(Time barrierTime);
177 void clearTimeBarrier();
179 void globalError(int32_t errorCode, std::string_view errorString);
180
181 private:
182 void processArgs(std::unique_ptr<helicsCLI11App>& app);
183 std::unique_ptr<helicsCLI11App> generateParser(bool noTypeOption = false);
184 std::shared_ptr<Broker> broker;
185 std::string name;
186 };
187
189 class BrokerKeeper {
190 public:
191 template<class... Args>
192 explicit BrokerKeeper(Args&&... args): brk(std::forward<Args...>(args...))
193 {
194 }
195 BrokerKeeper(BrokerKeeper&& brkeep) = default;
196 BrokerKeeper(const BrokerKeeper& brkeep) = default;
197 BrokerKeeper& operator=(BrokerKeeper&& brkeep) = default;
198 BrokerKeeper& operator=(const BrokerKeeper& brkeep) = default;
200 bool isConnected() { return brk.isConnected(); }
202 void forceTerminate() { brk.forceTerminate(); }
204 ~BrokerKeeper()
205 {
206 if (brk.isConnected()) {
207 brk.waitForDisconnect();
208 }
209 }
210
211 private:
212 BrokerApp brk;
213 };
214
215 } // namespace helics
helics::BrokerApp
Definition: application_api/BrokerApp.hpp:26
helics::BrokerApp::forceTerminate
void forceTerminate()
Definition: BrokerApp.cpp:189
helics::BrokerApp::getCopyofBrokerPointer
std::shared_ptr< Broker > getCopyofBrokerPointer() const
Definition: application_api/BrokerApp.hpp:172
helics::BrokerApp::waitForDisconnect
bool waitForDisconnect(std::chrono::milliseconds waitTime=std::chrono::milliseconds(0))
Definition: BrokerApp.cpp:127
helics::BrokerApp::isConnected
bool isConnected() const
Definition: BrokerApp.cpp:174
helics::BrokerApp::BrokerApp
BrokerApp()=default
helics::BrokerKeeper
Definition: application_api/BrokerApp.hpp:189
helics::BrokerKeeper::forceTerminate
void forceTerminate()
Force terminate the broker.
Definition: application_api/BrokerApp.hpp:202
helics::BrokerKeeper::~BrokerKeeper
~BrokerKeeper()
the destructor waits for the broker to terminate
Definition: application_api/BrokerApp.hpp:204
helics::BrokerKeeper::isConnected
bool isConnected()
is the broker connected
Definition: application_api/BrokerApp.hpp:200
HelicsSequencingModes
HelicsSequencingModes
Definition: helics_enums.h:427
HELICS_SEQUENCING_MODE_FAST
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:429
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::CoreType
CoreType
Definition: CoreTypes.hpp:46
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27