Skip to content
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

add option to wait for network on startup #11335

Merged
merged 1 commit into from Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/resource.language.en_gb/resources/strings.po
Expand Up @@ -20403,5 +20403,5 @@ msgstr ""
#. Description of setting with label #39011 "Maximum wait time for network"
#: system/settings/settings.xml
msgctxt "#39012"
msgid "Set the maximum time to wait for the network to come up after waking up. When time has passed before network is up startup will continue."
msgid "Set the maximum time to wait for the network to come up after starting or waking up. When time has passed before network is up startup will continue."
msgstr ""
2 changes: 1 addition & 1 deletion system/settings/settings.xml
Expand Up @@ -2890,7 +2890,7 @@
</constraints>
<control type="list" format="string" />
</setting>
<setting id="powermanagement.waitnetafterwakeup" type="integer" label="39011" help="39012">
<setting id="powermanagement.waitfornetwork" type="integer" label="39011" help="39012">
<level>2</level>
<default>0</default>
<constraints>
Expand Down
2 changes: 2 additions & 0 deletions xbmc/Application.cpp
Expand Up @@ -1106,6 +1106,8 @@ bool CApplication::Initialize()
g_peripherals.Initialise();
#endif

getNetwork().WaitForNet();

// Load curl so curl_global_init gets called before any service threads
// are started. Unloading will have no effect as curl is never fully unloaded.
// To quote man curl_global_init:
Expand Down
34 changes: 34 additions & 0 deletions xbmc/network/Network.cpp
Expand Up @@ -23,14 +23,19 @@
#include <arpa/inet.h>

#include "Network.h"
#include "ServiceBroker.h"
#include "messaging/ApplicationMessenger.h"
#include "network/NetworkServices.h"
#include "settings/Settings.h"
#include "utils/log.h"
#ifdef TARGET_WINDOWS
#include "utils/SystemInfo.h"
#include "platform/win32/WIN32Util.h"
#include "utils/CharsetConverter.h"
#endif
#ifdef TARGET_POSIX
#include "linux/XTimeUtils.h"
#endif
#include "utils/StringUtils.h"

using namespace KODI::MESSAGING;
Expand Down Expand Up @@ -549,3 +554,32 @@ int CreateTCPServerSocket(const int port, const bool bindLocal, const int backlo
return sock;
}

void CNetwork::WaitForNet()
{
const int timeout = CServiceBroker::GetSettings().GetInt(CSettings::SETTING_POWERMANAGEMENT_WAITFORNETWORK);
if (timeout <= 0)
return; // wait for network is disabled

// check if we have at least one network interface to wait for
if (!IsAvailable())
return;

CLog::Log(LOGNOTICE, "%s: Waiting for a network interface to come up (Timeout: %d s)", __FUNCTION__, timeout);

const static int intervalMs = 200;
const int numMaxTries = (timeout * 1000) / intervalMs;

for(int i=0; i < numMaxTries; ++i)
{
if (i > 0)
Sleep(intervalMs);

if (IsConnected())
{
CLog::Log(LOGNOTICE, "%s: A network interface is up after waiting %d ms", __FUNCTION__, i * intervalMs);
return;
}
}

CLog::Log(LOGNOTICE, "%s: No network interface did come up within %d s... Giving up...", __FUNCTION__, timeout);
}
3 changes: 3 additions & 0 deletions xbmc/network/Network.h
Expand Up @@ -149,6 +149,9 @@ class CNetwork

// Return true if given name or ip address corresponds to localhost
bool IsLocalHost(const std::string& hostname);

// Waits for the first network interface to become available
void WaitForNet();
};

#ifdef HAS_LINUX_NETWORK
Expand Down
38 changes: 1 addition & 37 deletions xbmc/powermanagement/PowerManager.cpp
Expand Up @@ -58,10 +58,6 @@
extern HWND g_hWnd;
#endif

#if defined(TARGET_POSIX)
#include "linux/XTimeUtils.h"
#endif

using namespace ANNOUNCEMENT;

