00001 #include "artdaq/Application/Routing/RoutingMasterPolicy.hh" 00002 #include "artdaq/Application/Routing/PolicyMacros.hh" 00003 #include <fhiclcpp/ParameterSet.h> 00004 00005 namespace artdaq 00006 { 00011 class RoundRobinPolicy : public RoutingMasterPolicy 00012 { 00013 public: 00020 explicit RoundRobinPolicy(fhicl::ParameterSet ps) : RoutingMasterPolicy(ps) {} 00021 00025 virtual ~RoundRobinPolicy() = default; 00026 00035 detail::RoutingPacket GetCurrentTable() override; 00036 }; 00037 00038 detail::RoutingPacket RoundRobinPolicy::GetCurrentTable() 00039 { 00040 auto tokens = getTokensSnapshot(); 00041 std::map<int, int> table; 00042 for (auto token : *tokens.get()) 00043 { 00044 table[token]++; 00045 } 00046 tokens->clear(); 00047 00048 detail::RoutingPacket output; 00049 auto endCondition = table.size() < GetReceiverCount(); 00050 while (!endCondition) 00051 { 00052 for (auto r : table) 00053 { 00054 output.emplace_back(detail::RoutingPacketEntry(next_sequence_id_++, r.first)); 00055 if(!endCondition) endCondition = r.second == 1; 00056 table[r.first]--; 00057 } 00058 } 00059 00060 for(auto r : table) 00061 { 00062 for(auto i = 0;i < r.second; ++i) 00063 { 00064 tokens->push_back(r.first); 00065 } 00066 } 00067 addUnusedTokens(std::move(tokens)); 00068 00069 return output; 00070 } 00071 } 00072 00073 DEFINE_ARTDAQ_ROUTING_POLICY(artdaq::RoundRobinPolicy)