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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_ADMIN_MODEL_HPP
|
|
|
|
#define INCLUDED_ADMIN_MODEL_HPP
|
|
|
|
|
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 <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <Poco/Net/WebSocket.h>
|
|
|
|
#include <Poco/Process.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
2016-02-24 10:58:05 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
class View
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
View(int nSessionId)
|
|
|
|
: _nSessionId(nSessionId),
|
|
|
|
_start(std::time(nullptr))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void expire()
|
|
|
|
{
|
|
|
|
_end = std::time(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isExpired()
|
|
|
|
{
|
2016-03-04 12:49:01 -06:00
|
|
|
return _end != 0 && std::time(nullptr) >= _end;
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int _nSessionId;
|
|
|
|
|
|
|
|
std::time_t _start;
|
|
|
|
std::time_t _end = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Document
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Document(Poco::Process::PID nPid, std::string sUrl)
|
|
|
|
: _nPid(nPid),
|
|
|
|
_sUrl(sUrl),
|
|
|
|
_start(std::time(nullptr))
|
|
|
|
{
|
|
|
|
Log::info("Document " + std::to_string(_nPid) + " ctor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
~Document()
|
|
|
|
{
|
|
|
|
Log::info("Document " + std::to_string(_nPid) + " dtor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Process::PID getPid() const
|
|
|
|
{
|
|
|
|
return _nPid;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getUrl() const
|
|
|
|
{
|
|
|
|
return _sUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void expire()
|
|
|
|
{
|
|
|
|
_end = std::time(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isExpired() const
|
|
|
|
{
|
2016-03-04 12:49:01 -06:00
|
|
|
return _end != 0 && std::time(nullptr) >= _end;
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void addView(int nSessionId)
|
|
|
|
{
|
|
|
|
std::pair<std::map<int, View>::iterator, bool > ret;
|
|
|
|
ret = _views.insert(std::pair<int, View>(nSessionId, View(nSessionId)));
|
|
|
|
if (!ret.second)
|
|
|
|
{
|
|
|
|
Log::warn() << "View with SessionID [" + std::to_string(nSessionId) + "] already exists." << Log::end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-04 12:49:01 -06:00
|
|
|
_nActiveViews++;
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeView(int nSessionId)
|
|
|
|
{
|
|
|
|
auto it = _views.find(nSessionId);
|
|
|
|
if (it != _views.end())
|
|
|
|
{
|
|
|
|
it->second.expire();
|
2016-03-04 12:49:01 -06:00
|
|
|
_nActiveViews--;
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 12:49:01 -06:00
|
|
|
unsigned getActiveViews() const
|
2016-03-03 12:01:37 -06:00
|
|
|
{
|
2016-03-04 12:49:01 -06:00
|
|
|
return _nActiveViews;
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Poco::Process::PID _nPid;
|
|
|
|
/// SessionId mapping to View object
|
|
|
|
std::map<int, View> _views;
|
|
|
|
/// Total number of active views
|
2016-03-04 12:49:01 -06:00
|
|
|
unsigned _nActiveViews = 0;
|
2016-03-03 12:01:37 -06:00
|
|
|
/// Hosted URL
|
|
|
|
std::string _sUrl;
|
|
|
|
|
|
|
|
std::time_t _start;
|
|
|
|
std::time_t _end = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Subscriber
|
|
|
|
{
|
|
|
|
public:
|
2016-03-05 09:21:25 -06:00
|
|
|
Subscriber(int nSessionId, std::shared_ptr<Poco::Net::WebSocket>& ws)
|
|
|
|
: _nSessionId(nSessionId),
|
|
|
|
_ws(ws),
|
|
|
|
_start(std::time(nullptr))
|
2016-03-03 12:01:37 -06:00
|
|
|
{
|
|
|
|
Log::info("Subscriber ctor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
~Subscriber()
|
|
|
|
{
|
|
|
|
Log::info("Subscriber dtor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool notify(const std::string& message)
|
|
|
|
{
|
2016-03-05 09:21:25 -06:00
|
|
|
Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
|
|
|
|
if (_subscriptions.find(tokens[0]) == _subscriptions.end())
|
|
|
|
return true;
|
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
auto webSocket = _ws.lock();
|
|
|
|
if (webSocket)
|
|
|
|
{
|
|
|
|
webSocket->sendFrame(message.data(), message.length());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-05 09:21:25 -06:00
|
|
|
bool subscribe(const std::string& command)
|
|
|
|
{
|
|
|
|
auto ret = _subscriptions.insert(command);
|
|
|
|
return ret.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unsubscribe(const std::string& command)
|
|
|
|
{
|
|
|
|
_subscriptions.erase(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expire()
|
|
|
|
{
|
|
|
|
_end = std::time(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isExpired() const
|
|
|
|
{
|
|
|
|
return _end != 0 && std::time(nullptr) >= _end;
|
|
|
|
}
|
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
private:
|
2016-03-05 09:21:25 -06:00
|
|
|
/// Admin session Id
|
|
|
|
int _nSessionId;
|
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
|
|
|
/// In case of huge number of documents,
|
|
|
|
/// client can tell us the specific page it is
|
|
|
|
/// interested in getting live notifications
|
|
|
|
unsigned _currentPage;
|
|
|
|
};
|
|
|
|
|
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-03-03 12:01:37 -06:00
|
|
|
void update(const std::string& data)
|
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens(data, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
|
|
|
|
Log::info("AdminModel Recv: " + data);
|
|
|
|
|
|
|
|
if (tokens[0] == "document")
|
|
|
|
{
|
|
|
|
addDocument(std::stoi(tokens[1]), tokens[2]);
|
2016-03-04 12:49:01 -06:00
|
|
|
unsigned mem = Util::getMemoryUsage(std::stoi(tokens[1]));
|
2016-03-03 12:01:37 -06:00
|
|
|
std::string response = data + std::to_string(mem);
|
|
|
|
notify(response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "addview")
|
|
|
|
{
|
|
|
|
auto it = _documents.find(std::stoi(tokens[1]));
|
|
|
|
if (it != _documents.end())
|
|
|
|
{
|
|
|
|
const unsigned nSessionId = Util::decodeId(tokens[2]);
|
|
|
|
it->second.addView(nSessionId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "rmview")
|
|
|
|
{
|
|
|
|
auto it = _documents.find(std::stoi(tokens[1]));
|
|
|
|
if (it != _documents.end())
|
|
|
|
{
|
|
|
|
const unsigned nSessionId = Util::decodeId(tokens[2]);
|
|
|
|
it->second.removeView(nSessionId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "rmdoc")
|
|
|
|
{
|
|
|
|
removeDocument(std::stoi(tokens[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
notify(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string query(const std::string command)
|
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens(command, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
|
|
|
|
if (tokens[0] == "documents")
|
|
|
|
{
|
|
|
|
return getDocuments();
|
|
|
|
}
|
2016-03-04 12:49:01 -06:00
|
|
|
else if (tokens[0] == "active_users_count")
|
|
|
|
{
|
|
|
|
return std::to_string(getTotalActiveViews());
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "active_docs_count")
|
|
|
|
{
|
|
|
|
return std::to_string(_nActiveDocuments);
|
|
|
|
}
|
2016-03-03 12:01:37 -06:00
|
|
|
|
|
|
|
return std::string("");
|
|
|
|
}
|
|
|
|
|
2016-03-04 12:49:01 -06:00
|
|
|
/// Returns memory consumed by all active loolkit processes
|
|
|
|
unsigned getTotalMemoryUsage()
|
2016-03-03 12:01:37 -06:00
|
|
|
{
|
2016-03-04 12:49:01 -06:00
|
|
|
unsigned totalMem = 0;
|
|
|
|
for (auto& it: _documents)
|
2016-03-03 12:01:37 -06:00
|
|
|
{
|
2016-03-04 12:49:01 -06:00
|
|
|
if (it.second.isExpired())
|
|
|
|
continue;
|
2016-03-03 12:01:37 -06:00
|
|
|
|
2016-03-04 12:49:01 -06:00
|
|
|
totalMem += Util::getMemoryUsage(it.second.getPid());
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
|
2016-03-04 12:49:01 -06:00
|
|
|
return totalMem;
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
|
2016-03-05 09:21:25 -06:00
|
|
|
void subscribe(int nSessionId, std::shared_ptr<Poco::Net::WebSocket>& ws)
|
2016-03-03 12:01:37 -06:00
|
|
|
{
|
2016-03-05 09:21:25 -06:00
|
|
|
auto ret = _subscribers.insert(std::pair<int, Subscriber>(nSessionId, Subscriber(nSessionId, ws)));
|
|
|
|
if (!ret.second)
|
|
|
|
{
|
|
|
|
Log::warn() << "Subscriber already exists" << Log::end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void subscribe(int nSessionId, const std::string& command)
|
|
|
|
{
|
|
|
|
auto subscriber = _subscribers.find(nSessionId);
|
|
|
|
if (subscriber == _subscribers.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
subscriber->second.subscribe(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unsubscribe(int nSessionId, const std::string& command)
|
|
|
|
{
|
|
|
|
auto subscriber = _subscribers.find(nSessionId);
|
|
|
|
if (subscriber == _subscribers.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
subscriber->second.unsubscribe(command);
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// FIXME: we have a problem if new document to be added has PID = expired document in the map
|
|
|
|
// Prolly, *move* expired documents to another container (?)
|
2016-02-26 00:25:13 -06:00
|
|
|
void addDocument(Poco::Process::PID pid, std::string url)
|
|
|
|
{
|
2016-03-03 12:01:37 -06:00
|
|
|
std::pair<std::map<Poco::Process::PID, Document>::iterator, bool > ret;
|
|
|
|
ret = _documents.insert(std::pair<Poco::Process::PID, Document>(pid, Document(pid, url)));
|
|
|
|
if (!ret.second)
|
|
|
|
{
|
|
|
|
Log::warn() << "Document with PID [" + std::to_string(pid) + "] already exists." << Log::end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-04 12:49:01 -06:00
|
|
|
_nActiveDocuments++;
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeDocument(Poco::Process::PID pid)
|
|
|
|
{
|
|
|
|
auto it = _documents.find(pid);
|
2016-03-04 12:49:01 -06:00
|
|
|
if (it != _documents.end() && !it->second.isExpired())
|
2016-03-03 12:01:37 -06:00
|
|
|
{
|
|
|
|
it->second.expire();
|
2016-03-04 12:49:01 -06:00
|
|
|
_nActiveDocuments--;
|
2016-03-03 12:01:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void notify(const std::string& message)
|
|
|
|
{
|
|
|
|
auto it = std::begin(_subscribers);
|
|
|
|
while (it != std::end(_subscribers))
|
|
|
|
{
|
2016-03-05 09:21:25 -06:00
|
|
|
if (!it->second.notify(message))
|
2016-03-03 12:01:37 -06:00
|
|
|
{
|
|
|
|
it = _subscribers.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
|
|
|
|
2016-03-04 12:49:01 -06:00
|
|
|
unsigned getTotalActiveViews()
|
|
|
|
{
|
|
|
|
unsigned nTotalViews = 0;
|
|
|
|
for (auto& it: _documents)
|
|
|
|
{
|
|
|
|
if (it.second.isExpired())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nTotalViews += it.second.getActiveViews();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nTotalViews;
|
|
|
|
}
|
|
|
|
|
2016-02-26 00:25:13 -06:00
|
|
|
std::string getDocuments()
|
|
|
|
{
|
2016-03-03 12:01:37 -06:00
|
|
|
std::ostringstream oss;
|
|
|
|
for (auto& it: _documents)
|
2016-02-26 00:25:13 -06:00
|
|
|
{
|
2016-03-03 12:01:37 -06:00
|
|
|
if (it.second.isExpired())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string sPid = std::to_string(it.second.getPid());
|
|
|
|
std::string sUrl = it.second.getUrl();
|
2016-03-04 12:49:01 -06:00
|
|
|
std::string sViews = std::to_string(it.second.getActiveViews());
|
|
|
|
std::string sMem = std::to_string(Util::getMemoryUsage(it.second.getPid()));
|
2016-03-03 12:01:37 -06:00
|
|
|
|
|
|
|
oss << sPid << " "
|
|
|
|
<< sUrl << " "
|
|
|
|
<< sViews << " "
|
|
|
|
<< sMem << " \n ";
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
|
|
|
|
2016-03-03 12:01:37 -06:00
|
|
|
return oss.str();
|
2016-02-26 00:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-05 09:21:25 -06:00
|
|
|
std::map<int, Subscriber> _subscribers;
|
2016-03-03 12:01:37 -06:00
|
|
|
std::map<Poco::Process::PID, Document> _documents;
|
|
|
|
|
|
|
|
/// Number of active documents
|
2016-03-04 12:49:01 -06:00
|
|
|
unsigned _nActiveDocuments = 0;
|
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: */
|