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/Net/WebSocket.h>
|
|
|
|
#include <Poco/Process.h>
|
|
|
|
|
2016-02-24 10:58:05 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
class View
|
|
|
|
{
|
|
|
|
public:
|
2016-04-14 15:29:31 -05:00
|
|
|
View(int sessionId)
|
2016-04-15 04:00:22 -05:00
|
|
|
: _sessionId(sessionId),
|
2016-03-03 12:01:37 -06:00
|
|
|
_start(std::time(nullptr))
|
|
|
|
{ }
|
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void expire() { _end = std::time(nullptr); }
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
bool isExpired() { return _end != 0 && std::time(nullptr) >= _end; }
|
2016-03-03 12:01:37 -06:00
|
|
|
|
|
|
|
private:
|
2016-04-15 04:00:22 -05:00
|
|
|
int _sessionId;
|
2016-03-03 12:01:37 -06:00
|
|
|
|
|
|
|
std::time_t _start;
|
|
|
|
std::time_t _end = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Document
|
|
|
|
{
|
|
|
|
public:
|
2016-04-15 04:00:22 -05:00
|
|
|
Document(std::string docKey, Poco::Process::PID pid, std::string filename)
|
|
|
|
: _docKey(docKey),
|
|
|
|
_pid(pid),
|
|
|
|
_filename(filename),
|
2016-03-03 12:01:37 -06:00
|
|
|
_start(std::time(nullptr))
|
|
|
|
{
|
2016-04-15 04:00:22 -05:00
|
|
|
Log::info("Document " + _docKey + " ctor.");
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
~Document()
|
|
|
|
{
|
2016-04-15 04:00:22 -05:00
|
|
|
Log::info("Document " + _docKey + " dtor.");
|
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-04-14 15:29:31 -05:00
|
|
|
void addView(int sessionId);
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-04-14 15:29:31 -05:00
|
|
|
int expireView(int 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
|
|
|
|
|
|
|
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
|
|
|
|
std::map<int, View> _views;
|
|
|
|
/// 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;
|
|
|
|
std::time_t _end = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Subscriber
|
|
|
|
{
|
|
|
|
public:
|
2016-04-14 15:29:31 -05:00
|
|
|
Subscriber(int sessionId, std::shared_ptr<Poco::Net::WebSocket>& 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
|
|
|
{
|
|
|
|
Log::info("Subscriber ctor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
~Subscriber()
|
|
|
|
{
|
|
|
|
Log::info("Subscriber dtor.");
|
|
|
|
}
|
|
|
|
|
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-03-03 12:01:37 -06:00
|
|
|
/// WebSocket to use to send messages to session
|
|
|
|
std::weak_ptr<Poco::Net::WebSocket> _ws;
|
|
|
|
|
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-02-24 10:58:05 -06:00
|
|
|
class AdminModel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AdminModel()
|
|
|
|
{
|
|
|
|
Log::info("AdminModel ctor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
~AdminModel()
|
|
|
|
{
|
|
|
|
Log::info("AdminModel dtor.");
|
|
|
|
}
|
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
|
2016-03-31 02:25:35 -05:00
|
|
|
unsigned getTotalMemoryUsage();
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-04-14 15:29:31 -05:00
|
|
|
void subscribe(int sessionId, std::shared_ptr<Poco::Net::WebSocket>& ws);
|
|
|
|
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-15 04:00:22 -05:00
|
|
|
void addDocument(const std::string& docKey, Poco::Process::PID pid, const std::string& filename, const int sessionId);
|
2016-04-14 15:29:31 -05:00
|
|
|
|
2016-04-15 04:00:22 -05:00
|
|
|
void removeDocument(const std::string& docKey, const int sessionId);
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2016-04-14 15:29:31 -05:00
|
|
|
private:
|
2016-03-05 14:39:35 -06:00
|
|
|
|
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-03-31 02:25:35 -05:00
|
|
|
std::string getDocuments();
|
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: */
|