1 #define TRACE_NAME "TransferWrapper"
3 #include "artdaq/ArtModules/detail/TransferWrapper.hh"
4 #include "artdaq/TransferPlugins/MakeTransferPlugin.hh"
5 #include "artdaq/ExternalComms/MakeCommanderPlugin.hh"
6 #include "artdaq/DAQdata/NetMonHeader.hh"
7 #include "artdaq/DAQdata/Globals.hh"
8 #include "artdaq-core/Utilities/ExceptionHandler.hh"
9 #include "artdaq-core/Data/Fragment.hh"
11 #include "cetlib/BasicPluginFactory.h"
12 #include "cetlib_except/exception.h"
13 #include "fhiclcpp/ParameterSet.h"
15 #include <TBufferFile.h>
32 void signal_handler(
int signal)
34 gSignalStatus = signal;
38 timeoutInUsecs_(pset.get<std::size_t>(
"timeoutInUsecs", 100000))
39 , dispatcherHost_(pset.get<std::string>(
"dispatcherHost",
"localhost"))
40 , dispatcherPort_(pset.get<std::string>(
"dispatcherPort",
"5266"))
41 , serverUrl_(pset.get<std::string>(
"server_url",
"http://" + dispatcherHost_ +
":" + dispatcherPort_ +
"/RPC2"))
42 , maxEventsBeforeInit_(pset.get<std::size_t>(
"maxEventsBeforeInit", 5))
43 , allowedFragmentTypes_(pset.get<std::vector<int>>(
"allowedFragmentTypes", { 226, 227, 229 }))
44 , quitOnFragmentIntegrityProblem_(pset.get<
bool>(
"quitOnFragmentIntegrityProblem",
true))
45 , monitorRegistered_(
false)
47 std::signal(SIGINT, signal_handler);
55 ExceptionHandler(ExceptionHandlerRethrow::yes,
56 "TransferWrapper: failure in call to MakeTransferPlugin");
61 metricMan->initialize(pset.get<fhicl::ParameterSet>(
"metrics", fhicl::ParameterSet()),
"Online Monitor");
62 metricMan->do_start();
67 ExceptionHandler(ExceptionHandlerRethrow::no,
"TransferWrapper: could not configure metrics");
71 fhicl::ParameterSet new_pset(pset);
72 if (!new_pset.has_key(
"server_url")) {
73 new_pset.put<std::string>(
"server_url", serverUrl_);
76 auto dispatcherConfig = pset.get<fhicl::ParameterSet>(
"dispatcher_config");
83 TLOG(TLVL_INFO) <<
"Attempting to register this monitor (\"" << transfer_->uniqueLabel()
84 <<
"\") with the dispatcher aggregator" ;
86 auto status = commander_->send_register_monitor(dispatcherConfig.to_string());
88 TLOG(TLVL_INFO) <<
"Response from dispatcher is \"" << status <<
"\"" ;
90 if (status ==
"Success")
92 monitorRegistered_ =
true;
97 TLOG(TLVL_WARNING) <<
"Error in TransferWrapper: attempt to register with dispatcher did not result in the \"Success\" response" ;
106 std::unique_ptr<artdaq::Fragment> fragmentPtr;
107 bool receivedFragment =
false;
108 static bool initialized =
false;
109 static size_t fragments_received = 0;
111 while (
true && !gSignalStatus)
113 fragmentPtr = std::make_unique<artdaq::Fragment>();
115 while (!receivedFragment)
119 TLOG(TLVL_INFO) <<
"Ctrl-C appears to have been hit" ;
126 auto result = transfer_->receiveFragment(*fragmentPtr, timeoutInUsecs_);
130 receivedFragment =
true;
131 fragments_received++;
133 static size_t cntr = 0;
134 auto mod = ++cntr % 10;
136 if (mod == 1) suffix =
"-st";
137 if (mod == 2) suffix =
"-nd";
138 if (mod == 3) suffix =
"-rd";
139 TLOG(TLVL_INFO) <<
"Received " << cntr << suffix <<
" event, "
140 <<
"seqID == " << fragmentPtr->sequenceID()
141 <<
", type == " << fragmentPtr->typeString() ;
146 TLOG(TLVL_ERROR) <<
"Transfer Plugin disconnected or other unrecoverable error. Shutting down.";
154 TLOG(TLVL_WARNING) <<
"Timeout occurred in call to transfer_->receiveFragmentFrom; will try again"
155 <<
", status = " << result;
161 ExceptionHandler(ExceptionHandlerRethrow::yes,
162 "Problem receiving data in TransferWrapper::receiveMessage");
166 if (fragmentPtr->type() == artdaq::Fragment::EndOfDataFragmentType)
177 extractTBufferFile(*fragmentPtr, msg);
181 ExceptionHandler(ExceptionHandlerRethrow::yes,
182 "Problem extracting TBufferFile from artdaq::Fragment in TransferWrapper::receiveMessage");
185 checkIntegrity(*fragmentPtr);
187 if (initialized || fragmentPtr->type() == artdaq::Fragment::InitFragmentType)
194 receivedFragment =
false;
196 if (fragments_received > maxEventsBeforeInit_)
198 throw cet::exception(
"TransferWrapper") <<
"First " << maxEventsBeforeInit_ <<
199 " events received did not include the \"Init\" event containing necessary info for art; exiting...";
207 artdaq::TransferWrapper::extractTBufferFile(
const artdaq::Fragment& fragment,
208 std::unique_ptr<TBufferFile>& tbuffer)
211 char* buffer = (
char *)malloc(header->
data_length);
212 memcpy(buffer, fragment.dataBeginBytes(), header->
data_length);
215 tbuffer.reset(
new TBufferFile(TBuffer::kRead, header->
data_length, buffer, kTRUE, 0));
219 artdaq::TransferWrapper::checkIntegrity(
const artdaq::Fragment& fragment)
const
221 const size_t artdaqheader = artdaq::detail::RawFragmentHeader::num_words() *
222 sizeof(artdaq::detail::RawFragmentHeader::RawDataType);
223 const size_t payload =
static_cast<size_t>(fragment.dataEndBytes() - fragment.dataBeginBytes());
225 const size_t totalsize = fragment.sizeBytes();
227 const size_t type =
static_cast<size_t>(fragment.type());
229 if (totalsize != artdaqheader + metadata + payload)
231 std::stringstream errmsg;
232 errmsg <<
"Error: artdaq fragment of type " <<
233 fragment.typeString() <<
", sequence ID " <<
234 fragment.sequenceID() <<
235 " has internally inconsistent measures of its size, signalling data corruption: in bytes," <<
236 " total size = " << totalsize <<
", artdaq fragment header = " << artdaqheader <<
237 ", metadata = " << metadata <<
", payload = " << payload;
239 TLOG(TLVL_ERROR) << errmsg.str() ;
241 if (quitOnFragmentIntegrityProblem_)
243 throw cet::exception(
"TransferWrapper") << errmsg.str();
251 auto findloc = std::find(allowedFragmentTypes_.begin(), allowedFragmentTypes_.end(),
static_cast<int>(type));
253 if (findloc == allowedFragmentTypes_.end())
255 std::stringstream errmsg;
256 errmsg <<
"Error: artdaq fragment appears to have type "
257 << type <<
", not found in the allowed fragment types list";
259 TLOG(TLVL_ERROR) << errmsg.str() ;
260 if (quitOnFragmentIntegrityProblem_)
262 throw cet::exception(
"TransferWrapper") << errmsg.str();
272 artdaq::TransferWrapper::unregisterMonitor()
274 if (!monitorRegistered_)
276 throw cet::exception(
"TransferWrapper") <<
277 "The function to unregister the monitor was called, but the monitor doesn't appear to be registered";
283 TLOG(TLVL_INFO) <<
"Requesting that this monitor (" << transfer_->uniqueLabel()
284 <<
") be unregistered from the dispatcher aggregator";
286 auto status = commander_->send_unregister_monitor(transfer_->uniqueLabel());
289 TLOG(TLVL_INFO) <<
"Response from dispatcher is \""
292 if (status ==
"Success")
294 monitorRegistered_ =
false;
297 else if (status ==
"busy")
301 throw cet::exception(
"TransferWrapper") <<
"Error in TransferWrapper: attempt to unregister with dispatcher did not result in the \"Success\" response";
310 if (monitorRegistered_)
318 ExceptionHandler(ExceptionHandlerRethrow::no,
319 "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.