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