1 #define TRACE_NAME "TransferWrapper"
3 #include "artdaq/ArtModules/detail/TransferWrapper.hh"
4 #include "artdaq-core/Data/Fragment.hh"
5 #include "artdaq-core/Utilities/ExceptionHandler.hh"
6 #include "artdaq/DAQdata/Globals.hh"
7 #include "artdaq/DAQdata/NetMonHeader.hh"
8 #include "artdaq/ExternalComms/MakeCommanderPlugin.hh"
9 #include "artdaq/TransferPlugins/MakeTransferPlugin.hh"
11 #include "cetlib/BasicPluginFactory.h"
12 #include "cetlib_except/exception.h"
13 #include "fhiclcpp/ParameterSet.h"
15 #include <TBufferFile.h>
31 void signal_handler(
int signal)
33 gSignalStatus = signal;
37 : timeoutInUsecs_(pset.get<std::size_t>(
"timeoutInUsecs", 100000))
38 , dispatcherHost_(pset.get<std::string>(
"dispatcherHost",
"localhost"))
39 , dispatcherPort_(pset.get<std::string>(
"dispatcherPort",
"5266"))
40 , serverUrl_(pset.get<std::string>(
"server_url",
"http://" + dispatcherHost_ +
":" + dispatcherPort_ +
"/RPC2"))
41 , maxEventsBeforeInit_(pset.get<std::size_t>(
"maxEventsBeforeInit", 5))
42 , allowedFragmentTypes_(pset.get<std::vector<int>>(
"allowedFragmentTypes", {226, 227, 229}))
43 , quitOnFragmentIntegrityProblem_(pset.get<
bool>(
"quitOnFragmentIntegrityProblem",
true))
44 , monitorRegistered_(
false)
46 std::signal(SIGINT, signal_handler);
54 ExceptionHandler(ExceptionHandlerRethrow::yes,
55 "TransferWrapper: failure in call to MakeTransferPlugin");
62 metricMan->initialize(pset.get<fhicl::ParameterSet>(
"metrics", fhicl::ParameterSet()),
"Online Monitor");
63 metricMan->do_start();
68 ExceptionHandler(ExceptionHandlerRethrow::no,
"TransferWrapper: could not configure metrics");
71 fhicl::ParameterSet new_pset(pset);
72 if (!new_pset.has_key(
"server_url"))
74 new_pset.put<std::string>(
"server_url", serverUrl_);
77 auto dispatcherConfig = pset.get<fhicl::ParameterSet>(
"dispatcher_config");
85 TLOG(TLVL_INFO) <<
"Attempting to register this monitor (\"" << transfer_->uniqueLabel()
86 <<
"\") with the dispatcher aggregator";
88 auto status = commander_->send_register_monitor(dispatcherConfig.to_string());
90 TLOG(TLVL_INFO) <<
"Response from dispatcher is \"" << status <<
"\"";
92 if (status ==
"Success")
94 monitorRegistered_ =
true;
99 TLOG(TLVL_WARNING) <<
"Error in TransferWrapper: attempt to register with dispatcher did not result in the \"Success\" response";
108 std::unique_ptr<artdaq::Fragment> fragmentPtr;
109 bool receivedFragment =
false;
110 static bool initialized =
false;
111 static size_t fragments_received = 0;
113 while (
true && !gSignalStatus)
115 fragmentPtr = std::make_unique<artdaq::Fragment>();
117 while (!receivedFragment)
121 TLOG(TLVL_INFO) <<
"Ctrl-C appears to have been hit";
128 auto result = transfer_->receiveFragment(*fragmentPtr, timeoutInUsecs_);
132 receivedFragment =
true;
133 fragments_received++;
135 static size_t cntr = 0;
136 auto mod = ++cntr % 10;
138 if (mod == 1) suffix =
"-st";
139 if (mod == 2) suffix =
"-nd";
140 if (mod == 3) suffix =
"-rd";
141 TLOG(TLVL_INFO) <<
"Received " << cntr << suffix <<
" event, "
142 <<
"seqID == " << fragmentPtr->sequenceID()
143 <<
", type == " << fragmentPtr->typeString();
148 TLOG(TLVL_ERROR) <<
"Transfer Plugin disconnected or other unrecoverable error. Shutting down.";
156 TLOG(TLVL_WARNING) <<
"Timeout occurred in call to transfer_->receiveFragmentFrom; will try again"
157 <<
", status = " << result;
162 ExceptionHandler(ExceptionHandlerRethrow::yes,
163 "Problem receiving data in TransferWrapper::receiveMessage");
167 if (fragmentPtr->type() == artdaq::Fragment::EndOfDataFragmentType)
178 extractTBufferFile(*fragmentPtr, msg);
182 ExceptionHandler(ExceptionHandlerRethrow::yes,
183 "Problem extracting TBufferFile from artdaq::Fragment in TransferWrapper::receiveMessage");
186 checkIntegrity(*fragmentPtr);
188 if (initialized || fragmentPtr->type() == artdaq::Fragment::InitFragmentType)
195 receivedFragment =
false;
197 if (fragments_received > maxEventsBeforeInit_)
199 throw cet::exception(
"TransferWrapper") <<
"First " << maxEventsBeforeInit_ <<
" events received did not include the \"Init\" event containing necessary info for art; exiting...";
205 void artdaq::TransferWrapper::extractTBufferFile(
const artdaq::Fragment& fragment,
206 std::unique_ptr<TBufferFile>& tbuffer)
210 memcpy(buffer, fragment.dataBeginBytes(), header->
data_length);
213 tbuffer.reset(
new TBufferFile(TBuffer::kRead, header->
data_length, buffer, kTRUE, 0));
216 void artdaq::TransferWrapper::checkIntegrity(
const artdaq::Fragment& fragment)
const
218 const size_t artdaqheader = artdaq::detail::RawFragmentHeader::num_words() *
219 sizeof(artdaq::detail::RawFragmentHeader::RawDataType);
220 const size_t payload =
static_cast<size_t>(fragment.dataEndBytes() - fragment.dataBeginBytes());
222 const size_t totalsize = fragment.sizeBytes();
224 const size_t type =
static_cast<size_t>(fragment.type());
226 if (totalsize != artdaqheader + metadata + payload)
228 std::stringstream errmsg;
229 errmsg <<
"Error: artdaq fragment of type " << fragment.typeString() <<
", sequence ID " << fragment.sequenceID() <<
" has internally inconsistent measures of its size, signalling data corruption: in bytes,"
230 <<
" total size = " << totalsize <<
", artdaq fragment header = " << artdaqheader <<
", metadata = " << metadata <<
", payload = " << payload;
232 TLOG(TLVL_ERROR) << errmsg.str();
234 if (quitOnFragmentIntegrityProblem_)
236 throw cet::exception(
"TransferWrapper") << errmsg.str();
244 auto findloc = std::find(allowedFragmentTypes_.begin(), allowedFragmentTypes_.end(),
static_cast<int>(type));
246 if (findloc == allowedFragmentTypes_.end())
248 std::stringstream errmsg;
249 errmsg <<
"Error: artdaq fragment appears to have type "
250 << type <<
", not found in the allowed fragment types list";
252 TLOG(TLVL_ERROR) << errmsg.str();
253 if (quitOnFragmentIntegrityProblem_)
255 throw cet::exception(
"TransferWrapper") << errmsg.str();
264 void artdaq::TransferWrapper::unregisterMonitor()
266 if (!monitorRegistered_)
268 throw cet::exception(
"TransferWrapper") <<
"The function to unregister the monitor was called, but the monitor doesn't appear to be registered";
274 TLOG(TLVL_INFO) <<
"Requesting that this monitor (" << transfer_->uniqueLabel()
275 <<
") be unregistered from the dispatcher aggregator";
277 auto status = commander_->send_unregister_monitor(transfer_->uniqueLabel());
279 TLOG(TLVL_INFO) <<
"Response from dispatcher is \""
282 if (status ==
"Success")
284 monitorRegistered_ =
false;
287 else if (status ==
"busy")
291 throw cet::exception(
"TransferWrapper") <<
"Error in TransferWrapper: attempt to unregister with dispatcher did not result in the \"Success\" response";
299 if (monitorRegistered_)
307 ExceptionHandler(ExceptionHandlerRethrow::no,
308 "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.
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
virtual ~TransferWrapper()
TransferWrapper Destructor.
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.
Value that is to be returned when a Transfer plugin determines that no more data will be arriving...
For code clarity, things checking for successful receive should check retval >= NO_RANK_INFO.