helics  2.8.1
application_api/BrokerApp.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 #pragma once
8 
9 #include "../core/core-types.hpp"
10 #include "../core/helics-time.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(core_type ctype, std::vector<std::string> args);
44  BrokerApp(core_type ctype, const std::string& broker_name, std::vector<std::string> args);
49  BrokerApp(int argc, char* argv[]);
55  BrokerApp(core_type ctype, int argc, char* argv[]);
62  BrokerApp(core_type ctype, const std::string& brokerName, int argc, char* argv[]);
66  explicit BrokerApp(const std::string& argString);
71  BrokerApp(core_type ctype, const std::string& argString);
77  BrokerApp(core_type ctype, const std::string& brokerName, const std::string& 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 dataLink(const std::string& source, const std::string& target);
97  void addSourceFilterToEndpoint(const std::string& filter, const std::string& endpoint);
99  void addDestinationFilterToEndpoint(const std::string& filter, const std::string& endpoint);
101  void makeConnections(const std::string& file);
103  const std::string& getIdentifier() const;
105  const std::string& getAddress() const;
114  std::string query(const std::string& target,
115  const std::string& queryStr,
123  void setGlobal(const std::string& valueName, const std::string& value);
124 
126  void setLogFile(const std::string& logFile);
128  void setLoggingLevel(int loglevel);
130  void reset();
131 #ifdef HELICS_CXX_STATIC_DEFINE
132 
134  auto* operator->() const { return broker.operator->(); }
135 #else
136  BrokerApp* operator->() { return this; }
137  const BrokerApp* operator->() const { return this; }
138 #endif
139 
140  std::shared_ptr<Broker> getCopyofBrokerPointer() const { return broker; }
141 
143  void setTimeBarrier(Time barrierTime);
145  void clearTimeBarrier();
147  void globalError(int32_t errorCode, const std::string& errorString);
148 
149  private:
150  void processArgs(std::unique_ptr<helicsCLI11App>& app);
151  std::unique_ptr<helicsCLI11App> generateParser(bool noTypeOption = false);
152  std::shared_ptr<Broker> broker;
153  std::string name;
154 };
155 
158  public:
159  template<class... Args>
160  explicit BrokerKeeper(Args&&... args): brk(std::forward<Args...>(args...))
161  {
162  }
163  BrokerKeeper(BrokerKeeper&& brkeep) = default;
164  BrokerKeeper(const BrokerKeeper& brkeep) = default;
165  BrokerKeeper& operator=(BrokerKeeper&& brkeep) = default;
166  BrokerKeeper& operator=(const BrokerKeeper& brkeep) = default;
168  bool isConnected() { return brk.isConnected(); }
170  void forceTerminate() { brk.forceTerminate(); }
173  {
174  if (brk.isConnected()) {
175  brk.waitForDisconnect();
176  }
177  }
178 
179  private:
180  BrokerApp brk;
181 };
182 
183 } // namespace helics
helics::BrokerKeeper::forceTerminate
void forceTerminate()
Force terminate the broker.
Definition: application_api/BrokerApp.hpp:170
helics::core_type
core_type
Definition: core-types.hpp:37
helics::BrokerApp::getCopyofBrokerPointer
std::shared_ptr< Broker > getCopyofBrokerPointer() const
Definition: application_api/BrokerApp.hpp:140
helics::BrokerApp::isConnected
bool isConnected() const
Definition: BrokerApp.cpp:150
helics::BrokerApp::isOpenToNewFederates
bool isOpenToNewFederates() const
Definition: BrokerApp.cpp:160
helics::BrokerApp::dataLink
void dataLink(const std::string &source, const std::string &target)
Definition: BrokerApp.cpp:175
helics::BrokerApp::clearTimeBarrier
void clearTimeBarrier()
Definition: BrokerApp.cpp:245
helics::BrokerApp::setLogFile
void setLogFile(const std::string &logFile)
Definition: BrokerApp.cpp:260
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helics-time.hpp:27
helics::BrokerApp
Definition: application_api/BrokerApp.hpp:26
helics::BrokerApp::getIdentifier
const std::string & getIdentifier() const
Definition: BrokerApp.cpp:205
helics::BrokerFactory::displayHelp
void displayHelp(core_type type)
Definition: BrokerFactory.cpp:311
helics::BrokerApp::globalError
void globalError(int32_t errorCode, const std::string &errorString)
Definition: BrokerApp.cpp:252
helics_sequencing_mode
helics_sequencing_mode
Definition: helics_enums.h:333
helics::core_type::DEFAULT
@ DEFAULT
ZMQ if available or UDP.
helics::RegistrationFailure
Definition: core-exceptions.hpp:85
helics::BrokerApp::waitForDisconnect
bool waitForDisconnect(std::chrono::milliseconds waitTime=std::chrono::milliseconds(0))
Definition: BrokerApp.cpp:103
helics::BrokerKeeper::~BrokerKeeper
~BrokerKeeper()
the destructor waits for the broker to terminate
Definition: application_api/BrokerApp.hpp:172
helics::BrokerFactory::findBroker
std::shared_ptr< Broker > findBroker(const std::string &brokerName)
Definition: BrokerFactory.cpp:185
helics::BrokerApp::reset
void reset()
Definition: BrokerApp.cpp:267
helics::BrokerApp::forceTerminate
void forceTerminate()
Definition: BrokerApp.cpp:165
helics::BrokerApp::addDestinationFilterToEndpoint
void addDestinationFilterToEndpoint(const std::string &filter, const std::string &endpoint)
Definition: BrokerApp.cpp:189
helics::BrokerApp::setTimeBarrier
void setTimeBarrier(Time barrierTime)
Definition: BrokerApp.cpp:238
helics::core_type::MULTI
@ MULTI
use the multi-broker
helics::BrokerApp::setLoggingLevel
void setLoggingLevel(int loglevel)
Definition: BrokerApp.cpp:231
helics::BrokerFactory::create
std::shared_ptr< Broker > create(core_type type, const std::string &configureString)
Definition: BrokerFactory.cpp:100
helics::BrokerApp::query
std::string query(const std::string &target, const std::string &queryStr, helics_sequencing_mode mode=helics_sequencing_mode_fast)
Definition: BrokerApp.cpp:217
helics::BrokerApp::connect
bool connect()
Definition: BrokerApp.cpp:155
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:168
helics::coreTypeFromString
core_type coreTypeFromString(std::string type) noexcept
Definition: typeOperations.cpp:17
helics::BrokerApp::setGlobal
void setGlobal(const std::string &valueName, const std::string &value)
Definition: BrokerApp.cpp:224
helics::BrokerApp::getAddress
const std::string & getAddress() const
Definition: BrokerApp.cpp:211
helics_sequencing_mode_fast
@ helics_sequencing_mode_fast
Definition: helics_enums.h:335
helics::BrokerKeeper
Definition: application_api/BrokerApp.hpp:157
helics::BrokerApp::BrokerApp
BrokerApp()=default
helics::BrokerApp::makeConnections
void makeConnections(const std::string &file)
Definition: BrokerApp.cpp:197
helics::BrokerApp::addSourceFilterToEndpoint
void addSourceFilterToEndpoint(const std::string &filter, const std::string &endpoint)
Definition: BrokerApp.cpp:182