::grpc::Status ProtocolService::subscribe(::grpc::ServerContext* context, const ::grpcservices::mcuinterface::SubscribeRequest* request, ::grpc::ServerWriter< ::grpcservices::mcuinterface::MCUStreamingPackets>* writer)
{
std::shared_ptr<IDataWriter> l_writer = std::make_shared<GrpcDataWriter>(writer);
uint16_t clientId = request->clientid();
m_dataRelayService->setDataWriter(l_writer,clientId);
//Message received successfully
//TODO Log the data
std::cout << "Client Subscribed with ID: " << clientId << std::endl;
//Every client which calls the subscribe function will set their respective data writer and
//waits here for the channel not to get closed and continues the streaming
m_continueStreaming[clientId] = true;
while(m_continueStreaming.at(clientId))
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));//50 ms
}
//The client is uncubscribed so remove the client from the map
m_continueStreaming.erase(clientId);
return ::grpc::Status::OK;
}
//function where i sends the data using writer
for(const auto& writer: m_writers)
{
std::shared_ptr<GrpcDataWriter> l_writer = std::dynamic_pointer_cast<GrpcDataWriter>(writer.second);
l_writer->setData(packet);
l_writer->writeData();
}