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/.
|
|
|
|
*/
|
|
|
|
|
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 "config.h"
|
|
|
|
|
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
|
|
|
|
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/DigestStream.h>
|
|
|
|
#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-05-20 12:28:43 -05: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
|
|
|
|
|
|
|
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);
|
|
|
|
auto uriPublic = Poco::URI(decodedUri);
|
|
|
|
|
|
|
|
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),
|
2016-07-10 23:50:25 -05:00
|
|
|
_lastEditableSession(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
|
|
|
|
2017-03-19 13:49:52 -05:00
|
|
|
LOG_INF("DocumentBroker [" << _uriPublic.toString() <<
|
|
|
|
"] 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();
|
|
|
|
}
|
|
|
|
|
2017-04-05 07:48:49 -05:00
|
|
|
void DocumentBroker::assertCorrectThread()
|
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.
|
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;
|
2017-06-21 20:49:08 -05:00
|
|
|
|
|
|
|
// Nominal time between retries, lest we busy-loop. getNewChild could also wait, so don't double that here.
|
|
|
|
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);
|
|
|
|
|
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 ...
|
|
|
|
#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
|
|
|
|
// FIXME: return something good down the websocket ...
|
2017-03-07 19:46:16 -06:00
|
|
|
_stop = true;
|
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-03-09 12:19:53 -06:00
|
|
|
auto last30SecCheckTime = std::chrono::steady_clock::now();
|
|
|
|
|
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);
|
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();
|
|
|
|
|
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-03-09 14:35:04 -06:00
|
|
|
_poll->poll(SocketPoll::DefaultPollTimeoutMs);
|
2017-03-09 12:19:53 -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
|
|
|
|
|
|
|
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)));
|
2017-06-03 16:53:57 -05:00
|
|
|
LOG_INF("Doc [" << _docKey << "] added sent: " << sent << " recv: " << recv << " bytes to totals");
|
|
|
|
adminSent = sent;
|
|
|
|
adminRecv = recv;
|
|
|
|
}
|
|
|
|
|
2017-04-18 23:54:42 -05:00
|
|
|
if (_lastSaveTime < _lastSaveRequestTime &&
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>
|
|
|
|
(now - _lastSaveRequestTime).count() <= COMMAND_TIMEOUT_MS)
|
|
|
|
{
|
|
|
|
// We are saving, nothing more to do but wait.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShutdownRequestFlag)
|
|
|
|
{
|
2017-07-07 06:42:19 -05:00
|
|
|
_closeReason = "recycling";
|
2017-06-21 06:22:03 -05:00
|
|
|
_stop = true;
|
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.
|
2017-05-07 12:28:57 -05:00
|
|
|
const bool idle = (getIdleTimeSecs() >= IdleDocTimeoutSecs);
|
2017-04-06 11:58:41 -05:00
|
|
|
|
2017-04-06 22:38:54 -05:00
|
|
|
// If all sessions have been removed, no reason to linger.
|
2017-04-18 23:54:42 -05:00
|
|
|
if ((isLoaded() || _markToDestroy) && (_sessions.empty() || idle))
|
2017-04-06 11:58:41 -05:00
|
|
|
{
|
|
|
|
LOG_INF("Terminating " << (idle ? "idle" : "dead") <<
|
|
|
|
" DocumentBroker for docKey [" << getDocKey() << "].");
|
2017-07-07 06:42:19 -05:00
|
|
|
_closeReason = (idle ? "idle" : "dead");
|
2017-04-06 11:58:41 -05:00
|
|
|
_stop = true;
|
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 <<
|
|
|
|
", TerminationFlag: " << TerminationFlag << ".");
|
2017-04-09 18:41:29 -05:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2017-04-05 15:31:15 -05:00
|
|
|
// Async cleanup.
|
2017-04-02 18:56:42 -05:00
|
|
|
LOOLWSD::doHousekeeping();
|
|
|
|
|
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
|
|
|
|
2016-10-22 09:25:57 -05:00
|
|
|
Admin::instance().rmDoc(_docKey);
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-04-05 15:31:15 -05:00
|
|
|
void DocumentBroker::stop()
|
|
|
|
{
|
|
|
|
_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
|
2016-03-28 06:01:19 -05:00
|
|
|
const auto jailPath = Poco::Path(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();
|
|
|
|
LOG_DBG("Loading, and creating new storage instance for URI [" << uriPublic.toString() << "].");
|
|
|
|
|
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;
|
|
|
|
}
|
2017-05-20 12:28:43 -05:00
|
|
|
|
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
|
2016-11-06 12:54:00 -06: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);
|
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());
|
2016-12-13 03:13:58 -06:00
|
|
|
userid = wopifileinfo->_userid;
|
|
|
|
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;
|
|
|
|
|
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);
|
2016-11-10 06:21:39 -06:00
|
|
|
|
|
|
|
std::ostringstream ossWopiInfo;
|
|
|
|
wopiInfo->stringify(ossWopiInfo);
|
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.
|
|
|
|
session->sendMessage("wopi: " + ossWopiInfo.str());
|
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
|
2016-11-22 11:38:40 -06: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
|
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();
|
2017-01-21 21:43:12 -06:00
|
|
|
userid = localfileinfo->_userid;
|
|
|
|
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
|
|
|
|
2017-10-03 22:54:05 -05:00
|
|
|
#if ENABLE_SUPPORT_KEY
|
|
|
|
if (!LOOLWSD::OverrideWatermark.empty())
|
|
|
|
watermarkText = LOOLWSD::OverrideWatermark;
|
|
|
|
#endif
|
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
LOG_DBG("Setting username [" << username << "] and userId [" << userid << "] for session [" << sessionId << "]");
|
2016-11-06 21:11:35 -06:00
|
|
|
session->setUserId(userid);
|
|
|
|
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
|
2016-11-06 12:54:00 -06:00
|
|
|
const auto fileInfo = _storage->getFileInfo();
|
|
|
|
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;
|
2017-06-10 09:45:00 -05:00
|
|
|
Log::debug() << "Document timestamp: " << _documentLastModifiedTime << Log::end;
|
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
|
2017-06-10 09:45:00 -05:00
|
|
|
Log::trace() << "Document modified time: " << fileInfo._modifiedTime << Log::end;
|
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
|
|
|
{
|
2017-06-10 09:45:00 -05:00
|
|
|
Log::trace() << "Document " << _docKey << "] has been modified behind our back. Informing all clients."
|
|
|
|
<< "Expected: " << _documentLastModifiedTime
|
|
|
|
<< "Actual: " << fileInfo._modifiedTime << Log::end;
|
|
|
|
|
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
|
|
|
{
|
2017-08-16 09:38:00 -05:00
|
|
|
const auto localPath = _storage->loadStorageFileToLocal(session->getAuthorization());
|
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();
|
2017-02-05 18:35:54 -06:00
|
|
|
LOG_INF("SHA1 for DocKey [" << _docKey << "] of [" << localPath << "]: " <<
|
|
|
|
Poco::DigestEngine::digestToHex(sha1.digest()));
|
2017-01-22 22:12:51 -06:00
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
_uriJailed = Poco::URI(Poco::URI("file://"), localPath);
|
|
|
|
_filename = fileInfo._filename;
|
|
|
|
|
|
|
|
// Use the local temp file's timestamp.
|
2017-03-21 21:56:16 -05:00
|
|
|
_lastFileModifiedTime = Poco::File(_storage->getRootFilePath()).getLastModified();
|
2017-05-12 10:42:03 -05:00
|
|
|
_tileCache.reset(new TileCache(_storage->getUri(), _lastFileModifiedTime, _cacheRoot));
|
2017-06-22 07:58:50 -05:00
|
|
|
_tileCache->setThreadOwner(std::this_thread::get_id());
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-04-10 12:03:57 -05:00
|
|
|
|
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();
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::saveToStorageInternal(const std::string& sessionId,
|
|
|
|
bool success, const std::string& result)
|
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
|
|
|
|
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-08-16 09:38:00 -05:00
|
|
|
const Authorization auth = it->second->getAuthorization();
|
2017-05-12 10:42:03 -05:00
|
|
|
const auto uri = it->second->getPublicUri().toString();
|
2016-04-27 19:52:32 -05:00
|
|
|
|
2017-01-09 12:54:37 -06:00
|
|
|
// If we aren't destroying the last editable session just yet,
|
|
|
|
// and the file timestamp hasn't changed, skip saving.
|
2017-03-21 21:56:16 -05:00
|
|
|
const auto newFileModifiedTime = Poco::File(_storage->getRootFilePath()).getLastModified();
|
2016-10-08 09:31:35 -05:00
|
|
|
if (!_lastEditableSession && newFileModifiedTime == _lastFileModifiedTime)
|
2016-04-25 19:44:24 -05:00
|
|
|
{
|
|
|
|
// Nothing to do.
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_DBG("Skipping unnecessary saving to URI [" << uri << "] with docKey [" << _docKey <<
|
|
|
|
"]. File last modified " << _lastFileModifiedTime.elapsed() / 1000000 << " seconds ago.");
|
2017-01-01 18:42:19 -06:00
|
|
|
_lastSaveTime = std::chrono::steady_clock::now();
|
2017-03-09 12:19:53 -06:00
|
|
|
_poll->wakeup();
|
2016-04-25 19:44:24 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_DBG("Persisting [" << _docKey << "] after saving to URI [" << uri << "].");
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
assert(_storage && _tileCache);
|
2017-08-16 09:38:00 -05:00
|
|
|
StorageBase::SaveResult storageSaveResult = _storage->saveLocalFileToStorage(auth);
|
2016-11-23 06:09:54 -06:00
|
|
|
if (storageSaveResult == StorageBase::SaveResult::OK)
|
2016-03-26 08:10:53 -05:00
|
|
|
{
|
2017-05-12 08:59:01 -05:00
|
|
|
setModified(false);
|
2016-04-25 19:44:24 -05:00
|
|
|
_lastFileModifiedTime = newFileModifiedTime;
|
|
|
|
_tileCache->saveLastModified(_lastFileModifiedTime);
|
|
|
|
_lastSaveTime = std::chrono::steady_clock::now();
|
2017-03-09 12:19:53 -06:00
|
|
|
_poll->wakeup();
|
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
|
|
|
|
|
|
|
// So set _documentLastModifiedTime then
|
|
|
|
_documentLastModifiedTime = _storage->getFileInfo()._modifiedTime;
|
2017-06-01 11:11:11 -05:00
|
|
|
|
2017-06-01 10:37:52 -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
|
|
|
|
2017-06-10 09:45:00 -05:00
|
|
|
Log::debug() << "Saved docKey [" << _docKey << "] to URI [" << uri
|
|
|
|
<< "] and updated tile cache. Document modified timestamp: "
|
|
|
|
<< _documentLastModifiedTime << Log::end;
|
2016-03-26 08:10:53 -05:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-23 06:09:54 -06:00
|
|
|
else if (storageSaveResult == StorageBase::SaveResult::DISKFULL)
|
|
|
|
{
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_WRN("Disk full while saving docKey [" << _docKey << "] to URI [" << uri <<
|
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-05-31 01:21:12 -05:00
|
|
|
else if (storageSaveResult == StorageBase::SaveResult::UNAUTHORIZED)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot save docKey [" << _docKey << "] to storage URI [" << uri << "]. Invalid or expired access token. Notifying client.");
|
|
|
|
it->second->sendTextFrame("error: cmd=storage kind=saveunauthorized");
|
|
|
|
}
|
2016-11-23 06:09:54 -06:00
|
|
|
else if (storageSaveResult == StorageBase::SaveResult::FAILED)
|
|
|
|
{
|
2017-01-01 11:06:05 -06:00
|
|
|
//TODO: Should we notify all clients?
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_ERR("Failed to save docKey [" << _docKey << "] to URI [" << uri << "]. Notifying client.");
|
2016-11-23 06:09:54 -06:00
|
|
|
it->second->sendTextFrame("error: cmd=storage kind=savefailed");
|
|
|
|
}
|
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
|
|
|
else if (storageSaveResult == StorageBase::SaveResult::DOC_CHANGED)
|
|
|
|
{
|
|
|
|
LOG_ERR("PutFile says that Document changed in storage");
|
2017-06-01 07:56:54 -05:00
|
|
|
_documentChangedInStorage = true;
|
2017-06-19 09:30:37 -05:00
|
|
|
if (_isModified)
|
|
|
|
{
|
|
|
|
broadcastMessage("error: cmd=storage kind=documentconflict");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
closeDocument("documentconflict");
|
|
|
|
}
|
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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// Save the document using first session available ...
|
|
|
|
if (savingSessionId.empty())
|
|
|
|
{
|
|
|
|
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 << "].");
|
2017-05-15 00:59:04 -05:00
|
|
|
sent = sendUnoSave(savingSessionId);
|
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
|
|
|
{
|
2017-01-01 18:42:19 -06:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
const auto inactivityTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastActivityTime).count();
|
|
|
|
const auto 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
|
|
|
|
2017-08-24 06:10:39 -05:00
|
|
|
static const auto idleSaveDurationMs = LOOLWSD::getConfigValue<int>("per_document.idlesave_duration_secs", 30) * 1000;
|
|
|
|
static const auto 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);
|
|
|
|
_storage->setUserModified(_isModified);
|
2017-10-03 04:59:39 -05:00
|
|
|
_storage->setIsAutosave(isAutosave || UnitWSD::get().isAutosave());
|
2017-09-25 12:16:48 -05:00
|
|
|
|
2016-08-31 22:34:41 -05:00
|
|
|
const auto 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)
|
|
|
|
{
|
|
|
|
LOG_ERR("Failed to add session to [" << _docKey << "] with URI [" << session->getPublicUri().toString() << "]: " << exc.what());
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-31 11:55:26 -05:00
|
|
|
// Below values are recalculated when destroyIfLastEditor() is called (before destroying the
|
2016-11-06 21:11:59 -06:00
|
|
|
// document). It is safe to reset their values to their defaults whenever a new session is added.
|
|
|
|
_lastEditableSession = false;
|
|
|
|
_markToDestroy = false;
|
2017-03-10 11:58:51 -06:00
|
|
|
_stop = false;
|
2016-11-06 21:11:59 -06:00
|
|
|
|
2017-03-25 13:19:17 -05:00
|
|
|
const auto id = session->getId();
|
2016-11-06 22:14:23 -06:00
|
|
|
|
|
|
|
// Request a new session from the child kit.
|
2017-04-02 14:55:56 -05:00
|
|
|
const std::string aMessage = "session " + id + ' ' + _docKey + ' ' + _docId;
|
2016-11-06 22:14:23 -06:00
|
|
|
_childProcess->sendTextFrame(aMessage);
|
|
|
|
|
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());
|
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();
|
|
|
|
|
|
|
|
const auto 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
|
|
|
}
|
|
|
|
|
2017-04-11 01:54:09 -05:00
|
|
|
size_t DocumentBroker::removeSession(const std::string& id, bool destroyIfLast)
|
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
|
|
|
|
|
|
|
if (destroyIfLast)
|
|
|
|
destroyIfLastEditor(id);
|
2016-04-10 12:06:35 -05:00
|
|
|
|
2016-11-27 20:28:21 -06:00
|
|
|
try
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
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 <<
|
|
|
|
", LastEditableSession: " << _lastEditableSession);
|
2017-01-08 11:22:21 -06:00
|
|
|
|
2017-03-12 13:13:48 -05:00
|
|
|
if (!_lastEditableSession || !autoSave(true))
|
2017-03-09 22:50:36 -06:00
|
|
|
return removeSessionInternal(id);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
2016-11-27 20:28:21 -06:00
|
|
|
Admin::instance().rmDoc(_docKey, id);
|
2016-10-09 15:37:13 -05:00
|
|
|
|
2016-11-27 20:28:21 -06:00
|
|
|
auto it = _sessions.find(id);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2017-02-05 20:59:08 -06:00
|
|
|
LOOLWSD::dumpEndSessionTrace(getJailId(), id, _uriOrig);
|
2017-02-05 18:35:54 -06:00
|
|
|
|
2017-01-03 14:47:31 -06:00
|
|
|
const auto 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);
|
2016-04-16 16:18:51 -05:00
|
|
|
|
2017-01-02 23:12:15 -06:00
|
|
|
const auto count = _sessions.size();
|
2017-01-03 14:47:31 -06:00
|
|
|
LOG_TRC("Removed " << (readonly ? "readonly" : "non-readonly") <<
|
2017-01-02 23:12:15 -06:00
|
|
|
" session [" << id << "] from docKey [" <<
|
|
|
|
_docKey << "] to have " << count << " sessions.");
|
2017-04-04 23:26:09 -05:00
|
|
|
for (const auto& pair : _sessions)
|
|
|
|
{
|
|
|
|
LOG_TRC("Session: " << pair.second->getName());
|
|
|
|
}
|
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
|
|
|
|
2016-09-19 18:18:07 -05:00
|
|
|
LOOLWSD::dumpOutgoingTrace(getJailId(), "0", msg);
|
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);
|
|
|
|
}
|
2017-08-18 08:19:40 -05:00
|
|
|
else if (command == "dialogpaint:")
|
|
|
|
{
|
2017-08-24 06:47:40 -05:00
|
|
|
handleDialogPaintResponse(payload, false);
|
|
|
|
}
|
|
|
|
else if (command == "dialogchildpaint:")
|
|
|
|
{
|
|
|
|
handleDialogPaintResponse(payload, true);
|
2017-08-18 08:19:40 -05:00
|
|
|
}
|
2017-01-22 22:18:37 -06:00
|
|
|
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);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
const auto 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);
|
2016-08-31 22:41:09 -05:00
|
|
|
const auto 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
|
|
|
}
|
|
|
|
|
2017-08-18 08:19:40 -05:00
|
|
|
void DocumentBroker::handleDialogRequest(const std::string& dialogId,
|
2017-08-24 06:47:40 -05:00
|
|
|
const std::shared_ptr<ClientSession>& /*session*/,
|
|
|
|
bool child)
|
2017-08-18 08:19:40 -05:00
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2017-08-24 06:47:40 -05:00
|
|
|
const std::string dialogCmd = child ? "dialogchild" : "dialog";
|
|
|
|
LOG_DBG("Sending " + dialogCmd + " render request for dialog " << dialogId);
|
|
|
|
_childProcess->sendTextFrame(dialogCmd + " " + dialogId);
|
2017-08-18 08:19:40 -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
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
// Satisfy as many tiles from the cache.
|
2016-09-25 15:04:27 -05:00
|
|
|
std::vector<TileDesc> tiles;
|
2016-05-29 16:40:27 -05:00
|
|
|
for (auto& tile : tileCombined.getTiles())
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
|
|
|
std::unique_ptr<std::fstream> cachedTile = _tileCache->lookupTile(tile);
|
|
|
|
if (cachedTile)
|
|
|
|
{
|
2016-08-18 09:28:55 -05:00
|
|
|
//TODO: Combine the response to reduce latency.
|
2016-05-22 10:45:28 -05:00
|
|
|
#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;
|
2016-07-18 03:25:05 -05:00
|
|
|
output.reserve(static_cast<size_t>(4) * tile.getWidth() * tile.getHeight());
|
2016-05-22 10:45:28 -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);
|
2016-09-13 17:40:23 -05:00
|
|
|
const auto pos = output.size();
|
2016-05-22 10:45:28 -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());
|
|
|
|
}
|
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.
|
2016-09-19 19:29:40 -05:00
|
|
|
tile.setVersion(++_tileVersion);
|
2016-09-21 17:31:13 -05:00
|
|
|
tileCache().subscribeToTileRendering(tile, session);
|
2016-09-25 15:04:27 -05:00
|
|
|
tiles.push_back(tile);
|
2016-10-11 07:39:56 -05:00
|
|
|
_debugRenderedTileCount++;
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
2016-05-29 16:40:27 -05:00
|
|
|
}
|
2016-09-25 15:04:27 -05:00
|
|
|
|
|
|
|
if (!tiles.empty())
|
|
|
|
{
|
|
|
|
auto newTileCombined = TileCombined::create(tiles);
|
|
|
|
|
|
|
|
// Forward to child to render.
|
|
|
|
const auto 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
|
|
|
}
|
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);
|
|
|
|
|
2016-09-19 21:16:45 -05:00
|
|
|
const auto canceltiles = tileCache().cancelTiles(session);
|
|
|
|
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
|
|
|
{
|
2016-09-22 16:47:12 -05:00
|
|
|
const auto 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
|
|
|
{
|
2016-10-30 14:24:27 -05:00
|
|
|
const auto tile = TileDesc::parse(firstLine);
|
|
|
|
const auto buffer = payload.data();
|
2016-10-29 20:15:00 -05:00
|
|
|
const auto 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
|
|
|
}
|
|
|
|
|
2017-08-24 06:47:40 -05:00
|
|
|
void DocumentBroker::handleDialogPaintResponse(const std::vector<char>& payload, bool child)
|
2017-08-18 08:19:40 -05:00
|
|
|
{
|
|
|
|
const std::string firstLine = getFirstLine(payload);
|
2017-08-24 06:47:40 -05:00
|
|
|
LOG_DBG("Handling " << (child ? "dialogchildpaint" : "dialogpaint") << " " << firstLine);
|
2017-08-18 08:19:40 -05:00
|
|
|
|
|
|
|
const auto length = payload.size();
|
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
|
|
|
{
|
|
|
|
const auto buffer = payload.data();
|
|
|
|
const auto offset = firstLine.size() + 1;
|
|
|
|
|
|
|
|
auto msgPayload = std::make_shared<Message>(firstLine,
|
|
|
|
Message::Dir::Out,
|
|
|
|
payload.size());
|
|
|
|
msgPayload->append("\n", 1);
|
|
|
|
msgPayload->append(buffer + offset, payload.size() - offset);
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
for (const auto& sessionIt : _sessions)
|
|
|
|
{
|
|
|
|
sessionIt.second->enqueueSendMessage(msgPayload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2016-09-22 16:47:12 -05:00
|
|
|
const auto length = payload.size();
|
2016-05-22 10:45:28 -05:00
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
|
|
|
{
|
2016-10-30 14:24:27 -05:00
|
|
|
const auto tileCombined = TileCombined::parse(firstLine);
|
|
|
|
const auto buffer = payload.data();
|
|
|
|
auto offset = firstLine.size() + 1;
|
2017-01-25 19:55:39 -06:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
for (const auto& tile : tileCombined.getTiles())
|
|
|
|
{
|
2016-09-25 10:33:37 -05:00
|
|
|
tileCache().saveTileAndNotify(tile, buffer + offset, tile.getImgSize());
|
2016-05-22 10:45:28 -05:00
|
|
|
offset += tile.getImgSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-02 23:45:40 -05:00
|
|
|
LOG_WRN("Dropping empty tilecombine response: " << firstLine);
|
2016-10-30 14:24:27 -05:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 12:19:53 -06:00
|
|
|
void DocumentBroker::destroyIfLastEditor(const std::string& id)
|
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
|
|
|
|
2016-10-08 09:31:35 -05:00
|
|
|
const auto currentSession = _sessions.find(id);
|
2017-03-07 20:17:24 -06:00
|
|
|
if (currentSession == _sessions.end())
|
|
|
|
{
|
|
|
|
// We could be called before adding any sessions.
|
|
|
|
// For example when a socket disconnects before loading.
|
2017-03-09 12:19:53 -06:00
|
|
|
return;
|
2017-03-07 20:17:24 -06:00
|
|
|
}
|
2016-07-10 23:50:25 -05:00
|
|
|
|
2016-10-08 09:31:35 -05:00
|
|
|
// Check if the session being destroyed is the last non-readonly session or not.
|
|
|
|
_lastEditableSession = !currentSession->second->isReadOnly();
|
|
|
|
if (_lastEditableSession && !_sessions.empty())
|
2016-07-10 23:50:25 -05:00
|
|
|
{
|
2016-10-29 20:15:00 -05:00
|
|
|
for (const auto& it : _sessions)
|
2016-07-10 23:50:25 -05:00
|
|
|
{
|
2016-10-08 09:31:35 -05:00
|
|
|
if (it.second->getId() != id &&
|
2017-04-20 21:17:08 -05:00
|
|
|
it.second->isViewLoaded() &&
|
2016-10-08 09:31:35 -05:00
|
|
|
!it.second->isReadOnly())
|
|
|
|
{
|
|
|
|
// Found another editable.
|
|
|
|
_lastEditableSession = false;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-10 23:50:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-08 09:07:17 -05:00
|
|
|
// Last view going away, can destroy.
|
|
|
|
_markToDestroy = (_sessions.size() <= 1);
|
2017-01-09 23:26:24 -06:00
|
|
|
LOG_DBG("startDestroy on session [" << id << "] on docKey [" << _docKey <<
|
2017-04-19 23:10:30 -05:00
|
|
|
"], sessions: " << _sessions.size() << " markToDestroy: " << _markToDestroy <<
|
|
|
|
", lastEditableSession: " << _lastEditableSession);
|
2016-04-17 22:29:03 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
void DocumentBroker::setModified(const bool value)
|
|
|
|
{
|
2017-05-12 08:59:01 -05:00
|
|
|
if(_isModified != value)
|
|
|
|
{
|
|
|
|
_isModified = value;
|
|
|
|
Admin::instance().modificationAlert(_docKey, getPid(), value);
|
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
_tileCache->setUnsavedChanges(value);
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
msg += " jail=" + _uriJailed.toString() + ' ';
|
|
|
|
msg += Poco::cat(std::string(" "), tokens.begin() + 3, tokens.end());
|
|
|
|
}
|
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.
|
|
|
|
removeSession(session->getId(), true);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
// Close all running sessions
|
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
|
|
|
|
2017-03-07 20:18:59 -06:00
|
|
|
// First flag to stop as it might be waiting on our lock
|
|
|
|
// to process some incoming message.
|
2017-06-21 05:07:38 -05:00
|
|
|
_childProcess->stop();
|
|
|
|
_childProcess->close(false);
|
2017-03-07 20:18:59 -06:00
|
|
|
}
|
2017-03-12 21:31:50 -05:00
|
|
|
|
2017-03-26 22:09:59 -05:00
|
|
|
_stop = true;
|
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);
|
2017-07-07 06:42:19 -05:00
|
|
|
_closeReason = reason; // used later in the polling loop
|
|
|
|
stop();
|
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();
|
2016-12-05 14:11:07 -06:00
|
|
|
Admin::instance().updateLastActivityTime(_docKey);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2017-03-18 09:59:09 -05:00
|
|
|
os << " Broker: " << _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;
|
2017-03-18 09:59:09 -05:00
|
|
|
os << "\n filename: " << _filename;
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n public uri: " << _uriPublic.toString();
|
|
|
|
os << "\n jailed uri: " << _uriJailed.toString();
|
|
|
|
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();
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n last editable?: " << _lastEditableSession;
|
2017-03-18 09:59:09 -05:00
|
|
|
std::time_t t = std::chrono::system_clock::to_time_t(
|
|
|
|
std::chrono::system_clock::now()
|
|
|
|
+ (_lastSaveTime - std::chrono::steady_clock::now()));
|
|
|
|
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: */
|