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
|
|
|
|
|
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-04-19 02:27:28 -05:00
|
|
|
#include <Poco/Net/WebSocket.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"
|
2016-02-17 09:27:27 -06:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
class Admin;
|
|
|
|
|
|
|
|
class AdminRequestHandler: public Poco::Net::HTTPRequestHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AdminRequestHandler(Admin* adminManager);
|
|
|
|
|
|
|
|
void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void handleWSRequests(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response, int nSessionId);
|
|
|
|
|
2016-04-19 02:27:28 -05:00
|
|
|
void sendTextFrame(std::shared_ptr<Poco::Net::WebSocket>& socket, const std::string& message);
|
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
private:
|
|
|
|
Admin* _admin;
|
|
|
|
};
|
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
/// An admin command processor.
|
2016-04-13 03:14:54 -05:00
|
|
|
class 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
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
unsigned getTotalMemoryUsage(AdminModel&);
|
|
|
|
|
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
|
|
|
static
|
|
|
|
AdminRequestHandler* createRequestHandler()
|
|
|
|
{
|
|
|
|
return new AdminRequestHandler(&instance());
|
|
|
|
}
|
2016-03-20 05:59:32 -05:00
|
|
|
|
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-02-26 00:25:13 -06:00
|
|
|
private:
|
2016-04-06 07:43:44 -05:00
|
|
|
Admin();
|
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;
|
|
|
|
Poco::Util::TimerTask::Ptr _memStatsTask;
|
|
|
|
unsigned _memStatsTaskInterval = 5000;
|
|
|
|
|
|
|
|
Poco::Util::Timer _cpuStatsTimer;
|
|
|
|
Poco::Util::TimerTask::Ptr _cpuStatsTask;
|
|
|
|
unsigned _cpuStatsTaskInterval = 5000;
|
2016-02-17 09:27:27 -06:00
|
|
|
};
|
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
class MemoryStats : public Poco::Util::TimerTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MemoryStats(Admin* admin)
|
|
|
|
: _admin(admin)
|
|
|
|
{
|
|
|
|
Log::info("Memory stat ctor");
|
|
|
|
}
|
|
|
|
|
|
|
|
~MemoryStats()
|
|
|
|
{
|
|
|
|
Log::info("Memory stat dtor");
|
|
|
|
}
|
|
|
|
|
|
|
|
void run() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Admin* _admin;
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
Log::info("Cpu stat ctor");
|
|
|
|
}
|
|
|
|
|
|
|
|
~CpuStats()
|
|
|
|
{
|
|
|
|
Log::info("Cpu stat dtor");
|
|
|
|
}
|
|
|
|
|
|
|
|
void run() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|