-
Notifications
You must be signed in to change notification settings - Fork 5
MP: rewrite iamclient #161
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
Open
MykolaSuperman
wants to merge
5
commits into
aosedge:feature_unification
Choose a base branch
from
MykolaSuperman:rewrite_iamclient
base: feature_unification
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a1a6171
common: iamclient: cancel RegisterNode stream on reconnect
201565f
mp: iamclient: refactor to use PublicNodesService
61cdd9b
mp: config: rename logprovider to logging
87251b5
build.sh: enable MP build
6c566a1
mp: temporarily disable uncompilable modules
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,190 @@ | ||
| /* | ||
| * Copyright (C) 2025 EPAM Systems, Inc. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <core/common/tools/logger.hpp> | ||
|
|
||
| #include "iamclient.hpp" | ||
|
|
||
| namespace aos::mp::iamclient { | ||
|
|
||
| /*********************************************************************************************************************** | ||
| * Public | ||
| **********************************************************************************************************************/ | ||
|
|
||
| Error IAMClient::Init(const config::IAMConfig& cfg, aos::iamclient::CertProviderItf& certProvider, | ||
| common::iamclient::TLSCredentialsItf& tlsCredentials, bool provisioningMode) | ||
| { | ||
| mCertProvider = &certProvider; | ||
| mCertStorage = cfg.mCertStorage; | ||
|
|
||
| const bool publicServer = mCertStorage.empty(); | ||
|
|
||
| LOG_DBG() << "Init IAM client: publicServer=" << publicServer << ", provisioningMode=" << provisioningMode; | ||
|
|
||
| return PublicNodesService::Init(publicServer ? cfg.mIAMMainPublicServerURL : cfg.mIAMMainProtectedServerURL, | ||
| tlsCredentials, provisioningMode, publicServer, mCertStorage); | ||
| } | ||
|
|
||
| Error IAMClient::Start() | ||
| { | ||
| std::lock_guard lock {mMutex}; | ||
|
|
||
| LOG_DBG() << "Start IAM client"; | ||
|
Collaborator
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. lock guard?
Author
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. Done |
||
|
|
||
| if (!mCertStorage.empty()) { | ||
| if (auto err = mCertProvider->SubscribeListener(String(mCertStorage.c_str()), *this); !err.IsNone()) { | ||
| return AOS_ERROR_WRAP(err); | ||
| } | ||
| } | ||
|
|
||
| mStarted = true; | ||
|
|
||
| mOutgoingMsgThread = std::thread(&IAMClient::ProcessOutgoingMessages, this); | ||
|
|
||
| return PublicNodesService::Start(); | ||
| } | ||
|
|
||
| void IAMClient::Stop() | ||
| { | ||
| LOG_DBG() << "Stop IAM client"; | ||
|
|
||
| { | ||
| std::lock_guard lock {mMutex}; | ||
|
|
||
| if (!mStarted) { | ||
| return; | ||
| } | ||
|
|
||
| mStarted = false; | ||
|
|
||
| mOutgoingMsgChannel.Close(); | ||
| mIncomingMsgChannel.Close(); | ||
| } | ||
|
|
||
| mCV.notify_all(); | ||
|
|
||
| PublicNodesService::Stop(); | ||
|
|
||
| if (mOutgoingMsgThread.joinable()) { | ||
| mOutgoingMsgThread.join(); | ||
| } | ||
|
|
||
| if (!mCertStorage.empty()) { | ||
| mCertProvider->UnsubscribeListener(*this); | ||
| } | ||
| } | ||
|
|
||
| Error IAMClient::SendMessages(std::vector<uint8_t> messages) | ||
| { | ||
| LOG_DBG() << "Send message"; | ||
|
|
||
| return mOutgoingMsgChannel.Send(std::move(messages)); | ||
| } | ||
|
|
||
| RetWithError<std::vector<uint8_t>> IAMClient::ReceiveMessages() | ||
| { | ||
| LOG_DBG() << "Receive message"; | ||
|
|
||
| return mIncomingMsgChannel.Receive(); | ||
| } | ||
|
|
||
| /*********************************************************************************************************************** | ||
| * Protected | ||
| **********************************************************************************************************************/ | ||
|
|
||
| Error IAMClient::ReceiveMessage(const iamanager::v6::IAMIncomingMessages& msg) | ||
| { | ||
| std::vector<uint8_t> message(msg.ByteSizeLong()); | ||
|
|
||
| LOG_DBG() << "Received message" << Log::Field("msg", msg.DebugString()); | ||
|
|
||
| if (!msg.SerializeToArray(message.data(), message.size())) { | ||
| return Error(ErrorEnum::eRuntime, "failed to serialize message"); | ||
| } | ||
|
|
||
| if (auto err = mIncomingMsgChannel.Send(std::move(message)); !err.IsNone()) { | ||
| return AOS_ERROR_WRAP(err); | ||
| } | ||
|
|
||
| return ErrorEnum::eNone; | ||
| } | ||
|
|
||
| void IAMClient::OnConnected() | ||
| { | ||
| LOG_DBG() << "IAM client connected"; | ||
|
|
||
| std::lock_guard lock {mMutex}; | ||
|
|
||
| mConnected = true; | ||
| mCV.notify_all(); | ||
| } | ||
|
|
||
| void IAMClient::OnDisconnected() | ||
| { | ||
| LOG_DBG() << "IAM client disconnected"; | ||
|
|
||
| std::lock_guard lock {mMutex}; | ||
|
|
||
| mConnected = false; | ||
| } | ||
|
|
||
| /*********************************************************************************************************************** | ||
| * Private | ||
| **********************************************************************************************************************/ | ||
|
|
||
| void IAMClient::OnCertChanged([[maybe_unused]] const CertInfo& info) | ||
| { | ||
| LOG_INF() << "Certificate changed, reconnecting"; | ||
|
|
||
| if (auto err = Reconnect(); !err.IsNone()) { | ||
| LOG_ERR() << "Failed to reconnect" << Log::Field(err); | ||
| } | ||
| } | ||
|
|
||
| void IAMClient::ProcessOutgoingMessages() | ||
| { | ||
| LOG_DBG() << "Processing outgoing messages"; | ||
|
|
||
| while (mStarted) { | ||
| auto [msg, err] = mOutgoingMsgChannel.Receive(); | ||
| if (!err.IsNone()) { | ||
| LOG_ERR() << "Failed to receive message" << Log::Field(err); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| { | ||
| std::unique_lock lock {mMutex}; | ||
|
|
||
| LOG_DBG() << "Received message from channel"; | ||
|
|
||
| mCV.wait(lock, [this] { return mConnected || !mStarted; }); | ||
|
|
||
| if (!mStarted) { | ||
| return; | ||
| } | ||
|
|
||
| iamanager::v6::IAMOutgoingMessages outgoingMsg; | ||
| if (!outgoingMsg.ParseFromArray(msg.data(), static_cast<int>(msg.size()))) { | ||
| LOG_ERR() << "Failed to parse outgoing message"; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| LOG_DBG() << "Sending message: msg=" << outgoingMsg.DebugString().c_str(); | ||
|
|
||
| if (auto sendErr = SendMessage(outgoingMsg); !sendErr.IsNone()) { | ||
| LOG_ERR() << "Failed to send message" << Log::Field(sendErr); | ||
|
|
||
| mMessageCache.push(outgoingMsg); | ||
|
|
||
| continue; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace aos::mp::iamclient | ||
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.
probably better leave removed.
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.
Done