2016-02-17 09:27:27 -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_HPP
|
|
|
|
#define INCLUDED_ADMIN_HPP
|
|
|
|
|
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 <mutex>
|
|
|
|
|
2016-02-24 10:58:05 -06:00
|
|
|
#include "AdminModel.hpp"
|
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-02-17 09:27:27 -06:00
|
|
|
|
2017-03-15 09:58:55 -05:00
|
|
|
#include "net/WebSocketHandler.hpp"
|
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
class Admin;
|
|
|
|
|
2017-03-15 09:58:55 -05:00
|
|
|
/// Handle admin client's Websocket requests & replies.
|
2017-03-16 12:32:38 -05:00
|
|
|
class AdminSocketHandler : public WebSocketHandler
|
2016-03-20 05:59:32 -05:00
|
|
|
{
|
|
|
|
public:
|
2017-03-16 12:32:38 -05:00
|
|
|
AdminSocketHandler(Admin* adminManager,
|
|
|
|
const std::weak_ptr<StreamSocket>& socket,
|
|
|
|
const Poco::Net::HTTPRequest& request);
|
2016-03-20 05:59:32 -05:00
|
|
|
|
2017-03-15 11:13:13 -05:00
|
|
|
/// Handle the initial Admin WS upgrade request.
|
|
|
|
/// @returns true if we should give this socket to the Admin poll.
|
|
|
|
static bool handleInitialRequest(const std::weak_ptr<StreamSocket> &socket,
|
2017-03-15 09:40:24 -05:00
|
|
|
const Poco::Net::HTTPRequest& request);
|
2016-03-20 05:59:32 -05:00
|
|
|
|
|
|
|
private:
|
2017-03-16 12:43:33 -05:00
|
|
|
/// Sends text frames simply to authenticated clients.
|
2016-05-03 02:10:21 -05:00
|
|
|
void sendTextFrame(const std::string& message);
|
|
|
|
|
2017-03-15 09:58:55 -05:00
|
|
|
/// Process incoming websocket messages
|
|
|
|
void handleMessage(bool fin, WSOpCode code, std::vector<char> &data);
|
2016-04-19 02:27:28 -05:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
private:
|
|
|
|
Admin* _admin;
|
2016-05-03 02:10:21 -05:00
|
|
|
int _sessionId;
|
2016-07-28 07:28:40 -05:00
|
|
|
bool _isAuthenticated;
|
2016-03-20 05:59:32 -05:00
|
|
|
};
|
|
|
|
|
2017-02-03 00:29:53 -06:00
|
|
|
class MemoryStatsTask;
|
2017-01-29 13:18:55 -06:00
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
/// An admin command processor.
|
2017-03-15 11:13:13 -05:00
|
|
|
class Admin : public SocketPoll
|
2016-02-17 09:27:27 -06:00
|
|
|
{
|
2017-03-15 09:58:55 -05:00
|
|
|
Admin();
|
2016-02-17 09:27:27 -06:00
|
|
|
public:
|
2016-04-06 07:43:44 -05:00
|
|
|
virtual ~Admin();
|
2016-02-17 09:27:27 -06:00
|
|
|
|
2016-04-06 07:43:44 -05:00
|
|
|
static Admin& instance()
|
|
|
|
{
|
|
|
|
static Admin admin;
|
|
|
|
return admin;
|
|
|
|
}
|
2016-02-17 09:27:27 -06:00
|
|
|
|
2017-03-15 11:13:13 -05:00
|
|
|
void start()
|
|
|
|
{
|
|
|
|
// FIXME: not if admin console is not enabled ?
|
|
|
|
startThread();
|
|
|
|
}
|
|
|
|
|
2017-03-15 11:39:13 -05:00
|
|
|
/// Custom poll thread function
|
|
|
|
void pollingThread() override;
|
|
|
|
|
2017-06-03 16:53:57 -05:00
|
|
|
size_t getTotalMemoryUsage();
|
2017-06-05 20:17:42 -05:00
|
|
|
size_t getTotalCpuUsage();
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2017-05-12 08:59:01 -05:00
|
|
|
void modificationAlert(const std::string& dockey, Poco::Process::PID pid, bool value);
|
2016-04-06 07:43:44 -05:00
|
|
|
/// Update the Admin Model.
|
|
|
|
void update(const std::string& message);
|
|
|
|
|
2016-04-14 15:29:31 -05:00
|
|
|
/// Calls with same pid will increment view count, if pid already exists
|
2017-04-07 09:19:04 -05:00
|
|
|
void addDoc(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
|
|
|
|
|
|
|
/// Decrement view count till becomes zero after which doc is removed
|
2016-04-17 21:11:10 -05:00
|
|
|
void rmDoc(const std::string& docKey, const std::string& sessionId);
|
2016-04-14 15:29:31 -05:00
|
|
|
|
2016-04-17 21:32:58 -05:00
|
|
|
/// Remove the document with all views. Used on termination or catastrophic failure.
|
|
|
|
void rmDoc(const std::string& docKey);
|
|
|
|
|
2016-04-07 03:27:43 -05:00
|
|
|
void setForKitPid(const int forKitPid) { _forKitPid = forKitPid; }
|
2017-06-11 22:47:48 -05:00
|
|
|
void setForKitWritePipe(const int forKitWritePipe) { _forKitWritePipe = forKitWritePipe; }
|
2016-04-06 07:43:44 -05:00
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
/// Callers must ensure that modelMutex is acquired
|
2016-03-03 12:01:37 -06:00
|
|
|
AdminModel& getModel();
|
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
unsigned getMemStatsInterval();
|
|
|
|
|
|
|
|
unsigned getCpuStatsInterval();
|
|
|
|
|
2017-06-09 20:28:16 -05:00
|
|
|
unsigned getNetStatsInterval();
|
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
void rescheduleMemTimer(unsigned interval);
|
|
|
|
|
|
|
|
void rescheduleCpuTimer(unsigned interval);
|
|
|
|
|
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);
|
2017-06-03 16:53:57 -05:00
|
|
|
void addBytes(const std::string& docKey, uint64_t sent, uint64_t recv);
|
2016-12-05 14:11:07 -06:00
|
|
|
|
2017-03-15 11:13:13 -05:00
|
|
|
void dumpState(std::ostream& os) override;
|
|
|
|
|
2017-06-11 21:51:38 -05:00
|
|
|
const DocProcSettings& getDefDocProcSettings() const { return _defDocProcSettings; }
|
2017-06-11 22:47:48 -05:00
|
|
|
void setDefDocProcSettings(const DocProcSettings& docProcSettings)
|
|
|
|
{
|
|
|
|
_defDocProcSettings = docProcSettings;
|
|
|
|
notifyForkit();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Notify Forkit of changed settings.
|
|
|
|
void notifyForkit();
|
2017-06-11 21:51:38 -05:00
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
private:
|
2017-04-03 12:21:20 -05:00
|
|
|
/// The model is accessed only during startup & in
|
|
|
|
/// the Admin Poll thread.
|
2016-02-24 10:58:05 -06:00
|
|
|
AdminModel _model;
|
2016-04-07 03:27:43 -05:00
|
|
|
int _forKitPid;
|
2017-06-11 22:47:48 -05:00
|
|
|
int _forKitWritePipe;
|
2017-06-03 16:53:57 -05:00
|
|
|
size_t _lastTotalMemory;
|
2017-06-05 20:17:42 -05:00
|
|
|
size_t _lastJiffies;
|
2017-06-09 20:28:16 -05:00
|
|
|
uint64_t _lastSentCount;
|
|
|
|
uint64_t _lastRecvCount;
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2017-03-15 11:39:13 -05:00
|
|
|
std::atomic<int> _memStatsTaskIntervalMs;
|
|
|
|
std::atomic<int> _cpuStatsTaskIntervalMs;
|
2017-06-11 21:51:38 -05:00
|
|
|
DocProcSettings _defDocProcSettings;
|
2017-06-09 20:28:16 -05:00
|
|
|
std::atomic<int> _networkStatsIntervalMs;
|
2016-03-05 14:39:35 -06:00
|
|
|
};
|
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|