Line data Source code
1 : #ifndef ARTDAQ_DAQDATA_HOSTMAP_HH
2 : #define ARTDAQ_DAQDATA_HOSTMAP_HH
3 :
4 : #include <map>
5 : #include <string>
6 : #include <vector>
7 :
8 : #include "fhiclcpp/ParameterSet.h"
9 : #include "fhiclcpp/types/Atom.h"
10 : #include "fhiclcpp/types/Comment.h"
11 : #include "fhiclcpp/types/Sequence.h" // Must pre-empt fhiclcpp/types/Atom.h
12 : #include "fhiclcpp/types/Table.h"
13 :
14 : #include "TRACE/trace.h"
15 :
16 : namespace artdaq {
17 :
18 : struct HostMap
19 : {
20 : /// <summary>
21 : /// Entries in the host_map should have these parameters. May be used for parameter validation
22 : /// </summary>
23 : struct HostConfig
24 : {
25 : /// "rank": Rank index
26 : fhicl::Atom<int> rank{fhicl::Name{"rank"}, fhicl::Comment{"Rank index"}};
27 : /// "host": Hostname for artdaq application with this rank
28 : fhicl::Atom<std::string> host{fhicl::Name{"host"}, fhicl::Comment{"Hostname for artdaq application with this rank"}};
29 : };
30 : /// <summary>
31 : /// Template for the host_map configuration parameter.
32 : /// </summary>
33 : struct Config
34 : {
35 : /// <summary>
36 : /// List of artdaq applications by rank and location. See artdaq::HostMap::HostConfig
37 : /// </summary>
38 : fhicl::Sequence<fhicl::Table<HostConfig>> host_map{fhicl::Name("host_map"), fhicl::Comment("List of artdaq applications by rank and location")};
39 : };
40 : };
41 :
42 : typedef std::map<int, std::string> hostMap_t; ///< The host_map is a map associating ranks with artdaq::DestinationInfo objects
43 :
44 : /// <summary>
45 : /// Create a list of HostMap::HostConfig ParameterSets from a hostMap_t map
46 : /// </summary>
47 : /// <param name="input">Input hostMap_t</param>
48 : /// <returns>std::vector containing HostMap::HostConfig ParameterSets</returns>
49 2 : inline std::vector<fhicl::ParameterSet> MakeHostMapPset(std::map<int, std::string> input)
50 : {
51 2 : std::vector<fhicl::ParameterSet> output;
52 2 : for (auto& rank : input)
53 : {
54 0 : fhicl::ParameterSet rank_output;
55 0 : rank_output.put<int>("rank", rank.first);
56 0 : rank_output.put<std::string>("host", rank.second);
57 0 : output.push_back(rank_output);
58 0 : }
59 2 : return output;
60 0 : }
61 :
62 : /// <summary>
63 : /// Make a hostMap_t from a HostMap::Config ParameterSet
64 : /// </summary>
65 : /// <param name="pset">fhicl::ParameterSet containing a HostMap::Config</param>
66 : /// <param name="map">Input map for consistency checking (Default: hostMap_t())</param>
67 : /// <returns>hostMap_t object</returns>
68 5 : inline hostMap_t MakeHostMap(fhicl::ParameterSet const& pset, hostMap_t map = hostMap_t())
69 : {
70 10 : if (pset.has_key("host_map"))
71 : {
72 0 : auto hosts = pset.get<std::vector<fhicl::ParameterSet>>("host_map");
73 0 : for (auto& ps : hosts)
74 : {
75 0 : auto rank = ps.get<int>("rank");
76 0 : auto hostname = ps.get<std::string>("host");
77 :
78 0 : if (map.count(rank) && (map[rank] != hostname))
79 : {
80 0 : TLOG(TLVL_ERROR) << "Inconsistent host maps supplied! Check configuration! There may be TCPSocket-related failures!";
81 : }
82 0 : map[rank] = hostname;
83 0 : }
84 0 : }
85 5 : return map;
86 : }
87 : } // namespace artdaq
88 :
89 : #endif // ARTDAQ_DAQDATA_HOSTMAP_HH
|