-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Participant parameters #165
Draft
KonradBkd
wants to merge
3
commits into
main
Choose a base branch
from
dev_participant_parameters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+328
−0
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-FileCopyrightText: 2023 Vector Informatik GmbH | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "gtest/gtest.h" | ||
#include "silkit/vendor/CreateSilKitRegistry.hpp" | ||
#include "silkit/SilKit.hpp" | ||
|
||
namespace { | ||
|
||
struct ITest_GetParameter : public testing::Test | ||
{ | ||
|
||
void checkGetParameterValues(SilKit::IParticipant* participant, std::unordered_map<SilKit::Parameter, std::string> expected) | ||
{ | ||
for (auto it : expected) | ||
{ | ||
EXPECT_EQ(it.second, participant->GetParameter(it.first)); | ||
} | ||
} | ||
|
||
const std::string _registryUriAnyPort = "silkit://127.0.0.1:0"; | ||
}; | ||
|
||
// Check that GetParameter return the values set via api | ||
TEST_F(ITest_GetParameter, get_parameter_set_by_api) | ||
{ | ||
const std::string participantNameByApi = "P1"; | ||
|
||
auto emptyParticipantConfig = SilKit::Config::ParticipantConfigurationFromString(""); | ||
|
||
auto registry = SilKit::Vendor::Vector::CreateSilKitRegistry(emptyParticipantConfig); | ||
auto registryUriByApi = registry->StartListening(_registryUriAnyPort); | ||
|
||
auto participant = SilKit::CreateParticipant(emptyParticipantConfig, participantNameByApi, registryUriByApi); | ||
|
||
checkGetParameterValues(participant.get(), {{SilKit::Parameter::ParticipantName, participantNameByApi}, | ||
{SilKit::Parameter::RegistryUri, registryUriByApi}}); | ||
} | ||
|
||
// Config values take precedence over api values | ||
// Check that GetParameter actually return the config values if both are set | ||
TEST_F(ITest_GetParameter, get_parameter_set_by_config) | ||
{ | ||
const std::string participantNameByApi = "P2"; | ||
const std::string registryUriByApi = "silkit://127.0.0.42:0"; | ||
|
||
const std::string participantNameByConfig = "P1"; | ||
|
||
auto emptyParticipantConfig = SilKit::Config::ParticipantConfigurationFromString(""); | ||
|
||
auto registry = SilKit::Vendor::Vector::CreateSilKitRegistry(emptyParticipantConfig); | ||
auto registryUriByConfig = registry->StartListening(_registryUriAnyPort); | ||
|
||
std::ostringstream ss; | ||
ss << R"({ "ParticipantName": ")" << participantNameByConfig << R"(", "Middleware": { "RegistryUri": ")" | ||
<< registryUriByConfig << R"(" }})"; | ||
auto participantConfig = SilKit::Config::ParticipantConfigurationFromString(ss.str()); | ||
|
||
auto participant = SilKit::CreateParticipant(participantConfig, participantNameByApi, registryUriByApi); | ||
|
||
checkGetParameterValues(participant.get(), {{SilKit::Parameter::ParticipantName, participantNameByConfig}, | ||
{SilKit::Parameter::RegistryUri, registryUriByConfig}}); | ||
|
||
} | ||
|
||
} //end namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#pragma once | ||
#include <stdint.h> | ||
#include "SilKitMacros.h" | ||
|
||
#pragma pack(push) | ||
#pragma pack(8) | ||
|
||
SILKIT_BEGIN_DECLS | ||
|
||
/*! Internal parameter provider. */ | ||
typedef struct SilKit_ParamterProvider SilKit_ParamterProvider; | ||
|
||
/*! A parameter set by an API call and/or the participant configuration. */ | ||
typedef int16_t SilKit_Parameter; | ||
|
||
/*! An undefined parameter */ | ||
#define SilKit_Parameter_Undefined ((SilKit_Parameter)0) | ||
/*! The name of the participant */ | ||
#define SilKit_Parameter_ParticipantName ((SilKit_Parameter)1) | ||
/*! The registry URI */ | ||
#define SilKit_Parameter_ReistryUri ((SilKit_Parameter)2) | ||
|
||
SILKIT_END_DECLS | ||
|
||
#pragma pack(pop) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
SilKit/include/silkit/detail/impl/participant/ParameterProvider.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include "silkit/capi/Parameters.h" | ||
|
||
namespace SilKit { | ||
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_BEGIN | ||
namespace Impl { | ||
|
||
class ParameterProvider | ||
{ | ||
public: | ||
inline ParameterProvider(); | ||
|
||
inline ~ParameterProvider() = default; | ||
|
||
inline auto GetParameter(SilKit_Participant* participant, Parameter parameter) -> const std::string&; | ||
|
||
private: | ||
std::unordered_map<Parameter, std::string> _parameterValues; | ||
}; | ||
|
||
} // namespace Impl | ||
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_CLOSE | ||
} // namespace SilKit | ||
|
||
|
||
// ================================================================================ | ||
// Inline Implementations | ||
// ================================================================================ | ||
|
||
#include "silkit/detail/impl/ThrowOnError.hpp" | ||
|
||
namespace SilKit { | ||
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_BEGIN | ||
namespace Impl { | ||
|
||
ParameterProvider::ParameterProvider() | ||
{ | ||
} | ||
|
||
auto ParameterProvider::GetParameter(SilKit_Participant* participant, Parameter parameter) -> const std::string& | ||
{ | ||
const char* cParameterValue; | ||
SilKit_Parameter cParameter = static_cast<SilKit_Parameter>(parameter); | ||
const auto returnCode = SilKit_Participant_GetParameter(&cParameterValue, cParameter, participant); | ||
_parameterValues[parameter] = std::string(cParameterValue); | ||
ThrowOnError(returnCode); | ||
return _parameterValues[parameter]; | ||
} | ||
|
||
} // namespace Impl | ||
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_CLOSE | ||
} // namespace SilKit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -35,6 +35,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | |||||
#include "silkit/services/rpc/RpcSpec.hpp" | ||||||
#include "silkit/services/rpc/RpcDatatypes.hpp" | ||||||
|
||||||
#include "silkit/participant/parameters.hpp" | ||||||
|
||||||
namespace SilKit { | ||||||
|
||||||
/*! \brief Communication interface to be used by SIL Kit participants | ||||||
|
@@ -99,6 +101,9 @@ class IParticipant | |||||
|
||||||
//! \brief Return the ILogger at this SIL Kit participant. | ||||||
virtual auto GetLogger() -> Services::Logging::ILogger* = 0; | ||||||
|
||||||
//! \brief Get a parameter set by an API call and/or the participant configuration. | ||||||
virtual auto GetParameter(SilKit::Parameter parameter) -> const std::string& = 0; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would produce a new
Suggested change
|
||||||
}; | ||||||
|
||||||
} // namespace SilKit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#pragma once | ||
|
||
#include "silkit/capi/Parameters.h" | ||
|
||
namespace SilKit { | ||
|
||
// note: never reuse old numbers! | ||
//! \brief Available parameters to query | ||
enum class Parameter : SilKit_Parameter | ||
{ | ||
//! An undefined parameter | ||
Undefined = SilKit_Parameter_Undefined, | ||
//! The name of the participant | ||
ParticipantName = SilKit_Parameter_ParticipantName, | ||
//! The registry URI | ||
RegistryUri = SilKit_Parameter_ReistryUri, | ||
}; | ||
|
||
} // namespace SilKit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we return a single
const char *
(which is owned by the SIL Kit participant and not to be deallocated by the user) then we have to ensure that the string this is pointing to never changes / gets reallocated. This will work for the current set of parameters (participant name and registry uri won't change), but who knows if we want to provide parameters that could change in the future.I would propose an API that copies the strings to user-provided buffer in a similar manner to
snprintf
. It must be allowed to specify the data-pointer asnullptr
which will only fill in the size (which can then be used to create an appropriately sized buffer).Same as with the C++ API, I think copying is fine here - these parameters shouldn't be performance critical or queried in hot-loops.