1 #define TRACE_NAME "RTIDDS"
3 #include "artdaq/RTIDDS/RTIDDS.hh"
4 #include "artdaq/DAQdata/Globals.hh"
6 #include <boost/tokenizer.hpp>
15 DDS_ReturnCode_t retcode = DDS_RETCODE_ERROR;
16 DDS_DomainParticipantQos participant_qos;
18 retcode = DDSDomainParticipantFactory::get_instance()->get_default_participant_qos(participant_qos);
20 if (retcode != DDS_RETCODE_OK)
22 TLOG(TLVL_WARNING) << name_ <<
": Problem obtaining default participant QoS, retcode was " << retcode;
25 retcode = DDSPropertyQosPolicyHelper::add_property(
26 participant_qos.property,
"dds.builtin_type.octets.max_size",
30 if (retcode != DDS_RETCODE_OK)
32 TLOG(TLVL_WARNING) << name_ <<
": Problem setting dds.builtin_type.octets.max_size, retcode was " << retcode;
35 participant_.reset(DDSDomainParticipantFactory::get_instance()->create_participant(
39 DDS_STATUS_MASK_NONE));
41 topic_octets_ = participant_->create_topic(
43 DDSOctetsTypeSupport::get_type_name(),
44 DDS_TOPIC_QOS_DEFAULT,
46 DDS_STATUS_MASK_NONE);
48 if (participant_ ==
nullptr || topic_octets_ ==
nullptr)
50 TLOG(TLVL_WARNING) << name_ <<
": Problem setting up the RTI-DDS participant and/or topic";
59 DDS_DataWriterQos writer_qos;
61 retcode = participant_->get_default_datawriter_qos(writer_qos);
63 if (retcode != DDS_RETCODE_OK)
65 TLOG(TLVL_WARNING) << name_ <<
": Problem obtaining default datawriter QoS, retcode was " << retcode;
68 retcode = DDSPropertyQosPolicyHelper::add_property(
69 writer_qos.property,
"dds.builtin_type.octets.alloc_size",
73 if (retcode != DDS_RETCODE_OK)
75 TLOG(TLVL_WARNING) << name_ <<
": Problem setting dds.builtin_type.octets.alloc_size, retcode was " << retcode;
78 if (iotype_ == IOType::writer)
80 octets_writer_ = DDSOctetsDataWriter::narrow(participant_->create_datawriter(
84 DDS_STATUS_MASK_NONE));
86 if (octets_writer_ ==
nullptr)
88 TLOG(TLVL_WARNING) << name_ <<
": Problem setting up the RTI-DDS writer objects";
93 octets_reader_ = participant_->create_datareader(
95 DDS_DATAREADER_QOS_DEFAULT,
97 DDS_DATA_AVAILABLE_STATUS);
99 if (octets_reader_ ==
nullptr)
101 TLOG(TLVL_WARNING) << name_ <<
": Problem setting up the RTI-DDS reader objects";
111 size_t fragmentType = fragment.type();
113 if (octets_writer_ == NULL) {
return; }
120 size_t max_fragment_size = boost::lexical_cast<
size_t>(max_size_) -
121 detail::RawFragmentHeader::num_words() *
sizeof(RawDataType);
123 if (fragment.type() != artdaq::Fragment::InvalidFragmentType &&
124 fragment.sizeBytes() < max_fragment_size)
126 if (fragment.type() == artdaq::Fragment::InitFragmentType)
131 DDS_ReturnCode_t retcode = octets_writer_->write(reinterpret_cast<unsigned char*>(fragment.headerBeginBytes()),
132 fragment.sizeBytes(),
135 if (retcode != DDS_RETCODE_OK)
137 TLOG(TLVL_WARNING) << name_ <<
": Problem writing octets (bytes), retcode was " << retcode;
139 TLOG(TLVL_WARNING) << name_ <<
": Fragment failed for DDS! "
140 <<
"fragment address and size = "
141 <<
static_cast<void*
>(fragment.headerBeginBytes()) <<
" " << static_cast<int>(fragment.sizeBytes()) <<
" "
142 <<
"sequence ID, fragment ID, and type = "
143 << fragment.sequenceID() <<
" "
144 << fragment.fragmentID() <<
" "
145 << ((int)fragment.type());
150 TLOG(TLVL_WARNING) << name_ <<
": Fragment invalid for DDS! "
151 <<
"fragment address and size = "
152 <<
static_cast<void*
>(fragment.headerBeginBytes()) <<
" " << static_cast<int>(fragment.sizeBytes()) <<
" "
153 <<
"sequence ID, fragment ID, and type = "
154 << fragment.sequenceID() <<
" "
155 << fragment.fragmentID() <<
" "
156 << ((int)fragment.type());
162 DDSOctetsDataReader* octets_reader = NULL;
164 DDS_ReturnCode_t retcode;
166 TLOG(TLVL_DEBUG) << name_ <<
": In OctetsListener::on_data_available";
168 octets_reader = DDSOctetsDataReader::narrow(reader);
169 if (octets_reader ==
nullptr)
171 TLOG(TLVL_ERROR) << name_ <<
": Error: Very unexpected - DDSOctetsDataReader::narrow failed";
179 retcode = octets_reader->take_next_sample(
182 if (retcode == DDS_RETCODE_NO_DATA)
187 else if (retcode != DDS_RETCODE_OK)
189 TLOG(TLVL_WARNING) <<
"Unable to take data from data reader, error "
200 std::lock_guard<std::mutex> lock(queue_mutex_);
202 dds_octets_queue_.push(dds_octets_);
211 const size_t receiveTimeout)
214 size_t sleepTime = 1000;
215 size_t nloops = receiveTimeout / sleepTime;
217 while (dds_octets_queue_.empty() && loopCount < nloops)
228 std::lock_guard<std::mutex> lock(queue_mutex_);
230 if (!dds_octets_queue_.empty())
232 fragment.resizeBytes(dds_octets_queue_.front().length);
233 memcpy(fragment.headerAddress(), dds_octets_queue_.front().value, dds_octets_queue_.front().length);
235 dds_octets_queue_.pop();
237 TLOG(TLVL_DEBUG) << name_
238 <<
": Received fragment from DDS, type ="
239 << ((int)fragment.type()) <<
", sequenceID = "
240 << fragment.sequenceID() <<
", size in bytes = "
241 << 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.