1 #define TRACE_NAME "RTIDDS"
3 #include "artdaq/RTIDDS/RTIDDS.hh"
4 #include "artdaq/DAQdata/Globals.hh"
6 #include <boost/lexical_cast.hpp>
7 #include <boost/tokenizer.hpp>
16 DDS_ReturnCode_t retcode = DDS_RETCODE_ERROR;
17 DDS_DomainParticipantQos participant_qos;
19 retcode = DDSDomainParticipantFactory::get_instance()->get_default_participant_qos(participant_qos);
21 if (retcode != DDS_RETCODE_OK)
23 TLOG(TLVL_WARNING) << name_ <<
": Problem obtaining default participant QoS, retcode was " << retcode;
26 retcode = DDSPropertyQosPolicyHelper::add_property(
27 participant_qos.property,
"dds.builtin_type.octets.max_size",
31 if (retcode != DDS_RETCODE_OK)
33 TLOG(TLVL_WARNING) << name_ <<
": Problem setting dds.builtin_type.octets.max_size, retcode was " << retcode;
36 participant_.reset(DDSDomainParticipantFactory::get_instance()->create_participant(
40 DDS_STATUS_MASK_NONE));
42 topic_octets_ = participant_->create_topic(
44 DDSOctetsTypeSupport::get_type_name(),
45 DDS_TOPIC_QOS_DEFAULT,
47 DDS_STATUS_MASK_NONE);
49 if (participant_ ==
nullptr || topic_octets_ ==
nullptr)
51 TLOG(TLVL_WARNING) << name_ <<
": Problem setting up the RTI-DDS participant and/or topic";
60 DDS_DataWriterQos writer_qos;
62 retcode = participant_->get_default_datawriter_qos(writer_qos);
64 if (retcode != DDS_RETCODE_OK)
66 TLOG(TLVL_WARNING) << name_ <<
": Problem obtaining default datawriter QoS, retcode was " << retcode;
69 retcode = DDSPropertyQosPolicyHelper::add_property(
70 writer_qos.property,
"dds.builtin_type.octets.alloc_size",
74 if (retcode != DDS_RETCODE_OK)
76 TLOG(TLVL_WARNING) << name_ <<
": Problem setting dds.builtin_type.octets.alloc_size, retcode was " << retcode;
79 if (iotype_ == IOType::writer)
81 octets_writer_ = DDSOctetsDataWriter::narrow(participant_->create_datawriter(
85 DDS_STATUS_MASK_NONE));
87 if (octets_writer_ ==
nullptr)
89 TLOG(TLVL_WARNING) << name_ <<
": Problem setting up the RTI-DDS writer objects";
94 octets_reader_ = participant_->create_datareader(
96 DDS_DATAREADER_QOS_DEFAULT,
98 DDS_DATA_AVAILABLE_STATUS);
100 if (octets_reader_ ==
nullptr)
102 TLOG(TLVL_WARNING) << name_ <<
": Problem setting up the RTI-DDS reader objects";
112 size_t fragmentType = fragment.type();
114 if (octets_writer_ == NULL) {
return; }
121 size_t max_fragment_size = boost::lexical_cast<
size_t>(max_size_) -
122 detail::RawFragmentHeader::num_words() *
sizeof(RawDataType);
124 if (fragment.type() != artdaq::Fragment::InvalidFragmentType &&
125 fragment.sizeBytes() < max_fragment_size)
127 if (fragment.type() == artdaq::Fragment::InitFragmentType)
132 DDS_ReturnCode_t retcode = octets_writer_->write(reinterpret_cast<unsigned char*>(fragment.headerBeginBytes()),
133 fragment.sizeBytes(),
136 if (retcode != DDS_RETCODE_OK)
138 TLOG(TLVL_WARNING) << name_ <<
": Problem writing octets (bytes), retcode was " << retcode;
140 TLOG(TLVL_WARNING) << name_ <<
": Fragment failed for DDS! "
141 <<
"fragment address and size = "
142 <<
static_cast<void*
>(fragment.headerBeginBytes()) <<
" " << static_cast<int>(fragment.sizeBytes()) <<
" "
143 <<
"sequence ID, fragment ID, and type = "
144 << fragment.sequenceID() <<
" "
145 << fragment.fragmentID() <<
" "
146 << ((int)fragment.type());
151 TLOG(TLVL_WARNING) << name_ <<
": Fragment invalid for DDS! "
152 <<
"fragment address and size = "
153 <<
static_cast<void*
>(fragment.headerBeginBytes()) <<
" " << static_cast<int>(fragment.sizeBytes()) <<
" "
154 <<
"sequence ID, fragment ID, and type = "
155 << fragment.sequenceID() <<
" "
156 << fragment.fragmentID() <<
" "
157 << ((int)fragment.type());
163 DDSOctetsDataReader* octets_reader = NULL;
165 DDS_ReturnCode_t retcode;
167 TLOG(TLVL_DEBUG + 32) << name_ <<
": In OctetsListener::on_data_available";
169 octets_reader = DDSOctetsDataReader::narrow(reader);
170 if (octets_reader ==
nullptr)
172 TLOG(TLVL_ERROR) << name_ <<
": Error: Very unexpected - DDSOctetsDataReader::narrow failed";
180 retcode = octets_reader->take_next_sample(
183 if (retcode == DDS_RETCODE_NO_DATA)
188 else if (retcode != DDS_RETCODE_OK)
190 TLOG(TLVL_WARNING) <<
"Unable to take data from data reader, error "
201 std::lock_guard<std::mutex> lock(queue_mutex_);
203 dds_octets_queue_.push(dds_octets_);
212 const size_t receiveTimeout)
215 size_t sleepTime = 1000;
216 size_t nloops = receiveTimeout / sleepTime;
218 while (dds_octets_queue_.empty() && loopCount < nloops)
229 std::lock_guard<std::mutex> lock(queue_mutex_);
231 if (!dds_octets_queue_.empty())
233 fragment.resizeBytes(dds_octets_queue_.front().length);
234 memcpy(fragment.headerAddress(), dds_octets_queue_.front().value, dds_octets_queue_.front().length);
236 dds_octets_queue_.pop();
238 TLOG(TLVL_DEBUG + 32) << name_
239 <<
": Received fragment from DDS, type ="
240 << ((int)fragment.type()) <<
", sequenceID = "
241 << fragment.sequenceID() <<
", size in bytes = "
242 << fragment.sizeBytes();
void transfer_fragment_min_blocking_mode_via_DDS_(artdaq::Fragment const &fragment)
Copy a Fragment to DDS.
OctetsListener octets_listener_
The receiver.
void on_data_available(DDSDataReader *reader)
Action to perform when data is available.
bool receiveFragmentFromDDS(artdaq::Fragment &fragment, const size_t receiveTimeout)
Receive a Fragment from DDS.
RTIDDS(std::string name, IOType iotype, std::string max_size="1000000")
Construct a RTIDDS transmitter.
void transfer_fragment_reliable_mode_via_DDS_(artdaq::Fragment &&fragment)
Move a Fragment to DDS.
IOType
Whether this DDS instance is a reader or a writer.