diff --git a/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp b/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp --- a/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp +++ b/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp @@ -214,26 +214,26 @@ if (messagesFromDatabase.size() > 0) { DeliveryBroker::getInstance().erase(clientDeviceID); } + auto responseToWriter = + [&writer](std::string fromDeviceID, std::string payload) { + tunnelbroker::GetResponse response; + response.set_fromdeviceid(fromDeviceID); + response.set_payload(payload); + if (!writer->Write(response)) { + throw std::runtime_error( + "gRPC: 'Get' writer error on sending data to the client"); + } + }; for (auto &messageFromDatabase : messagesFromDatabase) { - tunnelbroker::GetResponse response; - response.set_fromdeviceid(messageFromDatabase->getFromDeviceID()); - response.set_payload(messageFromDatabase->getPayload()); - if (!writer->Write(response)) { - throw std::runtime_error( - "gRPC: 'Get' writer error on sending data to the client"); - } + responseToWriter( + messageFromDatabase->getFromDeviceID(), + messageFromDatabase->getPayload()); database::DatabaseManager::getInstance().removeMessageItem( messageFromDatabase->getMessageID()); } while (1) { messageToDeliver = DeliveryBroker::getInstance().pop(clientDeviceID); - tunnelbroker::GetResponse response; - response.set_fromdeviceid(messageToDeliver.fromDeviceID); - response.set_payload(messageToDeliver.payload); - if (!writer->Write(response)) { - throw std::runtime_error( - "gRPC: 'Get' writer error on sending data to the client"); - } + responseToWriter(messageToDeliver.fromDeviceID, messageToDeliver.payload); comm::network::AmqpManager::getInstance().ack( messageToDeliver.deliveryTag); database::DatabaseManager::getInstance().removeMessageItem(