helics  3.0.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/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, const std::string& brokerName, int argc, char* argv[]);
66  explicit BrokerApp(const std::string& argString);
71  BrokerApp(CoreType ctype, const std::string& argString = std::string{});
77  BrokerApp(CoreType 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 linkEndpoints(const std::string& source, const std::string& target);
97  void dataLink(const std::string& source, const std::string& target);
99  void addSourceFilterToEndpoint(const std::string& filter, const std::string& endpoint);
101  void addDestinationFilterToEndpoint(const std::string& filter, const std::string& endpoint);
103  void makeConnections(const std::string& file);
105  const std::string& getIdentifier() const;
107  const std::string& getAddress() const;
116  std::string query(const std::string& target,
117  const std::string& queryStr,
126  void setGlobal(const std::string& valueName, const std::string& value);
127 
136  void sendCommand(const std::string& target,
137  const std::string& commandStr,
139 
141  void setLogFile(const std::string& logFile);
143  void setLoggingLevel(int loglevel);
145  void reset();
146 #ifdef HELICS_CXX_STATIC_DEFINE
147 
149  auto* operator->() const { return broker.operator->(); }
150 #else
151  BrokerApp* operator->() { return this; }
152  const BrokerApp* operator->() const { return this; }
153 #endif
154 
155  std::shared_ptr<Broker> getCopyofBrokerPointer() const { return broker; }
156 
158  void setTimeBarrier(Time barrierTime);
160  void clearTimeBarrier();
162  void globalError(int32_t errorCode, const std::string& errorString);
163 
164  private:
165  void processArgs(std::unique_ptr<helicsCLI11App>& app);
166  std::unique_ptr<helicsCLI11App> generateParser(bool noTypeOption = false);
167  std::shared_ptr<Broker> broker;
168  std::string name;
169 };
170 
173  public:
174  template<class... Args>
175  explicit BrokerKeeper(Args&&... args): brk(std::forward<Args...>(args...))
176  {
177  }
178  BrokerKeeper(BrokerKeeper&& brkeep) = default;
179  BrokerKeeper(const BrokerKeeper& brkeep) = default;
180  BrokerKeeper& operator=(BrokerKeeper&& brkeep) = default;
181  BrokerKeeper& operator=(const BrokerKeeper& brkeep) = default;
183  bool isConnected() { return brk.isConnected(); }
185  void forceTerminate() { brk.forceTerminate(); }
188  {
189  if (brk.isConnected()) {
190  brk.waitForDisconnect();
191  }
192  }
193 
194  private:
195  BrokerApp brk;
196 };
197 
198 } // namespace helics
helics::BrokerKeeper::forceTerminate
void forceTerminate()
Force terminate the broker.
Definition: application_api/BrokerApp.hpp:185
helics::CoreType::MULTI
@ MULTI
use the multi-broker
helics::BrokerApp::getCopyofBrokerPointer
std::shared_ptr< Broker > getCopyofBrokerPointer() const
Definition: application_api/BrokerApp.hpp:155
helics::BrokerApp::isConnected
bool isConnected() const
Definition: BrokerApp.cpp:148
helics::CoreType
CoreType
Definition: CoreTypes.hpp:36
helics::BrokerFactory::findBroker
std::shared_ptr< Broker > findBroker(const std::string &brokerName)
Definition: BrokerFactory.cpp:182
helics::BrokerApp::isOpenToNewFederates
bool isOpenToNewFederates() const
Definition: BrokerApp.cpp:158
helics::BrokerApp::dataLink
void dataLink(const std::string &source, const std::string &target)
Definition: BrokerApp.cpp:180
helics::coreTypeFromString
CoreType coreTypeFromString(std::string type) noexcept
Definition: typeOperations.cpp:17
helics::BrokerApp::clearTimeBarrier
void clearTimeBarrier()
Definition: BrokerApp.cpp:259
helics::BrokerApp::setLogFile
void setLogFile(const std::string &logFile)
Definition: BrokerApp.cpp:274
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:210
helics::BrokerApp::sendCommand
void sendCommand(const std::string &target, const std::string &commandStr, HelicsSequencingModes mode=HELICS_SEQUENCING_MODE_FAST)
Definition: BrokerApp.cpp:236
helics::BrokerFactory::displayHelp
void displayHelp(CoreType type)
Definition: BrokerFactory.cpp:317
helics::CoreType::DEFAULT
@ DEFAULT
ZMQ if available or UDP.
helics::BrokerApp::linkEndpoints
void linkEndpoints(const std::string &source, const std::string &target)
Definition: BrokerApp.cpp:173
helics::BrokerApp::globalError
void globalError(int32_t errorCode, const std::string &errorString)
Definition: BrokerApp.cpp:266
helics::RegistrationFailure
Definition: core-exceptions.hpp:84
helics::BrokerApp::query
std::string query(const std::string &target, const std::string &queryStr, HelicsSequencingModes mode=HELICS_SEQUENCING_MODE_FAST)
Definition: BrokerApp.cpp:222
helics::BrokerApp::waitForDisconnect
bool waitForDisconnect(std::chrono::milliseconds waitTime=std::chrono::milliseconds(0))
Definition: BrokerApp.cpp:101
helics::BrokerKeeper::~BrokerKeeper
~BrokerKeeper()
the destructor waits for the broker to terminate
Definition: application_api/BrokerApp.hpp:187
helics::BrokerApp::reset
void reset()
Definition: BrokerApp.cpp:281
helics::BrokerApp::forceTerminate
void forceTerminate()
Definition: BrokerApp.cpp:163
helics::BrokerApp::addDestinationFilterToEndpoint
void addDestinationFilterToEndpoint(const std::string &filter, const std::string &endpoint)
Definition: BrokerApp.cpp:194
HelicsSequencingModes
HelicsSequencingModes
Definition: helics_enums.h:363
helics::BrokerApp::setTimeBarrier
void setTimeBarrier(Time barrierTime)
Definition: BrokerApp.cpp:252
helics::BrokerApp::setLoggingLevel
void setLoggingLevel(int loglevel)
Definition: BrokerApp.cpp:245
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:183
HELICS_SEQUENCING_MODE_FAST
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:365
helics::BrokerApp::setGlobal
void setGlobal(const std::string &valueName, const std::string &value)
Definition: BrokerApp.cpp:229
helics::BrokerApp::getAddress
const std::string & getAddress() const
Definition: BrokerApp.cpp:216
helics::BrokerFactory::create
std::shared_ptr< Broker > create(CoreType type, const std::string &configureString)
Definition: BrokerFactory.cpp:98
helics::BrokerKeeper
Definition: application_api/BrokerApp.hpp:172
helics::BrokerApp::BrokerApp
BrokerApp()=default
helics::BrokerApp::makeConnections
void makeConnections(const std::string &file)
Definition: BrokerApp.cpp:202
helics::BrokerApp::addSourceFilterToEndpoint
void addSourceFilterToEndpoint(const std::string &filter, const std::string &endpoint)
Definition: BrokerApp.cpp:187