helics  3.5.2
application_api/BrokerApp.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 #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);
49  BrokerApp(int argc, char* argv[]);
55  BrokerApp(CoreType ctype, int argc, char* argv[]);
62  BrokerApp(CoreType ctype, std::string_view brokerName, int argc, char* argv[]);
66  explicit BrokerApp(std::string_view argString);
71  explicit BrokerApp(CoreType ctype, std::string_view argString = std::string_view{});
77  BrokerApp(CoreType ctype, std::string_view brokerName, std::string_view argString);
78 
80  explicit BrokerApp(std::shared_ptr<Broker> brk);
81 
83  bool isConnected() const;
85  bool connect();
88  bool isOpenToNewFederates() const;
89 
91  void forceTerminate();
93  bool waitForDisconnect(std::chrono::milliseconds waitTime = std::chrono::milliseconds(0));
95  void linkEndpoints(std::string_view source, std::string_view target);
97  void dataLink(std::string_view source, std::string_view target);
99  void addSourceFilterToEndpoint(std::string_view filter, std::string_view endpoint);
101  void addDestinationFilterToEndpoint(std::string_view filter, std::string_view endpoint);
103  void addAlias(std::string_view interfaceName, std::string_view alias);
105  void makeConnections(const std::string& file);
107  const std::string& getIdentifier() const;
109  const std::string& getAddress() const;
118  std::string query(std::string_view target,
119  std::string_view queryStr,
128  void setGlobal(std::string_view valueName, std::string_view value);
129 
138  void sendCommand(std::string_view target,
139  std::string_view commandStr,
141 
143  void setLogFile(std::string_view logFile);
145  void setLoggingLevel(int loglevel);
147  void reset();
148 #ifdef HELICS_CXX_STATIC_DEFINE
151  auto* operator->() const { return broker.operator->(); }
152 #else
153  BrokerApp* operator->() { return this; }
154  const BrokerApp* operator->() const { return this; }
155 #endif
157  std::shared_ptr<Broker> getCopyofBrokerPointer() const { return broker; }
158 
160  void setTimeBarrier(Time barrierTime);
162  void clearTimeBarrier();
164  void globalError(int32_t errorCode, std::string_view errorString);
165 
166  private:
167  void processArgs(std::unique_ptr<helicsCLI11App>& app);
168  std::unique_ptr<helicsCLI11App> generateParser(bool noTypeOption = false);
169  std::shared_ptr<Broker> broker;
170  std::string name;
171 };
172 
175  public:
176  template<class... Args>
177  explicit BrokerKeeper(Args&&... args): brk(std::forward<Args...>(args...))
178  {
179  }
180  BrokerKeeper(BrokerKeeper&& brkeep) = default;
181  BrokerKeeper(const BrokerKeeper& brkeep) = default;
182  BrokerKeeper& operator=(BrokerKeeper&& brkeep) = default;
183  BrokerKeeper& operator=(const BrokerKeeper& brkeep) = default;
185  bool isConnected() { return brk.isConnected(); }
187  void forceTerminate() { brk.forceTerminate(); }
190  {
191  if (brk.isConnected()) {
192  brk.waitForDisconnect();
193  }
194  }
195 
196  private:
197  BrokerApp brk;
198 };
199 
200 } // namespace helics
Definition: application_api/BrokerApp.hpp:26
void forceTerminate()
Definition: BrokerApp.cpp:163
std::shared_ptr< Broker > getCopyofBrokerPointer() const
Definition: application_api/BrokerApp.hpp:157
bool waitForDisconnect(std::chrono::milliseconds waitTime=std::chrono::milliseconds(0))
Definition: BrokerApp.cpp:101
bool isConnected() const
Definition: BrokerApp.cpp:148
BrokerApp()=default
Definition: application_api/BrokerApp.hpp:174
void forceTerminate()
Force terminate the broker.
Definition: application_api/BrokerApp.hpp:187
~BrokerKeeper()
the destructor waits for the broker to terminate
Definition: application_api/BrokerApp.hpp:189
bool isConnected()
is the broker connected
Definition: application_api/BrokerApp.hpp:185
HelicsSequencingModes
Definition: helics_enums.h:425
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:427
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
CoreType
Definition: CoreTypes.hpp:46
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27