helics  3.0.1
Endpoints.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 "Federate.hpp"
10 #include "data_view.hpp"
11 
12 #include <memory>
13 #include <string>
14 #include <type_traits>
15 #include <utility>
16 
17 namespace helics {
18 class MessageFederate;
19 
21 class HELICS_CXX_EXPORT Endpoint: public Interface {
22  private:
23  MessageFederate* fed{nullptr};
24  int referenceIndex{-1};
25  void* dataReference{nullptr};
26  bool disableAssign{false};
27  std::string defDest;
28  public:
30  Endpoint() = default;
31 
32  // constructor used by messageFederateManager
33  Endpoint(MessageFederate* mFed, const std::string& name, InterfaceHandle id, void* data);
34 
36  const std::string& name,
37  const std::string& type = std::string());
38 
39  template<class FedPtr>
40  Endpoint(FedPtr& mFed, const std::string& name, const std::string& type = std::string()):
41  Endpoint(std::addressof(*mFed), name, type)
42  {
43  static_assert(
44  std::is_base_of<MessageFederate, std::remove_reference_t<decltype(*mFed)>>::value,
45  "first argument must be a pointer to a MessageFederate");
46  }
54  MessageFederate* mFed,
55  const std::string& name,
56  const std::string& type = std::string());
63  template<class FedPtr>
65  FedPtr& mFed,
66  const std::string& name,
67  const std::string& type = std::string()):
68  Endpoint(locality, std::addressof(*mFed), name, type)
69  {
70  static_assert(
71  std::is_base_of<MessageFederate, std::remove_reference_t<decltype(*mFed)>>::value,
72  "second argument must be a pointer to a MessageFederate");
73  }
74  const std::string& getType() { return getExtractionType(); }
79  void send(const char* data, size_t data_size) const;
80 
82  void subscribe(const std::string& key);
83 
89  void sendTo(const char* data, size_t data_size, std::string_view dest) const;
95  void sendAt(const char* data, size_t data_size, Time sendTime) const;
96 
103  void sendTo(const data_view& data, std::string_view dest) const
104  {
105  sendTo(data.data(), data.size(), dest);
106  }
114  void sendToAt(const data_view& data, std::string_view dest, Time sendTime) const
115  {
116  sendToAt(data.data(), data.size(), dest, sendTime);
117  }
124  void sendToAt(const char* data, size_t data_size, std::string_view dest, Time sendTime) const;
125 
130  void send(const void* data, size_t data_size) const;
131 
137  void send(const data_view& data) const { send(data.data(), data.size()); }
144  void sendAt(const data_view& data, Time sendTime) const
145  {
146  sendAt(data.data(), data.size(), sendTime);
147  }
149  void send(std::unique_ptr<Message> mess) const;
150 
155  void send(const Message& mess) const { send(std::make_unique<Message>(mess)); }
156 
158  std::unique_ptr<Message> getMessage() const;
160  bool hasMessage() const;
162  std::uint64_t pendingMessageCount() const;
169  void setCallback(const std::function<void(const Endpoint&, Time)>& callback);
170 
172  void addSourceFilter(const std::string& filterName);
174  void addDestinationFilter(const std::string& filterName);
176  void setDefaultDestination(std::string_view target);
178  const std::string& getDefaultDestination() const;
179  virtual const std::string& getDisplayName() const override { return getName(); }
180 
181  private:
182  friend class MessageFederateManager;
183 };
184 } // namespace helics
helics::Interface::cr
Core * cr
pointer to the core object
Definition: application_api/Federate.hpp:629
helics::Endpoint::getDefaultDestination
const std::string & getDefaultDestination() const
Definition: Endpoints.cpp:136
helics::InterfaceVisibility
InterfaceVisibility
Definition: helicsTypes.hpp:39
helics::MessageFederate::registerEndpoint
Endpoint & registerEndpoint(const std::string &eptName=std::string(), const std::string &type=std::string())
Definition: MessageFederate.cpp:109
helics::Endpoint::sendAt
void sendAt(const char *data, size_t data_size, Time sendTime) const
Definition: Endpoints.cpp:65
helics::Endpoint::subscribe
void subscribe(const std::string &key)
Definition: Endpoints.cpp:142
helics::Core::send
virtual void send(InterfaceHandle sourceHandle, const void *data, uint64_t length)=0
helics::Interface::addDestinationTarget
void addDestinationTarget(std::string_view newTarget)
Definition: Federate.cpp:1375
helics::MessageFederate::registerGlobalEndpoint
Endpoint & registerGlobalEndpoint(const std::string &eptName, const std::string &type=std::string())
Definition: MessageFederate.cpp:126
helics::Endpoint::getDisplayName
virtual const std::string & getDisplayName() const override
Definition: Endpoints.hpp:179
helics::Federate::getCurrentMode
Modes getCurrentMode() const
Definition: application_api/Federate.hpp:565
helics::InterfaceHandle
Definition: LocalFederateId.hpp:65
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::Endpoint::send
void send(const Message &mess) const
Definition: Endpoints.hpp:155
helics::Core::addSourceTarget
virtual void addSourceTarget(InterfaceHandle handle, std::string_view name, InterfaceType hint=InterfaceType::UNKNOWN)=0
helics::Endpoint
Definition: Endpoints.hpp:21
helics::Endpoint::addDestinationFilter
void addDestinationFilter(const std::string &filterName)
Definition: Endpoints.cpp:181
helics::Core::sendToAt
virtual void sendToAt(InterfaceHandle sourceHandle, const void *data, uint64_t length, std::string_view destination, Time time)=0
helics::Endpoint::send
void send(const char *data, size_t data_size) const
Definition: Endpoints.cpp:38
helics::InterfaceType::FILTER
@ FILTER
handle to a filter
helics::data_view
Definition: data_view.hpp:22
helics::MessageFederate
Definition: application_api/MessageFederate.hpp:22
helics::Endpoint::setCallback
void setCallback(const std::function< void(const Endpoint &, Time)> &callback)
Definition: Endpoints.cpp:166
helics::MessageFederate::getMessage
std::unique_ptr< Message > getMessage(const Endpoint &ept)
Definition: MessageFederate.cpp:289
helics::InvalidFunctionCall
Definition: core-exceptions.hpp:66
helics::MessageFederate::pendingMessageCount
uint64_t pendingMessageCount(const Endpoint &ept) const
Definition: MessageFederate.cpp:265
helics::Interface
Definition: application_api/Federate.hpp:627
helics::MessageFederate::hasMessage
bool hasMessage() const
Definition: MessageFederate.cpp:249
helics::Endpoint::setDefaultDestination
void setDefaultDestination(std::string_view target)
Definition: Endpoints.cpp:128
helics::Core::getDestinationTargets
virtual const std::string & getDestinationTargets(InterfaceHandle handle) const =0
helics::Core::sendMessage
virtual void sendMessage(InterfaceHandle sourceHandle, std::unique_ptr< Message > message)=0
helics::Interface::handle
InterfaceHandle handle
the id as generated by the Federate
Definition: application_api/Federate.hpp:630
helics::MessageFederate::setMessageNotificationCallback
void setMessageNotificationCallback(const std::function< void(Endpoint &, Time)> &callback)
helics::Endpoint::getMessage
std::unique_ptr< Message > getMessage() const
Definition: Endpoints.cpp:149
helics::Federate::Modes::INITIALIZING
@ INITIALIZING
helics::Core::sendAt
virtual void sendAt(InterfaceHandle sourceHandle, const void *data, uint64_t length, Time time)=0
helics::Endpoint::hasMessage
bool hasMessage() const
Definition: Endpoints.cpp:155
helics::Core::addDestinationTarget
virtual void addDestinationTarget(InterfaceHandle handle, std::string_view dest, InterfaceType hint=InterfaceType::UNKNOWN)=0
helics::Core::sendTo
virtual void sendTo(InterfaceHandle sourceHandle, const void *data, uint64_t length, std::string_view destination)=0
helics::Endpoint::Endpoint
Endpoint(InterfaceVisibility locality, FedPtr &mFed, const std::string &name, const std::string &type=std::string())
Definition: Endpoints.hpp:64
helics::data_view::data
const char * data() const noexcept
Definition: data_view.hpp:107
helics::Endpoint::sendAt
void sendAt(const data_view &data, Time sendTime) const
Definition: Endpoints.hpp:144
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::Endpoint::sendToAt
void sendToAt(const data_view &data, std::string_view dest, Time sendTime) const
Definition: Endpoints.hpp:114
helics::Endpoint::sendTo
void sendTo(const data_view &data, std::string_view dest) const
Definition: Endpoints.hpp:103
helics::data_view::size
size_t size() const noexcept
Definition: data_view.hpp:114
helics::Endpoint::addSourceFilter
void addSourceFilter(const std::string &filterName)
Definition: Endpoints.cpp:174
helics::Endpoint::Endpoint
Endpoint()=default
helics::Endpoint::pendingMessageCount
std::uint64_t pendingMessageCount() const
Definition: Endpoints.cpp:161
helics::Endpoint::send
void send(const data_view &data) const
Definition: Endpoints.hpp:137
helics::Endpoint::sendTo
void sendTo(const char *data, size_t data_size, std::string_view dest) const
Definition: Endpoints.cpp:50
helics::MessageFederateManager
Definition: MessageFederateManager.hpp:34
helics::InterfaceType::PUBLICATION
@ PUBLICATION
handle to output interface
helics::Federate::Modes::EXECUTING
@ EXECUTING
helics::Message
Definition: core-data.hpp:29