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"
|
2016-11-10 02:47:25 -06:00
|
|
|
#include <LOOLWebSocket.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:
|
2016-04-17 21:11:10 -05:00
|
|
|
View(const std::string& sessionId) :
|
|
|
|
_sessionId(sessionId),
|
|
|
|
_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); }
|
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;
|
|
|
|
const std::time_t _start;
|
2016-03-03 12:01:37 -06:00
|
|
|
std::time_t _end = 0;
|
|
|
|
};
|
|
|
|
|
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),
|
2016-12-05 14:11:07 -06:00
|
|
|
_start(std::time(nullptr)),
|
|
|
|
_lastActivity(_start)
|
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; }
|
|
|
|
|
2016-04-17 21:11:10 -05:00
|
|
|
void addView(const std::string& sessionId);
|
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
|
|
|
|
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); }
|
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
private:
|
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;
|
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;
|
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// An Admin session subscriber.
|
2016-03-03 12:01:37 -06:00
|
|
|
class Subscriber
|
|
|
|
{
|
|
|
|
public:
|
2016-11-10 02:47:25 -06:00
|
|
|
Subscriber(int sessionId, std::shared_ptr<LOOLWebSocket>& 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
|
|
|
|
|
|
|
/// LOOLWebSocket to use to send messages to session
|
|
|
|
std::weak_ptr<LOOLWebSocket> _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:
|
|
|
|
AdminModel()
|
|
|
|
{
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_INF("AdminModel ctor.");
|
2016-02-24 10:58:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
~AdminModel()
|
|
|
|
{
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_INF("AdminModel dtor.");
|
2016-02-24 10:58:05 -06:00
|
|
|
}
|
2016-02-26 00:25:13 -06:00
|
|
|
|
2016-04-12 01:09:39 -05:00
|
|
|
std::string query(const std::string& command);
|
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();
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-11-10 02:47:25 -06:00
|
|
|
void subscribe(int sessionId, std::shared_ptr<LOOLWebSocket>& 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
|
|
|
|
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
|
|
|
|
2016-04-17 21:11:10 -05:00
|
|
|
void addDocument(const std::string& docKey, Poco::Process::PID pid, const std::string& filename, const std::string& sessionId);
|
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);
|
|
|
|
|
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;
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
std::list<unsigned> _memStats;
|
|
|
|
unsigned _memStatsSize = 100;
|
|
|
|
|
|
|
|
std::list<unsigned> _cpuStats;
|
|
|
|
unsigned _cpuStatsSize = 100;
|
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: */
|