artdaq  v3_09_06
transfer_plugin_sender.cc
1 #include "artdaq-core/Data/Fragment.hh"
2 #include "artdaq-core/Utilities/ExceptionHandler.hh"
3 #include "artdaq/TransferPlugins/TransferInterface.hh"
4 
5 #include "cetlib/BasicPluginFactory.h"
6 #include "cetlib/filepath_maker.h"
7 #include "fhiclcpp/ParameterSet.h"
8 #include "fhiclcpp/make_ParameterSet.h"
9 
10 #include <boost/asio.hpp>
11 #include <boost/lexical_cast.hpp>
12 
13 #include <algorithm>
14 #include <cstdlib>
15 #include <iostream>
16 #include <limits>
17 #include <memory>
18 #include <numeric>
19 #include <sstream>
20 #include <string>
21 
22 // DUPLICATED CODE: also found in transfer_plugin_receiver.cpp. Not as
23 // egregious as normal in that this function is unlikely to be
24 // changed, and this is a standalone app (not part of artdaq)
25 
26 fhicl::ParameterSet ReadParameterSet(const std::string& fhicl_filename)
27 {
28  if (std::getenv("FHICL_FILE_PATH") == nullptr)
29  {
30  std::cerr
31  << "INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
32  setenv("FHICL_FILE_PATH", ".", 0);
33  }
34 
35  fhicl::ParameterSet pset;
36  cet::filepath_lookup_after1 lookup_policy("FHICL_FILE_PATH");
37  fhicl::make_ParameterSet(fhicl_filename, lookup_policy, pset);
38 
39  return pset;
40 }
41 
42 int main(int argc, char* argv[]) try
43 {
44  if (argc != 4)
45  {
46  std::cerr << "Usage: <fhicl document> <number of sends (should be greater than 1) > <fragment payload size>" << std::endl;
47  return 1;
48  }
49 
50  auto fhicl_filename = boost::lexical_cast<std::string>(argv[1]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
51  auto num_sends = boost::lexical_cast<size_t>(argv[2]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
52  auto fragment_size = boost::lexical_cast<size_t>(argv[3]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
53 
54  if (num_sends <= 1)
55  {
56  std::cerr << "Logic in the program requires requested # of sends to be greater than 1" << std::endl;
57  return 1;
58  }
59 
60  std::unique_ptr<artdaq::TransferInterface> transfer;
61 
62  auto pset = ReadParameterSet(fhicl_filename);
63 
64  try
65  {
66  static cet::BasicPluginFactory bpf("transfer", "make");
67 
68  transfer =
69  bpf.makePlugin<std::unique_ptr<artdaq::TransferInterface>,
70  const fhicl::ParameterSet&,
72  pset.get<std::string>("transfer_plugin_type"),
73  pset,
75  }
76  catch (...)
77  {
78  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no,
79  "Error creating transfer plugin");
80  return 1;
81  }
82 
83  std::unique_ptr<artdaq::Fragment> frag = artdaq::Fragment::FragmentBytes(fragment_size);
84 
85  struct ArbitraryMetadata
86  {
87  const uint64_t val1 = 0;
88  const uint32_t val2 = 0;
89  };
90 
91  ArbitraryMetadata arbitraryMetadata;
92 
93  frag->setMetadata(arbitraryMetadata);
94 
95  // Fill the fragment with monotonically increasing 64-bit integers
96  // to be checked on the other end
97 
98  std::iota(reinterpret_cast<uint64_t*>(frag->dataBeginBytes()), // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
99  reinterpret_cast<uint64_t*>(frag->dataEndBytes()), // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
100  0);
101 
102  auto timeout = pset.get<size_t>("send_timeout_usecs", std::numeric_limits<size_t>::max());
103 
104  for (size_t i_i = 0; i_i < num_sends; ++i_i)
105  {
106  frag->setSequenceID(i_i + 1);
107  frag->setFragmentID(0);
108  frag->setUserType(artdaq::Fragment::FirstUserFragmentType);
109 
110  transfer->transfer_fragment_min_blocking_mode(*frag, timeout);
111  }
112 
113  std::cout << "# of sent fragments attempted == " << num_sends << std::endl;
114 
115  return 0;
116 }
117 catch (...)
118 {
119  return -1;
120 }
This TransferInterface is a Sender.
Role
Used to determine if a TransferInterface is a Sender or Receiver.