Skip to content

Commit

Permalink
Introduce ConfigSyncRule helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Feb 13, 2024
1 parent 008fcd1 commit 0c4bd97
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/remote/apilistener-configsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,39 @@ void ApiListener::SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient
Log(LogInformation, "ApiListener")
<< "Finished syncing runtime objects to endpoint '" << endpoint->GetName() << "'.";
}

void ConfigSyncRule::WaitForObject(const Type::Ptr& ptype, const String& objName)
{
std::unique_lock<std::mutex> lock(m_Mutex);
m_CV.wait(lock, [this, &ptype, &objName]{ return !IsObjectBlocked(ptype, objName); });
}

bool ConfigSyncRule::IsObjectBlocked(const Type::Ptr& ptype, const String& objName)
{
auto type = m_Objects.find(ptype.get());
if (type == m_Objects.end()) {
return false;
}

return type->second.find(objName) != type->second.end();
}

void ConfigSyncRule::BlockObject(const Type::Ptr& ptype, const String& objName)
{
std::unique_lock<std::mutex> lock(m_Mutex);
m_Objects[ptype.get()].emplace(objName);
}

void ConfigSyncRule::UnBlockNotify(const Type::Ptr& ptype, const String& objName, bool notifyAll)
{
{
std::unique_lock<std::mutex> lock(m_Mutex);
m_Objects[ptype.get()].erase(objName);
}

if (notifyAll) {
m_CV.notify_all();
} else {
m_CV.notify_one();
}
}
16 changes: 16 additions & 0 deletions lib/remote/apilistener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ enum class ApiCapabilities : uint_fast64_t
IfwApiCheckCommand = 1u << 1u,
};

class ConfigSyncRule
{
public:
void WaitForObject(const Type::Ptr& ptype, const String& objName);

void BlockObject(const Type::Ptr& ptype, const String& objName);
void UnBlockNotify(const Type::Ptr& ptype, const String& objName, bool notifyAll = false);

private:
bool IsObjectBlocked(const Type::Ptr& ptype, const String& objName);

std::mutex m_Mutex;
std::condition_variable m_CV;
std::unordered_map<Type*, std::set<String>> m_Objects;
};

/**
* @ingroup remote
*/
Expand Down

0 comments on commit 0c4bd97

Please sign in to comment.