libcluon  0.0.148
cluon::OD4Session Class Reference

#include <OD4Session.hpp>

Public Member Functions

 OD4Session (uint16_t CID, std::function< void(cluon::data::Envelope &&envelope)> delegate=nullptr) noexcept
 
void send (cluon::data::Envelope &&envelope) noexcept
 
bool dataTrigger (int32_t messageIdentifier, std::function< void(cluon::data::Envelope &&envelope)> delegate) noexcept
 
void timeTrigger (float freq, std::function< bool()> delegate) noexcept
 
template<typename T >
void send (T &message, const cluon::data::TimeStamp &sampleTimeStamp=cluon::data::TimeStamp(), uint32_t senderStamp=0) noexcept
 
bool isRunning () noexcept
 

Detailed Description

This class provides an interface to an OpenDaVINCI v4 session. An OpenDaVINCI v4 session allows the automatic exchange of time-stamped Envelopes carrying user-defined messages usually using UDP multicast. A running OD4Session will not receive the bytes that itself has sent to other microservices.

There are two ways to participate in an OpenDaVINCI session. Variant A is simply calling a user-supplied lambda whenever a new Envelope is received:

cluon::OD4Session od4{111, [](cluon::data::Envelope &&envelope){ std::cout << "Received cluon::Envelope" << std::endl;}
};
// Do something in parallel.
MyMessage msg;
od4.send(msg);

Variant B allows a more fine-grained setup where you specify the Envelopes of interest:

od4.dataTrigger(cluon::data::TimeStamp::ID(), [](cluon::data::Envelope &&envelope){ std::cout << "Received cluon::data::TimeStamp" << std::endl;});
od4.dataTrigger(MyMessage::ID(), [](cluon::data::Envelope &&envelope){ std::cout << "Received MyMessage" << std::endl;});
// Do something in parallel.
MyMessage msg;
od4.send(msg);

Next to receive Envelopes, OD4Session can call a user-supplied lambda in a time-triggered way. The lambda is executed as long as it does not return false or throws an exception that is then caught in the method timeTrigger and the method is exited:

const float FREQ{10}; // 10 Hz.
od4.timeTrigger(FREQ, [](){
// Do something time-triggered.
return false;
}); // This call blocks until the lambda returns false.

Constructor & Destructor Documentation

◆ OD4Session()

cluon::OD4Session::OD4Session ( uint16_t  CID,
std::function< void(cluon::data::Envelope &&envelope)>  delegate = nullptr 
)
noexcept

Constructor.

Parameters
CIDOpenDaVINCI v4 session identifier [1 .. 254]
delegateFunction to call on newly arriving Envelopes ("catch-all"); if a nullptr is passed, the method dataTrigger can be used to set message specific delegates. Please note that it is NOT possible to have both: a delegate for "catch-all" and the data-triggered ones.

Member Function Documentation

◆ dataTrigger()

bool cluon::OD4Session::dataTrigger ( int32_t  messageIdentifier,
std::function< void(cluon::data::Envelope &&envelope)>  delegate 
)
noexcept

This method sets a delegate to be called data-triggered on arrival of a new Envelope for a given message identifier.

Parameters
messageIdentifierMessage identifier to assign a delegate.
delegateFunction to call on newly arriving Envelopes; setting it to nullptr will erase it.
Returns
true if the given delegate could be successfully set or unset.

References cluon::time::convert(), cluon::extractEnvelope(), and retVal.

◆ isRunning()

bool cluon::OD4Session::isRunning ( )
noexcept

◆ send() [1/2]

void cluon::OD4Session::send ( cluon::data::Envelope &&  envelope)
noexcept

This method will send a given Envelope to this OpenDaVINCI v4 session.

Parameters
envelopeto be sent.

References cluon::serializeEnvelope().

◆ send() [2/2]

template<typename T >
void cluon::OD4Session::send ( T &  message,
const cluon::data::TimeStamp &  sampleTimeStamp = cluon::data::TimeStamp(),
uint32_t  senderStamp = 0 
)
inlinenoexcept

This method will send a given message to this OpenDaVINCI v4 session.

Parameters
messageMessage to be sent.
sampleTimeStampTime point when this sample to be sent was captured (default = sent time point).
senderStampOptional sender stamp (default = 0).

References cluon::ToProtoVisitor::encodedData(), and cluon::time::now().

◆ timeTrigger()

void cluon::OD4Session::timeTrigger ( float  freq,
std::function< bool()>  delegate 
)
noexcept

This method sets a delegate to be called time-triggered using the specified frequency until the delegate returns false. This method blocks until the delegate has returned false or threw an exception. Thus, this method is typically called as last statement in a main function of a program.

Parameters
freqFrequency in Hertz to run the given delegate.
delegateFunction to call according to the given frequency.

References cluon::TerminateHandler::instance(), cluon::time::now(), and cluon::time::toMicroseconds().