helics  3.3.0
application_api/MessageFederate.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 "Endpoints.hpp"
10 #include "Federate.hpp"
11 #include "data_view.hpp"
12 
13 #include <functional>
14 #include <memory>
15 #include <string>
16 #include <string_view>
17 
18 namespace helics {
19 class MessageFederateManager;
20 class Endpoint;
22 class HELICS_CXX_EXPORT MessageFederate:
23  public virtual Federate // using virtual inheritance to allow combination federate
24 {
25  public:
31  MessageFederate(std::string_view fedName, const FederateInfo& fi);
39  MessageFederate(std::string_view fedName,
40  const std::shared_ptr<Core>& core,
41  const FederateInfo& fi = FederateInfo{});
42 
50  MessageFederate(std::string_view fedName,
51  CoreApp& core,
52  const FederateInfo& fi = FederateInfo{});
53 
58  MessageFederate(std::string_view name, const std::string& configString);
63  explicit MessageFederate(const std::string& configString);
64 
71  explicit MessageFederate(const char* configString);
73  MessageFederate(MessageFederate&& mFed) noexcept;
75  MessageFederate(const MessageFederate& mFed) = delete;
80  explicit MessageFederate(bool res);
81  // copy constructor and copy assignment are disabled
83  virtual ~MessageFederate();
85  MessageFederate& operator=(MessageFederate&& mFed) noexcept;
87  MessageFederate& operator=(const MessageFederate& mFed) = delete;
88 
89  protected:
90  virtual void startupToInitializeStateTransition() override;
91  virtual void initializeToExecuteStateTransition(IterationResult result) override;
92  virtual void updateTime(Time newTime, Time oldTime) override;
93  virtual std::string localQuery(std::string_view queryStr) const override;
94 
95  public:
101  Endpoint& registerEndpoint(std::string_view eptName = std::string_view(),
102  std::string_view type = std::string_view());
103 
109  Endpoint& registerTargetedEndpoint(std::string_view eptName = std::string_view(),
110  std::string_view type = std::string_view());
111 
118  Endpoint& registerGlobalEndpoint(std::string_view eptName,
119  std::string_view type = std::string_view());
120 
126  Endpoint& registerGlobalTargetedEndpoint(std::string_view eptName,
127  std::string_view type = std::string_view());
128 
135  Endpoint& registerIndexedEndpoint(std::string_view eptName,
136  int index1,
137  std::string_view type = std::string_view())
138  {
139  return registerGlobalEndpoint(std::string(eptName) + '_' + std::to_string(index1), type);
140  }
141 
142  virtual void registerInterfaces(const std::string& configString) override;
143 
149  void registerMessageInterfaces(const std::string& configString);
150 
151  private:
156  void registerMessageInterfacesJson(const std::string& jsonString);
157 
162  void registerMessageInterfacesToml(const std::string& tomlString);
163 
164  public:
169  void subscribe(const Endpoint& ept, std::string_view key);
171  bool hasMessage() const;
172  /* check if a given endpoint has any unread messages*/
173  bool hasMessage(const Endpoint& ept) const;
174 
178  uint64_t pendingMessageCount(const Endpoint& ept) const;
182  uint64_t pendingMessageCount() const;
186  std::unique_ptr<Message> getMessage(const Endpoint& ept);
191  std::unique_ptr<Message> getMessage();
192 
196  Endpoint& getEndpoint(std::string_view name) const;
197 
201  Endpoint& getEndpoint(int index) const;
202 
206  void setMessageNotificationCallback(const std::function<void(Endpoint&, Time)>& callback);
211  void setMessageNotificationCallback(const Endpoint& ept,
212  const std::function<void(Endpoint&, Time)>& callback);
213 
214  virtual void disconnect() override;
215 
217  int getEndpointCount() const;
218 
219  private:
221  std::unique_ptr<MessageFederateManager> mfManager;
222 };
223 } // namespace helics
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::Endpoint
Definition: Endpoints.hpp:21
helics::MessageFederate
Definition: application_api/MessageFederate.hpp:22
helics::CoreApp
Definition: application_api/CoreApp.hpp:25
helics::FederateInfo
Definition: FederateInfo.hpp:24
helics::IterationResult
IterationResult
Definition: CoreTypes.hpp:83
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::Federate
Definition: application_api/Federate.hpp:48
helics::MessageFederate::registerIndexedEndpoint
Endpoint & registerIndexedEndpoint(std::string_view eptName, int index1, std::string_view type=std::string_view())
Definition: application_api/MessageFederate.hpp:135