1 #include "artdaq/ArtModules/detail/TransferWrapper.hh"
2 #include "artdaq/TransferPlugins/MakeTransferPlugin.hh"
3 #include "artdaq/ExternalComms/MakeCommanderPlugin.hh"
4 #include "artdaq/DAQdata/NetMonHeader.hh"
5 #include "artdaq/DAQdata/Globals.hh"
6 #include "artdaq-core/Utilities/ExceptionHandler.hh"
7 #include "artdaq-core/Data/Fragment.hh"
9 #include "cetlib/BasicPluginFactory.h"
10 #include "cetlib_except/exception.h"
11 #include "fhiclcpp/ParameterSet.h"
13 #include <TBufferFile.h>
30 void signal_handler(
int signal)
32 gSignalStatus = signal;
36 timeoutInUsecs_(pset.get<std::size_t>(
"timeoutInUsecs", 100000))
37 , dispatcherHost_(pset.get<std::string>(
"dispatcherHost",
"localhost"))
38 , dispatcherPort_(pset.get<std::string>(
"dispatcherPort",
"5266"))
39 , serverUrl_(pset.get<std::string>(
"server_url",
"http://" + dispatcherHost_ +
":" + dispatcherPort_ +
"/RPC2"))
40 , maxEventsBeforeInit_(pset.get<std::size_t>(
"maxEventsBeforeInit", 5))
41 , allowedFragmentTypes_(pset.get<std::vector<int>>(
"allowedFragmentTypes", { 226, 227, 229 }))
42 , quitOnFragmentIntegrityProblem_(pset.get<
bool>(
"quitOnFragmentIntegrityProblem",
true))
43 , monitorRegistered_(
false)
45 std::signal(SIGINT, signal_handler);
53 ExceptionHandler(ExceptionHandlerRethrow::yes,
54 "TransferWrapper: failure in call to MakeTransferPlugin");
57 fhicl::ParameterSet new_pset(pset);
58 if (!new_pset.has_key(
"server_url")) {
59 new_pset.put<std::string>(
"server_url", serverUrl_);
62 auto dispatcherConfig = pset.get<fhicl::ParameterSet>(
"dispatcher_config");
69 TLOG_INFO(
"TransferWrapper") <<
"Attempting to register this monitor (\"" << transfer_->uniqueLabel()
70 <<
"\") with the dispatcher aggregator" << TLOG_ENDL;
72 auto status = commander_->send_register_monitor(dispatcherConfig.to_string());
74 TLOG_INFO(
"TransferWrapper") <<
"Response from dispatcher is \"" << status <<
"\"" << TLOG_ENDL;
76 if (status ==
"Success")
78 monitorRegistered_ =
true;
83 TLOG_WARNING(
"TransferWrapper") <<
"Error in TransferWrapper: attempt to register with dispatcher did not result in the \"Success\" response" << TLOG_ENDL;
92 std::unique_ptr<artdaq::Fragment> fragmentPtr;
93 bool receivedFragment =
false;
94 static bool initialized =
false;
95 static size_t fragments_received = 0;
97 while (
true && !gSignalStatus)
99 fragmentPtr = std::make_unique<artdaq::Fragment>();
101 while (!receivedFragment)
105 TLOG_INFO(
"TransferWrapper") <<
"Ctrl-C appears to have been hit" << TLOG_ENDL;
112 auto result = transfer_->receiveFragment(*fragmentPtr, timeoutInUsecs_);
116 receivedFragment =
true;
117 fragments_received++;
119 static size_t cntr = 1;
121 TLOG_INFO(
"TransferWrapper") <<
"Received " << cntr++ <<
"-th event, "
122 <<
"seqID == " << fragmentPtr->sequenceID()
123 <<
", type == " << fragmentPtr->typeString() << TLOG_ENDL;
128 TLOG_WARNING(
"TransferWrapper") <<
"Timeout occurred in call to transfer_->receiveFragmentFrom; will try again" << TLOG_ENDL;
134 ExceptionHandler(ExceptionHandlerRethrow::yes,
135 "Problem receiving data in TransferWrapper::receiveMessage");
139 if (fragmentPtr->type() == artdaq::Fragment::EndOfDataFragmentType)
150 extractTBufferFile(*fragmentPtr, msg);
154 ExceptionHandler(ExceptionHandlerRethrow::yes,
155 "Problem extracting TBufferFile from artdaq::Fragment in TransferWrapper::receiveMessage");
158 checkIntegrity(*fragmentPtr);
160 if (initialized || fragmentPtr->type() == artdaq::Fragment::InitFragmentType)
167 receivedFragment =
false;
169 if (fragments_received > maxEventsBeforeInit_)
171 throw cet::exception(
"TransferWrapper") <<
"First " << maxEventsBeforeInit_ <<
172 " events received did not include the \"Init\" event containing necessary info for art; exiting...";
180 artdaq::TransferWrapper::extractTBufferFile(
const artdaq::Fragment& fragment,
181 std::unique_ptr<TBufferFile>& tbuffer)
184 char* buffer = (
char *)malloc(header->
data_length);
185 memcpy(buffer, fragment.dataBeginBytes(), header->
data_length);
188 tbuffer.reset(
new TBufferFile(TBuffer::kRead, header->
data_length, buffer, kTRUE, 0));
192 artdaq::TransferWrapper::checkIntegrity(
const artdaq::Fragment& fragment)
const
194 const size_t artdaqheader = artdaq::detail::RawFragmentHeader::num_words() *
195 sizeof(artdaq::detail::RawFragmentHeader::RawDataType);
196 const size_t payload =
static_cast<size_t>(fragment.dataEndBytes() - fragment.dataBeginBytes());
198 const size_t totalsize = fragment.sizeBytes();
200 const size_t type =
static_cast<size_t>(fragment.type());
202 if (totalsize != artdaqheader + metadata + payload)
204 std::stringstream errmsg;
205 errmsg <<
"Error: artdaq fragment of type " <<
206 fragment.typeString() <<
", sequence ID " <<
207 fragment.sequenceID() <<
208 " has internally inconsistent measures of its size, signalling data corruption: in bytes," <<
209 " total size = " << totalsize <<
", artdaq fragment header = " << artdaqheader <<
210 ", metadata = " << metadata <<
", payload = " << payload;
212 TLOG_ERROR(
"TransferWrapper") << errmsg.str() << TLOG_ENDL;
214 if (quitOnFragmentIntegrityProblem_)
216 throw cet::exception(
"TransferWrapper") << errmsg.str();
224 auto findloc = std::find(allowedFragmentTypes_.begin(), allowedFragmentTypes_.end(),
static_cast<int>(type));
226 if (findloc == allowedFragmentTypes_.end())
228 std::stringstream errmsg;
229 errmsg <<
"Error: artdaq fragment appears to have type "
230 << type <<
", not found in the allowed fragment types list";
232 TLOG_ERROR(
"TransferWrapper") << errmsg.str() << TLOG_ENDL;
233 if (quitOnFragmentIntegrityProblem_)
235 throw cet::exception(
"TransferWrapper") << errmsg.str();
245 artdaq::TransferWrapper::unregisterMonitor()
247 if (!monitorRegistered_)
249 throw cet::exception(
"TransferWrapper") <<
250 "The function to unregister the monitor was called, but the monitor doesn't appear to be registered";
253 TLOG_INFO(
"TransferWrapper") <<
"Requesting that this monitor (" << transfer_->uniqueLabel()
254 <<
") be unregistered from the dispatcher aggregator" << TLOG_ENDL;
256 auto status = commander_->send_unregister_monitor(transfer_->uniqueLabel());
259 TLOG_INFO(
"TransferWrapper") <<
"Response from dispatcher is \""
260 << status <<
"\"" << TLOG_ENDL;
262 if (status ==
"Success")
264 monitorRegistered_ =
false;
268 throw cet::exception(
"TransferWrapper") <<
"Error in TransferWrapper: attempt to unregister with dispatcher did not result in the \"Success\" response";
275 if (monitorRegistered_)
283 ExceptionHandler(ExceptionHandlerRethrow::no,
284 "An exception occurred when trying to unregister monitor during TransferWrapper's destruction");
Commandable is the base class for all artdaq components which implement the artdaq state machine...
void receiveMessage(std::unique_ptr< TBufferFile > &msg)
Receive a Fragment from the TransferInterface, and send it to art.
TransferWrapper(const fhicl::ParameterSet &pset)
TransferWrapper Constructor.
virtual ~TransferWrapper()
TransferWrapper Destructor.
static const int RECV_TIMEOUT
Value to be returned upon receive timeout. Because receivers otherwise return rank, this is also the limit on the number of ranks that artdaq currently supports.
This TransferInterface is a Receiver.
std::unique_ptr< artdaq::TransferInterface > MakeTransferPlugin(const fhicl::ParameterSet &pset, std::string plugin_label, TransferInterface::Role role)
Load a TransferInterface plugin.
volatile std::sig_atomic_t gSignalStatus
Stores singal from signal handler.
std::unique_ptr< artdaq::CommanderInterface > MakeCommanderPlugin(const fhicl::ParameterSet &commander_pset, artdaq::Commandable &commandable)
Load a CommanderInterface plugin.