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-03-20 05:59:32 -05:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPRequestHandler.h>
|
2016-02-17 09:27:27 -06:00
|
|
|
#include <Poco/Net/HTTPServer.h>
|
2016-02-26 00:25:13 -06:00
|
|
|
#include <Poco/Runnable.h>
|
|
|
|
#include <Poco/Types.h>
|
2016-03-05 14:39:35 -06:00
|
|
|
#include <Poco/Util/Timer.h>
|
|
|
|
#include <Poco/Util/TimerTask.h>
|
2016-02-17 09:27:27 -06:00
|
|
|
|
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-11-10 02:47:25 -06:00
|
|
|
#include <LOOLWebSocket.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.
|
|
|
|
class AdminRequestHandler : public WebSocketHandler
|
2016-03-20 05:59:32 -05:00
|
|
|
{
|
|
|
|
public:
|
2017-03-15 09:58:55 -05:00
|
|
|
AdminRequestHandler(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-01-13 06:52:08 -06:00
|
|
|
void handleWSRequests(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response, int sessionId);
|
2016-03-20 05:59:32 -05:00
|
|
|
|
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-01-29 13:18:55 -06:00
|
|
|
unsigned getTotalMemoryUsage();
|
2016-03-05 14:39:35 -06:00
|
|
|
|
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
|
2016-04-17 21:11:10 -05:00
|
|
|
void addDoc(const std::string& docKey, Poco::Process::PID pid, const std::string& filename, const std::string& sessionId);
|
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; }
|
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();
|
|
|
|
|
|
|
|
void rescheduleMemTimer(unsigned interval);
|
|
|
|
|
|
|
|
void rescheduleCpuTimer(unsigned interval);
|
|
|
|
|
2016-04-06 07:43:44 -05:00
|
|
|
std::unique_lock<std::mutex> getLock() { return std::unique_lock<std::mutex>(_modelMutex); }
|
2016-03-05 14:39:35 -06:00
|
|
|
|
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);
|
2016-12-05 14:11:07 -06:00
|
|
|
|
2017-03-15 11:13:13 -05:00
|
|
|
void dumpState(std::ostream& os) override;
|
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
private:
|
2016-02-24 10:58:05 -06:00
|
|
|
AdminModel _model;
|
2016-04-06 07:43:44 -05:00
|
|
|
std::mutex _modelMutex;
|
2016-04-07 03:27:43 -05:00
|
|
|
int _forKitPid;
|
2016-02-24 10:58:05 -06:00
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
Poco::Util::Timer _memStatsTimer;
|
2017-02-08 14:39:10 -06:00
|
|
|
Poco::AutoPtr<MemoryStatsTask> _memStatsTask;
|
2016-03-05 14:39:35 -06:00
|
|
|
unsigned _memStatsTaskInterval = 5000;
|
|
|
|
|
|
|
|
Poco::Util::Timer _cpuStatsTimer;
|
|
|
|
Poco::Util::TimerTask::Ptr _cpuStatsTask;
|
|
|
|
unsigned _cpuStatsTaskInterval = 5000;
|
2016-02-17 09:27:27 -06:00
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Memory statistics.
|
2017-02-03 00:29:53 -06:00
|
|
|
class MemoryStatsTask : public Poco::Util::TimerTask
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
|
|
|
public:
|
2017-02-03 00:29:53 -06:00
|
|
|
MemoryStatsTask(Admin* admin)
|
2016-04-29 20:59:13 -05:00
|
|
|
: _admin(admin),
|
|
|
|
_lastTotalMemory(0)
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
2017-01-30 11:44:53 -06:00
|
|
|
LOG_DBG("Memory stat ctor");
|
2016-03-05 14:39:35 -06:00
|
|
|
}
|
|
|
|
|
2017-02-03 00:29:53 -06:00
|
|
|
~MemoryStatsTask()
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
2017-01-30 11:44:53 -06:00
|
|
|
LOG_DBG("Memory stat dtor");
|
2016-03-05 14:39:35 -06:00
|
|
|
}
|
|
|
|
|
2017-01-29 13:18:55 -06:00
|
|
|
long getLastTotalMemory() { return _lastTotalMemory; }
|
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
void run() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Admin* _admin;
|
2016-04-29 20:59:13 -05:00
|
|
|
long _lastTotalMemory;
|
2016-03-05 14:39:35 -06:00
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// CPU statistics.
|
2016-03-05 14:39:35 -06:00
|
|
|
class CpuStats : public Poco::Util::TimerTask
|
|
|
|
{
|
|
|
|
public:
|
2016-03-16 03:11:50 -05:00
|
|
|
CpuStats(Admin* /*admin*/)
|
2016-03-05 14:39:35 -06:00
|
|
|
{
|
2017-01-30 11:44:53 -06:00
|
|
|
LOG_DBG("Cpu stat ctor");
|
2016-03-05 14:39:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
~CpuStats()
|
|
|
|
{
|
2017-01-30 11:44:53 -06:00
|
|
|
LOG_DBG("Cpu stat dtor");
|
2016-03-05 14:39:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void run() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|