Skip to content

Commit

Permalink
connectivity retry logic added
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuhammedrafi committed Apr 4, 2024
1 parent f2b6438 commit 2008bb3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
79 changes: 52 additions & 27 deletions Network/NetworkConnectivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,11 @@ namespace WPEFramework {

timeout.store(timeoutInSeconds >= MONITOR_TIMEOUT_INTERVAL_MIN ? timeoutInSeconds:defaultTimeoutInSec);

if (isMonitorThreadRunning())
if (isMonitorThreadRunning() && stopFlag == false)
{
isContinuesMonitoringNeeded = true;
resetTimeout = true;
cv_.notify_all();
LOGINFO("Connectivity monitor Restarted with %d", timeout.load());
}
else
{
Expand All @@ -475,7 +475,6 @@ namespace WPEFramework {
isContinuesMonitoringNeeded = true;
stopFlag = false;
thread_ = std::thread(&ConnectivityMonitor::connectivityMonitorFunction, this);
threadRunning = true;
LOGINFO("Connectivity monitor started with %d", timeout.load());
}

Expand All @@ -490,7 +489,7 @@ namespace WPEFramework {
return false;
}

if (isMonitorThreadRunning())
if (isMonitorThreadRunning() && stopFlag == false)
{
LOGINFO("Connectivity Monitor Thread is active so notify");
cv_.notify_all();
Expand All @@ -508,7 +507,6 @@ namespace WPEFramework {
stopFlag = false;
timeout.store(timeoutInSeconds >= MONITOR_TIMEOUT_INTERVAL_MIN ? timeoutInSeconds:defaultTimeoutInSec);
thread_ = std::thread(&ConnectivityMonitor::connectivityMonitorFunction, this);
threadRunning = true;
LOGINFO("Initial Connectivity Monitoring started with %d", timeout.load());
}

Expand All @@ -517,7 +515,7 @@ namespace WPEFramework {

bool ConnectivityMonitor::isMonitorThreadRunning()
{
return threadRunning.load();
return thread_.joinable();
}

bool ConnectivityMonitor::stopInitialConnectivityMonitoring()
Expand All @@ -527,16 +525,14 @@ namespace WPEFramework {
LOGWARN("Continuous Connectivity Monitor is running");
return true;
}
else
{
stopFlag = true;
cv_.notify_all();

if (thread_.joinable())
thread_.join();
threadRunning = false;
LOGINFO("Stoping Initial Connectivity Monitor");
}
stopFlag = true;
cv_.notify_all();

if (thread_.joinable())
thread_.join();

LOGINFO("Initial Connectivity Monitor stopped");

return true;
}
Expand All @@ -550,7 +546,6 @@ namespace WPEFramework {
thread_.join();

isContinuesMonitoringNeeded = false;
threadRunning = false;
LOGINFO("Continuous Connectivity monitor stopped");
return true;
}
Expand All @@ -559,15 +554,17 @@ namespace WPEFramework {
{
if (isMonitorThreadRunning())
{
/* Reset the global value to UNKNOWN state*/
resetConnectivityCache();
/* Reset the global value to UNKNOWN state so the cache is reset */
g_internetState = nsm_internetState::UNKNOWN;
cv_.notify_all();
}
}

void ConnectivityMonitor::connectivityMonitorFunction()
{
nsm_internetState InternetConnectionState = nsm_internetState::UNKNOWN;
int notifyWaitCount = DEFAULT_MONITOR_RETRY_COUNT;
int tempTimeout = defaultTimeoutInSec;
do
{
if(g_internetState.load() == nsm_internetState::FULLY_CONNECTED)
Expand All @@ -579,34 +576,62 @@ namespace WPEFramework {

if(g_internetState.load() != InternetConnectionState)
{
g_internetState.store(InternetConnectionState);
Network::notifyInternetStatusChange(g_internetState.load());
LOGINFO("notification count %d ...", notifyWaitCount);
if(InternetConnectionState == nsm_internetState::NO_INTERNET && notifyWaitCount > 0)
{
/* Decrease the notification count to create a delay in posting the 'no internet' state. */
notifyWaitCount--;
/* change the timeout value to 5 sec so that next check will happens with in 5 sec */
tempTimeout = 5;
LOGINFO("notification count change to %d timeout %d ...", notifyWaitCount, tempTimeout);
}
else
{
g_internetState.store(InternetConnectionState);
Network::notifyInternetStatusChange(g_internetState.load());
notifyWaitCount = DEFAULT_MONITOR_RETRY_COUNT;
/* change the timeout value to actual requested value */
tempTimeout = timeout.load();
LOGINFO("notification count change to default %d ...", notifyWaitCount);
}
}

if(!isContinuesMonitoringNeeded && (g_internetState.load() == FULLY_CONNECTED))
{
stopFlag = true;
LOGINFO("Initial Connectivity Monitoring done Exiting ... FULLY_CONNECTED");
threadRunning = false;
break;
}

if(stopFlag)
{
threadRunning = false;
break;
}
//wait for next timout or conditon signal
// wait for next timout or conditon signal
std::unique_lock<std::mutex> lock(mutex_);
if (cv_.wait_for(lock, std::chrono::seconds(timeout.load())) != std::cv_status::timeout)
if (cv_.wait_for(lock, std::chrono::seconds(tempTimeout)) != std::cv_status::timeout)
{
if(!stopFlag)
{
LOGINFO("Connectivity monitor received a trigger");
/*
* We don't need to notify immediately when restarting the thread.
* Immediate notification should occur only when any connection change happens.
*
*/

if(resetTimeout)
{
LOGINFO("Connectivity monitor Restarted with %d", timeout.load());
resetTimeout = false;
}
else
{
notifyWaitCount = -1;
LOGINFO("Connectivity monitor received trigger");
}
}
}

} while (!stopFlag);

g_internetState = nsm_internetState::UNKNOWN;
LOGWARN("Connectivity monitor exiting");
}
Expand Down
8 changes: 4 additions & 4 deletions Network/NetworkConnectivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#define CAPTIVEPORTAL_MAX_LEN 512
#define DEFAULT_MONITOR_TIMEOUT 60 // in seconds
#define MONITOR_TIMEOUT_INTERVAL_MIN 5
#define TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS 10000
#define TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS 5000
#define DEFAULT_MONITOR_RETRY_COUNT 2

enum nsm_ipversion {
NSM_IPRESOLVE_WHATEVER = 0, /* default, resolves addresses to all IP*/
Expand Down Expand Up @@ -112,9 +113,8 @@ namespace WPEFramework {
bool isConnectivityMonitorEndpointSet();
bool isMonitorThreadRunning();
void signalConnectivityMonitor();
void resetConnectivityCache() { g_internetState = nsm_internetState::UNKNOWN;}

ConnectivityMonitor() : stopFlag(false), threadRunning(false), isContinuesMonitoringNeeded(false)
ConnectivityMonitor() : stopFlag(false), resetTimeout(false), isContinuesMonitoringNeeded(false)
{
setConnectivityMonitorEndpoints(getConnectivityDefaultEndpoints());
}
Expand All @@ -134,7 +134,7 @@ namespace WPEFramework {

std::thread thread_;
std::atomic<bool> stopFlag;
std::atomic<bool> threadRunning;
std::atomic<bool> resetTimeout;
std::atomic<bool> isContinuesMonitoringNeeded;
std::condition_variable cv_;
std::atomic<int> timeout;
Expand Down

0 comments on commit 2008bb3

Please sign in to comment.