2016-03-23 07:41:18 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
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
|
|
|
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "DocumentBroker.hpp"
|
|
|
|
|
2017-05-20 12:28:43 -05:00
|
|
|
#include <atomic>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cassert>
|
2017-04-18 23:54:42 -05:00
|
|
|
#include <chrono>
|
2016-12-14 10:12:57 -06:00
|
|
|
#include <ctime>
|
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 <fstream>
|
2016-11-10 05:55:38 -06:00
|
|
|
#include <sstream>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-11-08 02:34:37 -06:00
|
|
|
#include <Poco/DigestStream.h>
|
2017-11-07 07:15:36 -06:00
|
|
|
#include <Poco/Exception.h>
|
2016-11-10 05:55:38 -06:00
|
|
|
#include <Poco/JSON/Object.h>
|
2016-03-23 07:41:18 -05:00
|
|
|
#include <Poco/Path.h>
|
2016-03-26 06:50:13 -05:00
|
|
|
#include <Poco/SHA1Engine.h>
|
2017-01-22 22:12:51 -06:00
|
|
|
#include <Poco/StreamCopier.h>
|
2016-09-28 14:07:07 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-10-22 09:25:57 -05:00
|
|
|
#include "Admin.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 "ClientSession.hpp"
|
2016-04-17 10:05:39 -05:00
|
|
|
#include "Exceptions.hpp"
|
|
|
|
#include "LOOLWSD.hpp"
|
2017-05-20 12:28:43 -05:00
|
|
|
#include "SenderQueue.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
#include "Storage.hpp"
|
|
|
|
#include "TileCache.hpp"
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <common/Log.hpp>
|
|
|
|
#include <common/Message.hpp>
|
|
|
|
#include <common/Protocol.hpp>
|
|
|
|
#include <common/Unit.hpp>
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2018-01-29 09:13:54 -06:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2018-07-31 08:42:58 -05:00
|
|
|
#define TILES_ON_FLY_MIN_UPPER_LIMIT 10.0f
|
2018-07-10 07:05:36 -05:00
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
2016-11-10 05:55:38 -06:00
|
|
|
using Poco::JSON::Object;
|
2016-09-28 14:07:07 -05:00
|
|
|
|
2017-03-07 22:46:02 -06:00
|
|
|
void ChildProcess::setDocumentBroker(const std::shared_ptr<DocumentBroker>& docBroker)
|
|
|
|
{
|
|
|
|
assert(docBroker && "Invalid DocumentBroker instance.");
|
|
|
|
_docBroker = docBroker;
|
|
|
|
|
|
|
|
// Add the prisoner socket to the docBroker poll.
|
|
|
|
docBroker->addSocketToPoll(_socket);
|
|
|
|
}
|
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Returns the cache path for a given document URI.
|
|
|
|
std::string getCachePath(const std::string& uri)
|
|
|
|
{
|
|
|
|
Poco::SHA1Engine digestEngine;
|
|
|
|
|
|
|
|
digestEngine.update(uri.c_str(), uri.size());
|
|
|
|
|
2017-03-30 04:15:28 -05:00
|
|
|
return LOOLWSD::Cache + '/' +
|
|
|
|
Poco::DigestEngine::digestToHex(digestEngine.digest()).insert(3, "/").insert(2, "/").insert(1, "/");
|
2016-03-26 06:50:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 03:05:00 -05:00
|
|
|
Poco::URI DocumentBroker::sanitizeURI(const std::string& uri)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
|
|
|
// The URI of the document should be url-encoded.
|
|
|
|
std::string decodedUri;
|
|
|
|
Poco::URI::decode(uri, decodedUri);
|
2018-02-07 03:17:59 -06:00
|
|
|
Poco::URI uriPublic(decodedUri);
|
2016-03-23 07:41:18 -05:00
|
|
|
|
|
|
|
if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
|
|
|
|
{
|
|
|
|
// TODO: Validate and limit access to local paths!
|
|
|
|
uriPublic.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uriPublic.getPath().empty())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid URI.");
|
|
|
|
}
|
|
|
|
|
2016-11-17 01:06:48 -06:00
|
|
|
// We decoded access token before embedding it in loleaflet.html
|
|
|
|
// So, we need to decode it now to get its actual value
|
|
|
|
Poco::URI::QueryParameters queryParams = uriPublic.getQueryParameters();
|
|
|
|
for (auto& param: queryParams)
|
|
|
|
{
|
|
|
|
// look for encoded query params (access token as of now)
|
|
|
|
if (param.first == "access_token")
|
|
|
|
{
|
|
|
|
std::string decodedToken;
|
|
|
|
Poco::URI::decode(param.second, decodedToken);
|
|
|
|
param.second = decodedToken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 21:43:12 -06:00
|
|
|
uriPublic.setQueryParameters(queryParams);
|
2016-03-23 07:41:18 -05:00
|
|
|
return uriPublic;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DocumentBroker::getDocKey(const Poco::URI& uri)
|
|
|
|
{
|
2017-05-19 03:32:24 -05:00
|
|
|
// If multiple host-names are used to access us, then
|
2017-05-23 06:35:07 -05:00
|
|
|
// they must be aliases. Permission to access aliased hosts
|
|
|
|
// is checked at the point of accepting incoming connections.
|
|
|
|
// At this point storing the hostname artificially discriminates
|
|
|
|
// between aliases and forces same document (when opened from
|
2017-05-19 03:32:24 -05:00
|
|
|
// alias hosts) to load as separate documents and sharing doesn't
|
|
|
|
// work. Worse, saving overwrites one another.
|
|
|
|
std::string docKey;
|
2017-05-23 06:35:07 -05:00
|
|
|
Poco::URI::encode(uri.getPath(), "", docKey);
|
2017-05-19 03:32:24 -05:00
|
|
|
return docKey;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2017-03-07 11:34:01 -06:00
|
|
|
/// The Document Broker Poll - one of these in a thread per document
|
2017-03-12 12:56:42 -05:00
|
|
|
class DocumentBroker::DocumentBrokerPoll final : public TerminatingPoll
|
2017-03-07 11:34:01 -06:00
|
|
|
{
|
2017-03-12 12:56:42 -05:00
|
|
|
/// The DocumentBroker owning us.
|
|
|
|
DocumentBroker& _docBroker;
|
|
|
|
|
2017-03-07 11:34:01 -06:00
|
|
|
public:
|
2017-03-12 12:56:42 -05:00
|
|
|
DocumentBrokerPoll(const std::string &threadName, DocumentBroker& docBroker) :
|
|
|
|
TerminatingPoll(threadName),
|
|
|
|
_docBroker(docBroker)
|
2017-03-07 11:34:01 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void pollingThread()
|
|
|
|
{
|
2017-03-12 12:56:42 -05:00
|
|
|
// Delegate to the docBroker.
|
|
|
|
_docBroker.pollThread();
|
2017-03-07 11:34:01 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-02 14:55:56 -05:00
|
|
|
std::atomic<unsigned> DocumentBroker::DocBrokerId(1);
|
|
|
|
|
2017-02-05 20:59:08 -06:00
|
|
|
DocumentBroker::DocumentBroker(const std::string& uri,
|
|
|
|
const Poco::URI& uriPublic,
|
2016-03-23 07:41:18 -05:00
|
|
|
const std::string& docKey,
|
2017-03-04 17:07:17 -06:00
|
|
|
const std::string& childRoot) :
|
2017-02-05 20:59:08 -06:00
|
|
|
_uriOrig(uri),
|
2016-03-23 07:41:18 -05:00
|
|
|
_uriPublic(uriPublic),
|
|
|
|
_docKey(docKey),
|
2017-04-02 14:55:56 -05:00
|
|
|
_docId(Util::encodeId(DocBrokerId++, 3)),
|
2016-03-23 07:41:18 -05:00
|
|
|
_childRoot(childRoot),
|
2016-03-26 06:50:13 -05:00
|
|
|
_cacheRoot(getCachePath(uriPublic.toString())),
|
2017-06-01 07:56:54 -05:00
|
|
|
_documentChangedInStorage(false),
|
2016-04-24 10:08:08 -05:00
|
|
|
_lastSaveTime(std::chrono::steady_clock::now()),
|
2017-03-25 23:52:34 -05:00
|
|
|
_lastSaveRequestTime(std::chrono::steady_clock::now() - std::chrono::milliseconds(COMMAND_TIMEOUT_MS)),
|
2016-04-21 23:11:24 -05:00
|
|
|
_markToDestroy(false),
|
2018-02-01 22:28:20 -06:00
|
|
|
_closeRequest(false),
|
2016-10-08 09:31:35 -05:00
|
|
|
_isLoaded(false),
|
|
|
|
_isModified(false),
|
2016-05-22 15:47:22 -05:00
|
|
|
_cursorPosX(0),
|
|
|
|
_cursorPosY(0),
|
2016-09-01 15:15:13 -05:00
|
|
|
_cursorWidth(0),
|
|
|
|
_cursorHeight(0),
|
2017-04-02 16:50:17 -05:00
|
|
|
_poll(new DocumentBrokerPoll("docbroker_" + _docId, *this)),
|
2017-03-12 21:11:25 -05:00
|
|
|
_stop(false),
|
2017-07-07 06:42:19 -05:00
|
|
|
_closeReason("stopped"),
|
2016-10-11 07:39:56 -05:00
|
|
|
_tileVersion(0),
|
|
|
|
_debugRenderedTileCount(0)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
|
|
|
assert(!_docKey.empty());
|
|
|
|
assert(!_childRoot.empty());
|
2016-05-02 06:21:30 -05:00
|
|
|
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_INF("DocumentBroker [" << LOOLWSD::anonymizeUrl(_uriPublic.toString()) <<
|
2017-03-19 13:49:52 -05:00
|
|
|
"] created with docKey [" << _docKey << "] and root [" << _childRoot << "]");
|
2017-03-04 17:07:17 -06:00
|
|
|
}
|
|
|
|
|
2017-03-13 07:00:31 -05:00
|
|
|
void DocumentBroker::startThread()
|
|
|
|
{
|
|
|
|
_poll->startThread();
|
|
|
|
}
|
|
|
|
|
2018-01-14 10:16:10 -06:00
|
|
|
void DocumentBroker::assertCorrectThread() const
|
2017-03-15 07:07:17 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
_poll->assertCorrectThread();
|
2017-03-15 07:07:17 -05:00
|
|
|
}
|
|
|
|
|
2017-03-09 12:19:53 -06:00
|
|
|
// The inner heart of the DocumentBroker - our poll loop.
|
2017-03-07 19:46:16 -06:00
|
|
|
void DocumentBroker::pollThread()
|
2017-03-04 17:07:17 -06:00
|
|
|
{
|
2017-03-09 22:50:36 -06:00
|
|
|
LOG_INF("Starting docBroker polling thread for docKey [" << _docKey << "].");
|
|
|
|
|
2017-03-11 16:01:27 -06:00
|
|
|
_threadStart = std::chrono::steady_clock::now();
|
|
|
|
|
2017-03-04 17:07:17 -06:00
|
|
|
// Request a kit process for this doc.
|
2018-09-13 11:16:00 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-03-29 22:11:34 -05:00
|
|
|
do
|
|
|
|
{
|
|
|
|
static const int timeoutMs = COMMAND_TIMEOUT_MS * 5;
|
|
|
|
_childProcess = getNewChild_Blocks();
|
|
|
|
if (_childProcess ||
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() -
|
|
|
|
_threadStart).count() > timeoutMs)
|
|
|
|
break;
|
2018-01-14 19:49:11 -06:00
|
|
|
// Nominal time between retries, lest we busy-loop. getNewChild could also wait, so don't double that here.
|
2017-06-21 20:49:08 -05:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(CHILD_REBALANCE_INTERVAL_MS / 10));
|
2017-03-29 22:11:34 -05:00
|
|
|
}
|
|
|
|
while (!_stop && _poll->continuePolling() && !TerminationFlag && !ShutdownRequestFlag);
|
2018-09-13 11:16:00 -05:00
|
|
|
#else
|
|
|
|
_childProcess = getNewChild_Blocks(getPublicUri().getPath());
|
|
|
|
#endif
|
2017-03-29 22:11:34 -05:00
|
|
|
|
2017-03-07 19:46:16 -06:00
|
|
|
if (!_childProcess)
|
2017-03-04 17:07:17 -06:00
|
|
|
{
|
|
|
|
// Let the client know we can't serve now.
|
|
|
|
LOG_ERR("Failed to get new child.");
|
|
|
|
|
|
|
|
// FIXME: need to notify all clients and shut this down ...
|
2018-01-14 19:49:11 -06:00
|
|
|
// FIXME: return something good down the websocket ...
|
2017-03-04 17:07:17 -06:00
|
|
|
#if 0
|
|
|
|
const std::string msg = SERVICE_UNAVAILABLE_INTERNAL_ERROR;
|
2017-03-29 19:03:01 -05:00
|
|
|
ws.sendMessage(msg);
|
2017-03-04 17:07:17 -06:00
|
|
|
// abnormal close frame handshake
|
|
|
|
ws.shutdown(WebSocketHandler::StatusCodes::ENDPOINT_GOING_AWAY);
|
|
|
|
#endif
|
2018-01-14 19:49:11 -06:00
|
|
|
stop("Failed to get new child.");
|
|
|
|
|
|
|
|
// Stop to mark it done and cleanup.
|
|
|
|
_poll->stop();
|
|
|
|
_poll->removeSockets();
|
|
|
|
|
|
|
|
// Async cleanup.
|
|
|
|
LOOLWSD::doHousekeeping();
|
2017-03-29 22:11:34 -05:00
|
|
|
|
|
|
|
LOG_INF("Finished docBroker polling thread for docKey [" << _docKey << "].");
|
2017-03-07 22:46:02 -06:00
|
|
|
return;
|
2017-03-04 17:07:17 -06:00
|
|
|
}
|
2017-03-07 19:46:16 -06:00
|
|
|
|
|
|
|
_childProcess->setDocumentBroker(shared_from_this());
|
2017-03-19 13:49:52 -05:00
|
|
|
LOG_INF("Doc [" << _docKey << "] attached to child [" << _childProcess->getPid() << "].");
|
2017-03-04 17:07:17 -06:00
|
|
|
|
2017-04-18 23:54:42 -05:00
|
|
|
static const bool AutoSaveEnabled = !std::getenv("LOOL_NO_AUTOSAVE");
|
2017-05-07 12:28:57 -05:00
|
|
|
static const size_t IdleDocTimeoutSecs = LOOLWSD::getConfigValue<int>(
|
|
|
|
"per_document.idle_timeout_secs", 3600);
|
2018-01-14 10:16:10 -06:00
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-06-03 16:53:57 -05:00
|
|
|
// Used to accumulate B/W deltas.
|
|
|
|
uint64_t adminSent = 0;
|
|
|
|
uint64_t adminRecv = 0;
|
|
|
|
auto lastBWUpdateTime = std::chrono::steady_clock::now();
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2018-01-14 10:16:10 -06:00
|
|
|
auto last30SecCheckTime = std::chrono::steady_clock::now();
|
2017-06-03 16:53:57 -05:00
|
|
|
|
2017-03-04 17:07:17 -06:00
|
|
|
// Main polling loop goodness.
|
2017-04-18 23:54:42 -05:00
|
|
|
while (!_stop && _poll->continuePolling() && !TerminationFlag)
|
2017-03-04 17:07:17 -06:00
|
|
|
{
|
2017-04-18 23:54:42 -05:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
2017-06-03 16:53:57 -05:00
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
_poll->poll(SocketPoll::DefaultPollTimeoutMs);
|
|
|
|
|
2018-09-13 11:16:00 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-06-03 16:53:57 -05:00
|
|
|
if (std::chrono::duration_cast<std::chrono::milliseconds>
|
|
|
|
(now - lastBWUpdateTime).count() >= 5 * 1000)
|
|
|
|
{
|
|
|
|
lastBWUpdateTime = now;
|
|
|
|
uint64_t sent, recv;
|
|
|
|
getIOStats(sent, recv);
|
|
|
|
// send change since last notification.
|
|
|
|
Admin::instance().addBytes(getDocKey(),
|
|
|
|
// connection drop transiently reduces this.
|
2017-06-09 20:28:16 -05:00
|
|
|
(sent > adminSent ? (sent - adminSent): uint64_t(0)),
|
|
|
|
(recv > adminRecv ? (recv - adminRecv): uint64_t(0)));
|
2018-01-14 10:16:10 -06:00
|
|
|
LOG_DBG("Doc [" << _docKey << "] added sent: " << sent << " recv: " << recv << " bytes to totals");
|
2017-06-03 16:53:57 -05:00
|
|
|
adminSent = sent;
|
|
|
|
adminRecv = recv;
|
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-06-03 16:53:57 -05:00
|
|
|
|
2018-01-14 17:59:11 -06:00
|
|
|
if (isSaving() &&
|
2017-04-18 23:54:42 -05:00
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>
|
|
|
|
(now - _lastSaveRequestTime).count() <= COMMAND_TIMEOUT_MS)
|
|
|
|
{
|
2018-01-14 10:16:10 -06:00
|
|
|
// We are saving, nothing more to do but wait (until we save or we timeout).
|
2017-04-18 23:54:42 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-01 22:28:20 -06:00
|
|
|
if (ShutdownRequestFlag || _closeRequest)
|
2017-04-18 23:54:42 -05:00
|
|
|
{
|
2018-02-01 22:28:20 -06:00
|
|
|
const std::string reason = ShutdownRequestFlag ? "recycling" : _closeReason;
|
|
|
|
LOG_INF("Autosaving DocumentBroker for docKey [" << getDocKey() << "] for " << reason);
|
2018-01-14 23:49:14 -06:00
|
|
|
if (!autoSave(isPossiblyModified()))
|
|
|
|
{
|
2018-02-01 22:28:20 -06:00
|
|
|
LOG_INF("Terminating DocumentBroker for docKey [" << getDocKey() << "].");
|
|
|
|
stop(reason);
|
2018-01-14 23:49:14 -06:00
|
|
|
}
|
2017-04-18 23:54:42 -05:00
|
|
|
}
|
|
|
|
else if (AutoSaveEnabled && !_stop &&
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(now - last30SecCheckTime).count() >= 30)
|
2017-03-09 12:19:53 -06:00
|
|
|
{
|
2017-04-18 23:54:42 -05:00
|
|
|
LOG_TRC("Triggering an autosave.");
|
2017-03-09 12:19:53 -06:00
|
|
|
autoSave(false);
|
|
|
|
last30SecCheckTime = std::chrono::steady_clock::now();
|
|
|
|
}
|
2017-03-19 18:01:03 -05:00
|
|
|
|
2017-04-06 11:58:41 -05:00
|
|
|
// Remove idle documents after 1 hour.
|
2018-01-14 23:49:14 -06:00
|
|
|
const bool idle = (isLoaded() && getIdleTimeSecs() >= IdleDocTimeoutSecs);
|
|
|
|
if (idle)
|
|
|
|
{
|
|
|
|
// Stop if there is nothing to save.
|
|
|
|
LOG_INF("Autosaving idle DocumentBroker for docKey [" << getDocKey() << "] to kill.");
|
|
|
|
if (!autoSave(isPossiblyModified()))
|
|
|
|
{
|
|
|
|
LOG_INF("Terminating idle DocumentBroker for docKey [" << getDocKey() << "].");
|
|
|
|
stop("idle");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_sessions.empty() && (isLoaded() || _markToDestroy))
|
2017-04-06 11:58:41 -05:00
|
|
|
{
|
2018-01-14 23:49:14 -06:00
|
|
|
// If all sessions have been removed, no reason to linger.
|
|
|
|
LOG_INF("Terminating dead DocumentBroker for docKey [" << getDocKey() << "].");
|
|
|
|
stop("dead");
|
2017-03-19 18:01:03 -05:00
|
|
|
}
|
2017-03-09 12:19:53 -06:00
|
|
|
}
|
|
|
|
|
2017-04-04 23:26:09 -05:00
|
|
|
LOG_INF("Finished polling doc [" << _docKey << "]. stop: " << _stop << ", continuePolling: " <<
|
2017-04-18 23:54:42 -05:00
|
|
|
_poll->continuePolling() << ", ShutdownRequestFlag: " << ShutdownRequestFlag <<
|
2018-01-11 00:29:08 -06:00
|
|
|
", TerminationFlag: " << TerminationFlag << ", closeReason: " << _closeReason << ". Flushing socket.");
|
2017-04-09 18:41:29 -05:00
|
|
|
|
2018-01-12 10:16:56 -06:00
|
|
|
if (_isModified)
|
|
|
|
{
|
|
|
|
std::stringstream state;
|
|
|
|
dumpState(state);
|
|
|
|
LOG_ERR("DocumentBroker stopping although modified " << state.str());
|
|
|
|
}
|
|
|
|
|
2017-05-21 18:13:55 -05:00
|
|
|
// Flush socket data first.
|
2017-03-27 19:50:30 -05:00
|
|
|
const int flushTimeoutMs = POLL_TIMEOUT_MS * 2; // ~1000ms
|
|
|
|
const auto flushStartTime = std::chrono::steady_clock::now();
|
|
|
|
while (_poll->getSocketCount())
|
|
|
|
{
|
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
const int elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - flushStartTime).count();
|
|
|
|
if (elapsedMs > flushTimeoutMs)
|
|
|
|
break;
|
|
|
|
|
|
|
|
_poll->poll(std::min(flushTimeoutMs - elapsedMs, POLL_TIMEOUT_MS / 5));
|
|
|
|
}
|
2017-03-26 22:10:24 -05:00
|
|
|
|
2018-01-11 00:29:08 -06:00
|
|
|
LOG_INF("Finished flushing socket for doc [" << _docKey << "]. stop: " << _stop << ", continuePolling: " <<
|
|
|
|
_poll->continuePolling() << ", ShutdownRequestFlag: " << ShutdownRequestFlag <<
|
|
|
|
", TerminationFlag: " << TerminationFlag << ". Terminating child with reason: [" << _closeReason << "].");
|
|
|
|
|
2017-05-21 18:13:55 -05:00
|
|
|
// Terminate properly while we can.
|
2017-07-07 06:42:19 -05:00
|
|
|
terminateChild(_closeReason);
|
2017-05-21 18:13:55 -05:00
|
|
|
|
2017-04-06 00:32:39 -05:00
|
|
|
// Stop to mark it done and cleanup.
|
|
|
|
_poll->stop();
|
2017-04-06 01:56:21 -05:00
|
|
|
_poll->removeSockets();
|
2017-04-06 00:32:39 -05:00
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-04-05 15:31:15 -05:00
|
|
|
// Async cleanup.
|
2017-04-02 18:56:42 -05:00
|
|
|
LOOLWSD::doHousekeeping();
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-04-02 18:56:42 -05:00
|
|
|
|
2017-04-18 05:13:04 -05:00
|
|
|
// Remove all tiles related to this document from the cache if configured so.
|
|
|
|
if (_tileCache && !LOOLWSD::TileCachePersistent)
|
|
|
|
_tileCache->completeCleanup();
|
|
|
|
|
2017-03-09 22:50:36 -06:00
|
|
|
LOG_INF("Finished docBroker polling thread for docKey [" << _docKey << "].");
|
2017-03-04 17:07:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::isAlive() const
|
|
|
|
{
|
2017-04-12 20:19:51 -05:00
|
|
|
if (!_stop || _poll->isAlive())
|
|
|
|
return true; // Polling thread not started or still running.
|
2017-03-04 17:07:17 -06:00
|
|
|
|
2017-03-12 21:11:25 -05:00
|
|
|
// Shouldn't have live child process outside of the polling thread.
|
|
|
|
return _childProcess && _childProcess->isAlive();
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2016-10-22 09:25:57 -05:00
|
|
|
DocumentBroker::~DocumentBroker()
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 23:11:06 -05:00
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2016-10-22 09:25:57 -05:00
|
|
|
Admin::instance().rmDoc(_docKey);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-10-22 09:25:57 -05:00
|
|
|
|
2017-04-18 23:54:42 -05:00
|
|
|
LOG_INF("~DocumentBroker [" << _docKey <<
|
2016-11-06 10:59:59 -06:00
|
|
|
"] destroyed with " << _sessions.size() << " sessions left.");
|
2016-10-30 13:31:34 -05:00
|
|
|
|
2017-03-31 11:28:20 -05:00
|
|
|
// Do this early - to avoid operating on _childProcess from two threads.
|
|
|
|
_poll->joinThread();
|
|
|
|
|
2016-11-03 22:05:48 -05:00
|
|
|
if (!_sessions.empty())
|
|
|
|
{
|
2017-04-18 23:54:42 -05:00
|
|
|
LOG_WRN("DocumentBroker [" << _docKey << "] still has unremoved sessions.");
|
2016-11-03 22:05:48 -05:00
|
|
|
}
|
2016-11-05 22:00:16 -05:00
|
|
|
|
|
|
|
// Need to first make sure the child exited, socket closed,
|
|
|
|
// and thread finished before we are destroyed.
|
|
|
|
_childProcess.reset();
|
2016-10-22 09:25:57 -05:00
|
|
|
}
|
|
|
|
|
2017-03-31 14:58:33 -05:00
|
|
|
void DocumentBroker::joinThread()
|
|
|
|
{
|
|
|
|
_poll->joinThread();
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:49:11 -06:00
|
|
|
void DocumentBroker::stop(const std::string& reason)
|
2017-04-05 15:31:15 -05:00
|
|
|
{
|
2018-01-14 19:49:11 -06:00
|
|
|
LOG_DBG("Closing DocumentBroker for docKey [" << _docKey << "] with reason: " << reason);
|
|
|
|
_closeReason = reason; // used later in the polling loop
|
2017-04-05 15:31:15 -05:00
|
|
|
_stop = true;
|
|
|
|
_poll->wakeup();
|
|
|
|
}
|
|
|
|
|
2017-03-25 12:56:17 -05:00
|
|
|
bool DocumentBroker::load(const std::shared_ptr<ClientSession>& session, const std::string& jailId)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-11-06 22:14:23 -06:00
|
|
|
|
|
|
|
const std::string sessionId = session->getId();
|
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
LOG_INF("Loading [" << _docKey << "] for session [" << sessionId << "] and jail [" << jailId << "].");
|
|
|
|
|
2016-10-20 16:09:00 -05:00
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
if (UnitWSD::get().filterLoad(sessionId, jailId, result))
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
if (_markToDestroy)
|
2016-04-06 23:32:28 -05:00
|
|
|
{
|
2016-10-16 11:40:52 -05:00
|
|
|
// Tearing down.
|
2016-11-06 12:54:00 -06:00
|
|
|
LOG_WRN("Will not load document marked to destroy. DocKey: [" << _docKey << "].");
|
2016-10-16 11:40:52 -05:00
|
|
|
return false;
|
2016-04-06 23:32:28 -05:00
|
|
|
}
|
2016-03-23 07:41:18 -05:00
|
|
|
|
|
|
|
_jailId = jailId;
|
|
|
|
|
|
|
|
// The URL is the publicly visible one, not visible in the chroot jail.
|
|
|
|
// We need to map it to a jailed path and copy the file there.
|
|
|
|
|
|
|
|
// user/doc/jailId
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::Path jailPath(JAILED_DOCUMENT_ROOT, jailId);
|
2016-08-01 03:05:37 -05:00
|
|
|
std::string jailRoot = getJailRoot();
|
2016-11-06 12:54:00 -06:00
|
|
|
|
|
|
|
LOG_INF("jailPath: " << jailPath.toString() << ", jailRoot: " << jailRoot);
|
2016-08-01 03:05:37 -05:00
|
|
|
|
2016-12-19 05:21:42 -06:00
|
|
|
bool firstInstance = false;
|
2016-10-14 05:09:43 -05:00
|
|
|
if (_storage == nullptr)
|
|
|
|
{
|
2017-01-21 21:43:12 -06:00
|
|
|
// Pass the public URI to storage as it needs to load using the token
|
|
|
|
// and other storage-specific data provided in the URI.
|
2017-05-12 10:42:03 -05:00
|
|
|
const Poco::URI& uriPublic = session->getPublicUri();
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_DBG("Loading, and creating new storage instance for URI [" << LOOLWSD::anonymizeUrl(uriPublic.toString()) << "].");
|
2017-05-12 10:42:03 -05:00
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
_storage = StorageBase::create(uriPublic, jailRoot, jailPath.toString());
|
2016-11-06 12:54:00 -06:00
|
|
|
if (_storage == nullptr)
|
2016-10-26 06:15:28 -05:00
|
|
|
{
|
2016-11-06 12:54:00 -06:00
|
|
|
// We should get an exception, not null.
|
|
|
|
LOG_ERR("Failed to create Storage instance for [" << _docKey << "] in " << jailPath.toString());
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-19 05:21:42 -06:00
|
|
|
firstInstance = true;
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
assert(_storage != nullptr);
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2016-12-13 03:13:58 -06:00
|
|
|
// Call the storage specific fileinfo functions
|
2018-07-08 21:50:09 -05:00
|
|
|
std::string userId, username;
|
2017-05-28 11:20:49 -05:00
|
|
|
std::string userExtraInfo;
|
2017-09-04 08:40:04 -05:00
|
|
|
std::string watermarkText;
|
2016-11-21 03:02:27 -06:00
|
|
|
std::chrono::duration<double> getInfoCallDuration(0);
|
2018-09-05 07:11:05 -05:00
|
|
|
|
2018-10-08 08:09:04 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-01-21 21:43:12 -06:00
|
|
|
WopiStorage* wopiStorage = dynamic_cast<WopiStorage*>(_storage.get());
|
|
|
|
if (wopiStorage != nullptr)
|
2016-11-06 12:54:00 -06:00
|
|
|
{
|
2017-08-16 09:38:00 -05:00
|
|
|
std::unique_ptr<WopiStorage::WOPIFileInfo> wopifileinfo = wopiStorage->getWOPIFileInfo(session->getAuthorization());
|
2018-07-08 21:50:09 -05:00
|
|
|
userId = wopifileinfo->_userId;
|
2016-12-13 03:13:58 -06:00
|
|
|
username = wopifileinfo->_username;
|
2017-05-28 11:20:49 -05:00
|
|
|
userExtraInfo = wopifileinfo->_userExtraInfo;
|
2017-09-04 08:40:04 -05:00
|
|
|
watermarkText = wopifileinfo->_watermarkText;
|
2016-10-25 02:13:00 -05:00
|
|
|
|
2017-06-06 22:43:48 -05:00
|
|
|
if (!wopifileinfo->_userCanWrite ||
|
|
|
|
LOOLWSD::IsViewFileExtension(wopiStorage->getFileExtension()))
|
2016-10-26 06:15:28 -05:00
|
|
|
{
|
2016-11-06 12:54:00 -06:00
|
|
|
LOG_DBG("Setting the session as readonly");
|
2016-11-06 21:11:35 -06:00
|
|
|
session->setReadOnly();
|
2016-10-26 06:15:28 -05:00
|
|
|
}
|
|
|
|
|
2016-11-10 06:21:39 -06:00
|
|
|
// Construct a JSON containing relevant WOPI host properties
|
|
|
|
Object::Ptr wopiInfo = new Object();
|
2016-12-13 03:13:58 -06:00
|
|
|
if (!wopifileinfo->_postMessageOrigin.empty())
|
2016-10-16 11:40:52 -05:00
|
|
|
{
|
2017-06-22 06:34:59 -05:00
|
|
|
// Update the scheme to https if ssl or ssl termination is on
|
|
|
|
if (wopifileinfo->_postMessageOrigin.substr(0, 7) == "http://" &&
|
|
|
|
(LOOLWSD::isSSLEnabled() || LOOLWSD::isSSLTermination()))
|
|
|
|
{
|
|
|
|
wopifileinfo->_postMessageOrigin.replace(0, 4, "https");
|
|
|
|
LOG_DBG("Updating PostMessageOrgin scheme to HTTPS. Updated origin is [" << wopifileinfo->_postMessageOrigin << "].");
|
|
|
|
}
|
|
|
|
|
2016-12-13 03:13:58 -06:00
|
|
|
wopiInfo->set("PostMessageOrigin", wopifileinfo->_postMessageOrigin);
|
2016-10-16 11:40:52 -05:00
|
|
|
}
|
|
|
|
|
2016-12-13 06:31:50 -06:00
|
|
|
// If print, export are disabled, order client to hide these options in the UI
|
|
|
|
if (wopifileinfo->_disablePrint)
|
|
|
|
wopifileinfo->_hidePrintOption = true;
|
|
|
|
if (wopifileinfo->_disableExport)
|
|
|
|
wopifileinfo->_hideExportOption = true;
|
|
|
|
|
2017-10-31 11:58:58 -05:00
|
|
|
wopiInfo->set("BaseFileName", wopiStorage->getFileInfo()._filename);
|
2016-12-13 03:13:58 -06:00
|
|
|
wopiInfo->set("HidePrintOption", wopifileinfo->_hidePrintOption);
|
|
|
|
wopiInfo->set("HideSaveOption", wopifileinfo->_hideSaveOption);
|
|
|
|
wopiInfo->set("HideExportOption", wopifileinfo->_hideExportOption);
|
2016-12-13 06:24:42 -06:00
|
|
|
wopiInfo->set("DisablePrint", wopifileinfo->_disablePrint);
|
|
|
|
wopiInfo->set("DisableExport", wopifileinfo->_disableExport);
|
|
|
|
wopiInfo->set("DisableCopy", wopifileinfo->_disableCopy);
|
2017-08-29 11:59:14 -05:00
|
|
|
wopiInfo->set("DisableInactiveMessages", wopifileinfo->_disableInactiveMessages);
|
2017-10-03 10:06:02 -05:00
|
|
|
wopiInfo->set("UserCanNotWriteRelative", wopifileinfo->_userCanNotWriteRelative);
|
2018-04-24 11:09:37 -05:00
|
|
|
if (wopifileinfo->_hideChangeTrackingControls != WopiStorage::WOPIFileInfo::TriState::Unset)
|
|
|
|
wopiInfo->set("HideChangeTrackingControls", wopifileinfo->_hideChangeTrackingControls == WopiStorage::WOPIFileInfo::TriState::True);
|
2016-11-10 06:21:39 -06:00
|
|
|
|
|
|
|
std::ostringstream ossWopiInfo;
|
|
|
|
wopiInfo->stringify(ossWopiInfo);
|
2018-07-17 01:01:05 -05:00
|
|
|
const std::string wopiInfoString = ossWopiInfo.str();
|
|
|
|
LOG_TRC("Sending wopi info to client: " << wopiInfoString);
|
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
// Contains PostMessageOrigin property which is necessary to post messages to parent
|
|
|
|
// frame. Important to send this message immediately and not enqueue it so that in case
|
|
|
|
// document load fails, loleaflet is able to tell its parent frame via PostMessage API.
|
2018-07-17 01:01:05 -05:00
|
|
|
session->sendMessage("wopi: " + wopiInfoString);
|
2016-11-10 06:21:39 -06:00
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
// Mark the session as 'Document owner' if WOPI hosts supports it
|
2018-07-08 21:50:09 -05:00
|
|
|
if (userId == _storage->getFileInfo()._ownerId)
|
2016-11-08 07:37:28 -06:00
|
|
|
{
|
2017-05-20 12:28:09 -05:00
|
|
|
LOG_DBG("Session [" << sessionId << "] is the document owner");
|
2016-11-08 07:37:28 -06:00
|
|
|
session->setDocumentOwner(true);
|
|
|
|
}
|
|
|
|
|
2016-12-13 03:13:58 -06:00
|
|
|
getInfoCallDuration = wopifileinfo->_callDuration;
|
|
|
|
|
|
|
|
// Pass the ownership to client session
|
|
|
|
session->setWopiFileInfo(wopifileinfo);
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
2017-01-21 21:43:12 -06:00
|
|
|
else
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-11-06 12:54:00 -06:00
|
|
|
{
|
2017-01-21 21:43:12 -06:00
|
|
|
LocalStorage* localStorage = dynamic_cast<LocalStorage*>(_storage.get());
|
|
|
|
if (localStorage != nullptr)
|
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
std::unique_ptr<LocalStorage::LocalFileInfo> localfileinfo = localStorage->getLocalFileInfo();
|
2018-07-08 21:50:09 -05:00
|
|
|
userId = localfileinfo->_userId;
|
2017-01-21 21:43:12 -06:00
|
|
|
username = localfileinfo->_username;
|
2017-06-14 06:51:53 -05:00
|
|
|
|
|
|
|
if (LOOLWSD::IsViewFileExtension(localStorage->getFileExtension()))
|
|
|
|
{
|
|
|
|
LOG_DBG("Setting the session as readonly");
|
|
|
|
session->setReadOnly();
|
|
|
|
}
|
2017-01-21 21:43:12 -06:00
|
|
|
}
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
2016-10-14 05:09:43 -05:00
|
|
|
|
2018-07-08 21:50:09 -05:00
|
|
|
|
2017-10-03 22:54:05 -05:00
|
|
|
#if ENABLE_SUPPORT_KEY
|
|
|
|
if (!LOOLWSD::OverrideWatermark.empty())
|
|
|
|
watermarkText = LOOLWSD::OverrideWatermark;
|
|
|
|
#endif
|
|
|
|
|
2018-06-10 10:42:15 -05:00
|
|
|
LOG_DBG("Setting username [" << LOOLWSD::anonymizeUsername(username) << "] and userId [" <<
|
2018-07-08 21:50:09 -05:00
|
|
|
LOOLWSD::anonymizeUsername(userId) << "] for session [" << sessionId << "]");
|
|
|
|
|
|
|
|
session->setUserId(userId);
|
2016-11-06 21:11:35 -06:00
|
|
|
session->setUserName(username);
|
2017-05-28 11:20:49 -05:00
|
|
|
session->setUserExtraInfo(userExtraInfo);
|
2017-09-04 08:40:04 -05:00
|
|
|
session->setWatermarkText(watermarkText);
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-12-22 06:48:20 -06:00
|
|
|
// Basic file information was stored by the above getWOPIFileInfo() or getLocalFileInfo() calls
|
2018-02-07 03:17:59 -06:00
|
|
|
const StorageBase::FileInfo fileInfo = _storage->getFileInfo();
|
2016-11-06 12:54:00 -06:00
|
|
|
if (!fileInfo.isValid())
|
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
LOG_ERR("Invalid fileinfo for URI [" << session->getPublicUri().toString() << "].");
|
2016-11-06 12:54:00 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-25 19:44:24 -05:00
|
|
|
|
2016-12-19 05:21:42 -06:00
|
|
|
if (firstInstance)
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
{
|
|
|
|
_documentLastModifiedTime = fileInfo._modifiedTime;
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_DBG("Document timestamp: " << _documentLastModifiedTime);
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check if document has been modified by some external action
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_TRC("Document modified time: " << fileInfo._modifiedTime);
|
2017-04-09 17:37:27 -05:00
|
|
|
static const Poco::Timestamp Zero(Poco::Timestamp::fromEpochTime(0));
|
|
|
|
if (_documentLastModifiedTime != Zero &&
|
|
|
|
fileInfo._modifiedTime != Zero &&
|
2016-12-21 06:06:07 -06:00
|
|
|
_documentLastModifiedTime != fileInfo._modifiedTime)
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
{
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_DBG("Document " << _docKey << "] has been modified behind our back. " <<
|
2018-01-14 19:39:06 -06:00
|
|
|
"Informing all clients. Expected: " << _documentLastModifiedTime <<
|
|
|
|
", Actual: " << fileInfo._modifiedTime);
|
2017-06-10 09:45:00 -05:00
|
|
|
|
2017-06-01 07:56:54 -05:00
|
|
|
_documentChangedInStorage = true;
|
2017-09-18 12:30:19 -05:00
|
|
|
std::string message = "close: documentconflict";
|
2017-06-19 09:30:37 -05:00
|
|
|
if (_isModified)
|
2017-09-18 12:30:19 -05:00
|
|
|
message = "error: cmd=storage kind=documentconflict";
|
|
|
|
|
|
|
|
session->sendTextFrame(message);
|
|
|
|
broadcastMessage(message);
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
}
|
|
|
|
}
|
2016-12-19 05:21:42 -06:00
|
|
|
|
2017-01-21 21:43:12 -06:00
|
|
|
// Let's load the document now, if not loaded.
|
|
|
|
if (!_storage->isLoaded())
|
2016-11-06 12:54:00 -06:00
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
std::string localPath = _storage->loadStorageFileToLocal(session->getAuthorization());
|
2017-11-07 07:15:36 -06:00
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-11-07 07:15:36 -06:00
|
|
|
// Check if we have a prefilter "plugin" for this document format
|
|
|
|
for (const auto& plugin : LOOLWSD::PluginConfigurations)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
const std::string extension(plugin->getString("prefilter.extension"));
|
|
|
|
const std::string newExtension(plugin->getString("prefilter.newextension"));
|
|
|
|
const std::string commandLine(plugin->getString("prefilter.commandline"));
|
2017-11-07 07:15:36 -06:00
|
|
|
|
|
|
|
if (localPath.length() > extension.length()+1 &&
|
|
|
|
strcasecmp(localPath.substr(localPath.length() - extension.length() -1).data(), (std::string(".") + extension).data()) == 0)
|
|
|
|
{
|
|
|
|
// Extension matches, try the conversion. We convert the file to another one in
|
|
|
|
// the same (jail) directory, with just the new extension tacked on.
|
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
const std::string newRootPath = _storage->getRootFilePath() + "." + newExtension;
|
2017-11-07 07:15:36 -06:00
|
|
|
|
|
|
|
// The commandline must contain the space-separated substring @INPUT@ that is
|
|
|
|
// replaced with the input file name, and @OUTPUT@ for the output file name.
|
|
|
|
Poco::StringTokenizer tokenizer(commandLine, " ");
|
|
|
|
if (tokenizer.replace("@INPUT@", _storage->getRootFilePath()) != 1 ||
|
|
|
|
tokenizer.replace("@OUTPUT@", newRootPath) != 1)
|
|
|
|
throw Poco::NotFoundException();
|
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
|
|
|
|
std::vector<std::string> args;
|
|
|
|
for (std::size_t i = 1; i < tokenizer.count(); ++i)
|
|
|
|
args.emplace_back(tokenizer[i]);
|
2017-11-07 07:15:36 -06:00
|
|
|
|
2018-01-29 09:13:54 -06:00
|
|
|
int process = Util::spawnProcess(tokenizer[0], args);
|
|
|
|
int status = -1;
|
|
|
|
const int rc = ::waitpid(process, &status, 0);
|
2017-11-07 07:15:36 -06:00
|
|
|
if (rc != 0)
|
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_ERR("Conversion from " << extension << " to " << newExtension << " failed (" << rc << ").");
|
2017-11-07 07:15:36 -06:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 19:39:06 -06:00
|
|
|
|
2017-11-07 07:15:36 -06:00
|
|
|
_storage->setRootFilePath(newRootPath);
|
|
|
|
localPath += "." + newExtension;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We successfully converted the file to something LO can use; break out of the for
|
|
|
|
// loop.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (const Poco::NotFoundException&)
|
|
|
|
{
|
|
|
|
// This plugin is not a proper prefilter one
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-01-22 22:12:51 -06:00
|
|
|
|
|
|
|
std::ifstream istr(localPath, std::ios::binary);
|
|
|
|
Poco::SHA1Engine sha1;
|
|
|
|
Poco::DigestOutputStream dos(sha1);
|
|
|
|
Poco::StreamCopier::copyStream(istr, dos);
|
|
|
|
dos.close();
|
2018-06-10 19:24:04 -05:00
|
|
|
LOG_INF("SHA1 for DocKey [" << _docKey << "] of [" << LOOLWSD::anonymizeUrl(localPath) << "]: " <<
|
2017-02-05 18:35:54 -06:00
|
|
|
Poco::DigestEngine::digestToHex(sha1.digest()));
|
2017-01-22 22:12:51 -06:00
|
|
|
|
2018-02-17 17:24:08 -06:00
|
|
|
// LibreOffice can't open files with '#' in the name
|
|
|
|
std::string localPathEncoded;
|
2018-06-10 10:41:17 -05:00
|
|
|
Poco::URI::encode(localPath, "#", localPathEncoded);
|
|
|
|
_uriJailed = Poco::URI(Poco::URI("file://"), localPathEncoded).toString();
|
|
|
|
_uriJailedAnonym = Poco::URI(Poco::URI("file://"), LOOLWSD::anonymizeUrl(localPathEncoded)).toString();
|
2018-02-17 17:24:08 -06:00
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
_filename = fileInfo._filename;
|
|
|
|
|
|
|
|
// Use the local temp file's timestamp.
|
2017-03-21 21:56:16 -05:00
|
|
|
_lastFileModifiedTime = Poco::File(_storage->getRootFilePath()).getLastModified();
|
2018-07-04 23:07:43 -05:00
|
|
|
_tileCache.reset(new TileCache(_storage->getUri(), _lastFileModifiedTime, _cacheRoot, LOOLWSD::TileCachePersistent));
|
2017-06-22 07:58:50 -05:00
|
|
|
_tileCache->setThreadOwner(std::this_thread::get_id());
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-02-10 00:09:01 -06:00
|
|
|
LOOLWSD::dumpNewSessionTrace(getJailId(), sessionId, _uriOrig, _storage->getRootFilePath());
|
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
// Since document has been loaded, send the stats if its WOPI
|
2017-01-21 21:43:12 -06:00
|
|
|
if (wopiStorage != nullptr)
|
2016-11-06 12:54:00 -06:00
|
|
|
{
|
|
|
|
// Get the time taken to load the file from storage
|
2017-01-21 21:43:12 -06:00
|
|
|
auto callDuration = wopiStorage->getWopiLoadDuration();
|
2016-11-06 12:54:00 -06:00
|
|
|
// Add the time taken to check file info
|
|
|
|
callDuration += getInfoCallDuration;
|
|
|
|
const std::string msg = "stats: wopiloadduration " + std::to_string(callDuration.count());
|
|
|
|
LOG_TRC("Sending to Client [" << msg << "].");
|
2016-11-06 21:11:35 -06:00
|
|
|
session->sendTextFrame(msg);
|
2016-04-07 15:59:27 -05:00
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-11-06 12:54:00 -06:00
|
|
|
return true;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2017-04-11 01:54:09 -05:00
|
|
|
bool DocumentBroker::saveToStorage(const std::string& sessionId,
|
2017-06-01 09:16:03 -05:00
|
|
|
bool success, const std::string& result, bool force)
|
2017-03-09 22:50:36 -06:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-03-29 21:50:29 -05:00
|
|
|
|
2017-06-01 09:16:03 -05:00
|
|
|
if (force)
|
|
|
|
{
|
|
|
|
LOG_TRC("Document will be saved forcefully to storage.");
|
|
|
|
_storage->forceSave();
|
|
|
|
}
|
2018-01-14 10:16:10 -06:00
|
|
|
|
2017-03-09 22:50:36 -06:00
|
|
|
const bool res = saveToStorageInternal(sessionId, success, result);
|
|
|
|
|
2017-04-20 21:43:27 -05:00
|
|
|
// If marked to destroy, or session is disconnected, remove.
|
|
|
|
const auto it = _sessions.find(sessionId);
|
|
|
|
if (_markToDestroy || (it != _sessions.end() && it->second->isCloseFrame()))
|
|
|
|
removeSessionInternal(sessionId);
|
2017-04-19 23:10:30 -05:00
|
|
|
|
2017-03-09 22:50:36 -06:00
|
|
|
// If marked to destroy, then this was the last session.
|
2017-04-19 23:10:30 -05:00
|
|
|
if (_markToDestroy || _sessions.empty())
|
2017-03-09 22:50:36 -06:00
|
|
|
{
|
|
|
|
// Stop so we get cleaned up and removed.
|
|
|
|
_stop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-20 11:12:05 -05:00
|
|
|
bool DocumentBroker::saveAsToStorage(const std::string& sessionId, const std::string& saveAsPath, const std::string& saveAsFilename)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
return saveToStorageInternal(sessionId, true, "", saveAsPath, saveAsFilename);
|
|
|
|
}
|
|
|
|
|
2017-03-09 22:50:36 -06:00
|
|
|
bool DocumentBroker::saveToStorageInternal(const std::string& sessionId,
|
2017-10-20 11:12:05 -05:00
|
|
|
bool success, const std::string& result,
|
|
|
|
const std::string& saveAsPath, const std::string& saveAsFilename)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-04-10 21:07:09 -05:00
|
|
|
|
2018-01-14 17:59:11 -06:00
|
|
|
// Record that we got a response to avoid timeing out on saving.
|
|
|
|
_lastSaveResponseTime = std::chrono::steady_clock::now();
|
|
|
|
|
2017-10-20 11:12:05 -05:00
|
|
|
const bool isSaveAs = !saveAsPath.empty();
|
|
|
|
|
2017-01-01 11:08:12 -06:00
|
|
|
// If save requested, but core didn't save because document was unmodified
|
|
|
|
// notify the waiting thread, if any.
|
2017-03-21 21:56:16 -05:00
|
|
|
LOG_TRC("Saving to storage docKey [" << _docKey << "] for session [" << sessionId <<
|
|
|
|
"]. Success: " << success << ", result: " << result);
|
2017-01-01 11:08:12 -06:00
|
|
|
if (!success && result == "unmodified")
|
|
|
|
{
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_DBG("Save skipped as document [" << _docKey << "] was not modified.");
|
2017-01-01 18:42:19 -06:00
|
|
|
_lastSaveTime = std::chrono::steady_clock::now();
|
2017-03-09 12:19:53 -06:00
|
|
|
_poll->wakeup();
|
2017-01-01 11:08:12 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
const auto it = _sessions.find(sessionId);
|
|
|
|
if (it == _sessions.end())
|
|
|
|
{
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_ERR("Session with sessionId [" << sessionId << "] not found while saving docKey [" << _docKey << "].");
|
2016-10-16 11:40:52 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-07 06:12:23 -06:00
|
|
|
// Check that we are actually about to upload a successfully saved document.
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot save docKey [" << _docKey << "], the .uno:Save has failed in LOK.");
|
|
|
|
it->second->sendTextFrame("error: cmd=storage kind=savefailed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-16 09:38:00 -05:00
|
|
|
const Authorization auth = it->second->getAuthorization();
|
2018-06-10 19:55:52 -05:00
|
|
|
const std::string uri = isSaveAs ? saveAsPath : it->second->getPublicUri().toString();
|
|
|
|
const std::string uriAnonym = LOOLWSD::anonymizeUrl(uri);
|
2016-04-27 19:52:32 -05:00
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
// If the file timestamp hasn't changed, skip saving.
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::Timestamp newFileModifiedTime = Poco::File(_storage->getRootFilePath()).getLastModified();
|
2018-01-14 16:17:00 -06:00
|
|
|
if (!isSaveAs && newFileModifiedTime == _lastFileModifiedTime)
|
2016-04-25 19:44:24 -05:00
|
|
|
{
|
|
|
|
// Nothing to do.
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_DBG("Skipping unnecessary saving to URI [" << uriAnonym << "] with docKey [" << _docKey <<
|
2017-01-01 14:39:23 -06:00
|
|
|
"]. File last modified " << _lastFileModifiedTime.elapsed() / 1000000 << " seconds ago.");
|
2017-03-09 12:19:53 -06:00
|
|
|
_poll->wakeup();
|
2016-04-25 19:44:24 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_DBG("Persisting [" << _docKey << "] after saving to URI [" << uriAnonym << "].");
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
assert(_storage && _tileCache);
|
2017-10-20 11:12:05 -05:00
|
|
|
StorageBase::SaveResult storageSaveResult = _storage->saveLocalFileToStorage(auth, saveAsPath, saveAsFilename);
|
2017-10-25 07:09:27 -05:00
|
|
|
if (storageSaveResult.getResult() == StorageBase::SaveResult::OK)
|
2016-03-26 08:10:53 -05:00
|
|
|
{
|
2017-10-20 11:12:05 -05:00
|
|
|
if (!isSaveAs)
|
|
|
|
{
|
2018-01-14 17:59:11 -06:00
|
|
|
// Saved and stored; update flags.
|
2017-10-20 11:12:05 -05:00
|
|
|
setModified(false);
|
|
|
|
_lastFileModifiedTime = newFileModifiedTime;
|
|
|
|
_tileCache->saveLastModified(_lastFileModifiedTime);
|
|
|
|
_lastSaveTime = std::chrono::steady_clock::now();
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
|
2018-01-14 17:59:11 -06:00
|
|
|
// Save the storage timestamp.
|
2017-10-20 11:12:05 -05:00
|
|
|
_documentLastModifiedTime = _storage->getFileInfo()._modifiedTime;
|
2017-06-01 11:11:11 -05:00
|
|
|
|
2017-10-20 11:12:05 -05:00
|
|
|
// After a successful save, we are sure that document in the storage is same as ours
|
|
|
|
_documentChangedInStorage = false;
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_DBG("Saved docKey [" << _docKey << "] to URI [" << uriAnonym << "] and updated timestamps. " <<
|
2018-01-14 19:39:06 -06:00
|
|
|
" Document modified timestamp: " << _documentLastModifiedTime);
|
2018-01-14 17:59:11 -06:00
|
|
|
|
|
|
|
// Resume polling.
|
|
|
|
_poll->wakeup();
|
2017-10-20 11:12:05 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
// normalize the url (mainly to " " -> "%20")
|
2018-06-10 19:55:52 -05:00
|
|
|
const std::string url = Poco::URI(storageSaveResult.getSaveAsUrl()).toString();
|
|
|
|
|
|
|
|
const std::string filename = storageSaveResult.getSaveAsName();
|
2017-10-25 07:09:27 -05:00
|
|
|
|
|
|
|
// encode the name
|
|
|
|
std::string encodedName;
|
2018-06-10 19:55:52 -05:00
|
|
|
Poco::URI::encode(filename, "", encodedName);
|
|
|
|
const std::string filenameAnonym = LOOLWSD::anonymizeUrl(filename);
|
2017-10-25 07:09:27 -05:00
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "saveas: url=" << url << " filename=" << encodedName
|
|
|
|
<< " xfilename=" << filenameAnonym;
|
|
|
|
it->second->sendTextFrame(oss.str());
|
2017-10-25 07:09:27 -05:00
|
|
|
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_DBG("Saved As docKey [" << _docKey << "] to URI [" << LOOLWSD::anonymizeUrl(url) <<
|
2018-06-10 19:55:52 -05:00
|
|
|
"] with name [" << filenameAnonym << "] successfully.");
|
2017-10-20 11:12:05 -05:00
|
|
|
}
|
2018-06-10 19:55:52 -05:00
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-25 07:09:27 -05:00
|
|
|
else if (storageSaveResult.getResult() == StorageBase::SaveResult::DISKFULL)
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_WRN("Disk full while saving docKey [" << _docKey << "] to URI [" << uriAnonym <<
|
2017-01-01 11:06:05 -06:00
|
|
|
"]. Making all sessions on doc read-only and notifying clients.");
|
|
|
|
|
|
|
|
// Make everyone readonly and tell everyone that storage is low on diskspace.
|
|
|
|
for (const auto& sessionIt : _sessions)
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
|
|
|
sessionIt.second->setReadOnly();
|
|
|
|
sessionIt.second->sendTextFrame("error: cmd=storage kind=savediskfull");
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 07:09:27 -05:00
|
|
|
else if (storageSaveResult.getResult() == StorageBase::SaveResult::UNAUTHORIZED)
|
2017-05-31 01:21:12 -05:00
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_ERR("Cannot save docKey [" << _docKey << "] to storage URI [" << uriAnonym <<
|
|
|
|
"]. Invalid or expired access token. Notifying client.");
|
2017-05-31 01:21:12 -05:00
|
|
|
it->second->sendTextFrame("error: cmd=storage kind=saveunauthorized");
|
|
|
|
}
|
2017-10-25 07:09:27 -05:00
|
|
|
else if (storageSaveResult.getResult() == StorageBase::SaveResult::FAILED)
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2017-01-01 11:06:05 -06:00
|
|
|
//TODO: Should we notify all clients?
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_ERR("Failed to save docKey [" << _docKey << "] to URI [" << uriAnonym << "]. Notifying client.");
|
2016-11-23 06:09:54 -06:00
|
|
|
it->second->sendTextFrame("error: cmd=storage kind=savefailed");
|
|
|
|
}
|
2017-10-25 07:09:27 -05:00
|
|
|
else if (storageSaveResult.getResult() == StorageBase::SaveResult::DOC_CHANGED)
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
{
|
|
|
|
LOG_ERR("PutFile says that Document changed in storage");
|
2017-06-01 07:56:54 -05:00
|
|
|
_documentChangedInStorage = true;
|
2018-02-01 11:13:47 -06:00
|
|
|
std::string message = "close: documentconflict";
|
2017-06-19 09:30:37 -05:00
|
|
|
if (_isModified)
|
2018-02-01 11:13:47 -06:00
|
|
|
message = "error: cmd=storage kind=documentconflict";
|
|
|
|
|
|
|
|
broadcastMessage(message);
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
}
|
2016-03-26 08:10:53 -05:00
|
|
|
|
|
|
|
return false;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 16:01:27 -06:00
|
|
|
void DocumentBroker::setLoaded()
|
|
|
|
{
|
2017-03-25 12:57:47 -05:00
|
|
|
if (!_isLoaded)
|
|
|
|
{
|
|
|
|
_isLoaded = true;
|
|
|
|
_loadDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now() - _threadStart);
|
|
|
|
LOG_TRC("Document loaded in " << _loadDuration.count() << "ms");
|
|
|
|
}
|
2017-03-11 16:01:27 -06:00
|
|
|
}
|
|
|
|
|
2017-03-09 12:19:53 -06:00
|
|
|
bool DocumentBroker::autoSave(const bool force)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 23:11:06 -05:00
|
|
|
|
2018-02-01 22:28:20 -06:00
|
|
|
LOG_TRC("autoSave(): forceful? " << force);
|
2016-05-09 00:11:09 -05:00
|
|
|
if (_sessions.empty() || _storage == nullptr || !_isLoaded ||
|
2016-10-15 16:07:40 -05:00
|
|
|
!_childProcess->isAlive() || (!_isModified && !force))
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-04-29 22:07:09 -05:00
|
|
|
// Nothing to do.
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Nothing to autosave [" << _docKey << "].");
|
2017-03-12 13:13:48 -05:00
|
|
|
return false;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
|
|
|
|
2017-03-09 12:19:53 -06:00
|
|
|
// Remember the last save time, since this is the predicate.
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Checking to autosave [" << _docKey << "].");
|
2016-05-09 00:11:09 -05:00
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
// Which session to use when auto saving ?
|
|
|
|
std::string savingSessionId;
|
|
|
|
for (auto& sessionIt : _sessions)
|
|
|
|
{
|
2018-01-14 23:49:14 -06:00
|
|
|
// Save the document using an editable session, or first ...
|
|
|
|
if (savingSessionId.empty() || !sessionIt.second->isReadOnly())
|
2017-05-15 00:59:04 -05:00
|
|
|
{
|
|
|
|
savingSessionId = sessionIt.second->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
// or if any of the sessions is document owner, use that.
|
|
|
|
if (sessionIt.second->isDocumentOwner())
|
|
|
|
{
|
|
|
|
savingSessionId = sessionIt.second->getId();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
bool sent = false;
|
2016-05-02 21:58:53 -05:00
|
|
|
if (force)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Sending forced save command for [" << _docKey << "].");
|
2018-01-11 00:29:08 -06:00
|
|
|
// Don't terminate editing as this can be invoked by the admin OOM, but otherwise force saving anyway.
|
2018-02-01 12:28:43 -06:00
|
|
|
sent = sendUnoSave(savingSessionId, /*dontTerminateEdit=*/ true, /*dontSaveIfUnmodified=*/ true, /*isAutosave=*/ false);
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
2016-05-02 21:58:53 -05:00
|
|
|
else if (_isModified)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
|
|
|
|
const std::chrono::milliseconds::rep inactivityTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastActivityTime).count();
|
|
|
|
const std::chrono::milliseconds::rep timeSinceLastSaveMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastSaveTime).count();
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_TRC("Time since last save of docKey [" << _docKey << "] is " << timeSinceLastSaveMs <<
|
2017-01-01 18:42:19 -06:00
|
|
|
"ms and most recent activity was " << inactivityTimeMs << "ms ago.");
|
2016-04-10 11:30:33 -05:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
static const int idleSaveDurationMs = LOOLWSD::getConfigValue<int>("per_document.idlesave_duration_secs", 30) * 1000;
|
|
|
|
static const int autoSaveDurationMs = LOOLWSD::getConfigValue<int>("per_document.autosave_duration_secs", 300) * 1000;
|
2016-05-02 21:58:53 -05:00
|
|
|
// Either we've been idle long enough, or it's auto-save time.
|
2017-08-24 06:10:39 -05:00
|
|
|
if (inactivityTimeMs >= idleSaveDurationMs ||
|
|
|
|
timeSinceLastSaveMs >= autoSaveDurationMs)
|
2016-04-25 19:48:02 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Sending timed save command for [" << _docKey << "].");
|
2017-10-03 04:59:39 -05:00
|
|
|
sent = sendUnoSave(savingSessionId, /*dontTerminateEdit=*/ true, /*dontSaveIfUnmodified=*/ true, /*isAutosave=*/ true);
|
2016-04-25 19:48:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sent;
|
2016-04-10 21:07:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-03 04:59:39 -05:00
|
|
|
bool DocumentBroker::sendUnoSave(const std::string& sessionId, bool dontTerminateEdit, bool dontSaveIfUnmodified, bool isAutosave)
|
2016-04-10 21:07:09 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 23:11:06 -05:00
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
LOG_INF("Saving doc [" << _docKey << "].");
|
2016-04-10 21:07:09 -05:00
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
if (_sessions.find(sessionId) != _sessions.end())
|
2016-04-10 21:07:09 -05:00
|
|
|
{
|
2016-09-19 02:03:31 -05:00
|
|
|
// Invalidate the timestamp to force persisting.
|
2016-12-19 04:34:13 -06:00
|
|
|
_lastFileModifiedTime = Poco::Timestamp::fromEpochTime(0);
|
2016-09-19 02:03:31 -05:00
|
|
|
|
|
|
|
// We do not want save to terminate editing mode if we are in edit mode now
|
2016-05-09 20:15:09 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
// arguments init
|
|
|
|
oss << "{";
|
2016-09-01 07:25:32 -05:00
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
if (dontTerminateEdit)
|
|
|
|
{
|
|
|
|
oss << "\"DontTerminateEdit\":"
|
|
|
|
<< "{"
|
|
|
|
<< "\"type\":\"boolean\","
|
|
|
|
<< "\"value\":true"
|
|
|
|
<< "}";
|
|
|
|
}
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
if (dontSaveIfUnmodified)
|
|
|
|
{
|
2017-05-15 00:59:04 -05:00
|
|
|
if (dontTerminateEdit)
|
|
|
|
oss << ",";
|
|
|
|
|
|
|
|
oss << "\"DontSaveIfUnmodified\":"
|
2016-07-14 04:49:21 -05:00
|
|
|
<< "{"
|
|
|
|
<< "\"type\":\"boolean\","
|
|
|
|
<< "\"value\":true"
|
|
|
|
<< "}";
|
2016-09-19 02:03:31 -05:00
|
|
|
}
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
// arguments end
|
|
|
|
oss << "}";
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2017-09-25 12:16:48 -05:00
|
|
|
assert(_storage);
|
2018-09-13 11:16:00 -05:00
|
|
|
_storage->setIsAutosave(isAutosave || UnitWSD::get().isAutosave());
|
2017-09-25 12:16:48 -05:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string saveArgs = oss.str();
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC(".uno:Save arguments: " << saveArgs);
|
2016-08-31 22:34:41 -05:00
|
|
|
const auto command = "uno .uno:Save " + saveArgs;
|
2017-05-15 00:59:04 -05:00
|
|
|
forwardToChild(sessionId, command);
|
2017-03-20 20:57:21 -05:00
|
|
|
_lastSaveRequestTime = std::chrono::steady_clock::now();
|
2016-09-19 02:03:31 -05:00
|
|
|
return true;
|
2016-04-10 21:07:09 -05:00
|
|
|
}
|
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
LOG_ERR("Failed to save doc [" << _docKey << "]: No valid sessions.");
|
2016-04-25 19:48:02 -05:00
|
|
|
return false;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string DocumentBroker::getJailRoot() const
|
|
|
|
{
|
|
|
|
assert(!_jailId.empty());
|
|
|
|
return Poco::Path(_childRoot, _jailId).toString();
|
|
|
|
}
|
|
|
|
|
2017-03-25 12:56:17 -05:00
|
|
|
size_t DocumentBroker::addSession(const std::shared_ptr<ClientSession>& session)
|
2017-05-22 14:04:35 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return addSessionInternal(session);
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_ERR("Failed to add session to [" << _docKey << "] with URI [" << LOOLWSD::anonymizeUrl(session->getPublicUri().toString()) << "]: " << exc.what());
|
2017-05-22 14:04:35 -05:00
|
|
|
if (_sessions.empty())
|
|
|
|
{
|
|
|
|
LOG_INF("Doc [" << _docKey << "] has no more sessions. Marking to destroy.");
|
|
|
|
_markToDestroy = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DocumentBroker::addSessionInternal(const std::shared_ptr<ClientSession>& session)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-01-10 16:10:10 -06:00
|
|
|
|
2016-11-06 21:11:59 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// First load the document, since this can fail.
|
2017-05-07 10:05:34 -05:00
|
|
|
if (!load(session, _childProcess->getJailId()))
|
2016-10-18 15:51:11 -05:00
|
|
|
{
|
2016-10-20 18:06:00 -05:00
|
|
|
const auto msg = "Failed to load document with URI [" + session->getPublicUri().toString() + "].";
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR(msg);
|
2016-10-20 18:06:00 -05:00
|
|
|
throw std::runtime_error(msg);
|
2016-10-19 05:21:29 -05:00
|
|
|
}
|
2016-10-12 22:04:07 -05:00
|
|
|
}
|
|
|
|
catch (const StorageSpaceLowException&)
|
|
|
|
{
|
2016-11-06 21:11:59 -06:00
|
|
|
LOG_ERR("Out of storage while loading document with URI [" << session->getPublicUri().toString() << "].");
|
|
|
|
|
2016-10-12 22:04:07 -05:00
|
|
|
// We use the same message as is sent when some of lool's own locations are full,
|
|
|
|
// even if in this case it might be a totally different location (file system, or
|
|
|
|
// some other type of storage somewhere). This message is not sent to all clients,
|
|
|
|
// though, just to all sessions of this document.
|
2016-11-17 08:00:05 -06:00
|
|
|
alertAllUsers("internal", "diskfull");
|
2016-10-12 22:04:07 -05:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string id = session->getId();
|
2016-11-06 22:14:23 -06:00
|
|
|
|
|
|
|
// Request a new session from the child kit.
|
2018-07-10 22:09:27 -05:00
|
|
|
const std::string aMessage = "session " + id + ' ' + _docKey + ' ' + _docId;
|
2016-11-06 22:14:23 -06:00
|
|
|
_childProcess->sendTextFrame(aMessage);
|
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2016-10-22 09:25:57 -05:00
|
|
|
// Tell the admin console about this new doc
|
2017-06-23 07:32:44 -05:00
|
|
|
Admin::instance().addDoc(_docKey, getPid(), getFilename(), id, session->getUserName(), session->getUserId());
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-10-22 09:25:57 -05:00
|
|
|
|
2017-05-22 14:04:35 -05:00
|
|
|
// Add and attach the session.
|
|
|
|
_sessions.emplace(session->getId(), session);
|
|
|
|
session->setAttached();
|
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t count = _sessions.size();
|
2017-01-02 23:12:15 -06:00
|
|
|
LOG_TRC("Added " << (session->isReadOnly() ? "readonly" : "non-readonly") <<
|
|
|
|
" session [" << id << "] to docKey [" <<
|
|
|
|
_docKey << "] to have " << count << " sessions.");
|
2016-10-12 22:04:07 -05:00
|
|
|
|
2016-11-06 22:14:23 -06:00
|
|
|
return count;
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
size_t DocumentBroker::removeSession(const std::string& id)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-03-09 12:19:53 -06:00
|
|
|
|
2016-11-27 20:28:21 -06:00
|
|
|
try
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2018-01-14 16:17:00 -06:00
|
|
|
const auto it = _sessions.find(id);
|
|
|
|
if (it == _sessions.end())
|
|
|
|
{
|
|
|
|
LOG_ERR("Invalid or unknown session [" << id << "] to remove.");
|
|
|
|
return _sessions.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last view going away, can destroy.
|
|
|
|
_markToDestroy = (_sessions.size() <= 1);
|
|
|
|
|
|
|
|
const bool lastEditableSession = !it->second->isReadOnly() && !haveAnotherEditableSession(id);
|
|
|
|
|
2017-01-08 11:22:21 -06:00
|
|
|
LOG_INF("Removing session [" << id << "] on docKey [" << _docKey <<
|
2017-04-19 23:10:30 -05:00
|
|
|
"]. Have " << _sessions.size() << " sessions. markToDestroy: " << _markToDestroy <<
|
2018-01-14 16:17:00 -06:00
|
|
|
", LastEditableSession: " << lastEditableSession);
|
2017-01-08 11:22:21 -06:00
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
// If last editable, save and don't remove until after uploading to storage.
|
2018-01-14 23:49:14 -06:00
|
|
|
if (!lastEditableSession || !autoSave(isPossiblyModified()))
|
2018-01-14 16:17:00 -06:00
|
|
|
removeSessionInternal(id);
|
2017-03-09 22:50:36 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Error while removing session [" << id << "]: " << ex.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return _sessions.size();
|
|
|
|
}
|
|
|
|
|
2017-04-11 01:54:09 -05:00
|
|
|
size_t DocumentBroker::removeSessionInternal(const std::string& id)
|
2017-03-09 22:50:36 -06:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-03-09 22:50:36 -06:00
|
|
|
try
|
|
|
|
{
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2016-11-27 20:28:21 -06:00
|
|
|
Admin::instance().rmDoc(_docKey, id);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-11-27 20:28:21 -06:00
|
|
|
auto it = _sessions.find(id);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-02-05 20:59:08 -06:00
|
|
|
LOOLWSD::dumpEndSessionTrace(getJailId(), id, _uriOrig);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-02-05 18:35:54 -06:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const bool readonly = (it->second ? it->second->isReadOnly() : false);
|
2017-04-06 12:49:44 -05:00
|
|
|
|
2017-06-19 20:53:58 -05:00
|
|
|
// Remove. The caller must have a reference to the session
|
|
|
|
// in question, lest we destroy from underneith them.
|
2016-11-27 20:28:21 -06:00
|
|
|
_sessions.erase(it);
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t count = _sessions.size();
|
2018-01-14 10:16:10 -06:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
Log::StreamLogger logger = Log::trace();
|
2018-01-14 10:16:10 -06:00
|
|
|
if (logger.enabled())
|
2017-04-04 23:26:09 -05:00
|
|
|
{
|
2018-01-14 10:16:10 -06:00
|
|
|
logger << "Removed " << (readonly ? "readonly" : "non-readonly")
|
|
|
|
<< " session [" << id << "] from docKey ["
|
|
|
|
<< _docKey << "] to have " << count << " sessions:";
|
|
|
|
for (const auto& pair : _sessions)
|
|
|
|
logger << pair.second->getId() << ' ';
|
|
|
|
|
2018-06-04 09:23:58 -05:00
|
|
|
LOG_END(logger, true);
|
2017-04-04 23:26:09 -05:00
|
|
|
}
|
2017-02-04 14:46:52 -06:00
|
|
|
|
|
|
|
// Let the child know the client has disconnected.
|
|
|
|
const std::string msg("child-" + id + " disconnect");
|
|
|
|
_childProcess->sendTextFrame(msg);
|
|
|
|
|
2017-01-02 23:12:15 -06:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-08 11:22:21 -06:00
|
|
|
LOG_TRC("Session [" << id << "] not found to remove from docKey [" <<
|
|
|
|
_docKey << "]. Have " << _sessions.size() << " sessions.");
|
2016-11-27 20:28:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Error while removing session [" << id << "]: " << ex.what());
|
|
|
|
}
|
2016-10-22 09:25:57 -05:00
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
return _sessions.size();
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
|
2017-04-11 01:54:09 -05:00
|
|
|
void DocumentBroker::addCallback(const SocketPoll::CallbackFn& fn)
|
2017-03-20 12:18:41 -05:00
|
|
|
{
|
|
|
|
_poll->addCallback(fn);
|
|
|
|
}
|
|
|
|
|
2017-03-07 11:34:01 -06:00
|
|
|
void DocumentBroker::addSocketToPoll(const std::shared_ptr<Socket>& socket)
|
|
|
|
{
|
|
|
|
_poll->insertNewSocket(socket);
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:00:05 -06:00
|
|
|
void DocumentBroker::alertAllUsers(const std::string& msg)
|
2016-09-28 14:07:07 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-09-28 14:07:07 -05:00
|
|
|
|
2017-06-16 11:55:23 -05:00
|
|
|
if (UnitWSD::get().filterAlertAllusers(msg))
|
|
|
|
return;
|
|
|
|
|
2017-01-21 18:51:02 -06:00
|
|
|
auto payload = std::make_shared<Message>(msg, Message::Dir::Out);
|
2016-12-13 18:20:05 -06:00
|
|
|
|
2016-11-22 21:06:05 -06:00
|
|
|
LOG_DBG("Alerting all users of [" << _docKey << "]: " << msg);
|
2016-10-29 20:15:00 -05:00
|
|
|
for (auto& it : _sessions)
|
2016-09-28 14:07:07 -05:00
|
|
|
{
|
2016-12-13 18:20:05 -06:00
|
|
|
it.second->enqueueSendMessage(payload);
|
2016-09-28 14:07:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:50:06 -06:00
|
|
|
/// Handles input from the prisoner / child kit process
|
2016-05-01 19:50:11 -05:00
|
|
|
bool DocumentBroker::handleInput(const std::vector<char>& payload)
|
|
|
|
{
|
2017-01-22 22:18:37 -06:00
|
|
|
auto message = std::make_shared<Message>(payload.data(), payload.size(), Message::Dir::Out);
|
|
|
|
const auto& msg = message->abbr();
|
2016-12-21 20:07:18 -06:00
|
|
|
LOG_TRC("DocumentBroker handling child message: [" << msg << "].");
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2016-09-19 18:18:07 -05:00
|
|
|
LOOLWSD::dumpOutgoingTrace(getJailId(), "0", msg);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-07-31 06:54:47 -05:00
|
|
|
|
2017-01-22 22:18:37 -06:00
|
|
|
if (LOOLProtocol::getFirstToken(message->forwardToken(), '-') == "client")
|
2016-05-01 19:50:11 -05:00
|
|
|
{
|
2017-01-22 22:18:37 -06:00
|
|
|
forwardToClient(message);
|
2016-09-28 14:07:07 -05:00
|
|
|
}
|
2016-09-19 18:18:07 -05:00
|
|
|
else
|
|
|
|
{
|
2017-01-22 22:18:37 -06:00
|
|
|
const auto& command = message->firstToken();
|
|
|
|
if (command == "tile:")
|
|
|
|
{
|
|
|
|
handleTileResponse(payload);
|
|
|
|
}
|
|
|
|
else if (command == "tilecombine:")
|
|
|
|
{
|
|
|
|
handleTileCombinedResponse(payload);
|
|
|
|
}
|
|
|
|
else if (command == "errortoall:")
|
|
|
|
{
|
|
|
|
LOG_CHECK_RET(message->tokens().size() == 3, false);
|
|
|
|
std::string cmd, kind;
|
|
|
|
LOOLProtocol::getTokenString((*message)[1], "cmd", cmd);
|
|
|
|
LOG_CHECK_RET(cmd != "", false);
|
|
|
|
LOOLProtocol::getTokenString((*message)[2], "kind", kind);
|
|
|
|
LOG_CHECK_RET(kind != "", false);
|
|
|
|
Util::alertAllUsers(cmd, kind);
|
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-02-03 00:29:53 -06:00
|
|
|
else if (command == "procmemstats:")
|
|
|
|
{
|
2017-02-07 08:39:56 -06:00
|
|
|
int dirty;
|
|
|
|
if (message->getTokenInteger("dirty", dirty))
|
2017-02-03 00:29:53 -06:00
|
|
|
{
|
2017-02-07 08:39:56 -06:00
|
|
|
Admin::instance().updateMemoryDirty(_docKey, dirty);
|
2017-02-03 00:29:53 -06:00
|
|
|
}
|
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-01-22 22:18:37 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_ERR("Unexpected message: [" << msg << "].");
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-19 18:18:07 -05:00
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-20 21:19:52 -05:00
|
|
|
void DocumentBroker::invalidateTiles(const std::string& tiles)
|
|
|
|
{
|
|
|
|
// Remove from cache.
|
|
|
|
_tileCache->invalidateTiles(tiles);
|
|
|
|
}
|
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
void DocumentBroker::handleTileRequest(TileDesc& tile,
|
2016-05-16 19:49:36 -05:00
|
|
|
const std::shared_ptr<ClientSession>& session)
|
2016-05-01 19:50:11 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-05-01 19:50:11 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
tile.setVersion(++_tileVersion);
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string tileMsg = tile.serialize();
|
2017-01-01 23:30:27 -06:00
|
|
|
LOG_TRC("Tile request for " << tileMsg);
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
std::unique_ptr<std::fstream> cachedTile = _tileCache->lookupTile(tile);
|
2016-05-01 19:50:11 -05:00
|
|
|
if (cachedTile)
|
|
|
|
{
|
|
|
|
#if ENABLE_DEBUG
|
2016-05-22 13:31:18 -05:00
|
|
|
const std::string response = tile.serialize("tile:") + " renderid=cached\n";
|
2016-05-04 20:14:39 -05:00
|
|
|
#else
|
2016-08-31 22:41:09 -05:00
|
|
|
const std::string response = tile.serialize("tile:") + '\n';
|
2016-05-01 19:50:11 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::vector<char> output;
|
2016-07-18 03:25:05 -05:00
|
|
|
output.reserve(static_cast<size_t>(4) * tile.getWidth() * tile.getHeight());
|
2016-05-01 19:50:11 -05:00
|
|
|
output.resize(response.size());
|
|
|
|
std::memcpy(output.data(), response.data(), response.size());
|
|
|
|
|
|
|
|
assert(cachedTile->is_open());
|
|
|
|
cachedTile->seekg(0, std::ios_base::end);
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t pos = output.size();
|
2016-05-01 19:50:11 -05:00
|
|
|
std::streamsize size = cachedTile->tellg();
|
|
|
|
output.resize(pos + size);
|
|
|
|
cachedTile->seekg(0, std::ios_base::beg);
|
|
|
|
cachedTile->read(output.data() + pos, size);
|
|
|
|
cachedTile->close();
|
|
|
|
|
|
|
|
session->sendBinaryFrame(output.data(), output.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-07 08:07:50 -05:00
|
|
|
if (tile.getBroadcast())
|
|
|
|
{
|
|
|
|
for (auto& it: _sessions)
|
|
|
|
{
|
|
|
|
tileCache().subscribeToTileRendering(tile, it.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tileCache().subscribeToTileRendering(tile, session);
|
|
|
|
}
|
2016-05-04 21:05:09 -05:00
|
|
|
|
2016-09-20 21:26:19 -05:00
|
|
|
// Forward to child to render.
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Sending render request for tile (" << tile.getPart() << ',' <<
|
|
|
|
tile.getTilePosX() << ',' << tile.getTilePosY() << ").");
|
2017-01-01 23:30:27 -06:00
|
|
|
const std::string request = "tile " + tileMsg;
|
2016-10-11 17:22:51 -05:00
|
|
|
_childProcess->sendTextFrame(request);
|
2016-10-11 07:39:56 -05:00
|
|
|
_debugRenderedTileCount++;
|
2016-05-01 19:50:11 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
void DocumentBroker::handleTileCombinedRequest(TileCombined& tileCombined,
|
|
|
|
const std::shared_ptr<ClientSession>& session)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("TileCombined request for " << tileCombined.serialize());
|
2016-05-22 13:31:18 -05:00
|
|
|
|
2018-06-07 06:13:36 -05:00
|
|
|
// Check which newly requested tiles needs rendering.
|
|
|
|
std::vector<TileDesc> tilesNeedsRendering;
|
2016-05-29 16:40:27 -05:00
|
|
|
for (auto& tile : tileCombined.getTiles())
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
2018-09-26 15:15:26 -05:00
|
|
|
tile.setVersion(++_tileVersion);
|
2016-05-22 10:45:28 -05:00
|
|
|
std::unique_ptr<std::fstream> cachedTile = _tileCache->lookupTile(tile);
|
2018-06-07 06:13:36 -05:00
|
|
|
if(cachedTile)
|
2016-05-22 10:45:28 -05:00
|
|
|
cachedTile->close();
|
2016-05-22 13:31:18 -05:00
|
|
|
else
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
2016-08-18 09:28:55 -05:00
|
|
|
// Not cached, needs rendering.
|
2018-06-07 06:13:36 -05:00
|
|
|
tilesNeedsRendering.push_back(tile);
|
2016-10-11 07:39:56 -05:00
|
|
|
_debugRenderedTileCount++;
|
2018-09-26 15:15:26 -05:00
|
|
|
tileCache().registerTileBeingRendered(tile);
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
2016-05-29 16:40:27 -05:00
|
|
|
}
|
2016-09-25 15:04:27 -05:00
|
|
|
|
2018-06-12 07:51:38 -05:00
|
|
|
// Send rendering request, prerender before we actually send the tiles
|
2018-06-07 06:13:36 -05:00
|
|
|
if (!tilesNeedsRendering.empty())
|
2016-09-25 15:04:27 -05:00
|
|
|
{
|
2018-06-07 06:13:36 -05:00
|
|
|
TileCombined newTileCombined = TileCombined::create(tilesNeedsRendering);
|
2016-09-25 15:04:27 -05:00
|
|
|
|
|
|
|
// Forward to child to render.
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string req = newTileCombined.serialize("tilecombine");
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Sending residual tilecombine: " << req);
|
2016-10-11 17:22:51 -05:00
|
|
|
_childProcess->sendTextFrame(req);
|
2016-09-25 15:04:27 -05:00
|
|
|
}
|
2018-06-07 06:13:36 -05:00
|
|
|
|
|
|
|
// Accumulate tiles
|
2018-08-03 07:25:21 -05:00
|
|
|
std::deque<TileDesc>& requestedTiles = session->getRequestedTiles();
|
|
|
|
if (requestedTiles.empty())
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
2018-08-03 07:25:21 -05:00
|
|
|
requestedTiles = std::deque<TileDesc>(tileCombined.getTiles().begin(), tileCombined.getTiles().end());
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
|
|
|
// Drop duplicated tiles, but use newer version number
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto& newTile : tileCombined.getTiles())
|
|
|
|
{
|
2018-08-03 07:25:21 -05:00
|
|
|
const TileDesc& firstOldTile = *(requestedTiles.begin());
|
2018-09-21 08:49:30 -05:00
|
|
|
if(!session->isTextDocument() && newTile.getPart() != firstOldTile.getPart())
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
2018-09-21 08:49:30 -05:00
|
|
|
LOG_WRN("Different part numbers in tile requests");
|
|
|
|
}
|
|
|
|
else if (newTile.getTileWidth() != firstOldTile.getTileWidth() ||
|
|
|
|
newTile.getTileHeight() != firstOldTile.getTileHeight() )
|
|
|
|
{
|
|
|
|
LOG_WRN("Different tile sizes in tile requests");
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tileFound = false;
|
2018-08-03 07:25:21 -05:00
|
|
|
for (auto& oldTile : requestedTiles)
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
|
|
|
if(oldTile.getTilePosX() == newTile.getTilePosX() &&
|
|
|
|
oldTile.getTilePosY() == newTile.getTilePosY() )
|
|
|
|
{
|
|
|
|
oldTile.setVersion(newTile.getVersion());
|
|
|
|
oldTile.setOldWireId(newTile.getOldWireId());
|
|
|
|
oldTile.setWireId(newTile.getWireId());
|
|
|
|
tileFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!tileFound)
|
2018-08-03 07:25:21 -05:00
|
|
|
requestedTiles.push_back(newTile);
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
lock.release();
|
|
|
|
sendRequestedTiles(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::sendRequestedTiles(const std::shared_ptr<ClientSession>& session)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2018-07-05 07:40:28 -05:00
|
|
|
// How many tiles we have on the visible area, set the upper limit accordingly
|
2018-08-30 10:40:42 -05:00
|
|
|
Util::Rectangle normalizedVisArea = session->getNormalizedVisibleArea();
|
2018-07-05 07:40:28 -05:00
|
|
|
|
2018-09-03 09:35:00 -05:00
|
|
|
float tilesOnFlyUpperLimit = 0;
|
|
|
|
if (normalizedVisArea.hasSurface() && session->getTileWidthInTwips() != 0 && session->getTileHeightInTwips() != 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
const int tilesFitOnWidth = std::ceil(normalizedVisArea._x2 / session->getTileWidthInTwips()) -
|
|
|
|
std::ceil(normalizedVisArea._x1 / session->getTileWidthInTwips()) + 1;
|
|
|
|
const int tilesFitOnHeight = std::ceil(normalizedVisArea._y2 / session->getTileHeightInTwips()) -
|
|
|
|
std::ceil(normalizedVisArea._y1 / session->getTileHeightInTwips()) + 1;
|
|
|
|
const int tilesInVisArea = tilesFitOnWidth * tilesFitOnHeight;
|
|
|
|
|
2018-09-26 15:15:41 -05:00
|
|
|
tilesOnFlyUpperLimit = std::max(TILES_ON_FLY_MIN_UPPER_LIMIT, tilesInVisArea * 1.1f);
|
2018-09-03 09:35:00 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tilesOnFlyUpperLimit = 200; // Have a big number here to get all tiles requested by file openning
|
|
|
|
}
|
2018-08-30 10:40:42 -05:00
|
|
|
|
2018-07-05 07:40:28 -05:00
|
|
|
|
2018-07-31 07:47:27 -05:00
|
|
|
// Update client's tilesBeingRendered list
|
|
|
|
session->removeOutdatedTileSubscriptions();
|
2018-08-23 05:46:49 -05:00
|
|
|
// Drop tiles which we are waiting for too long
|
|
|
|
session->removeOutdatedTilesOnFly();
|
2018-07-31 07:47:27 -05:00
|
|
|
|
2018-06-07 06:13:36 -05:00
|
|
|
// All tiles were processed on client side what we sent last time, so we can send a new banch of tiles
|
|
|
|
// which was invalidated / requested in the meantime
|
2018-08-03 07:25:21 -05:00
|
|
|
std::deque<TileDesc>& requestedTiles = session->getRequestedTiles();
|
|
|
|
if (!requestedTiles.empty())
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
2018-09-28 12:30:57 -05:00
|
|
|
size_t delayedTiles = 0;
|
2018-06-12 07:51:38 -05:00
|
|
|
std::vector<TileDesc> tilesNeedsRendering;
|
2018-08-20 11:07:16 -05:00
|
|
|
while(session->getTilesOnFlyCount() + session->getTilesBeingRenderedCount() < tilesOnFlyUpperLimit &&
|
2018-09-28 12:30:57 -05:00
|
|
|
!requestedTiles.empty() &&
|
|
|
|
// If we delayed all tiles we don't send any tile (we will when next tileprocessed message arrives)
|
|
|
|
delayedTiles < requestedTiles.size())
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
2018-08-03 07:25:21 -05:00
|
|
|
TileDesc& tile = *(requestedTiles.begin());
|
2018-07-10 07:05:36 -05:00
|
|
|
|
2018-09-28 12:30:57 -05:00
|
|
|
// We already sent out two versions of the same tile, let's not send the third one
|
|
|
|
// until we get a tileprocessed message for this specific tile.
|
|
|
|
if (session->countIdenticalTilesOnFly(tile) >= 2)
|
|
|
|
{
|
|
|
|
requestedTiles.push_back(requestedTiles.front());
|
|
|
|
requestedTiles.pop_front();
|
|
|
|
delayedTiles += 1;
|
|
|
|
LOG_INF("Requested tile was delayed!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-10 07:05:36 -05:00
|
|
|
// Satisfy as many tiles from the cache.
|
2018-06-07 06:13:36 -05:00
|
|
|
std::unique_ptr<std::fstream> cachedTile = _tileCache->lookupTile(tile);
|
|
|
|
if (cachedTile)
|
|
|
|
{
|
|
|
|
//TODO: Combine the response to reduce latency.
|
|
|
|
#if ENABLE_DEBUG
|
|
|
|
const std::string response = tile.serialize("tile:") + " renderid=cached\n";
|
|
|
|
#else
|
|
|
|
const std::string response = tile.serialize("tile:") + "\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::vector<char> output;
|
|
|
|
output.reserve(static_cast<size_t>(4) * tile.getWidth() * tile.getHeight());
|
|
|
|
output.resize(response.size());
|
|
|
|
std::memcpy(output.data(), response.data(), response.size());
|
|
|
|
|
|
|
|
assert(cachedTile->is_open());
|
|
|
|
cachedTile->seekg(0, std::ios_base::end);
|
|
|
|
const auto pos = output.size();
|
|
|
|
std::streamsize size = cachedTile->tellg();
|
|
|
|
output.resize(pos + size);
|
|
|
|
cachedTile->seekg(0, std::ios_base::beg);
|
|
|
|
cachedTile->read(output.data() + pos, size);
|
|
|
|
cachedTile->close();
|
|
|
|
|
|
|
|
session->sendBinaryFrame(output.data(), output.size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-12 07:51:38 -05:00
|
|
|
// Not cached, needs rendering.
|
2018-09-26 15:15:26 -05:00
|
|
|
if (!tileCache().hasTileBeingRendered(tile) || // There is no in progress rendering of the given tile
|
|
|
|
tileCache().getTileBeingRenderedVersion(tile) < tile.getVersion()) // We need a newer version
|
|
|
|
{
|
|
|
|
tile.setVersion(++_tileVersion);
|
|
|
|
tilesNeedsRendering.push_back(tile);
|
|
|
|
_debugRenderedTileCount++;
|
|
|
|
}
|
2018-06-07 06:13:36 -05:00
|
|
|
tileCache().subscribeToTileRendering(tile, session);
|
|
|
|
}
|
2018-08-03 07:25:21 -05:00
|
|
|
requestedTiles.pop_front();
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
2018-06-12 07:51:38 -05:00
|
|
|
|
|
|
|
// Send rendering request for those tiles which were not prerendered
|
|
|
|
if (!tilesNeedsRendering.empty())
|
|
|
|
{
|
|
|
|
TileCombined newTileCombined = TileCombined::create(tilesNeedsRendering);
|
|
|
|
|
|
|
|
// Forward to child to render.
|
|
|
|
const std::string req = newTileCombined.serialize("tilecombine");
|
|
|
|
LOG_DBG("Some of the tiles were not prerendered. Sending residual tilecombine: " << req);
|
|
|
|
_childProcess->sendTextFrame(req);
|
|
|
|
}
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
|
|
|
|
2016-08-30 22:15:44 -05:00
|
|
|
void DocumentBroker::cancelTileRequests(const std::shared_ptr<ClientSession>& session)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2018-06-07 06:13:36 -05:00
|
|
|
// Clear tile requests
|
2018-07-10 07:05:36 -05:00
|
|
|
session->clearTilesOnFly();
|
2018-08-03 07:25:21 -05:00
|
|
|
|
|
|
|
session->getRequestedTiles().clear();
|
2018-06-07 06:13:36 -05:00
|
|
|
|
2018-08-03 05:26:10 -05:00
|
|
|
session->clearTileSubscription();
|
|
|
|
|
2018-08-22 09:19:04 -05:00
|
|
|
session->resetWireIdMap();
|
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string canceltiles = tileCache().cancelTiles(session);
|
2016-09-19 21:16:45 -05:00
|
|
|
if (!canceltiles.empty())
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Forwarding canceltiles request: " << canceltiles);
|
2016-10-11 17:22:51 -05:00
|
|
|
_childProcess->sendTextFrame(canceltiles);
|
2016-09-19 21:16:45 -05:00
|
|
|
}
|
2016-08-30 22:15:44 -05:00
|
|
|
}
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
void DocumentBroker::handleTileResponse(const std::vector<char>& payload)
|
|
|
|
{
|
|
|
|
const std::string firstLine = getFirstLine(payload);
|
2016-12-09 17:15:10 -06:00
|
|
|
LOG_DBG("Handling tile: " << firstLine);
|
2016-10-30 14:24:27 -05:00
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
try
|
2016-05-05 09:19:13 -05:00
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t length = payload.size();
|
2016-05-22 15:47:22 -05:00
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
2016-05-15 17:47:08 -05:00
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const TileDesc tile = TileDesc::parse(firstLine);
|
|
|
|
const char* buffer = payload.data();
|
|
|
|
const size_t offset = firstLine.size() + 1;
|
2017-01-25 19:55:39 -06:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
tileCache().saveTileAndNotify(tile, buffer + offset, length - offset);
|
2016-05-15 17:47:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-02 23:45:40 -05:00
|
|
|
LOG_WRN("Dropping empty tile response: " << firstLine);
|
2016-10-30 14:24:27 -05:00
|
|
|
// They will get re-issued if we don't forget them.
|
2016-05-15 17:47:08 -05:00
|
|
|
}
|
2016-05-05 01:37:40 -05:00
|
|
|
}
|
2016-05-15 17:47:08 -05:00
|
|
|
catch (const std::exception& exc)
|
2016-05-05 01:37:40 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Failed to process tile response [" << firstLine << "]: " << exc.what() << ".");
|
2016-05-05 01:37:40 -05:00
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
void DocumentBroker::handleTileCombinedResponse(const std::vector<char>& payload)
|
|
|
|
{
|
|
|
|
const std::string firstLine = getFirstLine(payload);
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Handling tile combined: " << firstLine);
|
2016-05-22 10:45:28 -05:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-09-26 15:15:30 -05:00
|
|
|
const size_t length = payload.size();
|
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
|
|
|
{
|
|
|
|
const TileCombined tileCombined = TileCombined::parse(firstLine);
|
|
|
|
const char* buffer = payload.data();
|
|
|
|
size_t offset = firstLine.size() + 1;
|
2017-01-25 19:55:39 -06:00
|
|
|
|
2018-09-26 15:15:30 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2017-01-25 19:55:39 -06:00
|
|
|
|
2018-09-26 15:15:30 -05:00
|
|
|
for (const auto& tile : tileCombined.getTiles())
|
|
|
|
{
|
|
|
|
tileCache().saveTileAndNotify(tile, buffer + offset, tile.getImgSize());
|
|
|
|
offset += tile.getImgSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
2018-09-26 15:15:30 -05:00
|
|
|
LOG_WRN("Dropping empty tilecombine response: " << firstLine);
|
|
|
|
// They will get re-issued if we don't forget them.
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Failed to process tile response [" << firstLine << "]: " << exc.what() << ".");
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
bool DocumentBroker::haveAnotherEditableSession(const std::string& id) const
|
2016-04-17 22:29:03 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-04-17 22:29:03 -05:00
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
for (const auto& it : _sessions)
|
2017-03-07 20:17:24 -06:00
|
|
|
{
|
2018-01-14 16:17:00 -06:00
|
|
|
if (it.second->getId() != id &&
|
|
|
|
it.second->isViewLoaded() &&
|
|
|
|
!it.second->isReadOnly())
|
2016-07-10 23:50:25 -05:00
|
|
|
{
|
2018-01-14 16:17:00 -06:00
|
|
|
// This is a loaded session that is non-readonly.
|
|
|
|
return true;
|
2016-07-10 23:50:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
// None found.
|
|
|
|
return false;
|
2016-04-17 22:29:03 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
void DocumentBroker::setModified(const bool value)
|
|
|
|
{
|
2018-01-03 22:15:44 -06:00
|
|
|
if (_isModified != value)
|
2017-05-12 08:59:01 -05:00
|
|
|
{
|
|
|
|
_isModified = value;
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2017-05-12 08:59:01 -05:00
|
|
|
Admin::instance().modificationAlert(_docKey, getPid(), value);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-05-12 08:59:01 -05:00
|
|
|
}
|
|
|
|
|
2018-01-03 23:16:40 -06:00
|
|
|
_storage->setUserModified(value);
|
2016-04-22 06:00:11 -05:00
|
|
|
_tileCache->setUnsavedChanges(value);
|
|
|
|
}
|
|
|
|
|
2018-04-24 11:09:37 -05:00
|
|
|
bool DocumentBroker::isInitialSettingSet(const std::string& name) const
|
|
|
|
{
|
|
|
|
return _isInitialStateSet.find(name) != _isInitialStateSet.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::setInitialSetting(const std::string& name)
|
|
|
|
{
|
|
|
|
_isInitialStateSet.emplace(name);
|
|
|
|
}
|
|
|
|
|
2016-10-16 12:43:44 -05:00
|
|
|
bool DocumentBroker::forwardToChild(const std::string& viewId, const std::string& message)
|
2016-10-08 13:25:27 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 23:11:06 -05:00
|
|
|
|
2017-08-11 12:40:34 -05:00
|
|
|
// Ignore userinactive, useractive message until document is loaded
|
|
|
|
if (!isLoaded() && (message == "userinactive" || message == "useractive"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Forwarding payload to child [" << viewId << "]: " << message);
|
2016-10-08 13:25:27 -05:00
|
|
|
|
2017-03-07 04:44:29 -06:00
|
|
|
std::string msg = "child-" + viewId + ' ' + message;
|
|
|
|
|
2016-10-08 13:25:27 -05:00
|
|
|
const auto it = _sessions.find(viewId);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2017-03-07 04:44:29 -06:00
|
|
|
assert(!_uriJailed.empty());
|
2017-03-20 19:42:09 -05:00
|
|
|
|
|
|
|
std::vector<std::string> tokens = LOOLProtocol::tokenize(msg);
|
|
|
|
if (tokens.size() > 1 && tokens[1] == "load")
|
|
|
|
{
|
|
|
|
// The json options must come last.
|
|
|
|
msg = tokens[0] + ' ' + tokens[1] + ' ' + tokens[2];
|
2018-06-10 10:41:17 -05:00
|
|
|
msg += " jail=" + _uriJailed;
|
|
|
|
msg += " xjail=" + _uriJailedAnonym;
|
|
|
|
msg += ' ' + Poco::cat(std::string(" "), tokens.begin() + 3, tokens.end());
|
2017-03-20 19:42:09 -05:00
|
|
|
}
|
2017-03-07 04:44:29 -06:00
|
|
|
|
2016-10-11 17:22:51 -05:00
|
|
|
_childProcess->sendTextFrame(msg);
|
2016-10-08 17:21:35 -05:00
|
|
|
return true;
|
2016-10-08 13:25:27 -05:00
|
|
|
}
|
2017-03-25 13:19:17 -05:00
|
|
|
|
|
|
|
// try the not yet created sessions
|
|
|
|
LOG_WRN("Child session [" << viewId << "] not found to forward message: " << message);
|
2016-10-08 13:25:27 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-22 22:18:37 -06:00
|
|
|
bool DocumentBroker::forwardToClient(const std::shared_ptr<Message>& payload)
|
2016-10-08 12:13:23 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-03-25 12:57:08 -05:00
|
|
|
|
2017-01-22 22:18:37 -06:00
|
|
|
const std::string& msg = payload->abbr();
|
|
|
|
const std::string& prefix = payload->forwardToken();
|
|
|
|
LOG_TRC("Forwarding payload to [" << prefix << "]: " << msg);
|
2016-10-08 12:13:23 -05:00
|
|
|
|
|
|
|
std::string name;
|
|
|
|
std::string sid;
|
2017-01-22 22:18:37 -06:00
|
|
|
if (LOOLProtocol::parseNameValuePair(payload->forwardToken(), name, sid, '-') && name == "client")
|
2016-10-08 12:13:23 -05:00
|
|
|
{
|
2017-02-06 19:10:08 -06:00
|
|
|
const auto& data = payload->data().data();
|
|
|
|
const auto& size = payload->size();
|
|
|
|
|
|
|
|
if (sid == "all")
|
2016-10-08 12:13:23 -05:00
|
|
|
{
|
2017-02-06 19:10:08 -06:00
|
|
|
// Broadcast to all.
|
2017-06-19 20:53:58 -05:00
|
|
|
// Events could cause the removal of sessions.
|
|
|
|
std::map<std::string, std::shared_ptr<ClientSession>> sessions(_sessions);
|
|
|
|
for (const auto& pair : sessions)
|
2017-02-06 19:10:08 -06:00
|
|
|
{
|
2017-03-26 22:04:27 -05:00
|
|
|
pair.second->handleKitToClientMessage(data, size);
|
2017-02-06 19:10:08 -06:00
|
|
|
}
|
2016-10-08 12:13:23 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-06 19:10:08 -06:00
|
|
|
const auto it = _sessions.find(sid);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2017-06-19 20:53:58 -05:00
|
|
|
// Take a ref as the session could be removed from _sessions
|
|
|
|
// if it's the save confirmation keeping a stopped session alive.
|
|
|
|
std::shared_ptr<ClientSession> session = it->second;
|
|
|
|
return session->handleKitToClientMessage(data, size);
|
2017-02-06 19:10:08 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_WRN("Client session [" << sid << "] not found to forward message: " << msg);
|
|
|
|
}
|
2016-10-08 12:13:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-09 17:15:10 -06:00
|
|
|
LOG_ERR("Unexpected prefix of forward-to-client message: " << prefix);
|
2016-10-08 12:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-21 18:13:55 -05:00
|
|
|
void DocumentBroker::shutdownClients(const std::string& closeReason)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
2017-06-01 07:56:54 -05:00
|
|
|
LOG_INF("Terminating " << _sessions.size() << " clients of doc [" << _docKey << "] with reason: " << closeReason);
|
2017-05-21 18:13:55 -05:00
|
|
|
|
|
|
|
// First copy into local container, since removeSession
|
|
|
|
// will erase from _sessions, but will leave the last.
|
|
|
|
std::map<std::string, std::shared_ptr<ClientSession>> sessions = _sessions;
|
|
|
|
for (const auto& pair : sessions)
|
|
|
|
{
|
|
|
|
std::shared_ptr<ClientSession> session = pair.second;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Notify the client and disconnect.
|
|
|
|
session->shutdown(WebSocketHandler::StatusCodes::ENDPOINT_GOING_AWAY, closeReason);
|
|
|
|
|
|
|
|
// Remove session, save, and mark to destroy.
|
2018-01-14 16:17:00 -06:00
|
|
|
removeSession(session->getId());
|
2017-05-21 18:13:55 -05:00
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
LOG_WRN("Error while shutting down client [" <<
|
|
|
|
session->getName() << "]: " << exc.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:07:40 -05:00
|
|
|
void DocumentBroker::childSocketTerminated()
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-11-01 18:38:25 -05:00
|
|
|
|
2016-10-15 16:07:40 -05:00
|
|
|
if (!_childProcess->isAlive())
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Child for doc [" << _docKey << "] terminated prematurely.");
|
2016-10-15 16:07:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We could restore the kit if this was unexpected.
|
|
|
|
// For now, close the connections to cleanup.
|
2017-05-21 18:13:55 -05:00
|
|
|
shutdownClients("terminated");
|
2016-10-15 16:07:40 -05:00
|
|
|
}
|
|
|
|
|
2017-06-21 05:07:38 -05:00
|
|
|
void DocumentBroker::terminateChild(const std::string& closeReason)
|
2016-11-05 22:00:16 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-11-05 22:00:16 -05:00
|
|
|
|
2017-06-01 07:56:54 -05:00
|
|
|
LOG_INF("Terminating doc [" << _docKey << "] with reason: " << closeReason);
|
2016-11-05 22:00:16 -05:00
|
|
|
|
2018-01-14 19:49:11 -06:00
|
|
|
// Close all running sessions first.
|
2017-06-21 05:07:38 -05:00
|
|
|
shutdownClients(closeReason);
|
2016-11-08 07:37:28 -06:00
|
|
|
|
2017-03-07 20:18:59 -06:00
|
|
|
if (_childProcess)
|
|
|
|
{
|
|
|
|
LOG_INF("Terminating child [" << getPid() << "] of doc [" << _docKey << "].");
|
2016-11-05 22:00:16 -05:00
|
|
|
|
2018-05-18 06:00:16 -05:00
|
|
|
_childProcess->close();
|
2017-03-07 20:18:59 -06:00
|
|
|
}
|
2017-03-12 21:31:50 -05:00
|
|
|
|
2018-01-14 19:49:11 -06:00
|
|
|
stop(closeReason);
|
2016-11-05 22:00:16 -05:00
|
|
|
}
|
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
void DocumentBroker::closeDocument(const std::string& reason)
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-11-08 07:37:28 -06:00
|
|
|
|
2017-03-09 21:38:25 -06:00
|
|
|
LOG_DBG("Closing DocumentBroker for docKey [" << _docKey << "] with reason: " << reason);
|
2018-02-01 22:28:20 -06:00
|
|
|
_closeReason = reason;
|
|
|
|
_closeRequest = true;
|
2016-11-08 07:37:28 -06:00
|
|
|
}
|
|
|
|
|
2017-06-01 11:11:11 -05:00
|
|
|
void DocumentBroker::broadcastMessage(const std::string& message)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
LOG_DBG("Broadcasting message [" << message << "] to all sessions.");
|
|
|
|
for (const auto& sessionIt : _sessions)
|
|
|
|
{
|
|
|
|
sessionIt.second->sendTextFrame(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
void DocumentBroker::updateLastActivityTime()
|
|
|
|
{
|
2017-01-01 18:42:19 -06:00
|
|
|
_lastActivityTime = std::chrono::steady_clock::now();
|
2018-09-05 07:11:05 -05:00
|
|
|
#ifndef MOBILEAPP
|
2016-12-05 14:11:07 -06:00
|
|
|
Admin::instance().updateLastActivityTime(_docKey);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-12-05 14:11:07 -06:00
|
|
|
}
|
|
|
|
|
2017-05-31 21:40:01 -05:00
|
|
|
void DocumentBroker::getIOStats(uint64_t &sent, uint64_t &recv)
|
|
|
|
{
|
|
|
|
sent = 0;
|
|
|
|
recv = 0;
|
|
|
|
assertCorrectThread();
|
|
|
|
for (const auto& sessionIt : _sessions)
|
|
|
|
{
|
|
|
|
uint64_t s, r;
|
|
|
|
sessionIt.second->getIOStats(s, r);
|
|
|
|
sent += s;
|
|
|
|
recv += r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 18:03:45 -05:00
|
|
|
void DocumentBroker::dumpState(std::ostream& os)
|
2017-03-06 09:45:34 -06:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2017-05-31 21:40:01 -05:00
|
|
|
uint64_t sent, recv;
|
|
|
|
getIOStats(sent, recv);
|
|
|
|
|
2018-07-10 22:09:27 -05:00
|
|
|
os << " Broker: " << LOOLWSD::anonymizeUrl(_filename) << " pid: " << getPid();
|
2017-03-10 09:37:09 -06:00
|
|
|
if (_markToDestroy)
|
2017-03-12 18:03:45 -05:00
|
|
|
os << " *** Marked to destroy ***";
|
2017-03-10 09:37:09 -06:00
|
|
|
else
|
2017-03-12 18:03:45 -05:00
|
|
|
os << " has live sessions";
|
2017-03-11 16:01:27 -06:00
|
|
|
if (_isLoaded)
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n loaded in: " << _loadDuration.count() << "ms";
|
2017-03-11 16:01:27 -06:00
|
|
|
else
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n still loading...";
|
2017-05-31 21:40:01 -05:00
|
|
|
os << "\n sent: " << sent;
|
2017-06-03 16:53:57 -05:00
|
|
|
os << "\n recv: " << recv;
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n modified?: " << _isModified;
|
|
|
|
os << "\n jail id: " << _jailId;
|
2018-07-10 22:09:27 -05:00
|
|
|
os << "\n filename: " << LOOLWSD::anonymizeUrl(_filename);
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n public uri: " << _uriPublic.toString();
|
2018-07-10 22:09:27 -05:00
|
|
|
os << "\n jailed uri: " << LOOLWSD::anonymizeUrl(_uriJailed);
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n doc key: " << _docKey;
|
2017-04-03 23:11:06 -05:00
|
|
|
os << "\n doc id: " << _docId;
|
2017-04-06 12:49:22 -05:00
|
|
|
os << "\n num sessions: " << _sessions.size();
|
2018-09-04 03:55:31 -05:00
|
|
|
const std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::time_point_cast<std::chrono::seconds>(
|
2017-03-18 09:59:09 -05:00
|
|
|
std::chrono::system_clock::now()
|
2018-09-04 03:55:31 -05:00
|
|
|
+ (_lastSaveTime - std::chrono::steady_clock::now())));
|
2017-03-18 09:59:09 -05:00
|
|
|
os << "\n last saved: " << std::ctime(&t);
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n cursor " << _cursorPosX << ", " << _cursorPosY
|
|
|
|
<< "( " << _cursorWidth << "," << _cursorHeight << ")\n";
|
|
|
|
|
|
|
|
_poll->dumpState(os);
|
2017-03-06 09:45:34 -06:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|