2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-12 09:18:35 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "TileCache.hpp"
|
2015-03-12 09:18:35 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2015-05-28 04:53:14 -05:00
|
|
|
#include <cassert>
|
2015-05-29 00:39:21 -05:00
|
|
|
#include <climits>
|
2015-05-28 10:39:05 -05:00
|
|
|
#include <cstdio>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2016-04-04 19:59:46 -05:00
|
|
|
#include <mutex>
|
2015-08-04 13:37:05 -05:00
|
|
|
#include <sstream>
|
2015-05-05 06:57:51 -05:00
|
|
|
#include <string>
|
2016-04-15 10:24:00 -05:00
|
|
|
#include <vector>
|
2015-03-12 09:18:35 -05:00
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
#include <Poco/DigestEngine.h>
|
2015-05-28 10:39:05 -05:00
|
|
|
#include <Poco/DirectoryIterator.h>
|
2015-05-29 04:23:57 -05:00
|
|
|
#include <Poco/Exception.h>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <Poco/File.h>
|
|
|
|
#include <Poco/Path.h>
|
2015-05-28 04:53:14 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
2015-05-29 00:49:49 -05:00
|
|
|
#include <Poco/Timestamp.h>
|
2015-05-29 04:23:57 -05:00
|
|
|
#include <Poco/URI.h>
|
2015-03-12 09:18:35 -05:00
|
|
|
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "ClientSession.hpp"
|
2016-05-15 09:19:49 -05:00
|
|
|
#include "Common.hpp"
|
2015-07-21 06:53:53 -05:00
|
|
|
#include "LOOLProtocol.hpp"
|
2016-05-01 09:24:31 -05:00
|
|
|
#include "Unit.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Util.hpp"
|
2015-03-12 09:18:35 -05:00
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
using Poco::DirectoryIterator;
|
|
|
|
using Poco::File;
|
|
|
|
using Poco::StringTokenizer;
|
|
|
|
using Poco::Timestamp;
|
|
|
|
|
2015-07-21 06:53:53 -05:00
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
TileCache::TileCache(const std::string& docURL,
|
2016-04-01 10:56:08 -05:00
|
|
|
const Timestamp& modifiedTime,
|
2016-04-22 06:00:11 -05:00
|
|
|
const std::string& cacheDir) :
|
2015-06-24 15:05:49 -05:00
|
|
|
_docURL(docURL),
|
2016-04-22 06:00:11 -05:00
|
|
|
_cacheDir(cacheDir)
|
2015-03-12 18:34:42 -05:00
|
|
|
{
|
2016-04-25 21:44:58 -05:00
|
|
|
Log::info() << "TileCache ctor for uri [" << _docURL
|
|
|
|
<< "] modifiedTime=" << (modifiedTime.raw()/1000000)
|
|
|
|
<< " getLastModified()=" << (getLastModified().raw()/1000000) << Log::end;
|
2016-04-22 06:00:11 -05:00
|
|
|
File directory(_cacheDir);
|
|
|
|
if (directory.exists() &&
|
|
|
|
(getLastModified() < modifiedTime ||
|
|
|
|
getTextFile("unsaved.txt") != ""))
|
2016-03-26 09:42:15 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
// Document changed externally or modifications were not saved after all. Cache not useful.
|
|
|
|
Util::removeFile(_cacheDir, true);
|
|
|
|
Log::info("Completely cleared tile cache: " + _cacheDir);
|
2016-03-26 09:42:15 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
File(_cacheDir).createDirectories();
|
2016-03-26 09:42:15 -05:00
|
|
|
|
|
|
|
saveLastModified(modifiedTime);
|
2015-03-12 09:18:35 -05:00
|
|
|
}
|
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
TileCache::~TileCache()
|
|
|
|
{
|
2016-03-29 19:56:25 -05:00
|
|
|
Log::info("~TileCache dtor for uri [" + _docURL + "].");
|
2016-04-15 10:24:00 -05:00
|
|
|
}
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Tracks the rendering of a given tile
|
|
|
|
/// to avoid duplication and help clock
|
|
|
|
/// rendering latency.
|
2016-04-25 04:04:25 -05:00
|
|
|
struct TileCache::TileBeingRendered
|
2016-04-15 10:24:00 -05:00
|
|
|
{
|
2016-05-16 19:49:36 -05:00
|
|
|
std::vector<std::weak_ptr<ClientSession>> _subscribers;
|
2016-05-22 14:20:24 -05:00
|
|
|
TileBeingRendered(const std::string& cachedName, const int version)
|
2016-05-22 13:31:18 -05:00
|
|
|
: _startTime(std::chrono::steady_clock::now()),
|
2016-05-22 14:20:24 -05:00
|
|
|
_cachedName(cachedName),
|
2016-05-22 13:31:18 -05:00
|
|
|
_ver(version)
|
2016-04-25 04:04:25 -05:00
|
|
|
{
|
|
|
|
}
|
2016-05-15 09:19:49 -05:00
|
|
|
|
2016-05-22 14:20:24 -05:00
|
|
|
const std::string& getCacheName() const { return _cachedName; }
|
2016-05-22 13:31:18 -05:00
|
|
|
int getVersion() const { return _ver; }
|
|
|
|
|
2016-05-15 09:19:49 -05:00
|
|
|
std::chrono::steady_clock::time_point getStartTime() const { return _startTime; }
|
2016-05-22 18:17:01 -05:00
|
|
|
double getElapsedTimeMs() const { return std::chrono::duration_cast<std::chrono::milliseconds>
|
|
|
|
(std::chrono::steady_clock::now() - _startTime).count(); }
|
2016-05-15 09:19:49 -05:00
|
|
|
void resetStartTime()
|
|
|
|
{
|
|
|
|
_startTime = std::chrono::steady_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::chrono::steady_clock::time_point _startTime;
|
2016-05-22 14:20:24 -05:00
|
|
|
std::string _cachedName;
|
2016-05-22 13:31:18 -05:00
|
|
|
int _ver;
|
2016-04-25 04:04:25 -05:00
|
|
|
};
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
std::shared_ptr<TileCache::TileBeingRendered> TileCache::findTileBeingRendered(const TileDesc& tileDesc)
|
2016-04-15 10:24:00 -05:00
|
|
|
{
|
2016-05-15 17:47:08 -05:00
|
|
|
const std::string cachedName = cacheFileName(tileDesc);
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-05-02 18:17:46 -05:00
|
|
|
Util::assertIsLocked(_tilesBeingRenderedMutex);
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-05-01 19:34:39 -05:00
|
|
|
const auto tile = _tilesBeingRendered.find(cachedName);
|
2016-04-25 21:44:58 -05:00
|
|
|
return (tile != _tilesBeingRendered.end() ? tile->second : nullptr);
|
2016-04-15 10:24:00 -05:00
|
|
|
}
|
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
void TileCache::forgetTileBeingRendered(const TileDesc& tile)
|
2016-04-15 10:24:00 -05:00
|
|
|
{
|
2016-05-15 17:47:08 -05:00
|
|
|
const std::string cachedName = cacheFileName(tile);
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-05-02 18:17:46 -05:00
|
|
|
Util::assertIsLocked(_tilesBeingRenderedMutex);
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-05-01 19:34:39 -05:00
|
|
|
assert(_tilesBeingRendered.find(cachedName) != _tilesBeingRendered.end());
|
2016-04-15 10:24:00 -05:00
|
|
|
_tilesBeingRendered.erase(cachedName);
|
2016-03-26 06:50:13 -05:00
|
|
|
}
|
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
std::unique_ptr<std::fstream> TileCache::lookupTile(const TileDesc& tile)
|
2015-03-12 09:18:35 -05:00
|
|
|
{
|
2016-05-15 17:47:08 -05:00
|
|
|
const std::string fileName = _cacheDir + "/" + cacheFileName(tile);
|
2015-03-12 09:18:35 -05:00
|
|
|
|
2016-04-14 19:27:19 -05:00
|
|
|
std::unique_ptr<std::fstream> result(new std::fstream(fileName, std::ios::in));
|
2016-05-15 17:47:08 -05:00
|
|
|
UnitWSD::get().lookupTile(tile.getPart(), tile.getWidth(), tile.getHeight(),
|
|
|
|
tile.getTilePosX(), tile.getTilePosY(),
|
|
|
|
tile.getTileWidth(), tile.getTileHeight(), result);
|
2016-05-01 19:34:39 -05:00
|
|
|
|
2016-04-14 19:27:19 -05:00
|
|
|
if (result && result->is_open())
|
2016-03-26 07:20:48 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
Log::trace("Found cache tile: " + fileName);
|
2016-04-14 19:27:19 -05:00
|
|
|
return result;
|
2016-03-26 07:20:48 -05:00
|
|
|
}
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2016-04-14 19:27:19 -05:00
|
|
|
return nullptr;
|
2015-03-12 09:18:35 -05:00
|
|
|
}
|
|
|
|
|
2016-09-01 15:17:56 -05:00
|
|
|
void TileCache::saveTileAndNotify(const TileDesc& tile, const char *data, const size_t size, const bool /* priority */)
|
2015-03-12 09:18:35 -05:00
|
|
|
{
|
2016-05-22 14:20:24 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_tilesBeingRenderedMutex);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2016-05-22 14:20:24 -05:00
|
|
|
std::shared_ptr<TileBeingRendered> tileBeingRendered = findTileBeingRendered(tile);
|
2016-09-01 15:17:56 -05:00
|
|
|
#if 0
|
2016-05-22 15:47:22 -05:00
|
|
|
if (!priority && tileBeingRendered && tileBeingRendered->getVersion() != tile.getVersion())
|
2016-05-22 14:20:24 -05:00
|
|
|
{
|
|
|
|
Log::trace() << "Skipping unexpected tile ver: " << tile.getVersion()
|
|
|
|
<< ", waiting for ver " << tileBeingRendered->getVersion() << Log::end;
|
|
|
|
return;
|
|
|
|
}
|
2016-09-01 15:17:56 -05:00
|
|
|
#endif
|
2015-03-13 07:17:51 -05:00
|
|
|
|
2016-05-22 14:20:24 -05:00
|
|
|
// Save to disk.
|
|
|
|
const auto cachedName = (tileBeingRendered ? tileBeingRendered->getCacheName()
|
|
|
|
: cacheFileName(tile));
|
|
|
|
const auto fileName = _cacheDir + "/" + cachedName;
|
|
|
|
Log::trace() << "Saving cache tile: " << fileName << Log::end;
|
2015-03-12 18:34:42 -05:00
|
|
|
std::fstream outStream(fileName, std::ios::out);
|
2015-03-12 09:18:35 -05:00
|
|
|
outStream.write(data, size);
|
|
|
|
outStream.close();
|
2016-05-22 14:20:24 -05:00
|
|
|
|
|
|
|
// Notify subscribers, if any.
|
|
|
|
if (tileBeingRendered)
|
|
|
|
{
|
|
|
|
if (!tileBeingRendered->_subscribers.empty())
|
|
|
|
{
|
2016-09-01 07:25:32 -05:00
|
|
|
const std::string message = tile.serialize("tile");
|
|
|
|
Log::debug("Sending tile message to subscribers: " + message);
|
2016-05-22 14:20:24 -05:00
|
|
|
|
|
|
|
for (const auto& i: tileBeingRendered->_subscribers)
|
|
|
|
{
|
|
|
|
auto subscriber = i.lock();
|
|
|
|
if (subscriber)
|
|
|
|
{
|
2016-09-01 07:25:32 -05:00
|
|
|
//FIXME: This is inefficient; should just send directly to each client (although that is risky as well!
|
|
|
|
// Re-emit the tile command in the other thread(s) to re-check and hit
|
|
|
|
// the cache. Construct the message from scratch to contain only the
|
|
|
|
// mandatory parts of the message.
|
|
|
|
subscriber->sendToInputQueue(message);
|
2016-05-22 14:20:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove subscriptions.
|
2016-05-22 15:47:22 -05:00
|
|
|
if (tileBeingRendered->getVersion() == tile.getVersion())
|
|
|
|
{
|
2016-05-22 18:17:01 -05:00
|
|
|
Log::debug() << "STATISTICS: tile internal roundtrip "
|
|
|
|
<< tileBeingRendered->getElapsedTimeMs() << " ms." << Log::end;
|
2016-05-22 15:47:22 -05:00
|
|
|
_tilesBeingRendered.erase(cachedName);
|
|
|
|
}
|
2016-05-22 14:20:24 -05:00
|
|
|
}
|
2015-03-12 18:34:42 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
std::string TileCache::getTextFile(const std::string& fileName)
|
2015-03-13 07:17:51 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
const std::string fullFileName = _cacheDir + "/" + fileName;
|
2015-03-13 07:17:51 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
std::fstream textStream(fullFileName, std::ios::in);
|
2015-08-18 13:01:05 -05:00
|
|
|
if (!textStream.is_open())
|
2016-03-26 07:06:39 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
Log::info("Could not open " + fullFileName);
|
2015-03-13 07:17:51 -05:00
|
|
|
return "";
|
2016-03-26 07:06:39 -05:00
|
|
|
}
|
2015-03-16 14:08:07 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
std::vector<char> buffer;
|
2015-08-18 13:01:05 -05:00
|
|
|
textStream.seekg(0, std::ios_base::end);
|
|
|
|
std::streamsize size = textStream.tellg();
|
2016-04-22 06:00:11 -05:00
|
|
|
buffer.resize(size);
|
2015-08-18 13:01:05 -05:00
|
|
|
textStream.seekg(0, std::ios_base::beg);
|
2016-04-22 06:00:11 -05:00
|
|
|
textStream.read(buffer.data(), size);
|
2015-08-18 13:01:05 -05:00
|
|
|
textStream.close();
|
2015-03-13 07:17:51 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
if (buffer.size() > 0 && buffer.back() == '\n')
|
|
|
|
buffer.pop_back();
|
|
|
|
|
|
|
|
std::string result = std::string(buffer.data(), buffer.size());
|
2016-07-09 13:29:25 -05:00
|
|
|
Log::info("Read '" + LOOLProtocol::getAbbreviatedMessage(result.c_str(), result.size()) + "' from " + fullFileName);
|
2015-03-16 14:08:07 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
return result;
|
2015-03-13 07:17:51 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
void TileCache::saveTextFile(const std::string& text, const std::string& fileName)
|
2015-06-24 15:05:49 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
const std::string fullFileName = _cacheDir + "/" + fileName;
|
|
|
|
std::fstream textStream(fullFileName, std::ios::out);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
if (!textStream.is_open())
|
2016-03-26 07:06:39 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
Log::error("Could not save '" + text + "' to " + fullFileName);
|
|
|
|
return;
|
2016-04-01 10:52:17 -05:00
|
|
|
}
|
2016-04-22 06:00:11 -05:00
|
|
|
else
|
2016-04-01 10:52:17 -05:00
|
|
|
{
|
2016-07-01 09:59:19 -05:00
|
|
|
Log::info("Saving '" + LOOLProtocol::getAbbreviatedMessage(text.c_str(), text.size()) + "' to " + fullFileName);
|
2016-03-26 07:06:39 -05:00
|
|
|
}
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
textStream << text << std::endl;
|
|
|
|
textStream.close();
|
2015-06-24 15:05:49 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
void TileCache::setUnsavedChanges(bool state)
|
2015-03-13 07:17:51 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
if (state)
|
|
|
|
saveTextFile("1", "unsaved.txt");
|
|
|
|
else
|
|
|
|
removeFile("unsaved.txt");
|
2015-03-13 07:17:51 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 08:12:44 -06:00
|
|
|
void TileCache::saveRendering(const std::string& name, const std::string& dir, const char *data, size_t size)
|
|
|
|
{
|
|
|
|
// can fonts be invalidated?
|
2016-04-22 06:00:11 -05:00
|
|
|
const std::string dirName = _cacheDir + "/" + dir;
|
2015-11-27 08:12:44 -06:00
|
|
|
|
|
|
|
File(dirName).createDirectories();
|
|
|
|
|
2016-03-26 07:06:39 -05:00
|
|
|
const std::string fileName = dirName + "/" + name;
|
2015-11-27 08:12:44 -06:00
|
|
|
|
|
|
|
std::fstream outStream(fileName, std::ios::out);
|
|
|
|
outStream.write(data, size);
|
|
|
|
outStream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<std::fstream> TileCache::lookupRendering(const std::string& name, const std::string& dir)
|
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
const std::string dirName = _cacheDir + "/" + dir;
|
2016-03-26 07:06:39 -05:00
|
|
|
const std::string fileName = dirName + "/" + name;
|
2015-11-27 08:12:44 -06:00
|
|
|
File directory(dirName);
|
|
|
|
|
|
|
|
if (directory.exists() && directory.isDirectory() && File(fileName).exists())
|
|
|
|
{
|
|
|
|
std::unique_ptr<std::fstream> result(new std::fstream(fileName, std::ios::in));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-28 10:39:05 -05:00
|
|
|
void TileCache::invalidateTiles(int part, int x, int y, int width, int height)
|
|
|
|
{
|
2016-04-14 19:08:25 -05:00
|
|
|
Log::trace() << "Removing invalidated tiles: part: " << part
|
|
|
|
<< ", x: " << x << ", y: " << y
|
|
|
|
<< ", width: " << width
|
|
|
|
<< ", height: " << height << Log::end;
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
File dir(_cacheDir);
|
2016-05-22 13:31:18 -05:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_cacheMutex);
|
|
|
|
std::unique_lock<std::mutex> lockSubscribers(_tilesBeingRenderedMutex);
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
if (dir.exists() && dir.isDirectory())
|
2015-05-28 10:39:05 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
for (auto tileIterator = DirectoryIterator(dir); tileIterator != DirectoryIterator(); ++tileIterator)
|
2015-05-28 10:39:05 -05:00
|
|
|
{
|
2015-12-25 18:37:44 -06:00
|
|
|
const std::string fileName = tileIterator.path().getFileName();
|
2015-06-24 15:05:49 -05:00
|
|
|
if (intersectsTile(fileName, part, x, y, width, height))
|
2015-05-28 10:39:05 -05:00
|
|
|
{
|
2016-04-21 00:19:58 -05:00
|
|
|
Log::debug("Removing tile: " + tileIterator.path().toString());
|
2015-12-25 19:11:47 -06:00
|
|
|
Util::removeFile(tileIterator.path());
|
2015-05-28 10:39:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 13:31:18 -05:00
|
|
|
|
|
|
|
// Forget this tile as it will have to be rendered again.
|
|
|
|
for (auto it = _tilesBeingRendered.begin(); it != _tilesBeingRendered.end(); )
|
|
|
|
{
|
2016-05-22 14:20:24 -05:00
|
|
|
const std::string cachedName = it->first;
|
|
|
|
if (intersectsTile(cachedName, part, x, y, width, height))
|
2016-05-22 13:31:18 -05:00
|
|
|
{
|
2016-05-22 14:20:24 -05:00
|
|
|
Log::debug("Removing subscriptions for: " + cachedName);
|
2016-05-22 13:31:18 -05:00
|
|
|
it = _tilesBeingRendered.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2015-05-28 10:39:05 -05:00
|
|
|
}
|
|
|
|
|
2015-07-21 06:53:53 -05:00
|
|
|
void TileCache::invalidateTiles(const std::string& tiles)
|
2015-05-28 10:39:05 -05:00
|
|
|
{
|
2015-05-29 00:49:49 -05:00
|
|
|
StringTokenizer tokens(tiles, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
2015-05-28 10:39:05 -05:00
|
|
|
|
2015-05-28 15:22:27 -05:00
|
|
|
assert(tokens[0] == "invalidatetiles:");
|
2015-05-28 10:39:05 -05:00
|
|
|
|
2015-05-29 00:39:21 -05:00
|
|
|
if (tokens.count() == 2 && tokens[1] == "EMPTY")
|
|
|
|
{
|
|
|
|
invalidateTiles(-1, 0, 0, INT_MAX, INT_MAX);
|
|
|
|
}
|
2015-07-21 06:53:53 -05:00
|
|
|
else if (tokens.count() != 6)
|
2015-05-29 00:39:21 -05:00
|
|
|
{
|
2015-05-28 10:39:05 -05:00
|
|
|
return;
|
2015-05-29 00:39:21 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-21 06:53:53 -05:00
|
|
|
int part, x, y, width, height;
|
|
|
|
if (getTokenInteger(tokens[1], "part", part) &&
|
|
|
|
getTokenInteger(tokens[2], "x", x) &&
|
|
|
|
getTokenInteger(tokens[3], "y", y) &&
|
|
|
|
getTokenInteger(tokens[4], "width", width) &&
|
|
|
|
getTokenInteger(tokens[5], "height", height))
|
|
|
|
{
|
|
|
|
invalidateTiles(part, x, y, width, height);
|
|
|
|
}
|
2015-05-29 00:39:21 -05:00
|
|
|
}
|
2015-05-28 10:39:05 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 01:38:59 -05:00
|
|
|
void TileCache::removeFile(const std::string& fileName)
|
2016-02-10 11:51:24 -06:00
|
|
|
{
|
2016-04-22 08:36:09 -05:00
|
|
|
const std::string fullFileName = _cacheDir + "/" + fileName;
|
|
|
|
|
|
|
|
if (std::remove(fullFileName.c_str()) == 0)
|
2016-04-25 21:44:58 -05:00
|
|
|
Log::info("Removed file: " + fullFileName);
|
2015-06-24 15:05:49 -05:00
|
|
|
}
|
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
std::string TileCache::cacheFileName(const TileDesc& tile)
|
2015-03-12 18:34:42 -05:00
|
|
|
{
|
2016-03-26 08:16:23 -05:00
|
|
|
std::ostringstream oss;
|
2016-05-15 17:47:08 -05:00
|
|
|
oss << tile.getPart() << '_' << tile.getWidth() << 'x' << tile.getHeight() << '.'
|
|
|
|
<< tile.getTilePosX() << ',' << tile.getTilePosY() << '.'
|
|
|
|
<< tile.getTileWidth() << 'x' << tile.getTileHeight() << ".png";
|
2016-03-26 08:16:23 -05:00
|
|
|
return oss.str();
|
2015-03-12 18:34:42 -05:00
|
|
|
}
|
|
|
|
|
2015-12-25 18:37:44 -06:00
|
|
|
bool TileCache::parseCacheFileName(const std::string& fileName, int& part, int& width, int& height, int& tilePosX, int& tilePosY, int& tileWidth, int& tileHeight)
|
2015-05-28 10:39:05 -05:00
|
|
|
{
|
2015-06-24 15:05:49 -05:00
|
|
|
return (std::sscanf(fileName.c_str(), "%d_%dx%d.%d,%d.%dx%d.png", &part, &width, &height, &tilePosX, &tilePosY, &tileWidth, &tileHeight) == 7);
|
|
|
|
}
|
|
|
|
|
2015-12-25 18:37:44 -06:00
|
|
|
bool TileCache::intersectsTile(const std::string& fileName, int part, int x, int y, int width, int height)
|
2015-06-24 15:05:49 -05:00
|
|
|
{
|
|
|
|
int tilePart, tilePixelWidth, tilePixelHeight, tilePosX, tilePosY, tileWidth, tileHeight;
|
|
|
|
if (parseCacheFileName(fileName, tilePart, tilePixelWidth, tilePixelHeight, tilePosX, tilePosY, tileWidth, tileHeight))
|
|
|
|
{
|
|
|
|
if (part != -1 && tilePart != part)
|
|
|
|
return false;
|
|
|
|
|
2016-04-14 19:08:25 -05:00
|
|
|
const int left = std::max(x, tilePosX);
|
|
|
|
const int right = std::min(x + width, tilePosX + tileWidth);
|
|
|
|
const int top = std::max(y, tilePosY);
|
|
|
|
const int bottom = std::min(y + height, tilePosY + tileHeight);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
|
|
|
if (left <= right && top <= bottom)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-05-28 10:39:05 -05:00
|
|
|
}
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
Timestamp TileCache::getLastModified()
|
2015-03-12 18:34:42 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
std::fstream modTimeFile(_cacheDir + "/modtime.txt", std::ios::in);
|
2015-03-12 18:34:42 -05:00
|
|
|
|
|
|
|
if (!modTimeFile.is_open())
|
|
|
|
return 0;
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
Timestamp::TimeVal result;
|
2015-03-12 18:34:42 -05:00
|
|
|
modTimeFile >> result;
|
|
|
|
|
|
|
|
modTimeFile.close();
|
|
|
|
return result;
|
2015-03-12 09:18:35 -05:00
|
|
|
}
|
|
|
|
|
2016-04-01 10:56:08 -05:00
|
|
|
void TileCache::saveLastModified(const Timestamp& timestamp)
|
2015-08-04 13:37:05 -05:00
|
|
|
{
|
2016-04-22 06:00:11 -05:00
|
|
|
std::fstream modTimeFile(_cacheDir + "/modtime.txt", std::ios::out);
|
2015-08-04 13:37:05 -05:00
|
|
|
modTimeFile << timestamp.raw() << std::endl;
|
|
|
|
modTimeFile.close();
|
|
|
|
}
|
|
|
|
|
2016-04-25 04:04:25 -05:00
|
|
|
// FIXME: to be further simplified when we centralize tile messages.
|
2016-08-18 09:28:55 -05:00
|
|
|
int TileCache::subscribeToTileRendering(const TileDesc& tile, const std::shared_ptr<ClientSession> &subscriber)
|
2016-04-23 11:11:11 -05:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_tilesBeingRenderedMutex);
|
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
std::shared_ptr<TileBeingRendered> tileBeingRendered = findTileBeingRendered(tile);
|
2016-04-25 04:04:25 -05:00
|
|
|
|
2016-04-23 11:11:11 -05:00
|
|
|
if (tileBeingRendered)
|
|
|
|
{
|
2016-08-18 09:28:55 -05:00
|
|
|
Log::debug() << "Subscribing to tile (" << tile.getPart() << ',' << tile.getTilePosX() << ','
|
|
|
|
<< tile.getTilePosY() << ") which has "
|
|
|
|
<< tileBeingRendered->_subscribers.size()
|
|
|
|
<< " subscribers already. Adding one more." << Log::end;
|
2016-04-23 11:11:11 -05:00
|
|
|
assert(subscriber->getKind() == LOOLSession::Kind::ToClient);
|
2016-04-25 04:04:25 -05:00
|
|
|
|
|
|
|
for (const auto &s : tileBeingRendered->_subscribers)
|
|
|
|
{
|
|
|
|
if (s.lock().get() == subscriber.get())
|
|
|
|
{
|
|
|
|
Log::debug("Redundant request to re-subscribe on a tile");
|
2016-05-22 13:31:18 -05:00
|
|
|
return 0;
|
2016-04-25 04:04:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tileBeingRendered->_subscribers.push_back(subscriber);
|
|
|
|
|
2016-05-15 09:19:49 -05:00
|
|
|
const auto duration = (std::chrono::steady_clock::now() - tileBeingRendered->getStartTime());
|
|
|
|
if (std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() > COMMAND_TIMEOUT_MS)
|
|
|
|
{
|
|
|
|
// Tile painting has stalled. Reissue.
|
2016-05-22 13:31:18 -05:00
|
|
|
return tileBeingRendered->getVersion();
|
2016-05-15 09:19:49 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
return 0;
|
2016-04-23 11:11:11 -05:00
|
|
|
}
|
2016-04-25 04:04:25 -05:00
|
|
|
else
|
|
|
|
{
|
2016-08-18 09:28:55 -05:00
|
|
|
Log::debug() << "Subscribing to tile (" << tile.getPart() << ',' << tile.getTilePosX() << ','
|
|
|
|
<< tile.getTilePosY() << ") which has no subscribers. Subscribing for ver: "
|
2016-05-22 14:20:24 -05:00
|
|
|
<< tile.getVersion() << "." << Log::end;
|
2016-05-04 21:05:09 -05:00
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
const std::string cachedName = cacheFileName(tile);
|
2016-04-23 11:11:11 -05:00
|
|
|
|
2016-04-25 04:04:25 -05:00
|
|
|
assert(_tilesBeingRendered.find(cachedName) == _tilesBeingRendered.end());
|
|
|
|
|
2016-05-22 14:20:24 -05:00
|
|
|
tileBeingRendered = std::make_shared<TileBeingRendered>(cachedName, tile.getVersion());
|
2016-05-01 19:50:11 -05:00
|
|
|
tileBeingRendered->_subscribers.push_back(subscriber);
|
|
|
|
_tilesBeingRendered[cachedName] = tileBeingRendered;
|
2016-04-25 04:04:25 -05:00
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
return tileBeingRendered->getVersion();
|
2016-04-25 04:04:25 -05:00
|
|
|
}
|
2016-04-23 11:11:11 -05:00
|
|
|
}
|
|
|
|
|
2015-03-12 09:18:35 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|