Skip to content

Commit

Permalink
introduced the concept of an IPADataProcessor and removed the
Browse files Browse the repository at this point in the history
ExternalClientResponseListener.h
  • Loading branch information
schnelle committed Apr 11, 2024
1 parent 3d21e2d commit cd76c6e
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 76 deletions.
2 changes: 1 addition & 1 deletion source/w3cipa/w3cipademo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int main() {

std::shared_ptr<::reference::dialog::ReferenceIPAService> ipaService =
std::make_shared<::reference::dialog::ReferenceIPAService>(providerSelectionService);
providerSelectionService->addExternalClientResponseListener(ipaService);
providerSelectionService->addIPADataProcessorListener(ipaService);


// Prepare the request
Expand Down
3 changes: 2 additions & 1 deletion source/w3cipa/w3cipaframework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ set(HEADERS
include/w3c/voiceinteraction/ipa/ErrorMessage.h
include/w3c/voiceinteraction/ipa/ExternalClientInput.h
include/w3c/voiceinteraction/ipa/ExternalClientResponse.h
include/w3c/voiceinteraction/ipa/ExternalClientResponseListener.h
include/w3c/voiceinteraction/ipa/IPAData.h
include/w3c/voiceinteraction/ipa/IPADataProcessor.h
include/w3c/voiceinteraction/ipa/MetaData.h
include/w3c/voiceinteraction/ipa/ModalityType.h
include/w3c/voiceinteraction/ipa/MultiModalInput.h
Expand Down Expand Up @@ -66,6 +66,7 @@ set(SOURCES
src/w3c/voiceinteraction/ipa/ErrorMessage.cpp
src/w3c/voiceinteraction/ipa/ExternalClientResponse.cpp
src/w3c/voiceinteraction/ipa/IPAData.cpp
src/w3c/voiceinteraction/ipa/IPADataProcessor.cpp
src/w3c/voiceinteraction/ipa/MultiModalInput.cpp
src/w3c/voiceinteraction/ipa/MultiModalInputs.cpp
src/w3c/voiceinteraction/ipa/MultiModalOutput.cpp
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace w3c {
namespace voiceinteraction {
namespace ipa {


/**
* Base class for all data that is sent around through various processing
* stages.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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
*/

#ifndef IPADATAPROCESSOR_H
#define IPADATAPROCESSOR_H

#include <list>

#include "IPAData.h"

namespace w3c {
namespace voiceinteraction {
namespace ipa {

/**
* An IPA data processor is able to consume {@link IPAData}, process it
* and forward the processed result to registered other IPA data processors
* to eventually further process the data.
* @author Dirk Schnelle-Walka
*/
class IPADataProcessor {
public:
/**
* Creates a new instance.
*/
IPADataProcessor();

/**
* Destroys this instance.
*/
virtual ~IPADataProcessor();

/**
* Processes the data and informs all registered listeners afterwards
* via {@link #notifyListeners}.
*
* Conceptually, this method can be called multiple times depending on
* how many other processors this instance has been subsribed to.
* @param data the data to process;
*/
virtual void processIPAData(std::shared_ptr<IPAData> data) = 0;

/**
* Adds the given listener to the list of known listeners.
* @param listener the listener to add
*/
void addIPADataProcessorListener(
const std::shared_ptr<IPADataProcessor>& listener);

protected:
/**
* Asynchronously notifies all listeners about the processed data.
* @param data the processed data
*/
void notifyListeners(std::shared_ptr<IPAData> data);

private:
/** List of known listeners for processed results. */
std::list<std::shared_ptr<IPADataProcessor>> listeners;
};

} // namespace ipa
} // namespace voiceinteraction
} // namespace w3c

#endif // IPADATAPROCESSOR_H
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <list>

#include "w3c/voiceinteraction/ipa/ClientRequest.h"
#include "w3c/voiceinteraction/ipa/ExternalClientResponseListener.h"
#include "w3c/voiceinteraction/ipa/IPADataProcessor.h"
#include "w3c/voiceinteraction/ipa/external/ipa/ProviderRegistry.h"

using namespace w3c::voiceinteraction::ipa::external::ipa;
Expand All @@ -35,7 +35,7 @@ namespace external {
* the list of available providers.
* @author Dirk Schnelle-Walka
*/
class ProviderSelectionService {
class ProviderSelectionService : public IPADataProcessor {
public:
/**
* Constructs a new object.
Expand All @@ -54,20 +54,11 @@ class ProviderSelectionService {
*/
void processInput(const std::shared_ptr<ClientRequest>& request);

/**
* Adds the provided listener to the list of known external client
* responses.
* @param listener the listener to add
*/
void addExternalClientResponseListener(
const std::shared_ptr<ExternalClientResponseListener>& listener);
void processIPAData(std::shared_ptr<IPAData> data);

protected:
/** The provider registry. */
std::shared_ptr<ProviderRegistry> providerRegistry;

/** List of known listeners for externaö client responses. */
std::list<std::shared_ptr<ExternalClientResponseListener>> externalClientResponseListeners;
};

} // namespace external
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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
*/

#include <thread>

#include "w3c/voiceinteraction/ipa/IPADataProcessor.h"

namespace w3c {
namespace voiceinteraction {
namespace ipa {

IPADataProcessor::IPADataProcessor() {
}

IPADataProcessor::~IPADataProcessor() {
}

void IPADataProcessor::addIPADataProcessorListener(
const std::shared_ptr<IPADataProcessor>& listener) {
listeners.push_back(listener);
}

void IPADataProcessor::notifyListeners(std::shared_ptr<IPAData> data) {
for (const std::shared_ptr<IPADataProcessor>& listener : listeners) {
std::thread thread([&data, &listener]{
listener->processIPAData(data);
});
thread.join();
}
}


} // namespace ipa
} // namespace voiceinteraction
} // namespace w3c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ ProviderSelectionService::ProviderSelectionService(const std::shared_ptr<Provide
ProviderSelectionService::~ProviderSelectionService() {
}

void ProviderSelectionService::addExternalClientResponseListener(
const std::shared_ptr<ExternalClientResponseListener>& listener) {
externalClientResponseListeners.push_back(listener);
}

void ProviderSelectionService::processInput(
const std::shared_ptr<ClientRequest>& request) {
std::list<std::shared_ptr<ExternalClientResponse>> responses;
Expand Down Expand Up @@ -70,13 +65,15 @@ void ProviderSelectionService::processInput(
}

// Notify all listeners about the results.
for (const std::shared_ptr<ExternalClientResponseListener>& listener : externalClientResponseListeners) {
for (const std::shared_ptr<ExternalClientResponse>& response : responses) {
listener->processExternalClientResponse(response);
}
for (const std::shared_ptr<ExternalClientResponse>& response : responses) {
notifyListeners(response);
}
}

void ProviderSelectionService::processIPAData(
std::shared_ptr<IPAData> data) {
}

} // namespace external
} // namespace ipa
} // namespace voiceinteraction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <log4cplus/logger.h>

#include "w3c/voiceinteraction/ipa/CombinedId.h"
#include "w3c/voiceinteraction/ipa/ExternalClientResponseListener.h"
#include "w3c/voiceinteraction/ipa/IPADataProcessor.h"
#include <w3c/voiceinteraction/ipa/external/ipa/IPAService.h>

using namespace w3c::voiceinteraction::ipa::external;
Expand All @@ -34,14 +34,14 @@ namespace dialog {
* @brief A reference implementation of an IPA Service
* @author Dirk Schnelle-Walka
*/
class ReferenceIPAService : public IPAService, public ExternalClientResponseListener {
class ReferenceIPAService : public IPAService, public IPADataProcessor {
public:
ReferenceIPAService(const std::shared_ptr<ProviderSelectionService>& service);

const std::shared_ptr<ClientResponse> processInput(
const std::shared_ptr<ClientRequest>& request);

void processExternalClientResponse(
const std::shared_ptr<ExternalClientResponse>& response);
void processIPAData(std::shared_ptr<IPAData> data);

private:
/** Logger instance. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ const std::shared_ptr<ClientResponse> ReferenceIPAService::processInput(
return response;
}

void ReferenceIPAService::processExternalClientResponse(
const std::shared_ptr<ExternalClientResponse>& response) {
std::unique_lock<std::mutex> lck(mtx);
CombinedId combinedId(response->getSessionId(), response->getRequestId());
externalResponses[combinedId] = response;
cv.notify_one();
void ReferenceIPAService::processIPAData(std::shared_ptr<IPAData> data) {
if (std::shared_ptr<ExternalClientResponse> response =
std::dynamic_pointer_cast<ExternalClientResponse>(data)) {
std::unique_lock<std::mutex> 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"));
}
}


Expand Down

0 comments on commit cd76c6e

Please sign in to comment.