CPowerManager g_powerManager;
Expand Down Expand Up @@ -275,11 +271,7 @@ void CPowerManager::OnWake()
{
CLog::Log(LOGNOTICE, "%s: Running resume jobs", __FUNCTION__);

{
const int waitNetTimeout = CServiceBroker::GetSettings().GetInt(CSettings::SETTING_POWERMANAGEMENT_WAITNETAFTERWAKEUP);
if (waitNetTimeout > 0)
WaitForNet(waitNetTimeout);
}
g_application.getNetwork().WaitForNet();

// reset out timers
g_application.ResetShutdownTimers();
Expand Down Expand Up @@ -338,31 +330,3 @@ void CPowerManager::SettingOptionsShutdownStatesFiller(const CSetting *setting,
#endif
}
}

void CPowerManager::WaitForNet(int timeout) const
{
CNetwork& net = g_application.getNetwork();

// check if we have at least one network interface to wait for
if (!net.IsAvailable())
return;

CLog::Log(LOGNOTICE, "%s: Waiting for a network interface to come up (Timeout: %d s)", __FUNCTION__, timeout);

const static int intervalMs = 200;
const int numMaxTries = (timeout * 1000) / intervalMs;

for(int i=0; i < numMaxTries; ++i)
{
if (i > 0)
Sleep(intervalMs);

if (net.IsConnected())
{
CLog::Log(LOGNOTICE, "%s: A network interface is up after waiting %d ms", __FUNCTION__, i * intervalMs);
return;
}
}

CLog::Log(LOGNOTICE, "%s: No network interface did come up within %d s... Giving up...", __FUNCTION__, timeout);
}
2 changes: 0 additions & 2 deletions xbmc/powermanagement/PowerManager.h
Expand Up @@ -93,8 +93,6 @@ class CPowerManager : public IPowerEventsCallback

void OnLowBattery();

void WaitForNet(int timeout) const;

IPowerSyscall *m_instance;
};

Expand Down
2 changes: 1 addition & 1 deletion xbmc/settings/Settings.cpp
Expand Up @@ -392,7 +392,7 @@ const std::string CSettings::SETTING_POWERMANAGEMENT_DISPLAYSOFF = "powermanagem
const std::string CSettings::SETTING_POWERMANAGEMENT_SHUTDOWNTIME = "powermanagement.shutdowntime";
const std::string CSettings::SETTING_POWERMANAGEMENT_SHUTDOWNSTATE = "powermanagement.shutdownstate";
const std::string CSettings::SETTING_POWERMANAGEMENT_WAKEONACCESS = "powermanagement.wakeonaccess";
const std::string CSettings::SETTING_POWERMANAGEMENT_WAITNETAFTERWAKEUP = "powermanagement.waitnetafterwakeup";
const std::string CSettings::SETTING_POWERMANAGEMENT_WAITFORNETWORK = "powermanagement.waitfornetwork";
const std::string CSettings::SETTING_DEBUG_SHOWLOGINFO = "debug.showloginfo";
const std::string CSettings::SETTING_DEBUG_EXTRALOGGING = "debug.extralogging";
const std::string CSettings::SETTING_DEBUG_SETEXTRALOGLEVEL = "debug.setextraloglevel";
Expand Down
2 changes: 1 addition & 1 deletion xbmc/settings/Settings.h
Expand Up @@ -348,7 +348,7 @@ class CSettings : public CSettingCreator, public CSettingControlCreator
static const std::string SETTING_POWERMANAGEMENT_SHUTDOWNTIME;
static const std::string SETTING_POWERMANAGEMENT_SHUTDOWNSTATE;
static const std::string SETTING_POWERMANAGEMENT_WAKEONACCESS;
static const std::string SETTING_POWERMANAGEMENT_WAITNETAFTERWAKEUP;
static const std::string SETTING_POWERMANAGEMENT_WAITFORNETWORK;
static const std::string SETTING_DEBUG_SHOWLOGINFO;
static const std::string SETTING_DEBUG_EXTRALOGGING;
static const std::string SETTING_DEBUG_SETEXTRALOGLEVEL;
Expand Down