helics  3.5.2
application_api/MessageFederate.hpp
1 /*
2 Copyright (c) 2017-2024,
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& fedInfo);
39  MessageFederate(std::string_view fedName,
40  const std::shared_ptr<Core>& core,
41  const FederateInfo& fedInfo = FederateInfo{});
42 
50  MessageFederate(std::string_view fedName,
51  CoreApp& core,
52  const FederateInfo& fedInfo = 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(iteration_time 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:
100  Endpoint& registerEndpoint(std::string_view eptName = std::string_view(),
101  std::string_view type = std::string_view());
102 
108  Endpoint& registerTargetedEndpoint(std::string_view eptName = std::string_view(),
109  std::string_view type = std::string_view());
110 
116  Endpoint& registerGlobalEndpoint(std::string_view eptName,
117  std::string_view type = std::string_view());
118 
124  Endpoint& registerGlobalTargetedEndpoint(std::string_view eptName,
125  std::string_view type = std::string_view());
126 
133  Endpoint& registerIndexedEndpoint(std::string_view eptName,
134  int index1,
135  std::string_view type = std::string_view())
136  {
137  return registerGlobalEndpoint(std::string(eptName) + '_' + std::to_string(index1), type);
138  }
139 
144  Endpoint& registerDataSink(std::string_view sinkName = std::string_view());
145 
146  virtual void registerInterfaces(const std::string& configString) override;
147 
153  void registerMessageInterfaces(const std::string& configString);
154 
155  private:
157  void loadFederateData();
162  void registerMessageInterfacesJson(const std::string& jsonString);
163 
165  void registerMessageInterfacesJsonDetail(Json::Value& json, bool defaultGlobal);
166 
171  void registerMessageInterfacesToml(const std::string& tomlString);
172 
174  Endpoint& registerEndpoint(std::string_view eptName,
175  std::string_view type,
176  bool global,
177  bool targeted);
178 
179  public:
184  void subscribe(const Endpoint& ept, std::string_view key);
186  bool hasMessage() const;
187  /* check if a given endpoint has any unread messages*/
188  bool hasMessage(const Endpoint& ept) const;
189 
193  uint64_t pendingMessageCount(const Endpoint& ept) const;
197  uint64_t pendingMessageCount() const;
201  std::unique_ptr<Message> getMessage(const Endpoint& ept);
206  std::unique_ptr<Message> getMessage();
207 
211  Endpoint& getEndpoint(std::string_view name) const;
212 
216  Endpoint& getEndpoint(int index) const;
217 
221  Endpoint& getDataSink(std::string_view name) const;
222 
226  void setMessageNotificationCallback(const std::function<void(Endpoint&, Time)>& callback);
232  const std::function<void(Endpoint&, Time)>& callback);
233 
234  virtual void disconnect() override;
235 
237  int getEndpointCount() const;
238 
239  private:
241  std::unique_ptr<MessageFederateManager> mfManager;
242 };
243 } // namespace helics
Definition: application_api/CoreApp.hpp:25
Definition: Endpoints.hpp:21
Definition: FederateInfo.hpp:29
Definition: application_api/Federate.hpp:48
Definition: application_api/MessageFederate.hpp:24
Endpoint & registerIndexedEndpoint(std::string_view eptName, int index1, std::string_view type=std::string_view())
Definition: application_api/MessageFederate.hpp:133
MessageFederate & operator=(const MessageFederate &mFed)=delete
MessageFederate(MessageFederate &&mFed) noexcept
void setMessageNotificationCallback(const Endpoint &ept, const std::function< void(Endpoint &, Time)> &callback)
MessageFederate(const MessageFederate &mFed)=delete
void setMessageNotificationCallback(const std::function< void(Endpoint &, Time)> &callback)
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
Definition: helicsTime.hpp:43