2016-03-10 20:42:33 -06: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/.
|
|
|
|
*/
|
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
#ifndef INCLUDED_DOCUMENTBROKER_HPP
|
|
|
|
#define INCLUDED_DOCUMENTBROKER_HPP
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
#include <signal.h>
|
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
#include <atomic>
|
2016-03-25 21:56:18 -05:00
|
|
|
#include <memory>
|
2016-03-10 20:42:33 -06:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2016-03-23 11:55:28 -05:00
|
|
|
#include <map>
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2016-03-25 21:56:18 -05:00
|
|
|
#include <Poco/URI.h>
|
|
|
|
|
2016-04-05 21:37:29 -05:00
|
|
|
#include "IoUtil.hpp"
|
2016-03-23 11:55:28 -05:00
|
|
|
#include "MasterProcessSession.hpp"
|
|
|
|
#include "Util.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
|
|
|
|
// Forwards.
|
|
|
|
class StorageBase;
|
|
|
|
class TileCache;
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
/// Represents a new LOK child that is read
|
|
|
|
/// to host a document.
|
|
|
|
class ChildProcess
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChildProcess() :
|
|
|
|
_pid(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// pid is the process ID of the child.
|
|
|
|
/// ws is the control WebSocket to the child.
|
|
|
|
ChildProcess(const Poco::Process::PID pid, const std::shared_ptr<Poco::Net::WebSocket>& ws) :
|
|
|
|
_pid(pid),
|
|
|
|
_ws(ws)
|
|
|
|
{
|
|
|
|
Log::info("ChildProcess ctor [" + std::to_string(_pid) + "].");
|
|
|
|
}
|
|
|
|
|
|
|
|
ChildProcess(ChildProcess&& other) :
|
|
|
|
_pid(other._pid),
|
|
|
|
_ws(other._ws)
|
|
|
|
{
|
|
|
|
Log::info("ChildProcess move ctor [" + std::to_string(_pid) + "].");
|
|
|
|
other._pid = -1;
|
|
|
|
other._ws.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChildProcess& operator=(ChildProcess&& other)
|
|
|
|
{
|
|
|
|
Log::info("ChildProcess assign [" + std::to_string(_pid) + "].");
|
|
|
|
_pid = other._pid;
|
|
|
|
other._pid = -1;
|
|
|
|
_ws = other._ws;
|
|
|
|
other._ws.reset();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ChildProcess()
|
|
|
|
{
|
|
|
|
Log::info("~ChildProcess dtor [" + std::to_string(_pid) + "].");
|
2016-04-03 22:26:06 -05:00
|
|
|
close(false);
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void close(const bool rude)
|
|
|
|
{
|
|
|
|
Log::info("Closing child [" + std::to_string(_pid) + "].");
|
2016-04-05 21:37:29 -05:00
|
|
|
_ws.reset();
|
2016-04-03 20:40:14 -05:00
|
|
|
if (_pid != -1)
|
|
|
|
{
|
2016-04-03 22:26:06 -05:00
|
|
|
if (rude && kill(_pid, SIGINT) != 0 && kill(_pid, 0) != 0)
|
2016-04-03 20:40:14 -05:00
|
|
|
{
|
2016-04-07 02:36:38 -05:00
|
|
|
Log::syserror("Cannot terminate lokit [" + std::to_string(_pid) + "]. Abandoning.");
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_pid = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Process::PID getPid() const { return _pid; }
|
|
|
|
std::shared_ptr<Poco::Net::WebSocket> getWebSocket() const { return _ws; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Poco::Process::PID _pid;
|
|
|
|
std::shared_ptr<Poco::Net::WebSocket> _ws;
|
|
|
|
};
|
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
/// DocumentBroker is responsible for setting up a document
|
|
|
|
/// in jail and brokering loading it from Storage
|
|
|
|
/// and saving it back.
|
2016-03-10 20:42:33 -06:00
|
|
|
/// Contains URI, physical path, etc.
|
2016-03-12 18:29:17 -06:00
|
|
|
class DocumentBroker
|
2016-03-10 20:42:33 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
static
|
2016-03-23 07:41:18 -05:00
|
|
|
Poco::URI sanitizeURI(std::string uri);
|
2016-03-19 17:49:36 -05:00
|
|
|
|
|
|
|
/// Returns a document-specific key based
|
|
|
|
/// on the URI of the document.
|
|
|
|
static
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string getDocKey(const Poco::URI& uri);
|
2016-03-12 18:29:17 -06:00
|
|
|
|
2016-03-19 17:49:36 -05:00
|
|
|
DocumentBroker(const Poco::URI& uriPublic,
|
|
|
|
const std::string& docKey,
|
2016-04-03 20:40:14 -05:00
|
|
|
const std::string& childRoot,
|
|
|
|
std::shared_ptr<ChildProcess> childProcess);
|
2016-03-12 18:29:17 -06:00
|
|
|
|
|
|
|
~DocumentBroker()
|
|
|
|
{
|
2016-03-25 21:56:18 -05:00
|
|
|
Log::info() << "~DocumentBroker [" << _uriPublic.toString()
|
|
|
|
<< "] destroyed with " << _sessionsCount
|
|
|
|
<< " sessions." << Log::end;
|
2016-03-12 18:29:17 -06:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
void validate(const Poco::URI& uri);
|
2016-03-21 18:12:00 -05:00
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
/// Loads a document from the public URI into the jail.
|
2016-03-23 07:41:18 -05:00
|
|
|
bool load(const std::string& jailId);
|
2016-03-12 18:29:17 -06:00
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
bool save();
|
2016-03-10 21:01:34 -06:00
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
Poco::URI getPublicUri() const { return _uriPublic; }
|
|
|
|
Poco::URI getJailedUri() const { return _uriJailed; }
|
2016-03-12 18:29:17 -06:00
|
|
|
const std::string& getJailId() const { return _jailId; }
|
|
|
|
const std::string& getDocKey() const { return _docKey; }
|
2016-03-13 09:04:54 -05:00
|
|
|
unsigned decSessions() { return --_sessionsCount; }
|
|
|
|
unsigned incSessions() { return ++_sessionsCount; }
|
2016-03-14 10:53:31 -05:00
|
|
|
unsigned getSessionsCount() { return _sessionsCount; }
|
2016-03-25 21:56:18 -05:00
|
|
|
TileCache& tileCache() { return *_tileCache; }
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string getJailRoot() const;
|
2016-03-12 19:14:32 -06:00
|
|
|
|
2016-03-23 11:55:28 -05:00
|
|
|
/// Ignore input events from all web socket sessions
|
|
|
|
/// except this one
|
|
|
|
void takeEditLock(const std::string id);
|
|
|
|
|
|
|
|
void addWSSession(const std::string id, std::shared_ptr<MasterProcessSession>& ws);
|
|
|
|
|
|
|
|
void removeWSSession(const std::string id);
|
|
|
|
|
|
|
|
unsigned getWSSessionsCount() { return _wsSessions.size(); }
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::map<std::string, std::shared_ptr<MasterProcessSession>> _wsSessions;
|
|
|
|
std::mutex _wsSessionsMutex;
|
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
private:
|
|
|
|
const Poco::URI _uriPublic;
|
2016-03-12 18:29:17 -06:00
|
|
|
const std::string _docKey;
|
2016-03-12 19:14:32 -06:00
|
|
|
const std::string _childRoot;
|
2016-03-26 06:50:13 -05:00
|
|
|
const std::string _cacheRoot;
|
2016-03-12 18:29:17 -06:00
|
|
|
Poco::URI _uriJailed;
|
|
|
|
std::string _jailId;
|
2016-03-21 18:12:00 -05:00
|
|
|
std::string _filename;
|
2016-03-10 21:01:34 -06:00
|
|
|
std::unique_ptr<StorageBase> _storage;
|
2016-03-25 21:56:18 -05:00
|
|
|
std::unique_ptr<TileCache> _tileCache;
|
2016-04-03 20:40:14 -05:00
|
|
|
std::shared_ptr<ChildProcess> _childProcess;
|
2016-03-12 18:29:17 -06:00
|
|
|
std::mutex _mutex;
|
2016-03-13 09:04:54 -05:00
|
|
|
std::atomic<unsigned> _sessionsCount;
|
2016-03-10 20:42:33 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|