artdaq  v3_12_02
TransferInterface.hh
1 #ifndef artdaq_ArtModules_TransferInterface_hh
2 #define artdaq_ArtModules_TransferInterface_hh
3 
4 #include "TRACE/tracemf.h" // Pre-empt TRACE/trace.h from Fragment.hh.
5 #include "artdaq-core/Data/Fragment.hh"
6 
7 #include "artdaq/DAQdata/Globals.hh" // my_rank
8 
9 #include "artdaq-core/Data/detail/RawFragmentHeader.hh"
10 
11 #include "fhiclcpp/types/Atom.h"
12 #include "fhiclcpp/types/Comment.h"
13 #include "fhiclcpp/types/ConfigurationTable.h"
14 #include "fhiclcpp/types/Name.h"
15 
16 namespace fhicl {
17 class ParameterSet;
18 }
19 
20 #include "cetlib/compiler_macros.h" // EXTERN_C_FUNC_*
21 
22 #include <iostream>
23 #include <limits>
24 #include <memory>
25 #include <sstream>
26 #include <string>
27 
28 namespace artdaq {
33 {
34 public:
38  struct Config
39  {
41  fhicl::Atom<int> source_rank{fhicl::Name{"source_rank"}, fhicl::Comment{"The rank that data is coming from"}, my_rank};
43  fhicl::Atom<int> destination_rank{fhicl::Name{"destination_rank"}, fhicl::Comment{"The rank that data is going to"}, my_rank};
45  fhicl::Atom<std::string> unique_label{fhicl::Name{"unique_label"}, fhicl::Comment{"A label that uniquely identifies the TransferInterface instance"}, "transfer_between_[source_rank]_and_[destination_rank]"};
47  fhicl::Atom<size_t> buffer_count{fhicl::Name{"buffer_count"}, fhicl::Comment{"How many Fragments can the TransferInterface handle simultaneously"}, 10};
49  fhicl::Atom<size_t> max_fragment_size{fhicl::Name{"max_fragment_size_words"}, fhicl::Comment{"The maximum Fragment size expected.May be used for static memory allocation, and will cause errors if larger Fragments are sent."}, 1024};
50  };
52  using Parameters = fhicl::WrappedTable<Config>;
53 
57  enum ReceiveReturnCode : int
58  {
59  DATA_END = -2222,
60  RECV_TIMEOUT = -1111,
61  NO_RANK_INFO = -1,
63  };
64 
68  enum class Role
69  {
70  kSend,
71  kReceive
72  };
73 
78  enum class CopyStatus
79  {
80  kSuccess,
81  kTimeout,
83  };
84 
90  static std::string CopyStatusToString(CopyStatus in)
91  {
92  switch (in)
93  {
95  return "Success";
97  return "Timeout";
99  return "Error";
100  default:
101  return "UNKNOWN";
102  }
103  return "SWITCHERROR";
104  }
105 
111  TransferInterface(const fhicl::ParameterSet& ps, Role role);
112 
116  TransferInterface(const TransferInterface&) = delete;
117 
123 
127  virtual ~TransferInterface() = default;
128 
135  virtual int receiveFragment(artdaq::Fragment& fragment, size_t receive_timeout);
136 
143  virtual int receiveFragmentHeader(detail::RawFragmentHeader& header, size_t receiveTimeout) = 0;
144 
154  virtual int receiveFragmentData(RawDataType* destination, size_t wordCount) = 0;
155 
162  virtual CopyStatus transfer_fragment_min_blocking_mode(artdaq::Fragment const& fragment, size_t send_timeout_usec) = 0;
163 
169  virtual CopyStatus transfer_fragment_reliable_mode(artdaq::Fragment&& fragment) = 0;
170 
175  std::string uniqueLabel() const { return unique_label_; }
176 
181  virtual int source_rank() const { return source_rank_; }
186  virtual int destination_rank() const { return destination_rank_; }
187 
192  virtual bool isRunning() { return false; }
193 
197  virtual void flush_buffers() = 0;
198 
200 #define GetTraceName() unique_label_ << (role_ == Role::kSend ? std::string("_SEND: ") : std::string("_RECV: "))
201 
203 protected:
206 
207  const Role role_;
208 
209  const int source_rank_;
210  const int destination_rank_;
211  const std::string unique_label_;
212 
213  size_t buffer_count_;
215 
216 protected:
221  Role role() const { return role_; }
222 };
223 } // namespace artdaq
224 
227 #ifndef EXTERN_C_FUNC_DECLARE_START
228 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
229 #endif
230 
231 #define DEFINE_ARTDAQ_TRANSFER(klass) \
232  EXTERN_C_FUNC_DECLARE_START \
233  std::unique_ptr<artdaq::TransferInterface> make(fhicl::ParameterSet const& ps, artdaq::TransferInterface::Role role) \
234  { \
235  return std::unique_ptr<artdaq::TransferInterface>(new klass(ps, role)); \
236  } \
237  }
238 
241 #endif /* artdaq_ArtModules_TransferInterface.hh */
242 
243 // Local Variables:
244 // mode: c++
245 // End:
size_t buffer_count_
The number of Fragment transfers the TransferInterface can handle simultaneously. ...
ReceiveReturnCode
Return codes from receive operations
virtual int receiveFragmentHeader(detail::RawFragmentHeader &header, size_t receiveTimeout)=0
Receive a Fragment Header from the transport mechanism.
virtual int source_rank() const
Get the source rank for this TransferInterface instance.
TransferInterface & operator=(const TransferInterface &)=delete
Copy Assignment operator is deleted.
Role role() const
Get the TransferInterface::Role of this TransferInterface.
const std::string unique_label_
Unique label of transfer (ideally the same on sender and receiver)
virtual ~TransferInterface()=default
Default virtual Destructor.
This TransferInterface is a Receiver.
virtual void flush_buffers()=0
Flush any in-flight data. This should be used by the receiver after the receive loop has ended...
const int destination_rank_
Rank of destination.
static std::string CopyStatusToString(CopyStatus in)
Convert a CopyStatus variable to its string represenatation
virtual int receiveFragmentData(RawDataType *destination, size_t wordCount)=0
Receive the body of a Fragment to the given destination pointer.
fhicl::Atom< std::string > unique_label
&quot;unique_label&quot; (Default: &quot;transfer_between_[source_rank]_and_[destination_rank]&quot;) : A label that uniq...
Value that is to be returned when a Transfer plugin determines that no more data will be arriving...
Configuration of the TransferInterface. May be used for parameter validation
virtual bool isRunning()
Determine whether the TransferInterface plugin is able to send/receive data.
This TransferInterface is a Sender.
fhicl::Atom< size_t > max_fragment_size
&quot;max_fragment_size_words&quot; (Default: 1024) : The maximum Fragment size expected.May be used for static...
virtual CopyStatus transfer_fragment_reliable_mode(artdaq::Fragment &&fragment)=0
Transfer a Fragment to the destination. This should be reliable, if the underlying transport mechanis...
Some error occurred, but no exception was thrown.
Role
Used to determine if a TransferInterface is a Sender or Receiver.
Will be returned from a successful receive that does not know the source rank (Transfer to OM art pro...
The send operation completed successfully.
std::string uniqueLabel() const
Get the unique label of this TransferInterface instance.
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation (if desired)
virtual CopyStatus transfer_fragment_min_blocking_mode(artdaq::Fragment const &fragment, size_t send_timeout_usec)=0
Transfer a Fragment to the destination. May not necessarily be reliable, but will not block longer th...
This interface defines the functions used to transfer data between artdaq applications.
virtual int destination_rank() const
Get the destination rank for this TransferInterface instance.
fhicl::Atom< size_t > buffer_count
&quot;buffer_count&quot; (Default: 10) : How many Fragments can the TransferInterface handle simultaneously ...
TransferInterface(const fhicl::ParameterSet &ps, Role role)
TransferInterface Constructor.
For code clarity, things checking for successful receive should check retval &gt;= NO_RANK_INFO.
const Role role_
Whether this instance of TransferInterface is a sender or receiver.
fhicl::Atom< int > source_rank
&quot;source_rank&quot; (Default: my_rank) : The rank that data is coming from
fhicl::Atom< int > destination_rank
&quot;destination_rank&quot; (Default: my_rank) : The rank that data is going to
Value to be returned upon receive timeout.
virtual int receiveFragment(artdaq::Fragment &fragment, size_t receive_timeout)
Receive a Fragment from the transport mechanism.
CopyStatus
Returned from the send functions, this enumeration describes the possible return codes. If an exception occurs, it will be thrown and should be handled normally.
const size_t max_fragment_size_words_
The maximum size of the transferred Fragment objects, in artdaq::Fragment::RawDataType words...
const int source_rank_
Rank of source.