Dumps data to JSON files which can be later served by a web-server.pull/11/head
parent
5d6fa03f11
commit
295cbda449
@ -0,0 +1,196 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "p2pool_api.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <direct.h>
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
static constexpr char log_category_prefix[] = "P2Pool API ";
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
p2pool_api::p2pool_api(const std::string& api_path) : m_apiPath(api_path)
|
||||
{
|
||||
if (m_apiPath.empty()) {
|
||||
LOGERR(1, "api path is empty");
|
||||
panic();
|
||||
}
|
||||
|
||||
if ((m_apiPath.back() != '/')
|
||||
#ifdef _WIN32
|
||||
&& (m_apiPath.back() != '\\')
|
||||
#endif
|
||||
) {
|
||||
m_apiPath += '/';
|
||||
}
|
||||
|
||||
struct stat buf;
|
||||
if (stat(m_apiPath.c_str(), &buf) != 0) {
|
||||
LOGERR(1, "path " << m_apiPath << " doesn't exist");
|
||||
panic();
|
||||
}
|
||||
|
||||
int result = uv_async_init(uv_default_loop_checked(), &m_dumpToFileAsync, on_dump_to_file);
|
||||
if (result) {
|
||||
LOGERR(1, "uv_async_init failed, error " << uv_err_name(result));
|
||||
panic();
|
||||
}
|
||||
m_dumpToFileAsync.data = this;
|
||||
|
||||
uv_mutex_init_checked(&m_dumpDataLock);
|
||||
|
||||
m_networkPath = m_apiPath + "network/";
|
||||
|
||||
#ifdef _MSC_VER
|
||||
result = _mkdir(m_networkPath.c_str());
|
||||
#else
|
||||
result = mkdir(m_networkPath.c_str(), 0775);
|
||||
#endif
|
||||
|
||||
if (result < 0) {
|
||||
result = errno;
|
||||
if (result != EEXIST) {
|
||||
LOGERR(1, "mkdir(" << m_networkPath << ") failed, error " << result);
|
||||
panic();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p2pool_api::~p2pool_api()
|
||||
{
|
||||
uv_mutex_destroy(&m_dumpDataLock);
|
||||
}
|
||||
|
||||
void p2pool_api::on_stop()
|
||||
{
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(&m_dumpToFileAsync), nullptr);
|
||||
}
|
||||
|
||||
void p2pool_api::dump_to_file_async_internal(const Category& category, const char* filename, DumpFileCallbackBase&& callback)
|
||||
{
|
||||
std::vector<char> buf(log::Stream::BUF_SIZE + 1);
|
||||
log::Stream s(buf.data());
|
||||
callback(s);
|
||||
buf.resize(s.m_pos);
|
||||
|
||||
std::string path;
|
||||
|
||||
switch (category) {
|
||||
case Category::NETWORK: path = m_networkPath + filename;
|
||||
}
|
||||
|
||||
{
|
||||
MutexLock lock(m_dumpDataLock);
|
||||
m_dumpData[path] = std::move(buf);
|
||||
}
|
||||
|
||||
uv_async_send(&m_dumpToFileAsync);
|
||||
}
|
||||
|
||||
void p2pool_api::dump_to_file()
|
||||
{
|
||||
std::unordered_map<std::string, std::vector<char>> data;
|
||||
{
|
||||
MutexLock lock(m_dumpDataLock);
|
||||
data = std::move(m_dumpData);
|
||||
m_dumpData.clear();
|
||||
}
|
||||
|
||||
for (auto& it : data) {
|
||||
DumpFileWork* work = new DumpFileWork{ {}, {}, {}, it.first, std::move(it.second) };
|
||||
work->open_req.data = work;
|
||||
work->write_req.data = work;
|
||||
work->close_req.data = work;
|
||||
|
||||
const int flags = O_WRONLY | O_CREAT | O_TRUNC
|
||||
#ifdef O_BINARY
|
||||
| O_BINARY
|
||||
#endif
|
||||
;
|
||||
|
||||
const int result = uv_fs_open(uv_default_loop_checked(), &work->open_req, it.first.c_str(), flags, 0644, on_fs_open);
|
||||
if (result < 0) {
|
||||
LOGWARN(4, "failed to open " << it.first << ", error " << uv_err_name(result));
|
||||
delete work;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void p2pool_api::on_fs_open(uv_fs_t* req)
|
||||
{
|
||||
DumpFileWork* work = reinterpret_cast<DumpFileWork*>(req->data);
|
||||
const int fd = static_cast<int>(req->result);
|
||||
|
||||
if (fd < 0) {
|
||||
LOGWARN(4, "failed to open " << work->name << ", error " << uv_err_name(fd));
|
||||
uv_fs_req_cleanup(req);
|
||||
delete work;
|
||||
return;
|
||||
}
|
||||
|
||||
uv_buf_t buf[1];
|
||||
buf[0].base = work->buf.data();
|
||||
buf[0].len = static_cast<uint32_t>(work->buf.size());
|
||||
|
||||
const int result = uv_fs_write(uv_default_loop_checked(), &work->write_req, static_cast<uv_file>(fd), buf, 1, 0, on_fs_write);
|
||||
if (result < 0) {
|
||||
LOGWARN(4, "failed to write to " << work->name << ", error " << uv_err_name(result));
|
||||
uv_fs_req_cleanup(req);
|
||||
delete work;
|
||||
return;
|
||||
}
|
||||
|
||||
uv_fs_req_cleanup(req);
|
||||
}
|
||||
|
||||
void p2pool_api::on_fs_write(uv_fs_t* req)
|
||||
{
|
||||
DumpFileWork* work = reinterpret_cast<DumpFileWork*>(req->data);
|
||||
|
||||
if (req->result < 0) {
|
||||
LOGWARN(4, "failed to write to " << work->name << ", error " << uv_err_name(static_cast<int>(req->result)));
|
||||
}
|
||||
|
||||
const int result = uv_fs_close(uv_default_loop_checked(), &work->close_req, static_cast<uv_file>(work->open_req.result), on_fs_close);
|
||||
if (result < 0) {
|
||||
LOGWARN(4, "failed to close " << work->name << ", error " << uv_err_name(result));
|
||||
uv_fs_req_cleanup(req);
|
||||
delete work;
|
||||
return;
|
||||
}
|
||||
|
||||
uv_fs_req_cleanup(req);
|
||||
}
|
||||
|
||||
void p2pool_api::on_fs_close(uv_fs_t* req)
|
||||
{
|
||||
DumpFileWork* work = reinterpret_cast<DumpFileWork*>(req->data);
|
||||
|
||||
if (req->result < 0) {
|
||||
LOGWARN(4, "failed to close " << work->name << ", error " << uv_err_name(static_cast<int>(req->result)));
|
||||
}
|
||||
|
||||
uv_fs_req_cleanup(req);
|
||||
delete work;
|
||||
}
|
||||
|
||||
} // namespace p2pool
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
||||
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "uv_util.h"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace p2pool {
|
||||
|
||||
class p2pool_api
|
||||
{
|
||||
public:
|
||||
explicit p2pool_api(const std::string& api_path);
|
||||
~p2pool_api();
|
||||
|
||||
enum class Category {
|
||||
NETWORK,
|
||||
};
|
||||
|
||||
void on_stop();
|
||||
|
||||
template<typename T>
|
||||
void set(const Category& category, const char* filename, T&& callback) { dump_to_file_async_internal(category, filename, DumpFileCallback<T>(std::move(callback))); }
|
||||
|
||||
private:
|
||||
static void on_dump_to_file(uv_async_t* async) { reinterpret_cast<p2pool_api*>(async->data)->dump_to_file(); }
|
||||
|
||||
struct DumpFileWork {
|
||||
uv_fs_t open_req;
|
||||
uv_fs_t write_req;
|
||||
uv_fs_t close_req;
|
||||
|
||||
std::string name;
|
||||
std::vector<char> buf;
|
||||
};
|
||||
|
||||
struct DumpFileCallbackBase
|
||||
{
|
||||
virtual ~DumpFileCallbackBase() {}
|
||||
virtual void operator()(log::Stream&) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct DumpFileCallback : public DumpFileCallbackBase
|
||||
{
|
||||
explicit FORCEINLINE DumpFileCallback(T&& callback) : m_callback(std::move(callback)) {}
|
||||
void operator()(log::Stream& s) override { m_callback(s); }
|
||||
|
||||
private:
|
||||
DumpFileCallback& operator=(DumpFileCallback&&) = delete;
|
||||
|
||||
T m_callback;
|
||||
};
|
||||
|
||||
void dump_to_file_async_internal(const Category& category, const char* filename, DumpFileCallbackBase&& callback);
|
||||
void dump_to_file();
|
||||
static void on_fs_open(uv_fs_t* req);
|
||||
static void on_fs_write(uv_fs_t* req);
|
||||
static void on_fs_close(uv_fs_t* req);
|
||||
|
||||
std::string m_apiPath;
|
||||
std::string m_networkPath;
|
||||
|
||||
uv_mutex_t m_dumpDataLock;
|
||||
std::unordered_map<std::string, std::vector<char>> m_dumpData;
|
||||
|
||||
uv_async_t m_dumpToFileAsync;
|
||||
};
|
||||
|
||||
} // namespace p2pool
|
Loading…
Reference in new issue