2016-02-24 10:58:05 -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-04-08 03:08:41 -05:00
|
|
|
#ifndef INCLUDED_ADMINMODEL_HPP
|
|
|
|
#define INCLUDED_ADMINMODEL_HPP
|
2016-02-24 10:58:05 -06:00
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
#include <memory>
|
2016-03-05 09:21:25 -06:00
|
|
|
#include <set>
|
2016-03-03 12:01:37 -06:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <Poco/Process.h>
|
|
|
|
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
2017-03-15 09:58:55 -05:00
|
|
|
#include "net/WebSocketHandler.hpp"
|
2016-02-24 10:58:05 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// A client view in Admin controller.
|
2016-03-03 12:01:37 -06:00
|
|
|
class View
|
|
|
|
{
|
|
|
|
public:
|
2017-04-07 09:19:04 -05:00
|
|
|
View(const std::string& sessionId, const std::string& userName) :
|
2016-04-17 21:11:10 -05:00
|
|
|
_sessionId(sessionId),
|
2017-04-07 09:19:04 -05:00
|
|
|
_userName(userName),
|
2016-04-17 21:11:10 -05:00
|
|
|
_start(std::time(nullptr))
|
|
|
|
{
|
|
|
|
}
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void expire() { _end = std::time(nullptr); }
|
2017-04-07 09:19:04 -05:00
|
|
|
std::string getUserName() const { return _userName; }
|
|
|
|
std::string getSessionId() const { return _sessionId; }
|
2016-04-17 21:11:10 -05:00
|
|
|
bool isExpired() const { return _end != 0 && std::time(nullptr) >= _end; }
|
2016-03-03 12:01:37 -06:00
|
|
|
|
|
|
|
private:
|
2016-04-17 21:11:10 -05:00
|
|
|
const std::string _sessionId;
|
2017-04-07 09:19:04 -05:00
|
|
|
const std::string _userName;
|
2016-04-17 21:11:10 -05:00
|
|
|
const std::time_t _start;
|
2016-03-03 12:01:37 -06:00
|
|
|
std::time_t _end = 0;
|
|
|
|
};
|
|
|
|
|
2017-06-11 21:51:38 -05:00
|
|
|
struct DocProcSettings
|
|
|
|
{
|
|
|
|
size_t LimitVirtMemMb;
|
|
|
|
size_t LimitDataMemKb;
|
|
|
|
size_t LimitStackMemKb;
|
|
|
|
size_t LimitFileSizeMb;
|
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// A document in Admin controller.
|
2016-03-03 12:01:37 -06:00
|
|
|
class Document
|
|
|
|
{
|
|
|
|
public:
|
2016-11-23 21:42:27 -06:00
|
|
|
Document(const std::string& docKey,
|
|
|
|
Poco::Process::PID pid,
|
|
|
|
const std::string& filename)
|
|
|
|
: _docKey(docKey),
|
2016-04-15 04:00:22 -05:00
|
|
|
_pid(pid),
|
2016-11-23 21:42:27 -06:00
|
|
|
_filename(filename),
|
2017-02-07 08:39:56 -06:00
|
|
|
_memoryDirty(0),
|
2017-06-05 20:17:42 -05:00
|
|
|
_lastJiffy(0),
|
2016-12-05 14:11:07 -06:00
|
|
|
_start(std::time(nullptr)),
|
2017-06-03 16:53:57 -05:00
|
|
|
_lastActivity(_start),
|
|
|
|
_sentBytes(0),
|
|
|
|
_recvBytes(0)
|
2016-03-03 12:01:37 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:00:22 -05:00
|
|
|
Poco::Process::PID getPid() const { return _pid; }
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-04-15 04:00:22 -05:00
|
|
|
std::string getFilename() const { return _filename; }
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
bool isExpired() const { return _end != 0 && std::time(nullptr) >= _end; }
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-04-01 02:50:14 -05:00
|
|
|
std::time_t getElapsedTime() const { return std::time(nullptr) - _start; }
|
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
std::time_t getIdleTime() const { return std::time(nullptr) - _lastActivity; }
|
|
|
|
|
2017-04-07 09:19:04 -05:00
|
|
|
void addView(const std::string& sessionId, const std::string& userName);
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-04-17 21:11:10 -05:00
|
|
|
int expireView(const std::string& sessionId);
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-04-15 04:00:22 -05:00
|
|
|
unsigned getActiveViews() const { return _activeViews; }
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2017-06-05 20:17:42 -05:00
|
|
|
unsigned getLastJiffies() const { return _lastJiffy; }
|
|
|
|
void setLastJiffies(size_t newJ) { _lastJiffy = newJ; }
|
|
|
|
|
2016-04-17 21:32:58 -05:00
|
|
|
const std::map<std::string, View>& getViews() const { return _views; }
|
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
void updateLastActivityTime() { _lastActivity = std::time(nullptr); }
|
2017-02-07 12:52:01 -06:00
|
|
|
bool updateMemoryDirty(int dirty);
|
2017-02-07 08:39:56 -06:00
|
|
|
int getMemoryDirty() const { return _memoryDirty; }
|
2016-12-05 14:11:07 -06:00
|
|
|
|
2017-04-17 13:36:04 -05:00
|
|
|
std::pair<std::time_t, std::string> getSnapshot() const;
|
|
|
|
const std::string getHistory() const;
|
|
|
|
void takeSnapshot();
|
|
|
|
|
2017-05-12 08:59:01 -05:00
|
|
|
void setModified(bool value) { _isModified = value; }
|
|
|
|
bool getModifiedStatus() const { return _isModified; }
|
|
|
|
|
2017-06-03 16:53:57 -05:00
|
|
|
void addBytes(uint64_t sent, uint64_t recv)
|
|
|
|
{
|
|
|
|
_sentBytes += sent;
|
|
|
|
_recvBytes += recv;
|
|
|
|
}
|
|
|
|
|
2017-06-11 21:51:38 -05:00
|
|
|
const DocProcSettings& getDocProcSettings() const { return _docProcSettings; }
|
|
|
|
void setDocProcSettings(const DocProcSettings& docProcSettings) { _docProcSettings = docProcSettings; }
|
|
|
|
|
2017-04-17 13:36:04 -05:00
|
|
|
std::string to_string() const;
|
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
private:
|
2017-05-12 08:59:01 -05:00
|
|
|
bool _isModified;
|
2016-04-15 04:00:22 -05:00
|
|
|
const std::string _docKey;
|
|
|
|
const Poco::Process::PID _pid;
|
2016-03-03 12:01:37 -06:00
|
|
|
/// SessionId mapping to View object
|
2016-04-17 21:11:10 -05:00
|
|
|
std::map<std::string, View> _views;
|
2016-03-03 12:01:37 -06:00
|
|
|
/// Total number of active views
|
2016-04-15 04:00:22 -05:00
|
|
|
unsigned _activeViews = 0;
|
2016-04-14 15:29:31 -05:00
|
|
|
/// Hosted filename
|
2016-04-15 04:00:22 -05:00
|
|
|
std::string _filename;
|
2017-02-07 08:39:56 -06:00
|
|
|
/// The dirty (ie. un-shared) memory of the document's Kit process.
|
|
|
|
int _memoryDirty;
|
2017-06-05 20:17:42 -05:00
|
|
|
/// Last noted Jiffy count
|
|
|
|
unsigned _lastJiffy;
|
2016-03-03 12:01:37 -06:00
|
|
|
|
|
|
|
std::time_t _start;
|
2016-12-05 14:11:07 -06:00
|
|
|
std::time_t _lastActivity;
|
2016-03-03 12:01:37 -06:00
|
|
|
std::time_t _end = 0;
|
2017-04-17 13:36:04 -05:00
|
|
|
std::map<std::time_t,std::string> _snapshots;
|
2017-06-03 16:53:57 -05:00
|
|
|
|
|
|
|
/// Total bytes sent and recv'd by this document.
|
|
|
|
uint64_t _sentBytes, _recvBytes;
|
2017-06-11 21:51:38 -05:00
|
|
|
|
|
|
|
/// Per-doc kit process settings.
|
|
|
|
DocProcSettings _docProcSettings;
|
2016-03-03 12:01:37 -06:00
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// An Admin session subscriber.
|
2016-03-03 12:01:37 -06:00
|
|
|
class Subscriber
|
|
|
|
{
|
|
|
|
public:
|
2017-03-15 09:58:55 -05:00
|
|
|
Subscriber(int sessionId, const std::weak_ptr<WebSocketHandler>& ws)
|
2016-04-15 04:00:22 -05:00
|
|
|
: _sessionId(sessionId),
|
2016-03-05 09:21:25 -06:00
|
|
|
_ws(ws),
|
|
|
|
_start(std::time(nullptr))
|
2016-03-03 12:01:37 -06:00
|
|
|
{
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_INF("Subscriber ctor.");
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
~Subscriber()
|
|
|
|
{
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_INF("Subscriber dtor.");
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
bool notify(const std::string& message);
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
bool subscribe(const std::string& command);
|
2016-03-05 09:21:25 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void unsubscribe(const std::string& command);
|
2016-03-05 09:21:25 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void expire() { _end = std::time(nullptr); }
|
2016-03-05 09:21:25 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
bool isExpired() const { return _end != 0 && std::time(nullptr) >= _end; }
|
2016-03-05 09:21:25 -06:00
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
private:
|
2016-03-05 09:21:25 -06:00
|
|
|
/// Admin session Id
|
2016-04-15 04:00:22 -05:00
|
|
|
int _sessionId;
|
2016-11-10 02:47:25 -06:00
|
|
|
|
2017-03-15 09:58:55 -05:00
|
|
|
/// The underlying AdminRequestHandler
|
|
|
|
std::weak_ptr<WebSocketHandler> _ws;
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-05 09:21:25 -06:00
|
|
|
std::set<std::string> _subscriptions;
|
|
|
|
|
|
|
|
std::time_t _start;
|
|
|
|
std::time_t _end = 0;
|
2016-03-03 12:01:37 -06:00
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// The Admin controller implementation.
|
2016-02-24 10:58:05 -06:00
|
|
|
class AdminModel
|
|
|
|
{
|
|
|
|
public:
|
2017-04-03 12:21:20 -05:00
|
|
|
AdminModel() :
|
|
|
|
_owner(std::this_thread::get_id())
|
2016-02-24 10:58:05 -06:00
|
|
|
{
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_INF("AdminModel ctor.");
|
2016-02-24 10:58:05 -06:00
|
|
|
}
|
|
|
|
|
2017-04-17 13:36:04 -05:00
|
|
|
~AdminModel();
|
2016-02-26 00:25:13 -06:00
|
|
|
|
2017-04-03 12:21:20 -05:00
|
|
|
/// All methods here must be called from the Admin socket-poll
|
|
|
|
void setThreadOwner(const std::thread::id &id) { _owner = id; }
|
|
|
|
|
|
|
|
/// In debug mode check that code is running in the correct thread.
|
2017-04-05 07:48:49 -05:00
|
|
|
/// Asserts in the debug builds, otherwise just logs.
|
|
|
|
void assertCorrectThread() const;
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-04-12 01:09:39 -05:00
|
|
|
std::string query(const std::string& command);
|
2017-04-17 13:36:04 -05:00
|
|
|
std::string getAllHistory() const;
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-04 12:49:01 -06:00
|
|
|
/// Returns memory consumed by all active loolkit processes
|
2017-01-29 23:01:49 -06:00
|
|
|
unsigned getKitsMemoryUsage();
|
2017-06-05 20:17:42 -05:00
|
|
|
size_t getKitsJiffies();
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2017-03-15 09:58:55 -05:00
|
|
|
void subscribe(int sessionId, const std::weak_ptr<WebSocketHandler>& ws);
|
2016-04-14 15:29:31 -05:00
|
|
|
void subscribe(int sessionId, const std::string& command);
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-04-14 15:29:31 -05:00
|
|
|
void unsubscribe(int sessionId, const std::string& command);
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2017-05-12 08:59:01 -05:00
|
|
|
void modificationAlert(const std::string& docKey, Poco::Process::PID pid, bool value);
|
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void clearMemStats() { _memStats.clear(); }
|
2016-03-05 09:21:25 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void clearCpuStats() { _cpuStats.clear(); }
|
2016-03-05 09:21:25 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void addMemStats(unsigned memUsage);
|
2016-03-05 09:21:25 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void addCpuStats(unsigned cpuUsage);
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void setCpuStatsSize(unsigned size);
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void setMemStatsSize(unsigned size);
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void notify(const std::string& message);
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2017-04-07 09:19:04 -05:00
|
|
|
void addDocument(const std::string& docKey, Poco::Process::PID pid, const std::string& filename, const std::string& sessionId, const std::string& userName);
|
2016-04-14 15:29:31 -05:00
|
|
|
|
2016-04-17 21:11:10 -05:00
|
|
|
void removeDocument(const std::string& docKey, const std::string& sessionId);
|
2016-04-17 21:32:58 -05:00
|
|
|
void removeDocument(const std::string& docKey);
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
void updateLastActivityTime(const std::string& docKey);
|
2017-02-07 08:39:56 -06:00
|
|
|
void updateMemoryDirty(const std::string& docKey, int dirty);
|
2016-12-05 14:11:07 -06:00
|
|
|
|
2017-06-03 16:53:57 -05:00
|
|
|
void addBytes(const std::string& docKey, uint64_t sent, uint64_t recv);
|
|
|
|
|
|
|
|
uint64_t getSentBytesTotal() { return _sentBytesTotal; }
|
|
|
|
uint64_t getRecvBytesTotal() { return _recvBytesTotal; }
|
|
|
|
|
2016-04-14 15:29:31 -05:00
|
|
|
private:
|
2016-03-31 02:25:35 -05:00
|
|
|
std::string getMemStats();
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
std::string getCpuStats();
|
2016-03-04 12:49:01 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
unsigned getTotalActiveViews();
|
2016-03-04 12:49:01 -06:00
|
|
|
|
2016-11-09 20:12:09 -06:00
|
|
|
std::string getDocuments() const;
|
2016-02-26 00:25:13 -06:00
|
|
|
|
|
|
|
private:
|
2016-03-05 09:21:25 -06:00
|
|
|
std::map<int, Subscriber> _subscribers;
|
2016-04-15 04:00:22 -05:00
|
|
|
std::map<std::string, Document> _documents;
|
2017-04-17 13:36:04 -05:00
|
|
|
std::map<std::string, Document> _expiredDocuments;
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2017-02-07 08:39:56 -06:00
|
|
|
/// The last N total memory Dirty size.
|
2016-03-05 14:39:35 -06:00
|
|
|
std::list<unsigned> _memStats;
|
|
|
|
unsigned _memStatsSize = 100;
|
|
|
|
|
|
|
|
std::list<unsigned> _cpuStats;
|
|
|
|
unsigned _cpuStatsSize = 100;
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2017-06-03 16:53:57 -05:00
|
|
|
uint64_t _sentBytesTotal;
|
|
|
|
uint64_t _recvBytesTotal;
|
|
|
|
|
2017-04-10 07:54:17 -05:00
|
|
|
/// We check the owner even in the release builds, needs to be always correct.
|
2017-04-03 12:21:20 -05:00
|
|
|
std::thread::id _owner;
|
2016-02-24 10:58:05 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2016-03-08 01:31:29 -06:00
|
|
|
|
2016-02-24 10:58:05 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|