helics  2.8.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 "MessageFederate.hpp"
10 
11 #include <memory>
12 #include <string>
13 #include <type_traits>
14 #include <utility>
15 
16 namespace helics {
18 class HELICS_CXX_EXPORT Endpoint {
19  private:
20  MessageFederate* fed{nullptr};
21  interface_handle handle;
22  int referenceIndex{-1};
23  void* dataReference{nullptr};
24  std::string targetDest;
25  std::string actualName;
26  bool disableAssign{false};
27  public:
29  Endpoint() = default;
30 
31  // constructor used by messageFederateManager
32  Endpoint(MessageFederate* mfed, const std::string& name, interface_handle id, void* data):
33  fed(mfed), handle(id), dataReference(data), actualName(name)
34  {
35  }
36 
38  const std::string& name,
39  const std::string& type = std::string()):
40  Endpoint(mFed->registerEndpoint(name, type))
41  {
42  }
43 
44  template<class FedPtr>
45  Endpoint(FedPtr& mFed, const std::string& name, const std::string& type = std::string()):
46  Endpoint(mFed->registerEndpoint(name, type))
47  {
48  static_assert(
49  std::is_base_of<MessageFederate, std::remove_reference_t<decltype(*mFed)>>::value,
50  "first argument must be a pointer to a MessageFederate");
51  }
59  MessageFederate* mFed,
60  const std::string& name,
61  const std::string& type = std::string());
68  template<class FedPtr>
70  FedPtr& mFed,
71  const std::string& name,
72  const std::string& type = std::string()):
73  Endpoint(locality, std::addressof(*mFed), name, type)
74  {
75  static_assert(
76  std::is_base_of<MessageFederate, std::remove_reference_t<decltype(*mFed)>>::value,
77  "second argument must be a pointer to a MessageFederate");
78  }
80  bool isValid() const { return handle.isValid(); }
81  bool operator==(const Endpoint& ept) const { return (handle == ept.handle); }
82  bool operator!=(const Endpoint& ept) const { return (handle != ept.handle); }
83  bool operator<(const Endpoint& ept) const { return (handle < ept.handle); }
89  void send(const std::string& dest, const char* data, size_t data_size) const
90  {
91  fed->sendMessage(*this, dest, data_view(data, data_size));
92  }
93 
95  void subscribe(const std::string& key) { fed->subscribe(*this, key); }
102  void send(const std::string& dest, const char* data, size_t data_size, Time sendTime) const
103  {
104  fed->sendMessage(*this, dest, data_view(data, data_size), sendTime);
105  }
111  void send(const char* data, size_t data_size, Time sendTime) const
112  {
113  fed->sendMessage(*this, targetDest, data_view{data, data_size}, sendTime);
114  }
121  void send(const std::string& dest, const data_view& data) const
122  {
123  fed->sendMessage(*this, dest, data);
124  }
132  void send(const std::string& dest, const data_view& data, Time sendTime) const
133  {
134  fed->sendMessage(*this, dest, data, sendTime);
135  }
140  void send(const char* data, size_t data_size) const
141  {
142  fed->sendMessage(*this, targetDest, data_view{data, data_size});
143  }
149  void send(const data_view& data) const { fed->sendMessage(*this, targetDest, data); }
156  void send(const data_view& data, Time sendTime) const
157  {
158  fed->sendMessage(*this, targetDest, data, sendTime);
159  }
161  void send(std::unique_ptr<Message> mess) const
162  {
163  if (mess->dest.empty()) {
164  mess->dest = targetDest;
165  }
166  fed->sendMessage(*this, std::move(mess));
167  }
172  void send(const Message& mess) const { send(std::make_unique<Message>(mess)); }
174  auto getMessage() const { return fed->getMessage(*this); }
176  bool hasMessage() const { return fed->hasMessage(*this); }
178  auto pendingMessages() const { return fed->pendingMessages(*this); }
185  void setCallback(const std::function<void(const Endpoint&, Time)>& callback)
186  {
187  fed->setMessageNotificationCallback(*this, callback);
188  }
189 
191  void addSourceFilter(const std::string& filterName) { fed->addSourceFilter(*this, filterName); }
193  void addDestinationFilter(const std::string& filterName)
194  {
195  fed->addDestinationFilter(*this, filterName);
196  }
198  void setDefaultDestination(std::string target) { targetDest = std::move(target); }
200  const std::string& getDefaultDestination() const { return targetDest; }
202  const std::string& getName() const { return actualName; }
204  const std::string& getKey() const { return fed->getInterfaceName(handle); }
206  const std::string& getType() const { return fed->getExtractionType(*this); }
208  // int32_t getFilterCount() const {};
210  interface_handle getHandle() const { return handle; }
212  operator interface_handle() const { return handle; }
214  const std::string& getInfo() const { return fed->getInfo(handle); }
216  void setInfo(const std::string& info) { fed->setInfo(handle, info); }
217  void setOption(int32_t option, int32_t value = 1)
218  {
219  fed->setInterfaceOption(handle, option, value);
220  }
222  int32_t getOption(int32_t option) const { return fed->getInterfaceOption(handle, option); }
224  void close() { fed->closeInterface(handle); }
225 
226  private:
227  friend class MessageFederateManager;
228 };
229 } // namespace helics
helics::operator!=
bool operator!=(const data_block &db1, const data_block &db2)
Definition: core-data.hpp:140
data
@ data
print timing+data transmissions
Definition: loggingHelper.hpp:30
helics::Endpoint::isValid
bool isValid() const
Definition: Endpoints.hpp:80
helics::Endpoint::getInfo
const std::string & getInfo() const
Definition: Endpoints.hpp:214
helics::Endpoint::getMessage
auto getMessage() const
Definition: Endpoints.hpp:174
helics::MessageFederate::registerEndpoint
Endpoint & registerEndpoint(const std::string &eptName=std::string(), const std::string &type=std::string())
Definition: MessageFederate.cpp:109
helics::Endpoint::subscribe
void subscribe(const std::string &key)
Definition: Endpoints.hpp:95
helics::MessageFederate::registerGlobalEndpoint
Endpoint & registerGlobalEndpoint(const std::string &eptName, const std::string &type=std::string())
Definition: MessageFederate.cpp:117
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helics-time.hpp:27
helics::Endpoint::setDefaultDestination
void setDefaultDestination(std::string target)
Definition: Endpoints.hpp:198
helics::Endpoint::send
void send(const Message &mess) const
Definition: Endpoints.hpp:172
helics::Endpoint::send
void send(const std::string &dest, const char *data, size_t data_size, Time sendTime) const
Definition: Endpoints.hpp:102
helics::Endpoint
Definition: Endpoints.hpp:18
helics::Endpoint::addDestinationFilter
void addDestinationFilter(const std::string &filterName)
Definition: Endpoints.hpp:193
helics::Endpoint::getName
const std::string & getName() const
Definition: Endpoints.hpp:202
helics::Endpoint::send
void send(const char *data, size_t data_size) const
Definition: Endpoints.hpp:140
helics::data_view
Definition: data_view.hpp:22
helics::MessageFederate
Definition: application_api/MessageFederate.hpp:20
helics::Endpoint::getOption
int32_t getOption(int32_t option) const
Definition: Endpoints.hpp:222
helics::Endpoint::setCallback
void setCallback(const std::function< void(const Endpoint &, Time)> &callback)
Definition: Endpoints.hpp:185
helics::Endpoint::send
void send(std::unique_ptr< Message > mess) const
Definition: Endpoints.hpp:161
helics::Endpoint::getDefaultDestination
const std::string & getDefaultDestination() const
Definition: Endpoints.hpp:200
helics::Endpoint::Endpoint
Endpoint(interface_visibility locality, FedPtr &mFed, const std::string &name, const std::string &type=std::string())
Definition: Endpoints.hpp:69
helics::Endpoint::getHandle
interface_handle getHandle() const
‍** get the number of filters applied to the endpoint*‍/
Definition: Endpoints.hpp:210
helics::Endpoint::setInfo
void setInfo(const std::string &info)
Definition: Endpoints.hpp:216
fed
@ fed
special logging command for message coming from a fed
Definition: loggingHelper.hpp:32
helics::Endpoint::send
void send(const std::string &dest, const data_view &data) const
Definition: Endpoints.hpp:121
helics::Endpoint::send
void send(const char *data, size_t data_size, Time sendTime) const
Definition: Endpoints.hpp:111
helics::interface_handle
Definition: federate_id.hpp:65
helics::Endpoint::hasMessage
bool hasMessage() const
Definition: Endpoints.hpp:176
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::Endpoint::getKey
const std::string & getKey() const
Definition: Endpoints.hpp:204
helics::Endpoint::addSourceFilter
void addSourceFilter(const std::string &filterName)
Definition: Endpoints.hpp:191
helics::Endpoint::Endpoint
Endpoint()=default
helics::Endpoint::pendingMessages
auto pendingMessages() const
Definition: Endpoints.hpp:178
helics::Endpoint::send
void send(const std::string &dest, const char *data, size_t data_size) const
Definition: Endpoints.hpp:89
helics::Endpoint::send
void send(const data_view &data, Time sendTime) const
Definition: Endpoints.hpp:156
helics::interface_visibility
interface_visibility
Definition: helicsTypes.hpp:38
helics::Endpoint::close
void close()
Definition: Endpoints.hpp:224
helics::Endpoint::getType
const std::string & getType() const
Definition: Endpoints.hpp:206
helics::Endpoint::send
void send(const data_view &data) const
Definition: Endpoints.hpp:149
helics::MessageFederateManager
Definition: MessageFederateManager.hpp:34
helics::Endpoint::send
void send(const std::string &dest, const data_view &data, Time sendTime) const
Definition: Endpoints.hpp:132
helics::Message
Definition: core-data.hpp:146