Cleaning up serverlist caching.

This commit is contained in:
lotodore
2012-02-04 21:39:24 +00:00
parent b893f98d01
commit 2d19013fae
4 changed files with 29 additions and 26 deletions
+7 -24
View File
@@ -173,12 +173,14 @@ ClientStateStartServerListDownload::~ClientStateStartServerListDownload()
void
ClientStateStartServerListDownload::Enter(boost::shared_ptr<ClientThread> client)
{
const ClientContext &context = client->GetContext();
path tmpServerListPath(GetCacheServerListFileName(context));
if (exists(tmpServerListPath) && (last_write_time(tmpServerListPath) + 86400 < time(NULL))) {
path tmpServerListPath(client->GetCacheServerListFileName());
if (exists(tmpServerListPath)) {
// Download the current server list once a day.
// If the previous file is older than one day, delete it.
remove(tmpServerListPath);
// Also delete the file if it is empty.
if (file_size(tmpServerListPath) == 0 || (last_write_time(tmpServerListPath) + 86400 < time(NULL))) {
remove(tmpServerListPath);
}
}
if (exists(tmpServerListPath)) {
@@ -187,7 +189,7 @@ ClientStateStartServerListDownload::Enter(boost::shared_ptr<ClientThread> client
} else {
// Download the server list.
boost::shared_ptr<DownloadHelper> downloader(new DownloadHelper);
downloader->Init(context.GetServerListUrl(), tmpServerListPath.directory_string());
downloader->Init(client->GetContext().GetServerListUrl(), tmpServerListPath.directory_string());
ClientStateDownloadingServerList::Instance().SetDownloadHelper(downloader);
client->SetState(ClientStateDownloadingServerList::Instance());
}
@@ -199,20 +201,6 @@ ClientStateStartServerListDownload::Exit(boost::shared_ptr<ClientThread> /*clien
// Nothing to do.
}
string
ClientStateStartServerListDownload::GetCacheServerListFileName(const ClientContext &context)
{
path tmpServerListPath(context.GetCacheDir());
string serverListUrl(context.GetServerListUrl());
// Retrieve the file name from the URL.
size_t pos = serverListUrl.find_last_of('/');
if (context.GetCacheDir().empty() || serverListUrl.empty() || pos == string::npos || ++pos >= serverListUrl.length()) {
throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_URL, 0);
}
tmpServerListPath /= serverListUrl.substr(pos);
return tmpServerListPath.directory_string();
}
//-----------------------------------------------------------------------------
ClientStateDownloadingServerList &
@@ -507,11 +495,6 @@ ClientStateStartConnect::HandleConnect(const boost::system::error_code& ec, boos
client));
} else {
if (ec != boost::asio::error::operation_aborted) {
// Delete the cached server list, as it may be outdated.
path tmpServerListPath(ClientStateStartServerListDownload::GetCacheServerListFileName(client->GetContext()));
if (exists(tmpServerListPath)) {
remove(tmpServerListPath);
}
throw ClientException(__FILE__, __LINE__, ERR_SOCK_CONNECT_FAILED, ec.value());
}
}