From d1a9bc2b450a5f8406a56825891a0fad556a3f62 Mon Sep 17 00:00:00 2001 From: schnelle Date: Thu, 11 Apr 2024 20:08:05 +0200 Subject: [PATCH] refactoring: the ModalityManager is also an IPADataProcessor and no more ClientInput interface is needed --- source/w3cipa/w3cipademo/src/main.cpp | 15 +---- source/w3cipa/w3cipaframework/CMakeLists.txt | 1 - .../w3c/voiceinteraction/ipa/ClientInput.h | 58 ------------------- .../voiceinteraction/ipa/IPADataProcessor.h | 2 +- .../ipa/client/ModalityManager.h | 6 +- .../ipa/external/ipa/IPAService.h | 3 +- .../voiceinteraction/ipa/IPADataProcessor.cpp | 2 +- .../ipa/client/ModalityManager.cpp | 10 ++++ .../reference/dialog/ReferenceIPAService.h | 11 ++-- .../reference/dialog/ReferenceIPAService.cpp | 40 ++++++++----- 10 files changed, 52 insertions(+), 96 deletions(-) delete mode 100644 source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/ClientInput.h diff --git a/source/w3cipa/w3cipademo/src/main.cpp b/source/w3cipa/w3cipademo/src/main.cpp index f85935c..4266c9e 100644 --- a/source/w3cipa/w3cipademo/src/main.cpp +++ b/source/w3cipa/w3cipademo/src/main.cpp @@ -55,10 +55,10 @@ int main() { registry->addIPAProvider(chatGPT); std::shared_ptr providerSelectionService = std::make_shared(registry); - std::shared_ptr<::reference::dialog::ReferenceIPAService> ipaService = std::make_shared<::reference::dialog::ReferenceIPAService>(providerSelectionService); providerSelectionService->addIPADataProcessorListener(ipaService); + ipaService->addIPADataProcessorListener(modalityManager); // Prepare the request @@ -69,21 +69,12 @@ int main() { modalityManager->startInput(listener); std::shared_ptr multiModalInputs = listener->getMultiModalInputs(); - std::shared_ptr request = + std::shared_ptr request = std::make_shared(nullptr, requestId, multiModalInputs, nullptr, nullptr); // Actually make the request - std::shared_ptr response = ipaService->processInput(request); - if (response == nullptr) { - LOG4CPLUS_ERROR(LOGGER, "no response received"); - return -1; - } - - // Determine the output and process it - std::shared_ptr outputs = - response->getMultiModalOutputs(); - modalityManager->handleOutput(outputs); + ipaService->processIPAData(request); return 0; } diff --git a/source/w3cipa/w3cipaframework/CMakeLists.txt b/source/w3cipa/w3cipaframework/CMakeLists.txt index c5a8fd3..f4a9ec9 100644 --- a/source/w3cipa/w3cipaframework/CMakeLists.txt +++ b/source/w3cipa/w3cipaframework/CMakeLists.txt @@ -24,7 +24,6 @@ set(HEADERS include/w3c/voiceinteraction/ipa/AudioDeliveryType.h include/w3c/voiceinteraction/ipa/AudioEncoding.h include/w3c/voiceinteraction/ipa/CallResult.h - include/w3c/voiceinteraction/ipa/ClientInput.h include/w3c/voiceinteraction/ipa/ClientRequest.h include/w3c/voiceinteraction/ipa/ClientResponse.h include/w3c/voiceinteraction/ipa/CombinedId.h diff --git a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/ClientInput.h b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/ClientInput.h deleted file mode 100644 index f67d1a9..0000000 --- a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/ClientInput.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * IPA Reference Implementation: https://github.com/w3c/voiceinteraction - * - * Copyright (C) 2024 World Wide Web Consortium. All Rights Reserved. - * - * This work is distributed under the W3C Software and Document License [1] - * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even - * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * [1] https://www.w3.org/Consortium/Legal/copyright-software - */ - -#if !defined(CLIENTINPUT_H) -#define CLIENTINPUT_H - -#include - -#include "ClientRequest.h" -#include "ClientResponse.h" - -namespace w3c { -namespace voiceinteraction { -namespace ipa { - -/** - * The interface for the client input. - * @author Dirk Schnelle-Walka - */ -class ClientInput -{ - -public: - /** - * Constructs a new object. - */ - ClientInput() { - } - - /** - * Destroys the object. - */ - virtual ~ClientInput() { - }; - - /** - * Processes the input from the client. - * @param request the request coming from the client - */ - virtual const std::shared_ptr processInput( - const std::shared_ptr& request) =0; - -}; - -} // namespace ipa -} // namespace voiceinteraction -} // namespace w3c - -#endif // !defined(CLIENTINPUT_H) diff --git a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/IPADataProcessor.h b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/IPADataProcessor.h index ee31db0..af8d7d6 100644 --- a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/IPADataProcessor.h +++ b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/IPADataProcessor.h @@ -61,7 +61,7 @@ class IPADataProcessor { * Asynchronously notifies all listeners about the processed data. * @param data the processed data */ - void notifyListeners(std::shared_ptr data); + void notifyListeners(const std::shared_ptr& data); private: /** List of known listeners for processed results. */ diff --git a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/client/ModalityManager.h b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/client/ModalityManager.h index bf94a7e..cc2cc5a 100644 --- a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/client/ModalityManager.h +++ b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/client/ModalityManager.h @@ -20,6 +20,7 @@ #include #include "w3c/voiceinteraction/ipa/MultiModalOutputs.h" +#include "w3c/voiceinteraction/ipa/IPADataProcessor.h" #include "ModalityComponent.h" #include "InputModalityComponent.h" #include "InputModalityComponentListener.h" @@ -34,8 +35,7 @@ namespace client { * A component that manages multiple modalities. * @author Dirk Schnelle-Walka */ -class ModalityManager -{ +class ModalityManager : public IPADataProcessor { public: /** @@ -72,6 +72,8 @@ class ModalityManager */ void startInput(std::shared_ptr& listener) const; + void processIPAData(std::shared_ptr data); + /** * Handles the provided multimodal output with all known modality handlers. * @param outputs the outputs to process diff --git a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/IPAService.h b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/IPAService.h index 51f15e3..64fba28 100644 --- a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/IPAService.h +++ b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/IPAService.h @@ -15,7 +15,6 @@ #include -#include "w3c/voiceinteraction/ipa/ClientInput.h" #include "w3c/voiceinteraction/ipa/external/ProviderSelectionService.h" namespace w3c { @@ -29,7 +28,7 @@ namespace ipa { * The IPAService is the base class for the IPA service. * @author Dirk Schnelle-Walka */ -class IPAService : public ClientInput { +class IPAService { public: /** * Constructs a new object. diff --git a/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/IPADataProcessor.cpp b/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/IPADataProcessor.cpp index a451a66..de16e24 100644 --- a/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/IPADataProcessor.cpp +++ b/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/IPADataProcessor.cpp @@ -29,7 +29,7 @@ void IPADataProcessor::addIPADataProcessorListener( listeners.push_back(listener); } -void IPADataProcessor::notifyListeners(std::shared_ptr data) { +void IPADataProcessor::notifyListeners(const std::shared_ptr &data) { for (const std::shared_ptr& listener : listeners) { std::thread thread([&data, &listener]{ listener->processIPAData(data); diff --git a/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/client/ModalityManager.cpp b/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/client/ModalityManager.cpp index 19098fe..5a9c724 100644 --- a/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/client/ModalityManager.cpp +++ b/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/client/ModalityManager.cpp @@ -10,6 +10,7 @@ * [1] https://www.w3.org/Consortium/Legal/copyright-software */ +#include "w3c/voiceinteraction/ipa/ClientResponse.h" #include "w3c/voiceinteraction/ipa/client/ModalityManager.h" namespace w3c { @@ -82,6 +83,15 @@ void ModalityManager::startInput(std::shared_ptr } } +void ModalityManager::processIPAData(std::shared_ptr data) { + if (std::shared_ptr response = + std::dynamic_pointer_cast(data)) { + std::shared_ptr outputs = + response->getMultiModalOutputs(); + handleOutput(outputs); + } +} + void ModalityManager::handleOutput(const std::shared_ptr& outputs) const { std::list outputModalities = outputs->getModalityTypes(); for (ModalityType& outputModality : outputModalities) { diff --git a/source/w3cipa/w3cipareferenceimplementation/include/w3c/voiceinteraction/ipa/reference/dialog/ReferenceIPAService.h b/source/w3cipa/w3cipareferenceimplementation/include/w3c/voiceinteraction/ipa/reference/dialog/ReferenceIPAService.h index 9f83262..9d14fc9 100644 --- a/source/w3cipa/w3cipareferenceimplementation/include/w3c/voiceinteraction/ipa/reference/dialog/ReferenceIPAService.h +++ b/source/w3cipa/w3cipareferenceimplementation/include/w3c/voiceinteraction/ipa/reference/dialog/ReferenceIPAService.h @@ -36,14 +36,17 @@ namespace dialog { */ class ReferenceIPAService : public IPAService, public IPADataProcessor { public: - ReferenceIPAService(const std::shared_ptr& service); - - const std::shared_ptr processInput( - const std::shared_ptr& request); + ReferenceIPAService( + const std::shared_ptr& service); void processIPAData(std::shared_ptr data); private: + void processIPAData(std::shared_ptr request); + + void processIPAData(std::shared_ptr request); + + /** Logger instance. */ const static log4cplus::Logger LOGGER; std::mutex mtx; diff --git a/source/w3cipa/w3cipareferenceimplementation/src/w3c/voiceinteraction/ipa/reference/dialog/ReferenceIPAService.cpp b/source/w3cipa/w3cipareferenceimplementation/src/w3c/voiceinteraction/ipa/reference/dialog/ReferenceIPAService.cpp index e40274c..df877b6 100644 --- a/source/w3cipa/w3cipareferenceimplementation/src/w3c/voiceinteraction/ipa/reference/dialog/ReferenceIPAService.cpp +++ b/source/w3cipa/w3cipareferenceimplementation/src/w3c/voiceinteraction/ipa/reference/dialog/ReferenceIPAService.cpp @@ -14,6 +14,8 @@ #include +#include + #include "w3c/voiceinteraction/ipa/reference/UUIDSessionId.h" #include "w3c/voiceinteraction/ipa/reference/TextMultiModalOutput.h" #include "w3c/voiceinteraction/ipa/reference/dialog/ReferenceIPAService.h" @@ -34,8 +36,21 @@ ReferenceIPAService::ReferenceIPAService( : IPAService(service) { } -const std::shared_ptr ReferenceIPAService::processInput( - const std::shared_ptr &request) { +void ReferenceIPAService::processIPAData(std::shared_ptr data) { + if (std::shared_ptr request = + std::dynamic_pointer_cast(data)) { + processIPAData(request); + } else if (std::shared_ptr response = + std::dynamic_pointer_cast(data)) { + processIPAData(response); + } else { + LOG4CPLUS_WARN(LOGGER, + LOG4CPLUS_TEXT("No valid conversion for the received data")); + } +} + +void ReferenceIPAService::processIPAData( + std::shared_ptr request) { // Check if there is already a session identifer and set one if there is // none const std::shared_ptr& id = request->getSessionId(); @@ -71,7 +86,7 @@ const std::shared_ptr ReferenceIPAService::processInput( if (externalResponse == nullptr) { LOG4CPLUS_ERROR(LOGGER, LOG4CPLUS_TEXT("No valid response received")); - return nullptr; + return; } // Create a reply to the client @@ -95,20 +110,15 @@ const std::shared_ptr ReferenceIPAService::processInput( externalResponse->getRequestId(), externalResponse->getMultiModalOutputs(), nullptr, nullptr); } - return response; + notifyListeners(response); } -void ReferenceIPAService::processIPAData(std::shared_ptr data) { - if (std::shared_ptr response = - std::dynamic_pointer_cast(data)) { - std::unique_lock lck(mtx); - CombinedId combinedId(response->getSessionId(), response->getRequestId()); - externalResponses[combinedId] = response; - cv.notify_one(); - } else { - LOG4CPLUS_WARN(LOGGER, - LOG4CPLUS_TEXT("No valid conversion for the received data")); - } +void ReferenceIPAService::processIPAData( + std::shared_ptr response) { + std::unique_lock lck(mtx); + CombinedId combinedId(response->getSessionId(), response->getRequestId()); + externalResponses[combinedId] = response; + cv.notify_one(); }