helics  3.3.0
application_api/BrokerApp.hpp
1 /*
2 Copyright (c) 2017-2022,
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
149 
151  auto* operator->() const
152  {
153  return broker.operator->();
154  }
155 #else
156  BrokerApp* operator->()
157  {
158  return this;
159  }
160  const BrokerApp* operator->() const
161  {
162  return this;
163  }
164 #endif
165 
166  std::shared_ptr<Broker> getCopyofBrokerPointer() const
167  {
168  return broker;
169  }
170 
172  void setTimeBarrier(Time barrierTime);
174  void clearTimeBarrier();
176  void globalError(int32_t errorCode, std::string_view errorString);
177 
178  private:
179  void processArgs(std::unique_ptr<helicsCLI11App>& app);
180  std::unique_ptr<helicsCLI11App> generateParser(bool noTypeOption = false);
181  std::shared_ptr<Broker> broker;
182  std::string name;
183 };
184 
187  public:
188  template<class... Args>
189  explicit BrokerKeeper(Args&&... args): brk(std::forward<Args...>(args...))
190  {
191  }
192  BrokerKeeper(BrokerKeeper&& brkeep) = default;
193  BrokerKeeper(const BrokerKeeper& brkeep) = default;
194  BrokerKeeper& operator=(BrokerKeeper&& brkeep) = default;
195  BrokerKeeper& operator=(const BrokerKeeper& brkeep) = default;
197  bool isConnected() { return brk.isConnected(); }
199  void forceTerminate() { brk.forceTerminate(); }
202  {
203  if (brk.isConnected()) {
204  brk.waitForDisconnect();
205  }
206  }
207 
208  private:
209  BrokerApp brk;
210 };
211 
212 } // namespace helics
helics::BrokerKeeper::forceTerminate
void forceTerminate()
Force terminate the broker.
Definition: application_api/BrokerApp.hpp:199
helics::BrokerApp::addSourceFilterToEndpoint
void addSourceFilterToEndpoint(std::string_view filter, std::string_view endpoint)
Definition: BrokerApp.cpp:187
helics::CoreType::MULTI
@ MULTI
use the multi-broker
helics::coreTypeFromString
CoreType coreTypeFromString(std::string_view type) noexcept
Definition: typeOperations.cpp:17
helics::BrokerApp::getCopyofBrokerPointer
std::shared_ptr< Broker > getCopyofBrokerPointer() const
Definition: application_api/BrokerApp.hpp:166
helics::BrokerApp::isConnected
bool isConnected() const
Definition: BrokerApp.cpp:148
helics::CoreType
CoreType
Definition: CoreTypes.hpp:36
helics::BrokerApp::isOpenToNewFederates
bool isOpenToNewFederates() const
Definition: BrokerApp.cpp:158
helics::BrokerApp::linkEndpoints
void linkEndpoints(std::string_view source, std::string_view target)
Definition: BrokerApp.cpp:173
helics::BrokerApp::query
std::string query(std::string_view target, std::string_view queryStr, HelicsSequencingModes mode=HELICS_SEQUENCING_MODE_FAST)
Definition: BrokerApp.cpp:229
helics::BrokerApp::clearTimeBarrier
void clearTimeBarrier()
Definition: BrokerApp.cpp:264
helics::BrokerApp::setLogFile
void setLogFile(std::string_view logFile)
Definition: BrokerApp.cpp:279
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::BrokerApp
Definition: application_api/BrokerApp.hpp:26
helics::BrokerApp::getIdentifier
const std::string & getIdentifier() const
Definition: BrokerApp.cpp:216
helics::BrokerFactory::displayHelp
void displayHelp(CoreType type)
Definition: BrokerFactory.cpp:319
helics::CoreType::DEFAULT
@ DEFAULT
ZMQ if available or UDP.
helics::BrokerApp::globalError
void globalError(int32_t errorCode, std::string_view errorString)
Definition: BrokerApp.cpp:271
helics::BrokerFactory::create
std::shared_ptr< Broker > create(CoreType type, std::string_view configureString)
Definition: BrokerFactory.cpp:99
helics::BrokerApp::dataLink
void dataLink(std::string_view source, std::string_view target)
Definition: BrokerApp.cpp:180
helics::RegistrationFailure
Definition: core-exceptions.hpp:85
helics::BrokerApp::waitForDisconnect
bool waitForDisconnect(std::chrono::milliseconds waitTime=std::chrono::milliseconds(0))
Definition: BrokerApp.cpp:101
helics::BrokerFactory::findBroker
std::shared_ptr< Broker > findBroker(std::string_view brokerName)
Definition: BrokerFactory.cpp:183
helics::BrokerKeeper::~BrokerKeeper
~BrokerKeeper()
the destructor waits for the broker to terminate
Definition: application_api/BrokerApp.hpp:201
helics::BrokerApp::reset
void reset()
Definition: BrokerApp.cpp:286
helics::BrokerApp::forceTerminate
void forceTerminate()
Definition: BrokerApp.cpp:163
HelicsSequencingModes
HelicsSequencingModes
Definition: helics_enums.h:395
helics::BrokerApp::setTimeBarrier
void setTimeBarrier(Time barrierTime)
Definition: BrokerApp.cpp:257
helics::BrokerApp::sendCommand
void sendCommand(std::string_view target, std::string_view commandStr, HelicsSequencingModes mode=HELICS_SEQUENCING_MODE_FAST)
Definition: BrokerApp.cpp:241
helics::BrokerApp::setLoggingLevel
void setLoggingLevel(int loglevel)
Definition: BrokerApp.cpp:250
helics::BrokerApp::connect
bool connect()
Definition: BrokerApp.cpp:153
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::BrokerKeeper::isConnected
bool isConnected()
is the broker connected
Definition: application_api/BrokerApp.hpp:197
HELICS_SEQUENCING_MODE_FAST
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:397
helics::BrokerApp::setGlobal
void setGlobal(std::string_view valueName, std::string_view value)
Definition: BrokerApp.cpp:234
helics::BrokerApp::addAlias
void addAlias(std::string_view interfaceName, std::string_view alias)
Definition: BrokerApp.cpp:201
helics::BrokerApp::getAddress
const std::string & getAddress() const
Definition: BrokerApp.cpp:222
helics::BrokerKeeper
Definition: application_api/BrokerApp.hpp:186
helics::BrokerApp::addDestinationFilterToEndpoint
void addDestinationFilterToEndpoint(std::string_view filter, std::string_view endpoint)
Definition: BrokerApp.cpp:194
helics::BrokerApp::BrokerApp
BrokerApp()=default
helics::BrokerApp::makeConnections
void makeConnections(const std::string &file)
Definition: BrokerApp.cpp:208