From cca657c8f2031ec88366c52f5e8e10c2e04129f9 Mon Sep 17 00:00:00 2001 From: Michael Meeks Date: Thu, 24 Nov 2016 14:52:34 +0000 Subject: Apply the pre-branch rename script to re-organize the source. --- wsd/AdminModel.hpp | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 wsd/AdminModel.hpp (limited to 'wsd/AdminModel.hpp') diff --git a/wsd/AdminModel.hpp b/wsd/AdminModel.hpp new file mode 100644 index 0000000000..f2163b52ec --- /dev/null +++ b/wsd/AdminModel.hpp @@ -0,0 +1,191 @@ +/* -*- 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/. + */ + +#ifndef INCLUDED_ADMINMODEL_HPP +#define INCLUDED_ADMINMODEL_HPP + +#include +#include +#include + +#include + +#include "Log.hpp" +#include +#include "Util.hpp" + +/// A client view in Admin controller. +class View +{ +public: + View(const std::string& sessionId) : + _sessionId(sessionId), + _start(std::time(nullptr)) + { + } + + void expire() { _end = std::time(nullptr); } + bool isExpired() const { return _end != 0 && std::time(nullptr) >= _end; } + +private: + const std::string _sessionId; + const std::time_t _start; + std::time_t _end = 0; +}; + +/// A document in Admin controller. +class Document +{ +public: + Document(const std::string& docKey, + Poco::Process::PID pid, + const std::string& filename) + : _docKey(docKey), + _pid(pid), + _filename(filename), + _start(std::time(nullptr)) + { + } + + Poco::Process::PID getPid() const { return _pid; } + + std::string getFilename() const { return _filename; } + + bool isExpired() const { return _end != 0 && std::time(nullptr) >= _end; } + + std::time_t getElapsedTime() const { return std::time(nullptr) - _start; } + + void addView(const std::string& sessionId); + + int expireView(const std::string& sessionId); + + unsigned getActiveViews() const { return _activeViews; } + + const std::map& getViews() const { return _views; } + +private: + const std::string _docKey; + const Poco::Process::PID _pid; + /// SessionId mapping to View object + std::map _views; + /// Total number of active views + unsigned _activeViews = 0; + /// Hosted filename + std::string _filename; + + std::time_t _start; + std::time_t _end = 0; +}; + +/// An Admin session subscriber. +class Subscriber +{ +public: + Subscriber(int sessionId, std::shared_ptr& ws) + : _sessionId(sessionId), + _ws(ws), + _start(std::time(nullptr)) + { + Log::info("Subscriber ctor."); + } + + ~Subscriber() + { + Log::info("Subscriber dtor."); + } + + bool notify(const std::string& message); + + bool subscribe(const std::string& command); + + void unsubscribe(const std::string& command); + + void expire() { _end = std::time(nullptr); } + + bool isExpired() const { return _end != 0 && std::time(nullptr) >= _end; } + +private: + /// Admin session Id + int _sessionId; + + /// LOOLWebSocket to use to send messages to session + std::weak_ptr _ws; + + std::set _subscriptions; + + std::time_t _start; + std::time_t _end = 0; +}; + +/// The Admin controller implementation. +class AdminModel +{ +public: + AdminModel() + { + Log::info("AdminModel ctor."); + } + + ~AdminModel() + { + Log::info("AdminModel dtor."); + } + + std::string query(const std::string& command); + + /// Returns memory consumed by all active loolkit processes + unsigned getTotalMemoryUsage(); + + void subscribe(int sessionId, std::shared_ptr& ws); + void subscribe(int sessionId, const std::string& command); + + void unsubscribe(int sessionId, const std::string& command); + + void clearMemStats() { _memStats.clear(); } + + void clearCpuStats() { _cpuStats.clear(); } + + void addMemStats(unsigned memUsage); + + void addCpuStats(unsigned cpuUsage); + + void setCpuStatsSize(unsigned size); + + void setMemStatsSize(unsigned size); + + void notify(const std::string& message); + + void addDocument(const std::string& docKey, Poco::Process::PID pid, const std::string& filename, const std::string& sessionId); + + void removeDocument(const std::string& docKey, const std::string& sessionId); + void removeDocument(const std::string& docKey); + +private: + std::string getMemStats(); + + std::string getCpuStats(); + + unsigned getTotalActiveViews(); + + std::string getDocuments() const; + +private: + std::map _subscribers; + std::map _documents; + + std::list _memStats; + unsigned _memStatsSize = 100; + + std::list _cpuStats; + unsigned _cpuStatsSize = 100; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit