[traffic] API to reconfigure a running HttpTrafficSource

Signed-off-by: mvglasow <michael -at- vonglasow.com>
This commit is contained in:
mvglasow
2025-07-23 21:34:11 +03:00
parent be3792b93a
commit 3b1fca01e3
6 changed files with 80 additions and 0 deletions

View File

@@ -2589,6 +2589,11 @@ void Framework::SaveTrafficEnabled(bool trafficEnabled)
settings::Set(kTrafficEnabledKey, trafficEnabled);
}
void Framework::SetTrafficHttpEnabled(bool enabled)
{
m_trafficManager.SetHttpTraffSource(enabled, LoadTrafficHttpUrl());
}
bool Framework::LoadTrafficHttpEnabled()
{
bool enabled;
@@ -2602,6 +2607,11 @@ void Framework::SaveTrafficHttpEnabled(bool trafficHttpEnabled)
settings::Set(kTrafficHttpEnabledKey, trafficHttpEnabled);
}
void Framework::SetTrafficHttpUrl(std::string url)
{
m_trafficManager.SetHttpTraffSource(LoadTrafficHttpEnabled(), url);
}
std::string Framework::LoadTrafficHttpUrl()
{
std::string url;

View File

@@ -740,9 +740,11 @@ public:
bool LoadTrafficEnabled();
void SaveTrafficEnabled(bool trafficEnabled);
void SetTrafficHttpEnabled(bool enabled);
bool LoadTrafficHttpEnabled();
void SaveTrafficHttpEnabled(bool trafficHttpEnabled);
void SetTrafficHttpUrl(std::string url);
std::string LoadTrafficHttpUrl();
void SaveTrafficHttpUrl(std::string trafficHttpUrl);

View File

@@ -923,6 +923,27 @@ bool TrafficManager::IsEnabled() const
return m_state != TrafficState::Disabled;
}
void TrafficManager::SetHttpTraffSource(bool enabled, std::string url)
{
if (IsTestMode())
return;
{
std::lock_guard<std::mutex> lock(m_trafficSourceMutex);
for (auto it = m_trafficSources.begin(); it != m_trafficSources.end(); )
if (traffxml::HttpTraffSource* httpSource = dynamic_cast<traffxml::HttpTraffSource*>(it->get()))
{
httpSource->Close();
m_trafficSources.erase(it);
}
else
++it;
}
if (enabled)
traffxml::HttpTraffSource::Create(*this, url);
}
bool TrafficManager::IsInvalidState() const
{
return m_state == TrafficState::NetworkError;

View File

@@ -173,6 +173,23 @@ public:
*/
bool IsEnabled() const;
/**
* @brief Sets the enabled state and URL for the `HttpTraffSource`.
*
* If the traffic manager is in test mode, this function is a no-op.
*
* Otherwise this function is expected to be called only if the enabled state and/or URL have
* actually changed. Setting both to the current state will remove the current source and create
* a new one with identical settings.
*
* This function currently assumes that there is never more than one `HttpTraffSource` configured
* at the same time.
*
* @param enabled Whether the HTTP TraFF source is enabled.
* @param url The URL for the TraFF API.
*/
void SetHttpTraffSource(bool enabled, std::string url);
/**
* @brief Starts the traffic manager.
*

View File

@@ -136,6 +136,27 @@ HttpTraffSource::HttpTraffSource(TraffSourceManager & manager, std::string const
, m_url(url)
{}
void HttpTraffSource::Close()
{
std::string data;
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_subscriptionId.empty())
return;
data = "<request operation=\"UNSUBSCRIBE\" subscription_id=\"" + m_subscriptionId + "\"/>";
m_subscriptionId.clear();
}
LOG(LDEBUG, ("Sending request:\n", data));
threads::SimpleThread thread([this, data]() {
TraffResponse response = HttpPost(m_url, data);
return;
});
thread.detach();
}
void HttpTraffSource::Subscribe(std::set<MwmSet::MwmId> & mwms)
{
std::string data = "<request operation=\"SUBSCRIBE\">\n<filter_list>\n"

View File

@@ -441,6 +441,15 @@ public:
*/
static void Create(TraffSourceManager & manager, std::string const & url);
/**
* @brief Prepares the HTTP traffic source for unloading.
*
* If there is still an active subscription, it unsubscribes, but without processing the result
* received from the service. Otherwise, teardown is a no-op.
*/
// TODO move this to the parent class and override it here?
void Close();
/**
* @brief Subscribes to a traffic service.
*