artdaq  v3_03_00
transfer_plugin_receiver.cc
1 #include "artdaq/TransferPlugins/TransferInterface.hh"
2 
3 #include "artdaq-core/Data/Fragment.hh"
4 #include "artdaq-core/Utilities/ExceptionHandler.hh"
5 
6 #include "cetlib/BasicPluginFactory.h"
7 #include "cetlib_except/exception.h"
8 #include "cetlib/filepath_maker.h"
9 #include "fhiclcpp/ParameterSet.h"
10 #include "fhiclcpp/make_ParameterSet.h"
11 
12 #include <boost/asio.hpp>
13 #include <boost/bind.hpp>
14 
15 #include <iostream>
16 #include <string>
17 #include <limits>
18 
19 
20 // DUPLICATED CODE: also found in transfer_plugin_sender.cpp. Not as egregious as
21 // normal in that this function is unlikely to be changed, and this is
22 // a standalone app (not part of artdaq)
23 
24 fhicl::ParameterSet ReadParameterSet(const std::string& fhicl_filename)
25 {
26  if (std::getenv("FHICL_FILE_PATH") == nullptr)
27  {
28  std::cerr
29  << "INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
30  setenv("FHICL_FILE_PATH", ".", 0);
31  }
32 
33  fhicl::ParameterSet pset;
34  cet::filepath_lookup_after1 lookup_policy("FHICL_FILE_PATH");
35  fhicl::make_ParameterSet(fhicl_filename, lookup_policy, pset);
36 
37  return pset;
38 }
39 
40 
41 int do_check(const artdaq::Fragment& frag);
42 
43 int main(int argc, char* argv[])
44 {
45  if (argc != 2)
46  {
47  std::cerr << "Usage: transfer_plugin_receiver <fhicl document>" << std::endl;
48  return 1;
49  }
50 
51  std::string fhicl_filename = boost::lexical_cast<std::string>(argv[1]);
52 
53  std::unique_ptr<artdaq::TransferInterface> transfer;
54  auto pset = ReadParameterSet(fhicl_filename);
55 
56  try
57  {
58  static cet::BasicPluginFactory bpf("transfer", "make");
59 
60  transfer =
61  bpf.makePlugin<std::unique_ptr<artdaq::TransferInterface>,
62  const fhicl::ParameterSet&,
64  pset.get<std::string>("transfer_plugin_type"),
65  pset,
67  }
68  catch (...)
69  {
70  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::yes,
71  "Error creating transfer plugin");
72  }
73 
74 
75  while (true)
76  {
77  artdaq::Fragment myfrag;
78  size_t timeout = 10 * 1e6;
79 
80  auto retval = transfer->receiveFragment(myfrag, timeout);
81 
83  {
84  std::cout << "Returned from call to transfer_->receiveFragmentFrom; fragment with seqID == " <<
85  myfrag.sequenceID() << ", fragID == " << myfrag.fragmentID() << " has size " <<
86  myfrag.sizeBytes() << " bytes" << std::endl;
87  }
88  else
89  {
90  std::cerr << "RECV_TIMEOUT received from call to transfer->receiveFragmentFrom" << std::endl;
91  continue;
92  }
93 
94  if (do_check(myfrag) != 0)
95  {
96  std::cerr << "Error: do_check indicates fragment failed to transmit correctly" << std::endl;
97  }
98  else
99  {
100  std::cerr << "Success: do_check indicates fragment transmitted correctly" << std::endl;
101  }
102  }
103 
104  return 0;
105 }
106 
107 // JCF, Jun-22-2016
108 
109 // do_check assumes std::iota was used to fill the sent fragment with
110 // monotonically incrementing 64-bit unsigned integers
111 
112 int do_check(const artdaq::Fragment& frag)
113 {
114  uint64_t variable_to_compare = 0;
115 
116  for (auto ptr_into_frag = reinterpret_cast<const uint64_t*>(frag.dataBeginBytes());
117  ptr_into_frag != reinterpret_cast<const uint64_t*>(frag.dataEndBytes());
118  ++ptr_into_frag , ++variable_to_compare)
119  {
120  if (variable_to_compare != *ptr_into_frag)
121  {
122  std::cerr << "ERROR for fragment with sequence ID " << frag.sequenceID() << ", fragment ID " <<
123  frag.fragmentID() << ": expected ADC value of " << variable_to_compare << ", got " << *ptr_into_frag << std::endl;
124  return 1;
125  }
126  }
127 
128  return 0;
129 }
This TransferInterface is a Receiver.
Role
Used to determine if a TransferInterface is a Sender or Receiver.
For code clarity, things checking for successful receive should check retval &gt;= NO_RANK_INFO.