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>
|
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>
|
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"
|
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 "LOOLProtocol.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"
|
|
|
|
#include "PrisonerSession.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
#include "Storage.hpp"
|
|
|
|
#include "TileCache.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-11-05 16:48:15 -05:00
|
|
|
const auto message = LOOLProtocol::getAbbreviatedMessage(payload);
|
2016-11-06 21:29:19 -06:00
|
|
|
LOG_TRC("Recv from child [" << message << "].");
|
2016-11-05 16:48:15 -05:00
|
|
|
|
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 <<
|
|
|
|
" has no DocumentBroker to handle message: [" << message << "].");
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentBroker::DocumentBroker(const Poco::URI& uriPublic,
|
|
|
|
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) :
|
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-09-28 03:20:24 -05:00
|
|
|
_mutex(),
|
|
|
|
_saveMutex(),
|
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();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
LOG_INF("jailPath: " << jailPath.toString() << ", jailRoot: " << jailRoot);
|
2016-08-01 03:05:37 -05:00
|
|
|
|
2016-10-14 05:09:43 -05:00
|
|
|
if (_storage == nullptr)
|
|
|
|
{
|
2016-10-16 11:40:52 -05:00
|
|
|
// TODO: Maybe better to pass docKey to storage here instead of uriPublic here because
|
|
|
|
// uriPublic would be different for each view of the document (due to
|
|
|
|
// different query params like access token etc.)
|
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-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-11-06 12:54:00 -06:00
|
|
|
// Call the storage specific file info functions
|
|
|
|
std::string userid, username;
|
2016-11-21 03:02:27 -06:00
|
|
|
std::chrono::duration<double> getInfoCallDuration(0);
|
2016-11-06 12:54:00 -06:00
|
|
|
if (dynamic_cast<WopiStorage*>(_storage.get()) != nullptr)
|
|
|
|
{
|
|
|
|
const WopiStorage::WOPIFileInfo wopifileinfo = static_cast<WopiStorage*>(_storage.get())->getWOPIFileInfo(uriPublic);
|
|
|
|
userid = wopifileinfo._userid;
|
|
|
|
username = wopifileinfo._username;
|
2016-10-25 02:13:00 -05:00
|
|
|
|
2016-11-06 12:54:00 -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-11-06 12:54:00 -06:00
|
|
|
if (!wopifileinfo._postMessageOrigin.empty())
|
2016-10-16 11:40:52 -05:00
|
|
|
{
|
2016-11-10 05:55:38 -06:00
|
|
|
wopiInfo->set("PostMessageOrigin", wopifileinfo._postMessageOrigin);
|
2016-10-16 11:40:52 -05:00
|
|
|
}
|
|
|
|
|
2016-11-10 06:21:39 -06:00
|
|
|
wopiInfo->set("HidePrintOption", wopifileinfo._hidePrintOption);
|
|
|
|
wopiInfo->set("HideSaveOption", wopifileinfo._hideSaveOption);
|
|
|
|
wopiInfo->set("HideExportOption", wopifileinfo._hideExportOption);
|
|
|
|
|
|
|
|
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
|
|
|
|
if (wopifileinfo._enableOwnerTermination && userid == _storage->getFileInfo()._ownerId)
|
|
|
|
{
|
|
|
|
LOG_DBG("Session [" + sessionId + "] is the document owner");
|
|
|
|
session->setDocumentOwner(true);
|
|
|
|
}
|
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
getInfoCallDuration = wopifileinfo._callDuration;
|
|
|
|
}
|
|
|
|
else if (dynamic_cast<LocalStorage*>(_storage.get()) != nullptr)
|
|
|
|
{
|
|
|
|
const LocalStorage::LocalFileInfo localfileinfo = static_cast<LocalStorage*>(_storage.get())->getLocalFileInfo(uriPublic);
|
|
|
|
userid = localfileinfo._userid;
|
|
|
|
username = localfileinfo._username;
|
|
|
|
}
|
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-11-06 12:54:00 -06:00
|
|
|
// Get basic file information from the storage
|
|
|
|
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-11-06 12:54:00 -06:00
|
|
|
// Lets load the document now
|
|
|
|
const bool loaded = _storage->isLoaded();
|
|
|
|
if (!loaded)
|
|
|
|
{
|
|
|
|
const auto localPath = _storage->loadStorageFileToLocal();
|
|
|
|
_uriJailed = Poco::URI(Poco::URI("file://"), localPath);
|
|
|
|
_filename = fileInfo._filename;
|
|
|
|
|
|
|
|
// Use the local temp file's timestamp.
|
|
|
|
_lastFileModifiedTime = Poco::File(_storage->getLocalRootPath()).getLastModified();
|
|
|
|
_tileCache.reset(new TileCache(_uriPublic.toString(), _lastFileModifiedTime, _cacheRoot));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since document has been loaded, send the stats if its WOPI
|
|
|
|
if (dynamic_cast<WopiStorage*>(_storage.get()) != nullptr)
|
|
|
|
{
|
|
|
|
// Get the time taken to load the file from storage
|
|
|
|
auto callDuration = static_cast<WopiStorage*>(_storage.get())->getWopiLoadDuration();
|
|
|
|
// 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);
|
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
const auto it = _sessions.find(sessionId);
|
|
|
|
if (it == _sessions.end())
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Session with sessionId [" << sessionId << "] not found while saving");
|
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
|
|
|
|
2016-07-14 04:49:21 -05:00
|
|
|
// If save requested, but core didn't save because document was unmodified
|
|
|
|
// notify the waiting thread, if any.
|
|
|
|
if (!success && result == "unmodified")
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Save skipped as document was not modified");
|
2016-07-14 04:49:21 -05:00
|
|
|
_saveCV.notify_all();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-10 23:50:25 -05:00
|
|
|
// If we aren't destroying the last editable session just yet, and the file
|
2016-05-09 20:15:09 -05:00
|
|
|
// 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.
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Skipping unnecessary saving to URI [" << uri << "]. File last modified " <<
|
|
|
|
_lastFileModifiedTime.elapsed() / 1000000 << " seconds ago.");
|
2016-04-25 19:44:24 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Saving to URI [" << uri << "].");
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
assert(_storage && _tileCache);
|
2016-10-16 11:40:52 -05:00
|
|
|
if (_storage->saveLocalFileToStorage(uriPublic))
|
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();
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Saved to URI [" << uri << "] and updated tile cache.");
|
2016-04-10 21:07:09 -05:00
|
|
|
_saveCV.notify_all();
|
2016-03-26 08:10:53 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Failed to save to URI [" << uri << "].");
|
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
|
|
|
{
|
2016-04-25 19:48:02 -05:00
|
|
|
// Find the most recent activity.
|
|
|
|
double inactivityTimeMs = std::numeric_limits<double>::max();
|
2016-10-29 20:15:00 -05:00
|
|
|
for (auto& sessionIt : _sessions)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-04-25 19:48:02 -05:00
|
|
|
inactivityTimeMs = std::min(sessionIt.second->getInactivityMS(), inactivityTimeMs);
|
|
|
|
}
|
2016-04-09 22:20:20 -05:00
|
|
|
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Most recent activity was " << inactivityTimeMs << " ms ago.");
|
2016-04-25 19:48:02 -05:00
|
|
|
const auto timeSinceLastSaveMs = getTimeSinceLastSaveMs();
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Time since last save is " << timeSinceLastSaveMs << " ms.");
|
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 << "].");
|
2016-04-25 19:48:02 -05:00
|
|
|
if (_saveCV.wait_for(lock, std::chrono::milliseconds(waitTimeoutMs)) == std::cv_status::no_timeout)
|
|
|
|
{
|
2016-11-06 10:59:59 -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
|
|
|
|
|
|
|
return (lastSaveTime != _lastSaveTime);
|
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-05-17 07:17:21 -05:00
|
|
|
// Save using session holding the edit-lock (or first if multview).
|
2016-10-29 20:15:00 -05:00
|
|
|
for (auto& sessionIt : _sessions)
|
2016-04-10 21:07:09 -05:00
|
|
|
{
|
2016-09-19 02:03:31 -05:00
|
|
|
// Invalidate the timestamp to force persisting.
|
|
|
|
_lastFileModifiedTime.fromEpochTime(0);
|
|
|
|
|
|
|
|
// 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-10-16 12:43:44 -05:00
|
|
|
forwardToChild(sessionIt.second->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
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
const auto id = session->getId();
|
2016-11-03 23:13:47 -05:00
|
|
|
const std::string aMessage = "session " + id + " " + _docKey;
|
2016-04-16 16:18:51 -05:00
|
|
|
|
2016-11-06 22:14:23 -06:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2016-04-03 20:40:14 -05:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (session->isReadOnly())
|
|
|
|
{
|
|
|
|
LOG_DBG("Adding a readonly session [" << id << "]");
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
// Request a new session from the child kit.
|
|
|
|
_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);
|
|
|
|
|
2016-11-13 11:26:00 -06:00
|
|
|
// Now we are ready to bridge between the kit and client.
|
|
|
|
session->bridgePrisonerSession();
|
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-04-16 16:18:51 -05:00
|
|
|
auto it = _sessions.find(id);
|
|
|
|
if (it != _sessions.end())
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
_sessions.erase(it);
|
2016-10-09 15:37:13 -05:00
|
|
|
|
|
|
|
// Let the child know the client has disconnected.
|
|
|
|
const std::string msg("child-" + id + " disconnect");
|
2016-10-11 17:22:51 -05:00
|
|
|
_childProcess->sendTextFrame(msg);
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
2016-04-16 16:18:51 -05:00
|
|
|
|
2016-10-22 09:25:57 -05:00
|
|
|
// Lets remove this session from the admin console too
|
|
|
|
Admin::instance().rmDoc(_docKey, id);
|
|
|
|
|
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
|
|
|
|
2016-11-19 09:22:53 -06:00
|
|
|
LOG_DBG("Alerting all users: " << msg);
|
2016-10-29 20:15:00 -05:00
|
|
|
for (auto& it : _sessions)
|
2016-09-28 14:07:07 -05:00
|
|
|
{
|
2016-11-19 09:04:42 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
it.second->sendTextFrame(msg);
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Error while alerting all users [" << msg << "]: " << ex.what());
|
|
|
|
}
|
2016-09-28 14:07:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
bool DocumentBroker::handleInput(const std::vector<char>& payload)
|
|
|
|
{
|
2016-09-19 18:18:07 -05:00
|
|
|
const auto msg = LOOLProtocol::getAbbreviatedMessage(payload);
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("DocumentBroker got 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
|
|
|
|
2016-10-08 12:13:23 -05:00
|
|
|
const auto command = LOOLProtocol::getFirstToken(msg);
|
|
|
|
if (command == "tile:")
|
2016-05-01 19:50:11 -05:00
|
|
|
{
|
|
|
|
handleTileResponse(payload);
|
|
|
|
}
|
2016-10-08 12:13:23 -05:00
|
|
|
else if (command == "tilecombine:")
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
2016-10-29 20:15:00 -05:00
|
|
|
handleTileCombinedResponse(payload);
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
2016-10-08 12:13:23 -05:00
|
|
|
else if (LOOLProtocol::getFirstToken(command, '-') == "client")
|
|
|
|
{
|
|
|
|
forwardToClient(command, payload);
|
|
|
|
}
|
|
|
|
else if (command == "errortoall:")
|
2016-09-28 14:07:07 -05:00
|
|
|
{
|
|
|
|
StringTokenizer tokens(msg, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
assert(tokens.count() == 3);
|
|
|
|
std::string cmd, kind;
|
|
|
|
LOOLProtocol::getTokenString(tokens, "cmd", cmd);
|
|
|
|
assert(cmd != "");
|
|
|
|
LOOLProtocol::getTokenString(tokens, "kind", kind);
|
|
|
|
assert(kind != "");
|
|
|
|
Util::alertAllUsers(cmd, kind);
|
|
|
|
}
|
2016-09-19 18:18:07 -05:00
|
|
|
else
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Unexpected message: [" << msg << "].");
|
2016-09-19 18:18:07 -05:00
|
|
|
return false;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
// 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();
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Tile request for " << tile.serialize());
|
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() << ").");
|
2016-09-20 21:26:19 -05:00
|
|
|
const std::string request = "tile " + tile.serialize();
|
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);
|
|
|
|
newTileCombined.setVersion(++_tileVersion);
|
|
|
|
|
|
|
|
// 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-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Handling tile combined: " << 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;
|
|
|
|
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;
|
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 &&
|
|
|
|
!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);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:13:23 -05:00
|
|
|
bool DocumentBroker::forwardToClient(const std::string& prefix, const std::vector<char>& payload)
|
|
|
|
{
|
2016-10-31 20:25:19 -05:00
|
|
|
assert(payload.size() > prefix.size());
|
|
|
|
|
|
|
|
// Remove the prefix and trim.
|
|
|
|
size_t index = prefix.size();
|
|
|
|
for ( ; index < payload.size(); ++index)
|
|
|
|
{
|
|
|
|
if (payload[index] != ' ')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data = payload.data() + index;
|
|
|
|
auto size = payload.size() - index;
|
|
|
|
const auto message = getAbbreviatedMessage(data, size);
|
2016-11-05 15:22:07 -05:00
|
|
|
LOG_TRC("Forwarding payload to " << prefix << ' ' << message);
|
2016-10-08 12:13:23 -05:00
|
|
|
|
|
|
|
std::string name;
|
|
|
|
std::string sid;
|
|
|
|
if (LOOLProtocol::parseNameValuePair(prefix, name, sid, '-') && name == "client")
|
|
|
|
{
|
|
|
|
const auto it = _sessions.find(sid);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2016-10-09 10:28:22 -05:00
|
|
|
const auto peer = it->second->getPeer();
|
|
|
|
if (peer)
|
|
|
|
{
|
2016-10-31 20:25:19 -05:00
|
|
|
return peer->handleInput(data, size);
|
2016-10-09 10:28:22 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-05 15:22:07 -05:00
|
|
|
LOG_WRN("Client session [" << sid << "] has no peer to forward message: " << message);
|
2016-10-09 10:28:22 -05:00
|
|
|
}
|
2016-10-08 12:13:23 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-05 15:22:07 -05:00
|
|
|
LOG_WRN("Client session [" << sid << "] not found to forward message: " << message);
|
2016-10-08 12:13:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-05 15:22:07 -05:00
|
|
|
LOG_ERR("Failed to parse 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
|
|
|
|
{
|
|
|
|
pair.second->shutdown(Poco::Net::WebSocket::WS_ENDPOINT_GOING_AWAY);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
for (auto& pair : _sessions)
|
|
|
|
{
|
2016-11-19 09:04:42 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// See protocol.txt for this application-level close frame.
|
|
|
|
pair.second->sendTextFrame("close: " + closeReason);
|
|
|
|
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-03-23 07:41:18 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|