helics  2.8.1
core/BrokerFactory.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 "Broker.hpp"
10 #include "core-types.hpp"
11 
12 #include <chrono>
13 #include <memory>
14 #include <string>
15 #include <type_traits>
16 #include <vector>
17 
18 namespace helics {
22 namespace BrokerFactory {
23 
24  class BrokerBuilder {
25  public:
27  virtual std::shared_ptr<Broker> build(const std::string& name) = 0;
28  };
29 
31  template<class BrokerTYPE>
32  class BrokerTypeBuilder final: public BrokerBuilder {
33  public:
34  static_assert(std::is_base_of<Broker, BrokerTYPE>::value,
35  "Type does not inherit from helics::Core");
36 
37  using broker_build_type = BrokerTYPE;
38  virtual std::shared_ptr<Broker> build(const std::string& name) override
39  {
40  return std::make_shared<BrokerTYPE>(name);
41  }
42  };
43 
45  void defineBrokerBuilder(std::shared_ptr<BrokerBuilder> cb, const std::string& name, int code);
46 
48  template<class BrokerTYPE>
49  std::shared_ptr<BrokerBuilder> addBrokerType(const std::string& brokerTypeName, int code)
50  {
51  auto bld = std::make_shared<BrokerTypeBuilder<BrokerTYPE>>();
52  std::shared_ptr<BrokerBuilder> bbld = std::static_pointer_cast<BrokerBuilder>(bld);
53  defineBrokerBuilder(bbld, brokerTypeName, code);
54  return bbld;
55  }
61  std::shared_ptr<Broker> create(core_type type, const std::string& configureString);
63  std::shared_ptr<Broker> create(core_type type, int argc, char* argv[]);
65  std::shared_ptr<Broker> create(core_type type, std::vector<std::string> args);
66 
67  std::shared_ptr<Broker>
68  create(core_type type, const std::string& brokerName, const std::string& configureString);
69 
70  std::shared_ptr<Broker>
71  create(core_type type, const std::string& brokerName, int argc, char* argv[]);
72 
74  std::shared_ptr<Broker>
75  create(core_type type, const std::string& brokerName, std::vector<std::string> args);
76 
80  std::shared_ptr<Broker> findBroker(const std::string& brokerName);
83  std::shared_ptr<Broker> getConnectedBroker();
87  std::shared_ptr<Broker> getBrokerByIndex(size_t index);
88 
90  std::shared_ptr<Broker> findJoinableBrokerOfType(core_type type);
91 
93  std::vector<std::shared_ptr<Broker>> getAllBrokers();
94 
96  bool brokersActive();
97 
104  bool registerBroker(const std::shared_ptr<Broker>& broker, core_type type);
108  void unregisterBroker(const std::string& name);
109 
111  void addAssociatedBrokerType(const std::string& name, core_type type);
112 
119  size_t cleanUpBrokers();
120 
128  size_t cleanUpBrokers(std::chrono::milliseconds delay);
129 
133  bool copyBrokerIdentifier(const std::string& copyFromName, const std::string& copyToName);
134 
137 
139  void terminateAllBrokers();
141  void abortAllBrokers(int errorCode, const std::string& errorString);
142 } // namespace BrokerFactory
143 } // namespace helics
core-types.hpp
helics::BrokerFactory::cleanUpBrokers
size_t cleanUpBrokers()
Definition: BrokerFactory.cpp:266
helics::BrokerFactory::copyBrokerIdentifier
bool copyBrokerIdentifier(const std::string &copyFromName, const std::string &copyToName)
Definition: BrokerFactory.cpp:290
helics::core_type
core_type
Definition: core-types.hpp:37
helics::BrokerFactory::brokersActive
bool brokersActive()
Definition: BrokerFactory.cpp:225
helics::BrokerFactory::getConnectedBroker
std::shared_ptr< Broker > getConnectedBroker()
Definition: BrokerFactory.cpp:206
helics::BrokerFactory::BrokerTypeBuilder
Definition: core/BrokerFactory.hpp:32
helics::BrokerFactory::BrokerBuilder
Definition: core/BrokerFactory.hpp:24
helics::BrokerFactory::displayHelp
void displayHelp(core_type type)
Definition: BrokerFactory.cpp:311
helics::core_type::UNRECOGNIZED
@ UNRECOGNIZED
unknown
helics::BrokerFactory::defineBrokerBuilder
void defineBrokerBuilder(std::shared_ptr< BrokerBuilder > cb, const std::string &name, int code)
Definition: BrokerFactory.cpp:84
helics::BrokerFactory::unregisterBroker
void unregisterBroker(const std::string &name)
Definition: BrokerFactory.cpp:295
helics::BrokerFactory::getBrokerByIndex
std::shared_ptr< Broker > getBrokerByIndex(size_t index)
Definition: BrokerFactory.cpp:211
helics::core_type::DEFAULT
@ DEFAULT
ZMQ if available or UDP.
helics::RegistrationFailure
Definition: core-exceptions.hpp:85
helics::core_type::INPROC
@ INPROC
core/broker using a stripped down in process core type
helics::BrokerFactory::findBroker
std::shared_ptr< Broker > findBroker(const std::string &brokerName)
Definition: BrokerFactory.cpp:185
helics::BrokerFactory::addAssociatedBrokerType
void addAssociatedBrokerType(const std::string &name, core_type type)
Definition: BrokerFactory.cpp:303
helics::BrokerFactory::abortAllBrokers
void abortAllBrokers(int errorCode, const std::string &errorString)
Definition: BrokerFactory.cpp:281
helics::BrokerFactory::findJoinableBrokerOfType
std::shared_ptr< Broker > findJoinableBrokerOfType(core_type type)
Definition: BrokerFactory.cpp:217
helics::core_type::IPC
@ IPC
same as INTERPROCESS
helics::core_type::INTERPROCESS
@ INTERPROCESS
use when all federates are on the same machine
helics::BrokerFactory::create
std::shared_ptr< Broker > create(core_type type, const std::string &configureString)
Definition: BrokerFactory.cpp:100
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::core_type::TCP_SS
@ TCP_SS
helics::BrokerFactory::registerBroker
bool registerBroker(const std::shared_ptr< Broker > &broker, core_type type)
Definition: BrokerFactory.cpp:246
helics::BrokerFactory::addBrokerType
std::shared_ptr< BrokerBuilder > addBrokerType(const std::string &brokerTypeName, int code)
Definition: core/BrokerFactory.hpp:49
helics::BrokerFactory::BrokerTypeBuilder::build
virtual std::shared_ptr< Broker > build(const std::string &name) override
Definition: core/BrokerFactory.hpp:38
helics::BrokerFactory::terminateAllBrokers
void terminateAllBrokers()
Definition: BrokerFactory.cpp:272
core-exceptions.hpp
helics::BrokerFactory::getAllBrokers
std::vector< std::shared_ptr< Broker > > getAllBrokers()
Definition: BrokerFactory.cpp:223
helics::BrokerFactory::BrokerBuilder::build
virtual std::shared_ptr< Broker > build(const std::string &name)=0
helics::core_type::NULLCORE
@ NULLCORE
explicit core type that doesn't exist
helics::HelicsException
Definition: core-exceptions.hpp:18
helics::core_type::TEST
@ TEST
use the Test core if all federates are in the same process