Skip to content

Commit

Permalink
test commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuhammedrafi committed Apr 2, 2024
1 parent cbeec66 commit 5bca227
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 138 deletions.
18 changes: 3 additions & 15 deletions Network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ typedef struct _IARM_BUS_NetSrvMgr_Iface_EventData_t {
bool result = false;
if (parameters.HasLabel("interval"))
{
result = connectivityMonitor.doContinuousConnectivityMonitoring(parameters["interval"].Number());
result = connectivityMonitor.startConnectivityMonitor(parameters["interval"].Number());
}
else
{
Expand All @@ -1299,7 +1299,7 @@ typedef struct _IARM_BUS_NetSrvMgr_Iface_EventData_t {

uint32_t Network::stopConnectivityMonitoring(const JsonObject& parameters, JsonObject& response)
{
bool result = connectivityMonitor.stopContinuousConnectivityMonitoring();
bool result = connectivityMonitor.stopConnectivityMonitor();
returnResponse(result);
}

Expand Down Expand Up @@ -1461,19 +1461,7 @@ typedef struct _IARM_BUS_NetSrvMgr_Iface_EventData_t {
m_defInterfaceCache = "";

sendNotify("onConnectionStatusChanged", params);
if(connected)
{
connectivityMonitor.doInitialConnectivityMonitoring(30);
}
else
{
if (!connectivityMonitor.isMonitorThreadRunning())
{
/*run the thread again to notify no_internet state*/
connectivityMonitor.doInitialConnectivityMonitoring(30);
}
connectivityMonitor.stopInitialConnectivityMonitoring();
}
connectivityMonitor.signalConnectivityMonitor();
}

void Network::onInternetStatusChange(nsm_internetState InternetConnectionState)
Expand Down
171 changes: 57 additions & 114 deletions Network/NetworkConnectivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ namespace WPEFramework {

static bool curlVerboseEnabled() {
std::ifstream fileStream("/tmp/network.plugin.debug");
return fileStream.is_open();
//return fileStream.is_open();
return true;
}

static long current_time ()
Expand All @@ -253,7 +254,7 @@ namespace WPEFramework {
return size * nmemb;
}

nsm_internetState Connectivity::testConnectivity(const std::vector<std::string>& endpoints, long timeout_ms, nsm_ipversion ipversion, bool connectOnly)
nsm_internetState Connectivity::testConnectivity(const std::vector<std::string>& endpoints, long timeout_ms, nsm_ipversion ipversion, bool connectOnly)
{
long deadline = current_time() + timeout_ms, time_now = 0, time_earlier = 0;
if(endpoints.size() < 1) {
Expand All @@ -273,6 +274,7 @@ namespace WPEFramework {
std::vector<int> http_responses;
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, "Cache-Control: no-cache, no-store");
chunk = curl_slist_append(chunk, "Connection: close");
for (const auto& endpoint : endpoints)
{
CURL *curl_easy_handle = curl_easy_init();
Expand Down Expand Up @@ -325,21 +327,28 @@ namespace WPEFramework {
continue;
if (CURLE_OK == msg->data.result) {
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &endpoint);
if(connectOnly)
response_code = HttpStatus_204_No_Content;
else if (curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &response_code) == CURLE_OK) {
if(connectOnly) {
LOGINFO("curl check type CURLOPT_CONNECT_ONLY");
response_code = HttpStatus_204_No_Content;
}
else if (curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &response_code) == CURLE_OK) {
if(curlVerboseEnabled())
LOGINFO("endpoint = <%s> http response code <%d>", endpoint, static_cast<int>(response_code));
LOGINFO("endpoint = <%s> response code <%d>",endpoint, static_cast<int>(response_code));
if (HttpStatus_302_Found == response_code) {
if ( (curl_easy_getinfo(msg->easy_handle, CURLINFO_REDIRECT_URL, &url) == CURLE_OK) && url != nullptr) {
LOGWARN("captive portal found !!!");
//LOGWARN("captive portal found !!!");
setCaptivePortal(url);
}
}
}
}
else
LOGERR("endpoint = <%s> curl error = %d (%s)", endpoint, msg->data.result, curl_easy_strerror(msg->data.result));
else {
LOGERR("endpoint = <%s> error = %d (%s)", endpoint, msg->data.result, curl_easy_strerror(msg->data.result));
// if (msg->data.result == CURLE_OPERATION_TIMEDOUT)
// {
// response_code = HttpStatus_204_No_Content;
// }
}
http_responses.push_back(response_code);
}
time_earlier = time_now;
Expand Down Expand Up @@ -368,10 +377,12 @@ namespace WPEFramework {
}
#endif
}

if(curlVerboseEnabled()) {
LOGINFO("endpoints count = %d response count %d, handles = %d, deadline = %ld, time_now = %ld, time_earlier = %ld",
static_cast<int>(endpoints.size()), static_cast<int>(http_responses.size()), handles, deadline, time_now, time_earlier);
}

for (const auto& curl_easy_handle : curl_easy_handles)
{
curl_easy_getinfo(curl_easy_handle, CURLINFO_PRIVATE, &endpoint);
Expand Down Expand Up @@ -434,18 +445,16 @@ namespace WPEFramework {
break;
default:
InternetConnectionState = NO_INTERNET;
if(http_response_code == -1)
LOGERR("Internet State: NO_INTERNET curl error");
else
LOGWARN("Internet State: NO_INTERNET Received http response code: <%d> %.1f%%", static_cast<int>(http_response_code), percentage * 100);
break;
LOGINFO("Internet State: NO_INTERNET Response code: <%d> %.1f%%", static_cast<int>(http_response_code), (percentage*100));
}
}

return InternetConnectionState;
}

bool ConnectivityMonitor::doContinuousConnectivityMonitoring(int timeoutInSeconds)
bool ConnectivityMonitor::startConnectivityMonitor(int timeoutInSeconds)
{

if(!isConnectivityMonitorEndpointSet())
{
LOGINFO("Connectivity monitor endpoints are not set !");
Expand All @@ -454,63 +463,18 @@ namespace WPEFramework {

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

if (isMonitorThreadRunning())
if (!isMonitorThreadRunning())
{
isContinuesMonitoringNeeded = true;
cv_.notify_all();
LOGINFO("Connectivity monitor Restarted with %d", timeout.load());
}
else
{
if (thread_.joinable())
{
LOGWARN("Connectivity Monitor joinable Thread is active");
stopFlag = true;
cv_.notify_all();
thread_.join();
}

isContinuesMonitoringNeeded = true;
stopFlag = false;
stopFlag.store(false);
thread_ = std::thread(&ConnectivityMonitor::connectivityMonitorFunction, this);
threadRunning = true;
LOGINFO("Connectivity monitor started with %d", timeout.load());
}

return true;
}

bool ConnectivityMonitor::doInitialConnectivityMonitoring(int timeoutInSeconds)
{
if(!isConnectivityMonitorEndpointSet())
{
LOGINFO("Connectivity monitor endpoints are not set !");
return false;
}

if (isMonitorThreadRunning())
{
LOGINFO("Connectivity Monitor Thread is active so notify");
resetConnectivityCache();
cv_.notify_all();
}
else
{
if (thread_.joinable())
{
LOGWARN("Connectivity Monitor joinable Thread is active");
stopFlag = true;
cv_.notify_all();
thread_.join();
}

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());
cv_.notify_all();
LOGINFO("Connectivity monitor Restarted with %d", timeout.load());
}

return true;
}

Expand All @@ -519,39 +483,22 @@ namespace WPEFramework {
return threadRunning.load();
}

bool ConnectivityMonitor::stopInitialConnectivityMonitoring()
bool ConnectivityMonitor::stopConnectivityMonitor()
{
if(isContinuesMonitoringNeeded)
{
LOGWARN("Continuous Connectivity Monitor is running");
return true;
}
else
{
stopFlag = true;
if (isMonitorThreadRunning()) {
stopFlag.store(true);
cv_.notify_all();

if (thread_.joinable())
if (thread_.joinable()) {
thread_.join();
threadRunning = false;
LOGINFO("Stoping Initial Connectivity Monitor");
threadRunning = false;
LOGINFO("Connectivity monitor stopped");
g_internetState.store(nsm_internetState::UNKNOWN);
}
return true;
}

return true;
}

bool ConnectivityMonitor::stopContinuousConnectivityMonitoring()
{
cv_.notify_all();
stopFlag = true;

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

isContinuesMonitoringNeeded = false;
threadRunning = false;
LOGINFO("Continuous Connectivity monitor stopped");
return true;
else
LOGWARN("Connectivity monitor not running");
return false;
}

void ConnectivityMonitor::signalConnectivityMonitor()
Expand All @@ -567,47 +514,46 @@ namespace WPEFramework {
void ConnectivityMonitor::connectivityMonitorFunction()
{
nsm_internetState InternetConnectionState = nsm_internetState::UNKNOWN;
int notifyWaitCount = 2;
do
int notifyWaitCount = DEFAULT_MONITOR_RETRY_COUNT;
int tempTimeout = defaultTimeoutInSec;
while (!stopFlag)
{
if(g_internetState.load() == nsm_internetState::FULLY_CONNECTED)
/* If the previous check was fully connected, then perform only the curl check for connection. */
/*if previous check was fully connected then do connect only curl check*/
InternetConnectionState = testConnectivity(getConnectivityMonitorEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, NSM_IPRESOLVE_WHATEVER, true);
else
/* If not fully connected, use a curl GET request. */
InternetConnectionState = testConnectivity(getConnectivityMonitorEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, NSM_IPRESOLVE_WHATEVER, false);

if(g_internetState.load() != InternetConnectionState)
{
LOGINFO("notification count %d ...", notifyWaitCount);
if(InternetConnectionState == NO_INTERNET && notifyWaitCount > 0)
{
/* Decrease the notification count to create a delay in posting the 'no internet' state. */
notifyWaitCount--;
LOGINFO("notification count change to %d ...", 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 = 2;
LOGINFO("notification count change to %d ...", notifyWaitCount);
notifyWaitCount = DEFAULT_MONITOR_RETRY_COUNT;
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");
break;
}

if(stopFlag)
{
break;
}
//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)
{
LOGINFO("Connectivity monitor thread timeout %d ", tempTimeout);
}
else
{
if(!stopFlag)
{
Expand All @@ -616,11 +562,8 @@ namespace WPEFramework {
notifyWaitCount = -1;
}
}
} while (!stopFlag);

}
LOGWARN("Connectivity monitor exiting");
g_internetState = nsm_internetState::UNKNOWN;
threadRunning = false;
}

} // namespace Plugin
Expand Down
20 changes: 11 additions & 9 deletions Network/NetworkConnectivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define DEFAULT_MONITOR_TIMEOUT 60 // in seconds
#define MONITOR_TIMEOUT_INTERVAL_MIN 5
#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 @@ -101,29 +102,31 @@ namespace WPEFramework {

class ConnectivityMonitor : public Connectivity {
public:
static ConnectivityMonitor& getInstance() {
static ConnectivityMonitor instance;
return instance;
}

bool isConnectedToInternet(nsm_ipversion ipversion);
nsm_internetState getInternetConnectionState(nsm_ipversion ipversion);
std::string getCaptivePortalURI();
void setConnectivityMonitorEndpoints(const std::vector<std::string> &endpoints);
bool doContinuousConnectivityMonitoring(int timeoutInSeconds);
bool doInitialConnectivityMonitoring(int timeoutInSeconds);
bool stopContinuousConnectivityMonitoring();
bool stopInitialConnectivityMonitoring();
bool startConnectivityMonitor(int timeoutInSeconds);
bool stopConnectivityMonitor();
bool isConnectivityMonitorEndpointSet();
bool isMonitorThreadRunning();
void signalConnectivityMonitor();
void resetConnectivityCache() { g_internetState = nsm_internetState::UNKNOWN;}

ConnectivityMonitor() : stopFlag(false), threadRunning(false), isContinuesMonitoringNeeded(false)
private:
ConnectivityMonitor() : stopFlag(false), threadRunning(false)
{
setConnectivityMonitorEndpoints(getConnectivityDefaultEndpoints());
}

~ConnectivityMonitor() {
LOGINFO("~ConnectivityMonitor");
stopContinuousConnectivityMonitoring();
stopConnectivityMonitor();
}
private:

std::vector<std::string> getConnectivityMonitorEndpoints();
ConnectivityMonitor(const ConnectivityMonitor&) = delete;
Expand All @@ -135,7 +138,6 @@ namespace WPEFramework {
std::thread thread_;
std::atomic<bool> stopFlag;
std::atomic<bool> threadRunning;
std::atomic<bool> isContinuesMonitoringNeeded;
std::condition_variable cv_;
std::atomic<int> timeout;
std::vector<std::string> monitorEndpoints;
Expand Down

0 comments on commit 5bca227

Please sign in to comment.