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 "DocumentBroker.hpp"
|
|
|
|
#include "config.h"
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cassert>
|
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"
|
2017-01-22 22:12:51 -06:00
|
|
|
#include "Message.hpp"
|
2016-11-24 08:56:06 -06:00
|
|
|
#include "Protocol.hpp"
|
2016-04-17 10:05:39 -05:00
|
|
|
#include "LOOLWSD.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 "Log.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
#include "Storage.hpp"
|
|
|
|
#include "TileCache.hpp"
|
2016-12-10 21:33:45 -06:00
|
|
|
#include "SenderQueue.hpp"
|
2016-05-20 21:31:53 -05:00
|
|
|
#include "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
|
|
|
using Poco::StringTokenizer;
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
void ChildProcess::socketProcessor()
|
|
|
|
{
|
2016-11-13 21:23:04 -06:00
|
|
|
const auto name = "docbrk_ws_" + std::to_string(_pid);
|
|
|
|
Util::setThreadName(name);
|
2016-11-06 21:29:19 -06:00
|
|
|
|
2016-11-13 21:23:04 -06:00
|
|
|
IoUtil::SocketProcessor(_ws, name,
|
2016-05-01 19:50:11 -05:00
|
|
|
[this](const std::vector<char>& payload)
|
|
|
|
{
|
2016-05-20 21:31:53 -05:00
|
|
|
if (UnitWSD::get().filterChildMessage(payload))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
auto docBroker = this->_docBroker.lock();
|
|
|
|
if (docBroker)
|
|
|
|
{
|
2016-11-06 22:01:29 -06:00
|
|
|
// We should never destroy the broker, since
|
|
|
|
// it owns us and will wait on this thread.
|
|
|
|
assert(docBroker.use_count() > 1);
|
2016-05-01 19:50:11 -05:00
|
|
|
return docBroker->handleInput(payload);
|
|
|
|
}
|
|
|
|
|
2016-11-05 16:48:15 -05:00
|
|
|
LOG_WRN("Child " << this->_pid <<
|
2016-12-21 20:07:18 -06:00
|
|
|
" has no DocumentBroker to handle message: [" <<
|
|
|
|
LOOLProtocol::getAbbreviatedMessage(payload) << "].");
|
2016-05-01 19:50:11 -05:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[]() { },
|
2016-10-14 22:01:14 -05:00
|
|
|
[this]() { return TerminationFlag || this->_stop; });
|
2016-10-15 16:07:40 -05:00
|
|
|
|
2016-11-05 16:48:15 -05:00
|
|
|
LOG_DBG("Child [" << getPid() << "] WS terminated. Notifying DocBroker.");
|
2016-10-15 16:07:40 -05:00
|
|
|
|
|
|
|
// Notify the broker that we're done.
|
|
|
|
auto docBroker = _docBroker.lock();
|
2016-10-22 18:10:07 -05:00
|
|
|
if (docBroker && !_stop)
|
2016-10-15 16:07:40 -05:00
|
|
|
{
|
2016-10-22 18:10:07 -05:00
|
|
|
// No need to notify if asked to stop.
|
2016-10-15 16:07:40 -05:00
|
|
|
docBroker->childSocketTerminated();
|
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
}
|
2016-03-23 07:41:18 -05:00
|
|
|
|
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());
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
return (LOOLWSD::Cache + '/' +
|
2016-03-26 06:50:13 -05:00
|
|
|
Poco::DigestEngine::digestToHex(digestEngine.digest()).insert(3, "/").insert(2, "/").insert(1, "/"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-11-19 08:50:42 -06:00
|
|
|
// If multiple host-names are used to access us, then
|
|
|
|
// 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
|
|
|
|
// alias hosts) to load as separate documents and sharing doesn't
|
|
|
|
// work. Worse, saving overwrites one another.
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string docKey;
|
2016-11-19 08:50:42 -06:00
|
|
|
Poco::URI::encode(uri.getPath(), "", docKey);
|
2016-03-23 07:41:18 -05:00
|
|
|
return docKey;
|
|
|
|
}
|
|
|
|
|
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,
|
2016-04-03 20:40:14 -05:00
|
|
|
const std::string& childRoot,
|
2016-10-14 21:52:33 -05:00
|
|
|
const std::shared_ptr<ChildProcess>& childProcess) :
|
2017-02-05 20:59:08 -06:00
|
|
|
_uriOrig(uri),
|
2016-03-23 07:41:18 -05:00
|
|
|
_uriPublic(uriPublic),
|
|
|
|
_docKey(docKey),
|
|
|
|
_childRoot(childRoot),
|
2016-03-26 06:50:13 -05:00
|
|
|
_cacheRoot(getCachePath(uriPublic.toString())),
|
2016-10-14 21:52:33 -05:00
|
|
|
_childProcess(childProcess),
|
2016-04-24 10:08:08 -05:00
|
|
|
_lastSaveTime(std::chrono::steady_clock::now()),
|
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),
|
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
|
|
|
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_INF("DocumentBroker [" << _uriPublic.toString() << "] created. DocKey: [" << _docKey << "]");
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2016-10-22 09:25:57 -05:00
|
|
|
DocumentBroker::~DocumentBroker()
|
|
|
|
{
|
|
|
|
Admin::instance().rmDoc(_docKey);
|
|
|
|
|
2016-11-03 23:50:51 -05:00
|
|
|
LOG_INF("~DocumentBroker [" << _uriPublic.toString() <<
|
2016-11-06 10:59:59 -06:00
|
|
|
"] destroyed with " << _sessions.size() << " sessions left.");
|
2016-10-30 13:31:34 -05:00
|
|
|
|
2016-11-03 22:05:48 -05:00
|
|
|
if (!_sessions.empty())
|
|
|
|
{
|
|
|
|
LOG_WRN("DocumentBroker still has unremoved sessions.");
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-11-06 22:14:23 -06:00
|
|
|
bool DocumentBroker::load(std::shared_ptr<ClientSession>& session, const std::string& jailId)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
2016-11-06 22:14:23 -06:00
|
|
|
Util::assertIsLocked(_mutex);
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-11-06 21:11:35 -06:00
|
|
|
const Poco::URI& uriPublic = session->getPublicUri();
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Loading from URI: " << uriPublic.toString());
|
2016-10-16 11:40:52 -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();
|
2017-02-06 16:26:38 -06:00
|
|
|
#ifndef KIT_IN_PROCESS
|
2016-08-01 03:05:37 -05:00
|
|
|
if (LOOLWSD::NoCapsForKit)
|
2016-11-06 12:54:00 -06:00
|
|
|
{
|
2016-08-01 03:05:37 -05:00
|
|
|
jailRoot = jailPath.toString() + "/" + getJailRoot();
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
2017-02-06 16:26:38 -06:00
|
|
|
#endif
|
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.
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("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;
|
|
|
|
}
|
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;
|
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-01-21 21:43:12 -06:00
|
|
|
std::unique_ptr<WopiStorage::WOPIFileInfo> wopifileinfo =
|
|
|
|
wopiStorage->getWOPIFileInfo(uriPublic);
|
2016-12-13 03:13:58 -06:00
|
|
|
userid = wopifileinfo->_userid;
|
|
|
|
username = wopifileinfo->_username;
|
2016-10-25 02:13:00 -05:00
|
|
|
|
2016-12-13 03:13:58 -06:00
|
|
|
if (!wopifileinfo->_userCanWrite)
|
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
|
|
|
{
|
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);
|
2016-11-10 06:21:39 -06:00
|
|
|
|
|
|
|
std::ostringstream ossWopiInfo;
|
|
|
|
wopiInfo->stringify(ossWopiInfo);
|
|
|
|
session->sendTextFrame("wopi: " + ossWopiInfo.str());
|
|
|
|
|
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
|
|
|
{
|
|
|
|
LOG_DBG("Session [" + sessionId + "] is the document owner");
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::unique_ptr<LocalStorage::LocalFileInfo> localfileinfo =
|
|
|
|
localStorage->getLocalFileInfo(uriPublic);
|
|
|
|
userid = localfileinfo->_userid;
|
|
|
|
username = localfileinfo->_username;
|
|
|
|
}
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
2016-10-14 05:09:43 -05:00
|
|
|
|
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);
|
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())
|
|
|
|
{
|
|
|
|
LOG_ERR("Invalid fileinfo for URI [" << uriPublic.toString() << "].");
|
|
|
|
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;
|
|
|
|
LOG_DBG("Document timestamp: " << Poco::DateTimeFormatter::format(Poco::DateTime(_documentLastModifiedTime),
|
|
|
|
Poco::DateTimeFormat::ISO8601_FORMAT));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check if document has been modified by some external action
|
|
|
|
LOG_DBG("Timestamp now: " << Poco::DateTimeFormatter::format(Poco::DateTime(fileInfo._modifiedTime),
|
|
|
|
Poco::DateTimeFormat::ISO8601_FORMAT));
|
2016-12-21 06:06:07 -06:00
|
|
|
if (_documentLastModifiedTime != Poco::Timestamp::fromEpochTime(0) &&
|
|
|
|
fileInfo._modifiedTime != Poco::Timestamp::fromEpochTime(0) &&
|
|
|
|
_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
|
|
|
{
|
|
|
|
LOG_ERR("Document has been modified behind our back, URI [" << uriPublic.toString() << "].");
|
|
|
|
// What do do?
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
const auto localPath = _storage->loadStorageFileToLocal();
|
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.
|
|
|
|
_lastFileModifiedTime = Poco::File(_storage->getLocalRootPath()).getLastModified();
|
2017-01-21 21:43:12 -06:00
|
|
|
_tileCache.reset(new TileCache(uriPublic.toString(), _lastFileModifiedTime, _cacheRoot));
|
2017-02-05 18:35:54 -06:00
|
|
|
|
2017-02-05 20:59:08 -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
|
|
|
}
|
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
bool DocumentBroker::save(const std::string& sessionId, bool success, const std::string& result)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
2016-04-10 21:07:09 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_saveMutex);
|
|
|
|
|
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.
|
|
|
|
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-01-09 12:54:37 -06:00
|
|
|
lock.unlock();
|
2017-01-01 11:08:12 -06:00
|
|
|
_saveCV.notify_all();
|
|
|
|
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 << "].");
|
2017-01-09 12:54:37 -06:00
|
|
|
lock.unlock();
|
2017-01-01 11:08:12 -06:00
|
|
|
_saveCV.notify_all();
|
2016-10-16 11:40:52 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Poco::URI& uriPublic = it->second->getPublicUri();
|
|
|
|
const auto uri = uriPublic.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.
|
2016-04-25 19:44:24 -05:00
|
|
|
const auto newFileModifiedTime = Poco::File(_storage->getLocalRootPath()).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-01-09 12:54:37 -06:00
|
|
|
lock.unlock();
|
2017-01-01 11:08:12 -06:00
|
|
|
_saveCV.notify_all();
|
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
|
|
|
|
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
|
|
|
// FIXME: We should check before persisting the document that it hasn't been updated in its
|
|
|
|
// storage behind our backs.
|
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
assert(_storage && _tileCache);
|
2016-11-23 06:09:54 -06:00
|
|
|
StorageBase::SaveResult storageSaveResult = _storage->saveLocalFileToStorage(uriPublic);
|
|
|
|
if (storageSaveResult == StorageBase::SaveResult::OK)
|
2016-03-26 08:10:53 -05:00
|
|
|
{
|
2016-04-21 23:11:24 -05:00
|
|
|
_isModified = false;
|
2016-04-22 06:00:11 -05:00
|
|
|
_tileCache->setUnsavedChanges(false);
|
2016-04-25 19:44:24 -05:00
|
|
|
_lastFileModifiedTime = newFileModifiedTime;
|
|
|
|
_tileCache->saveLastModified(_lastFileModifiedTime);
|
|
|
|
_lastSaveTime = std::chrono::steady_clock::now();
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
|
|
|
|
// Calling getWOPIFileInfo() or getLocalFileInfo() has the side-effect of updating
|
|
|
|
// StorageBase::_fileInfo. Get the timestamp of the document as persisted in its storage
|
|
|
|
// from there.
|
|
|
|
// FIXME: Yes, of course we should turn this stuff into a virtual function and avoid this
|
|
|
|
// dynamic_cast dance.
|
|
|
|
if (dynamic_cast<WopiStorage*>(_storage.get()) != nullptr)
|
|
|
|
{
|
|
|
|
auto wopiFileInfo = static_cast<WopiStorage*>(_storage.get())->getWOPIFileInfo(uriPublic);
|
|
|
|
}
|
|
|
|
else if (dynamic_cast<LocalStorage*>(_storage.get()) != nullptr)
|
|
|
|
{
|
|
|
|
auto localFileInfo = static_cast<LocalStorage*>(_storage.get())->getLocalFileInfo(uriPublic);
|
|
|
|
}
|
|
|
|
// So set _documentLastModifiedTime then
|
|
|
|
_documentLastModifiedTime = _storage->getFileInfo()._modifiedTime;
|
|
|
|
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_DBG("Saved docKey [" << _docKey << "] to URI [" << uri << "] and updated tile cache. Document modified timestamp: " <<
|
|
|
|
Poco::DateTimeFormatter::format(Poco::DateTime(_documentLastModifiedTime),
|
|
|
|
Poco::DateTimeFormat::ISO8601_FORMAT));
|
2017-01-09 12:54:37 -06:00
|
|
|
lock.unlock();
|
2016-04-10 21:07:09 -05:00
|
|
|
_saveCV.notify_all();
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
2016-03-26 08:10:53 -05:00
|
|
|
|
2017-01-09 12:54:37 -06:00
|
|
|
lock.unlock();
|
2017-01-01 11:08:12 -06:00
|
|
|
_saveCV.notify_all();
|
2016-03-26 08:10:53 -05:00
|
|
|
return false;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2016-11-05 21:15:44 -05:00
|
|
|
bool DocumentBroker::autoSave(const bool force, const size_t waitTimeoutMs, std::unique_lock<std::mutex>& lock)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-11-05 21:15:44 -05:00
|
|
|
Util::assertIsLocked(lock);
|
|
|
|
|
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 << "].");
|
2016-04-29 22:07:09 -05:00
|
|
|
return true;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
|
|
|
|
2016-05-09 00:11:09 -05:00
|
|
|
// Remeber the last save time, since this is the predicate.
|
|
|
|
const auto lastSaveTime = _lastSaveTime;
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Checking to autosave [" << _docKey << "].");
|
2016-05-09 00:11:09 -05:00
|
|
|
|
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 << "].");
|
2016-07-14 04:49:21 -05:00
|
|
|
sent = sendUnoSave(true);
|
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
|
|
|
|
2016-05-02 21:58:53 -05:00
|
|
|
// Either we've been idle long enough, or it's auto-save time.
|
|
|
|
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 << "].");
|
2016-07-14 04:49:21 -05:00
|
|
|
sent = sendUnoSave(true);
|
2016-04-25 19:48:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sent && waitTimeoutMs > 0)
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Waiting for save event for [" << _docKey << "].");
|
2017-01-01 14:39:23 -06:00
|
|
|
_saveCV.wait_for(lock, std::chrono::milliseconds(waitTimeoutMs));
|
|
|
|
if (lastSaveTime != _lastSaveTime)
|
2016-04-25 19:48:02 -05:00
|
|
|
{
|
2017-01-01 11:06:05 -06:00
|
|
|
LOG_DBG("Successfully persisted document [" << _docKey << "] or document was not modified.");
|
2016-04-25 19:48:02 -05:00
|
|
|
return true;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
2016-04-25 19:48:02 -05:00
|
|
|
|
2017-01-01 14:39:23 -06:00
|
|
|
return false;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
2016-04-10 21:07:09 -05:00
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
return sent;
|
2016-04-10 21:07:09 -05:00
|
|
|
}
|
|
|
|
|
2016-07-14 04:49:21 -05:00
|
|
|
bool DocumentBroker::sendUnoSave(const bool dontSaveIfUnmodified)
|
2016-04-10 21:07:09 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_INF("Autosave triggered for doc [" << _docKey << "].");
|
2016-05-02 18:17:46 -05:00
|
|
|
Util::assertIsLocked(_mutex);
|
2016-04-10 21:07:09 -05:00
|
|
|
|
2016-11-22 11:40:10 -06:00
|
|
|
std::shared_ptr<ClientSession> savingSession;
|
2016-10-29 20:15:00 -05:00
|
|
|
for (auto& sessionIt : _sessions)
|
2016-11-22 11:40:10 -06:00
|
|
|
{
|
|
|
|
// Save the document using first session available ...
|
|
|
|
if (!savingSession)
|
|
|
|
{
|
|
|
|
savingSession = sessionIt.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// or if any of the sessions is document owner, use that.
|
|
|
|
if (sessionIt.second->isDocumentOwner())
|
|
|
|
{
|
|
|
|
savingSession = sessionIt.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (savingSession)
|
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
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
// Mention DontTerminateEdit always
|
|
|
|
oss << "\"DontTerminateEdit\":"
|
|
|
|
<< "{"
|
|
|
|
<< "\"type\":\"boolean\","
|
|
|
|
<< "\"value\":true"
|
|
|
|
<< "}";
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
// Mention DontSaveIfUnmodified
|
|
|
|
if (dontSaveIfUnmodified)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
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;
|
2016-11-22 11:40:10 -06:00
|
|
|
forwardToChild(savingSession->getId(), command);
|
2016-09-19 02:03:31 -05:00
|
|
|
return true;
|
2016-04-10 21:07:09 -05:00
|
|
|
}
|
|
|
|
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Failed to auto-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();
|
|
|
|
}
|
|
|
|
|
2016-05-16 06:46:27 -05:00
|
|
|
size_t DocumentBroker::addSession(std::shared_ptr<ClientSession>& session)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2017-01-10 16:10:10 -06:00
|
|
|
Util::assertIsLocked(_mutex);
|
|
|
|
|
2016-11-06 21:11:59 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// First load the document, since this can fail.
|
2016-11-06 22:14:23 -06:00
|
|
|
if (!load(session, std::to_string(_childProcess->getPid())))
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-06 21:11:59 -06:00
|
|
|
// Below values are recalculated when startDestroy() is called (before destroying the
|
|
|
|
// document). It is safe to reset their values to their defaults whenever a new session is added.
|
|
|
|
_lastEditableSession = false;
|
|
|
|
_markToDestroy = false;
|
|
|
|
|
2017-01-13 17:10:10 -06:00
|
|
|
const auto id = session->getId();
|
2016-11-06 22:14:23 -06:00
|
|
|
if (!_sessions.emplace(id, session).second)
|
|
|
|
{
|
|
|
|
LOG_WRN("DocumentBroker: Trying to add already existing session.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto count = _sessions.size();
|
|
|
|
|
|
|
|
// Request a new session from the child kit.
|
2017-01-13 17:10:10 -06:00
|
|
|
const std::string aMessage = "session " + id + ' ' + _docKey;
|
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
|
|
|
|
Admin::instance().addDoc(_docKey, getPid(), getFilename(), id);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
size_t DocumentBroker::removeSession(const std::string& id)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-11-05 21:15:44 -05:00
|
|
|
Util::assertIsLocked(_mutex);
|
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 <<
|
|
|
|
"]. Have " << _sessions.size() << " sessions.");
|
|
|
|
|
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);
|
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-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
|
|
|
}
|
|
|
|
|
2016-11-17 08:00:05 -06:00
|
|
|
void DocumentBroker::alertAllUsers(const std::string& msg)
|
2016-09-28 14:07:07 -05:00
|
|
|
{
|
2016-11-06 21:11:59 -06:00
|
|
|
Util::assertIsLocked(_mutex);
|
2016-09-28 14:07:07 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Render request declined for " << firstLine);
|
2016-10-30 14:24:27 -05:00
|
|
|
// They will get re-issued if we don't forget them.
|
2016-05-15 17:47:08 -05:00
|
|
|
}
|
2016-05-05 01:37:40 -05:00
|
|
|
}
|
2016-05-15 17:47:08 -05:00
|
|
|
catch (const std::exception& exc)
|
2016-05-05 01:37:40 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Failed to process tile response [" << firstLine << "]: " << exc.what() << ".");
|
2016-05-05 01:37:40 -05:00
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
void DocumentBroker::handleTileCombinedResponse(const std::vector<char>& payload)
|
|
|
|
{
|
|
|
|
const std::string firstLine = getFirstLine(payload);
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Handling tile combined: " << firstLine);
|
2016-05-22 10:45:28 -05:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
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
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Render request declined for " << 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 09:31:35 -05:00
|
|
|
bool DocumentBroker::startDestroy(const std::string& id)
|
2016-04-17 22:29:03 -05:00
|
|
|
{
|
2016-11-05 21:15:44 -05:00
|
|
|
Util::assertIsLocked(_mutex);
|
2016-04-17 22:29:03 -05:00
|
|
|
|
2016-10-08 09:31:35 -05:00
|
|
|
const auto currentSession = _sessions.find(id);
|
2016-07-10 23:50:25 -05:00
|
|
|
assert(currentSession != _sessions.end());
|
|
|
|
|
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-01-11 15:45:14 -06:00
|
|
|
it.second->isLoaded() &&
|
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 <<
|
|
|
|
"], markToDestroy: " << _markToDestroy << ", lastEditableSession: " << _lastEditableSession);
|
2016-10-08 09:31:35 -05:00
|
|
|
return _lastEditableSession;
|
2016-04-17 22:29:03 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
void DocumentBroker::setModified(const bool value)
|
|
|
|
{
|
|
|
|
_tileCache->setUnsavedChanges(value);
|
|
|
|
_isModified = 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
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Forwarding payload to child [" << viewId << "]: " << message);
|
2016-10-08 13:25:27 -05:00
|
|
|
|
|
|
|
const auto it = _sessions.find(viewId);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2016-10-08 17:21:35 -05:00
|
|
|
const auto msg = "child-" + viewId + ' ' + message;
|
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
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_WRN("Client 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-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();
|
|
|
|
|
2017-01-25 19:55:39 -06:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2017-02-06 19:10:08 -06:00
|
|
|
if (sid == "all")
|
2016-10-08 12:13:23 -05:00
|
|
|
{
|
2017-02-06 19:10:08 -06:00
|
|
|
// Broadcast to all.
|
|
|
|
for (const auto& pair : _sessions)
|
|
|
|
{
|
|
|
|
if (!pair.second->isHeadless() && !pair.second->isCloseFrame())
|
|
|
|
{
|
|
|
|
pair.second->handleKitToClientMessage(data, size);
|
|
|
|
}
|
|
|
|
}
|
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())
|
|
|
|
{
|
|
|
|
return it->second->handleKitToClientMessage(data, size);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:07:40 -05:00
|
|
|
void DocumentBroker::childSocketTerminated()
|
|
|
|
{
|
2016-11-01 18:38:25 -05:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
|
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.
|
|
|
|
for (auto& pair : _sessions)
|
|
|
|
{
|
2016-11-19 09:04:42 -06:00
|
|
|
try
|
|
|
|
{
|
2017-02-04 23:26:55 -06:00
|
|
|
pair.second->shutdown(Poco::Net::WebSocket::WS_ENDPOINT_GOING_AWAY, "");
|
2016-11-19 09:04:42 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Error while terminating client connection [" << pair.first << "]: " << ex.what());
|
|
|
|
}
|
2016-10-15 16:07:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
void DocumentBroker::terminateChild(std::unique_lock<std::mutex>& lock, const std::string& closeReason)
|
2016-11-05 22:00:16 -05:00
|
|
|
{
|
|
|
|
Util::assertIsLocked(_mutex);
|
|
|
|
Util::assertIsLocked(lock);
|
|
|
|
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_INF("Terminating child [" << getPid() << "] of doc [" << _docKey << "].");
|
2016-11-05 22:00:16 -05:00
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
// Close all running sessions
|
2016-12-30 16:17:07 -06:00
|
|
|
for (const auto& pair : _sessions)
|
2016-11-08 07:37:28 -06:00
|
|
|
{
|
2016-11-19 09:04:42 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
pair.second->shutdown(Poco::Net::WebSocket::WS_ENDPOINT_GOING_AWAY, closeReason);
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Error while terminating client connection [" << pair.first << "]: " << ex.what());
|
|
|
|
}
|
2016-11-08 07:37:28 -06:00
|
|
|
}
|
|
|
|
|
2016-11-05 22:00:16 -05:00
|
|
|
// First flag to stop as it might be waiting on our lock
|
|
|
|
// to process some incoming message.
|
|
|
|
_childProcess->stop();
|
|
|
|
|
|
|
|
// Release the lock and wait for the thread to finish.
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
_childProcess->close(false);
|
|
|
|
}
|
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
void DocumentBroker::closeDocument(const std::string& reason)
|
|
|
|
{
|
|
|
|
auto lock = getLock();
|
|
|
|
|
|
|
|
terminateChild(lock, reason);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|