helics  2.8.1
Endpoint.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 #ifndef HELICS_CPP98_ENDPOINT_HPP_
8 #define HELICS_CPP98_ENDPOINT_HPP_
9 #pragma once
10 
11 #include "../shared_api_library/MessageFederate.h"
12 #include "helicsExceptions.hpp"
13 
14 #include <string>
15 #include <vector>
16 
17 namespace helicscpp {
18 class Endpoint;
19 class Federate;
20 
21 class Message {
22  public:
24  Message() HELICS_NOTHROW: mo(HELICS_NULL_POINTER) {}
26  explicit Message(const Federate& fed);
28  explicit Message(const Endpoint& ept);
30  explicit Message(helics_message_object hmo) HELICS_NOTHROW: mo(hmo) {}
31 
33  Message(const Message& mess) HELICS_NOTHROW:
34  mo(helicsMessageClone(mess.mo, HELICS_IGNORE_ERROR))
35  {
36  }
38  Message& operator=(const Message& mess) HELICS_NOTHROW
39  {
40  if (mo != HELICS_NULL_POINTER) {
42  }
43  mo = helicsMessageClone(mess.mo, HELICS_IGNORE_ERROR);
44  return *this;
45  }
46 #ifdef HELICS_HAS_RVALUE_REFS
47 
48  Message(Message&& mess) HELICS_NOTHROW: mo(mess.release()) {}
50  Message& operator=(Message&& mess) HELICS_NOTHROW
51  {
52  mo = mess.release();
53  return *this;
54  }
55 #endif
56 
58  {
59  if (mo != HELICS_NULL_POINTER) {
61  }
62  }
64  operator helics_message_object() const { return mo; }
66  bool isValid() const { return (helicsMessageIsValid(mo) == helics_true); }
68  const char* source() const { return helicsMessageGetSource(mo); }
70  Message& source(const std::string& src)
71  {
72  helicsMessageSetSource(mo, src.c_str(), hThrowOnError());
73  return *this;
74  }
76  Message& source(const char* src)
77  {
79  return *this;
80  }
82  const char* destination() const { return helicsMessageGetDestination(mo); }
84  Message& destination(const std::string& dest)
85  {
86  helicsMessageSetDestination(mo, dest.c_str(), hThrowOnError());
87  return *this;
88  }
90  Message& destination(const char* dest)
91  {
93  return *this;
94  }
97  const char* originalSource() const { return helicsMessageGetOriginalSource(mo); }
99  Message& originalSource(const std::string& osrc)
100  {
101  helicsMessageSetOriginalSource(mo, osrc.c_str(), hThrowOnError());
102  return *this;
103  }
105  const char* originalDestination() const { return helicsMessageGetOriginalDestination(mo); }
107  Message& originalDestination(const std::string& odest)
108  {
110  return *this;
111  }
113  int size() const { return helicsMessageGetRawDataSize(mo); }
115  void resize(int newSize) { helicsMessageResize(mo, newSize, hThrowOnError()); }
118  void reserve(int newSize) { helicsMessageReserve(mo, newSize, hThrowOnError()); }
120  void* data() const { return helicsMessageGetRawDataPointer(mo); }
122  Message& data(const void* raw, int size)
123  {
125  return *this;
126  }
128  Message& data(const std::string& str)
129  {
130  helicsMessageSetString(mo, str.c_str(), hThrowOnError());
131  return *this;
132  }
134  Message& data(const char* str)
135  {
137  return *this;
138  }
140  Message& append(const void* raw, int size)
141  {
143  return *this;
144  }
146  Message& append(const std::string& str)
147  {
148  helicsMessageAppendData(mo, str.c_str(), static_cast<int>(str.size()), hThrowOnError());
149  return *this;
150  }
152  const char* c_str() const { return helicsMessageGetString(mo); }
154  helics_time time() const { return helicsMessageGetTime(mo); }
157  {
159  return *this;
160  }
162  Message& setFlag(int flag, bool val)
163  {
165  return *this;
166  }
168  bool checkFlag(int flag) const { return (helicsMessageCheckFlag(mo, flag) == helics_true); }
170  int messageID() const { return helicsMessageGetMessageID(mo); }
172  Message& messageID(int newId)
173  {
175  return *this;
176  }
180  {
181  helics_message_object mreturn = mo;
182  mo = HELICS_NULL_POINTER;
183  return mreturn;
184  }
185  void clear() { helicsMessageClear(mo, HELICS_IGNORE_ERROR); }
187  Message& newMessageObject(const Federate& fed);
188 
190  Message& newMessageObject(const Endpoint& ept);
191 
192  private:
194 };
195 
197 class Endpoint {
198  public:
200  explicit Endpoint(helics_endpoint hep) HELICS_NOTHROW: ep(hep) {}
202  Endpoint() HELICS_NOTHROW: ep(HELICS_NULL_POINTER) {}
204  Endpoint(const Endpoint& endpoint) HELICS_NOTHROW: ep(endpoint.ep) {}
206  Endpoint& operator=(const Endpoint& endpoint)
207  {
208  ep = endpoint.ep;
209  return *this;
210  }
212  operator helics_endpoint() { return ep; }
214  helics_endpoint baseObject() const { return ep; }
216  bool isValid() const { return (helicsEndpointIsValid(ep) == helics_true); }
217  /* Checks if endpoint has unread messages **/
218  bool hasMessage() const
219  {
220  // returns int, 1 = true, 0 = false
221  return helicsEndpointHasMessage(ep) > 0;
222  }
224  void setDefaultDestination(const std::string& dest)
225  {
227  }
229  const char* getDefaultDestination() const { return helicsEndpointGetDefaultDestination(ep); }
231  uint64_t pendingMessages() const { return helicsEndpointPendingMessageCount(ep); }
232 
235 
238  {
240  }
241 
247  void sendMessage(const char* data, size_t data_size)
248  {
250  ep, HELICS_NULL_POINTER, data, static_cast<int>(data_size), hThrowOnError());
251  }
252 
258  void sendMessage(const std::string& dest, const char* data, size_t data_size)
259  {
261  ep, dest.c_str(), data, static_cast<int>(data_size), hThrowOnError());
262  }
268  void sendMessage(const char* data, size_t data_size, helics_time time)
269  {
271  ep, HELICS_NULL_POINTER, data, static_cast<int>(data_size), time, hThrowOnError());
272  }
279  void sendMessage(const std::string& dest, const char* data, size_t data_size, helics_time time)
280  {
282  ep, dest.c_str(), data, static_cast<int>(data_size), time, hThrowOnError());
283  }
287  void sendMessage(const std::string& data)
288  {
290  ep, HELICS_NULL_POINTER, data.c_str(), static_cast<int>(data.size()), hThrowOnError());
291  }
292 
297  void sendMessage(const std::string& dest, const std::string& data)
298  {
300  ep, dest.c_str(), data.c_str(), static_cast<int>(data.size()), hThrowOnError());
301  }
306  void sendMessage(const std::string& data, helics_time time)
307  {
309  HELICS_NULL_POINTER,
310  data.c_str(),
311  static_cast<int>(data.size()),
312  time,
313  hThrowOnError());
314  }
320  void sendMessage(const std::string& dest, const std::string& data, helics_time time)
321  {
323  ep, dest.c_str(), data.c_str(), static_cast<int>(data.size()), time, hThrowOnError());
324  }
325 
329  void sendMessage(const std::vector<char>& data)
330  {
332  ep, HELICS_NULL_POINTER, data.data(), static_cast<int>(data.size()), hThrowOnError());
333  }
334 
339  void sendMessage(const std::string& dest, const std::vector<char>& data)
340  {
342  ep, dest.c_str(), data.data(), static_cast<int>(data.size()), hThrowOnError());
343  }
348  void sendMessage(const std::vector<char>& data, helics_time time)
349  {
351  HELICS_NULL_POINTER,
352  data.data(),
353  static_cast<int>(data.size()),
354  time,
355  hThrowOnError());
356  }
362  void sendMessage(const std::string& dest, const std::vector<char>& data, helics_time time)
363  {
365  ep, dest.c_str(), data.data(), static_cast<int>(data.size()), time, hThrowOnError());
366  }
367 
370  void sendMessage(const Message& message)
371  {
372  // returns helicsStatus
374  }
375 #ifdef HELICS_HAS_RVALUE_REFS
376 
378  void sendMessage(Message&& message)
379  {
380  // returns helicsStatus
381  helicsEndpointSendMessageObjectZeroCopy(ep, message.release(), hThrowOnError());
382  }
383 #endif
384 
387  {
388  // returns helicsStatus
390  static_cast<helics_message_object>(message),
391  hThrowOnError());
392  message.release();
393  }
395  const char* getName() const { return helicsEndpointGetName(ep); }
397  const char* getType() { return helicsEndpointGetType(ep); }
398 
400  const char* getInfo() const { return helicsEndpointGetInfo(ep); }
402  void setInfo(const std::string& info)
403  {
404  helicsEndpointSetInfo(ep, info.c_str(), HELICS_IGNORE_ERROR);
405  }
406 
407  private:
408  helics_endpoint ep;
409 };
410 
411 inline Message::Message(const Endpoint& ept):
412  mo(helicsEndpointCreateMessageObject(ept.baseObject(), hThrowOnError()))
413 {
414 }
415 
417 {
418  helics_message_object newmo =
420  if (mo != HELICS_NULL_POINTER) {
421  helicsMessageFree(mo);
422  }
423  mo = newmo;
424  return *this;
425 }
426 
427 } // namespace helicscpp
428 #endif
data
@ data
print timing+data transmissions
Definition: loggingHelper.hpp:30
helicscpp::Endpoint::getName
const char * getName() const
Definition: Endpoint.hpp:395
helicscpp::Message::source
const char * source() const
Definition: Endpoint.hpp:68
helicscpp::Message
Definition: Endpoint.hpp:21
helicscpp::Message::reserve
void reserve(int newSize)
Definition: Endpoint.hpp:118
helicscpp::Endpoint::getInfo
const char * getInfo() const
Definition: Endpoint.hpp:400
helicsMessageSetMessageID
void helicsMessageSetMessageID(helics_message_object message, int32_t messageID, helics_error *err)
Definition: MessageFederateExport.cpp:950
helicscpp::Message::destination
Message & destination(const char *dest)
Definition: Endpoint.hpp:90
helicscpp::Endpoint::pendingMessages
uint64_t pendingMessages() const
Definition: Endpoint.hpp:231
helicscpp::Endpoint::sendMessage
void sendMessage(const char *data, size_t data_size, helics_time time)
Definition: Endpoint.hpp:268
helicsMessageGetMessageID
int helicsMessageGetMessageID(helics_message_object message)
Definition: MessageFederateExport.cpp:798
helicsEndpointSendMessageObject
void helicsEndpointSendMessageObject(helics_endpoint endpoint, helics_message_object message, helics_error *err)
Definition: MessageFederateExport.cpp:276
helicsMessageSetOriginalDestination
void helicsMessageSetOriginalDestination(helics_message_object message, const char *dst, helics_error *err)
Definition: MessageFederateExport.cpp:905
helicsEndpointPendingMessageCount
int helicsEndpointPendingMessageCount(helics_endpoint endpoint)
Definition: MessageFederateExport.cpp:370
helicsEndpointSetDefaultDestination
void helicsEndpointSetDefaultDestination(helics_endpoint endpoint, const char *dst, helics_error *err)
Definition: MessageFederateExport.cpp:154
helicsMessageSetTime
void helicsMessageSetTime(helics_message_object message, helics_time time, helics_error *err)
Definition: MessageFederateExport.cpp:913
helicscpp::Endpoint::sendMessage
void sendMessage(const std::string &dest, const std::string &data, helics_time time)
Definition: Endpoint.hpp:320
helicscpp::Endpoint::sendMessage
void sendMessage(const std::vector< char > &data)
Definition: Endpoint.hpp:329
helicscpp::Message::destination
Message & destination(const std::string &dest)
Definition: Endpoint.hpp:84
helics_message_object
void * helics_message_object
Definition: api-data.h:76
helicscpp::Message::release
helics_message_object release()
Definition: Endpoint.hpp:179
helicsMessageClone
helics_message_object helicsMessageClone(helics_message_object message, helics_error *err)
Definition: MessageFederateExport.cpp:1042
helics_time
double helics_time
Definition: api-data.h:81
helicscpp::Federate
Definition: cpp98/Federate.hpp:186
helicsMessageGetOriginalDestination
const char * helicsMessageGetOriginalDestination(helics_message_object message)
Definition: MessageFederateExport.cpp:780
helicscpp::Message::time
Message & time(helics_time val)
Definition: Endpoint.hpp:156
helicscpp::Message::append
Message & append(const std::string &str)
Definition: Endpoint.hpp:146
helicscpp::Message::setFlag
Message & setFlag(int flag, bool val)
Definition: Endpoint.hpp:162
helicsEndpointIsValid
helics_bool helicsEndpointIsValid(helics_endpoint endpoint)
Definition: MessageFederateExport.cpp:145
helicscpp::Message::resize
void resize(int newSize)
Definition: Endpoint.hpp:115
helicsMessageClear
void helicsMessageClear(helics_message_object message, helics_error *err)
Definition: MessageFederateExport.cpp:1013
helics_false
const helics_bool helics_false
Definition: api-data.h:95
helicsEndpointSendEventRaw
void helicsEndpointSendEventRaw(helics_endpoint endpoint, const char *dst, const void *data, int inputDataLength, helics_time time, helics_error *err)
Definition: MessageFederateExport.cpp:207
helicscpp::Message::checkFlag
bool checkFlag(int flag) const
Definition: Endpoint.hpp:168
helicsMessageSetFlagOption
void helicsMessageSetFlagOption(helics_message_object message, int flag, helics_bool flagValue, helics_error *err)
Definition: MessageFederateExport.cpp:968
helics_true
const helics_bool helics_true
Definition: api-data.h:94
helicscpp::Message::operator=
Message & operator=(const Message &mess) HELICS_NOTHROW
Definition: Endpoint.hpp:38
helicscpp::Message::originalSource
const char * originalSource() const
Definition: Endpoint.hpp:97
helicscpp::Endpoint::sendMessage
void sendMessage(const std::string &dest, const std::string &data)
Definition: Endpoint.hpp:297
helicsEndpointCreateMessageObject
helics_message_object helicsEndpointCreateMessageObject(helics_endpoint endpoint, helics_error *err)
Definition: MessageFederateExport.cpp:574
helicscpp::Message::Message
Message(const Message &mess) HELICS_NOTHROW
Definition: Endpoint.hpp:33
helicsMessageGetString
const char * helicsMessageGetString(helics_message_object message)
Definition: MessageFederateExport.cpp:820
helicsMessageIsValid
helics_bool helicsMessageIsValid(helics_message_object message)
Definition: MessageFederateExport.cpp:871
helicsMessageCheckFlag
helics_bool helicsMessageCheckFlag(helics_message_object message, int flag)
Definition: MessageFederateExport.cpp:807
helicscpp::Endpoint::sendMessage
void sendMessage(const std::string &data, helics_time time)
Definition: Endpoint.hpp:306
helicsEndpointGetType
const char * helicsEndpointGetType(helics_endpoint endpoint)
Definition: MessageFederateExport.cpp:629
helicscpp::Endpoint::getMessage
Message getMessage()
Definition: Endpoint.hpp:234
helicsMessageSetOriginalSource
void helicsMessageSetOriginalSource(helics_message_object message, const char *src, helics_error *err)
Definition: MessageFederateExport.cpp:897
helicsMessageGetDestination
const char * helicsMessageGetDestination(helics_message_object message)
Definition: MessageFederateExport.cpp:762
helicscpp::Endpoint::baseObject
helics_endpoint baseObject() const
Definition: Endpoint.hpp:214
helicscpp::Message::data
Message & data(const char *str)
Definition: Endpoint.hpp:134
helicsEndpointSendMessageRaw
void helicsEndpointSendMessageRaw(helics_endpoint endpoint, const char *dst, const void *data, int inputDataLength, helics_error *err)
Definition: MessageFederateExport.cpp:181
helicsMessageSetSource
void helicsMessageSetSource(helics_message_object message, const char *src, helics_error *err)
Definition: MessageFederateExport.cpp:880
helicscpp::Message::size
int size() const
Definition: Endpoint.hpp:113
helicscpp::Message::append
Message & append(const void *raw, int size)
Definition: Endpoint.hpp:140
helicscpp::Endpoint::sendMessage
void sendMessage(const std::string &dest, const std::vector< char > &data)
Definition: Endpoint.hpp:339
helicscpp::Endpoint::sendMessageZeroCopy
void sendMessageZeroCopy(Message &message)
Definition: Endpoint.hpp:386
helicscpp::Message::originalDestination
Message & originalDestination(const std::string &odest)
Definition: Endpoint.hpp:107
helicscpp::Message::data
Message & data(const void *raw, int size)
Definition: Endpoint.hpp:122
helics_endpoint
void * helics_endpoint
Definition: api-data.h:36
helicsMessageGetTime
helics_time helicsMessageGetTime(helics_message_object message)
Definition: MessageFederateExport.cpp:789
helicscpp::Message::Message
Message(helics_message_object hmo) HELICS_NOTHROW
Definition: Endpoint.hpp:30
fed
@ fed
special logging command for message coming from a fed
Definition: loggingHelper.hpp:32
helicsMessageResize
void helicsMessageResize(helics_message_object message, int newSize, helics_error *err)
Definition: MessageFederateExport.cpp:922
helicscpp::hThrowOnError
Definition: helicsExceptions.hpp:38
helicscpp::Endpoint
Definition: Endpoint.hpp:197
helicscpp::Endpoint::getDefaultDestination
const char * getDefaultDestination() const
Definition: Endpoint.hpp:229
helicscpp::Message::messageID
int messageID() const
Definition: Endpoint.hpp:170
helicscpp::Endpoint::sendMessage
void sendMessage(const std::string &dest, const char *data, size_t data_size, helics_time time)
Definition: Endpoint.hpp:279
helicscpp::Message::originalSource
Message & originalSource(const std::string &osrc)
Definition: Endpoint.hpp:99
helicsMessageSetDestination
void helicsMessageSetDestination(helics_message_object message, const char *dst, helics_error *err)
Definition: MessageFederateExport.cpp:889
helicsMessageSetString
void helicsMessageSetString(helics_message_object message, const char *str, helics_error *err)
Definition: MessageFederateExport.cpp:986
helicscpp::Message::originalDestination
const char * originalDestination() const
Definition: Endpoint.hpp:105
helicscpp::Message::data
Message & data(const std::string &str)
Definition: Endpoint.hpp:128
helicsMessageGetSource
const char * helicsMessageGetSource(helics_message_object message)
Definition: MessageFederateExport.cpp:753
helicscpp::Message::c_str
const char * c_str() const
Definition: Endpoint.hpp:152
helicsMessageReserve
void helicsMessageReserve(helics_message_object message, int reserveSize, helics_error *err)
Definition: MessageFederateExport.cpp:936
helicscpp::Endpoint::createMessage
Message createMessage()
Definition: Endpoint.hpp:237
helicscpp::Message::Message
Message() HELICS_NOTHROW
Definition: Endpoint.hpp:24
helicsMessageFree
void helicsMessageFree(helics_message_object message)
Definition: MessageFederateExport.cpp:1066
helicsEndpointGetName
const char * helicsEndpointGetName(helics_endpoint endpoint)
Definition: MessageFederateExport.cpp:647
helicscpp::Endpoint::isValid
bool isValid() const
Definition: Endpoint.hpp:216
helicscpp::Endpoint::sendMessage
void sendMessage(const std::vector< char > &data, helics_time time)
Definition: Endpoint.hpp:348
helicsMessageAppendData
void helicsMessageAppendData(helics_message_object message, const void *data, int inputDataLength, helics_error *err)
Definition: MessageFederateExport.cpp:1004
helicscpp::Endpoint::sendMessage
void sendMessage(const std::string &dest, const char *data, size_t data_size)
Definition: Endpoint.hpp:258
helicscpp::Endpoint::Endpoint
Endpoint(const Endpoint &endpoint) HELICS_NOTHROW
Definition: Endpoint.hpp:204
helicsMessageGetRawDataPointer
void * helicsMessageGetRawDataPointer(helics_message_object message)
Definition: MessageFederateExport.cpp:862
helicscpp::Message::~Message
~Message()
Definition: Endpoint.hpp:57
helicsEndpointGetInfo
const char * helicsEndpointGetInfo(helics_endpoint end)
Definition: MessageFederateExport.cpp:667
helicsMessageGetRawDataSize
int helicsMessageGetRawDataSize(helics_message_object message)
Definition: MessageFederateExport.cpp:829
helicscpp::Message::messageID
Message & messageID(int newId)
Definition: Endpoint.hpp:172
helicscpp::Message::source
Message & source(const char *src)
Definition: Endpoint.hpp:76
helicsEndpointHasMessage
helics_bool helicsEndpointHasMessage(helics_endpoint endpoint)
Definition: MessageFederateExport.cpp:347
helicsMessageSetData
void helicsMessageSetData(helics_message_object message, const void *data, int inputDataLength, helics_error *err)
Definition: MessageFederateExport.cpp:995
helicscpp::Endpoint::setInfo
void setInfo(const std::string &info)
Definition: Endpoint.hpp:402
helicscpp::Endpoint::Endpoint
Endpoint() HELICS_NOTHROW
Definition: Endpoint.hpp:202
helicscpp::Endpoint::operator=
Endpoint & operator=(const Endpoint &endpoint)
Definition: Endpoint.hpp:206
helicscpp::Message::data
void * data() const
Definition: Endpoint.hpp:120
helicsEndpointGetMessageObject
helics_message_object helicsEndpointGetMessageObject(helics_endpoint endpoint)
Definition: MessageFederateExport.cpp:531
helicscpp::Message::destination
const char * destination() const
Definition: Endpoint.hpp:82
helicscpp::Endpoint::Endpoint
Endpoint(helics_endpoint hep) HELICS_NOTHROW
Definition: Endpoint.hpp:200
helicscpp::Endpoint::sendMessage
void sendMessage(const std::string &dest, const std::vector< char > &data, helics_time time)
Definition: Endpoint.hpp:362
helicscpp::Endpoint::sendMessage
void sendMessage(const std::string &data)
Definition: Endpoint.hpp:287
helicscpp::Endpoint::setDefaultDestination
void setDefaultDestination(const std::string &dest)
Definition: Endpoint.hpp:224
helicscpp::Message::isValid
bool isValid() const
Definition: Endpoint.hpp:66
helicscpp::Message::source
Message & source(const std::string &src)
Definition: Endpoint.hpp:70
helicsEndpointSetInfo
void helicsEndpointSetInfo(helics_endpoint endpoint, const char *info, helics_error *err)
Definition: MessageFederateExport.cpp:683
helicsMessageGetOriginalSource
const char * helicsMessageGetOriginalSource(helics_message_object message)
Definition: MessageFederateExport.cpp:771
helicscpp::Endpoint::sendMessage
void sendMessage(const char *data, size_t data_size)
Definition: Endpoint.hpp:247
helicsEndpointGetDefaultDestination
const char * helicsEndpointGetDefaultDestination(helics_endpoint endpoint)
Definition: MessageFederateExport.cpp:171
helicscpp
Definition: cpp98/Broker.hpp:18
helicscpp::Message::time
helics_time time() const
Definition: Endpoint.hpp:154
helicscpp::Message::newMessageObject
Message & newMessageObject(const Federate &fed)
Definition: cpp98/MessageFederate.hpp:119
helicscpp::Endpoint::getType
const char * getType()
Definition: Endpoint.hpp:397
helicsEndpointSendMessageObjectZeroCopy
void helicsEndpointSendMessageObjectZeroCopy(helics_endpoint endpoint, helics_message_object message, helics_error *err)
Definition: MessageFederateExport.cpp:294
helicscpp::Endpoint::sendMessage
void sendMessage(const Message &message)
Definition: Endpoint.hpp:370