From 8be2fd33f4f8908da2a98e74f0e1a445de7a5f73 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Thu, 11 Jul 2019 17:56:11 +0200 Subject: [PATCH] Refactor some device code for better readability --- fairmq/FairMQDevice.cxx | 91 +++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/fairmq/FairMQDevice.cxx b/fairmq/FairMQDevice.cxx index 18ba6a86..a8202a79 100644 --- a/fairmq/FairMQDevice.cxx +++ b/fairmq/FairMQDevice.cxx @@ -197,6 +197,7 @@ void FairMQDevice::WaitForEndOfState(Transition transition) void FairMQDevice::InitWrapper() { + // run initialization once CompleteInit transition is requested fStateMachine.WaitForPendingState(); fId = fConfig->GetProperty("id", DefaultId); @@ -228,33 +229,33 @@ void FairMQDevice::InitWrapper() string networkInterface = fConfig->GetProperty("network-interface", DefaultNetworkInterface); // Fill the uninitialized channel containers - for (auto& mi : fChannels) { + for (auto& channel : fChannels) { int subChannelIndex = 0; - for (auto& vi : mi.second) { + for (auto& subChannel : channel.second) { // set channel transport - LOG(debug) << "Initializing transport for channel " << vi.fName << ": " << fair::mq::TransportNames.at(vi.fTransportType); - vi.InitTransport(AddTransport(vi.fTransportType)); + LOG(debug) << "Initializing transport for channel " << subChannel.fName << ": " << fair::mq::TransportNames.at(subChannel.fTransportType); + subChannel.InitTransport(AddTransport(subChannel.fTransportType)); - if (vi.fMethod == "bind") { + if (subChannel.fMethod == "bind") { // if binding address is not specified, try getting it from the configured network interface - if (vi.fAddress == "unspecified" || vi.fAddress == "") { + if (subChannel.fAddress == "unspecified" || subChannel.fAddress == "") { // if the configured network interface is default, get its name from the default route if (networkInterface == "default") { networkInterface = tools::getDefaultRouteNetworkInterface(); } - vi.fAddress = "tcp://" + tools::getInterfaceIP(networkInterface) + ":1"; + subChannel.fAddress = "tcp://" + tools::getInterfaceIP(networkInterface) + ":1"; } // fill the uninitialized list - fUninitializedBindingChannels.push_back(&vi); - } else if (vi.fMethod == "connect") { + fUninitializedBindingChannels.push_back(&subChannel); + } else if (subChannel.fMethod == "connect") { // fill the uninitialized list - fUninitializedConnectingChannels.push_back(&vi); - } else if (vi.fAddress.find_first_of("@+>") != string::npos) { + fUninitializedConnectingChannels.push_back(&subChannel); + } else if (subChannel.fAddress.find_first_of("@+>") != string::npos) { // fill the uninitialized list - fUninitializedConnectingChannels.push_back(&vi); + fUninitializedConnectingChannels.push_back(&subChannel); } else { - LOG(error) << "Cannot update configuration. Socket method (bind/connect) for channel '" << vi.fName << "' not specified."; - throw runtime_error(tools::ToString("Cannot update configuration. Socket method (bind/connect) for channel ", vi.fName, " not specified.")); + LOG(error) << "Cannot update configuration. Socket method (bind/connect) for channel '" << subChannel.fName << "' not specified."; + throw runtime_error(tools::ToString("Cannot update configuration. Socket method (bind/connect) for channel ", subChannel.fName, " not specified.")); } subChannelIndex++; @@ -724,7 +725,7 @@ void FairMQDevice::SetConfig(ProgOptions& config) void FairMQDevice::LogSocketRates() { - vector filteredSockets; + vector filteredChannels; vector filteredChannelNames; vector logIntervals; vector intervalCounters; @@ -732,40 +733,40 @@ void FairMQDevice::LogSocketRates() size_t chanNameLen = 0; // iterate over the channels map - for (const auto& mi : fChannels) { + for (auto& channel : fChannels) { // iterate over the channels vector - for (auto vi = (mi.second).begin(); vi != (mi.second).end(); ++vi) { - if (vi->fRateLogging > 0) { - filteredSockets.push_back(vi->fSocket.get()); - logIntervals.push_back(vi->fRateLogging); + for (auto& subChannel : channel.second) { + if (subChannel.fRateLogging > 0) { + filteredChannels.push_back(&subChannel); + logIntervals.push_back(subChannel.fRateLogging); intervalCounters.push_back(0); - filteredChannelNames.push_back(tools::ToString(mi.first, "[", vi - (mi.second).begin(), "]")); + filteredChannelNames.push_back(subChannel.GetName()); chanNameLen = max(chanNameLen, filteredChannelNames.back().length()); } } } - vector bytesIn(filteredSockets.size()); - vector msgIn(filteredSockets.size()); - vector bytesOut(filteredSockets.size()); - vector msgOut(filteredSockets.size()); + vector bytesIn(filteredChannels.size()); + vector msgIn(filteredChannels.size()); + vector bytesOut(filteredChannels.size()); + vector msgOut(filteredChannels.size()); - vector bytesInNew(filteredSockets.size()); - vector msgInNew(filteredSockets.size()); - vector bytesOutNew(filteredSockets.size()); - vector msgOutNew(filteredSockets.size()); + vector bytesInNew(filteredChannels.size()); + vector msgInNew(filteredChannels.size()); + vector bytesOutNew(filteredChannels.size()); + vector msgOutNew(filteredChannels.size()); - vector mbPerSecIn(filteredSockets.size()); - vector msgPerSecIn(filteredSockets.size()); - vector mbPerSecOut(filteredSockets.size()); - vector msgPerSecOut(filteredSockets.size()); + vector mbPerSecIn(filteredChannels.size()); + vector msgPerSecIn(filteredChannels.size()); + vector mbPerSecOut(filteredChannels.size()); + vector msgPerSecOut(filteredChannels.size()); int i = 0; - for (const auto& vi : filteredSockets) { - bytesIn.at(i) = vi->GetBytesRx(); - bytesOut.at(i) = vi->GetBytesTx(); - msgIn.at(i) = vi->GetMessagesRx(); - msgOut.at(i) = vi->GetMessagesTx(); + for (const auto& channel : filteredChannels) { + bytesIn.at(i) = channel->GetBytesRx(); + bytesOut.at(i) = channel->GetBytesTx(); + msgIn.at(i) = channel->GetMessagesRx(); + msgOut.at(i) = channel->GetMessagesTx(); ++i; } @@ -782,17 +783,17 @@ void FairMQDevice::LogSocketRates() i = 0; - for (const auto& vi : filteredSockets) { + for (const auto& channel : filteredChannels) { intervalCounters.at(i)++; if (intervalCounters.at(i) == logIntervals.at(i)) { intervalCounters.at(i) = 0; if (msSinceLastLog > 0) { - bytesInNew.at(i) = vi->GetBytesRx(); - msgInNew.at(i) = vi->GetMessagesRx(); - bytesOutNew.at(i) = vi->GetBytesTx(); - msgOutNew.at(i) = vi->GetMessagesTx(); + bytesInNew.at(i) = channel->GetBytesRx(); + msgInNew.at(i) = channel->GetMessagesRx(); + bytesOutNew.at(i) = channel->GetBytesTx(); + msgOutNew.at(i) = channel->GetMessagesTx(); mbPerSecIn.at(i) = (static_cast(bytesInNew.at(i) - bytesIn.at(i)) / (1000. * 1000.)) / static_cast(msSinceLastLog) * 1000.; msgPerSecIn.at(i) = static_cast(msgInNew.at(i) - msgIn.at(i)) / static_cast(msSinceLastLog) * 1000.; @@ -822,8 +823,8 @@ void FairMQDevice::LogSocketRates() void FairMQDevice::UnblockTransports() { - for (auto& t : fTransports) { - t.second->Interrupt(); + for (auto& transport : fTransports) { + transport.second->Interrupt(); } }