Small case namespaces
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace config::client
|
||||
{
|
||||
struct CollectorConfig
|
||||
{
|
||||
std::vector<std::string> disks{{"/"}};
|
||||
};
|
||||
} // namespace config::client
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "Config/Common/IniParser.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace Config::Client
|
||||
namespace config::client
|
||||
{
|
||||
Config Config::load(const std::string &path)
|
||||
{
|
||||
@@ -13,7 +13,8 @@ namespace Config::Client
|
||||
cfg.network.serverHost = ini.get("network", "server_host", "127.0.0.1");
|
||||
cfg.network.serverPort = std::stoi(ini.get("network", "server_port", "5005"));
|
||||
cfg.network.intervalMs = std::chrono::milliseconds(std::stoi(ini.get("network", "interval_ms", "1000")));
|
||||
cfg.collector.disks = config::splitList(ini.get("collector", "disks", "/"));
|
||||
|
||||
return cfg;
|
||||
}
|
||||
} // namespace Config::Client
|
||||
} // namespace config::client
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
#include <string>
|
||||
#include "Display/Graphics/Color.h"
|
||||
#include "Config/Client/NetworkConfig.h"
|
||||
#include "Config/Client/CollectorConfig.h"
|
||||
|
||||
namespace Config::Client
|
||||
namespace config::client
|
||||
{
|
||||
class Config
|
||||
{
|
||||
@@ -11,5 +12,6 @@ namespace Config::Client
|
||||
static Config load(const std::string &path);
|
||||
|
||||
NetworkConfig network;
|
||||
CollectorConfig collector;
|
||||
};
|
||||
} // namespace Config::Client
|
||||
} // namespace config::client
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
namespace Config::Client
|
||||
namespace config::client
|
||||
{
|
||||
struct NetworkConfig
|
||||
{
|
||||
std::string serverHost = "127.0.0.1";
|
||||
int serverPort = 5005;
|
||||
std::chrono::milliseconds intervalMs = std::chrono::milliseconds(1000);
|
||||
std::string serverHost{"127.0.0.1"};
|
||||
int serverPort{5005};
|
||||
std::chrono::milliseconds intervalMs{std::chrono::milliseconds(1000)};
|
||||
};
|
||||
} // namespace Config::Client
|
||||
} // namespace config::client
|
||||
|
||||
@@ -3,56 +3,75 @@
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
static std::string trim(std::string s)
|
||||
namespace config
|
||||
{
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c)
|
||||
{ return !isspace(c); }));
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c)
|
||||
{ return !isspace(c); })
|
||||
.base(),
|
||||
s.end());
|
||||
return s;
|
||||
}
|
||||
|
||||
bool IniParser::load(const std::string &path)
|
||||
{
|
||||
std::ifstream f(path);
|
||||
if (!f)
|
||||
return false;
|
||||
|
||||
std::string line, section;
|
||||
while (std::getline(f, line))
|
||||
std::string trim(std::string s)
|
||||
{
|
||||
line = trim(line);
|
||||
if (line.empty() || line[0] == '#' || line[0] == ';')
|
||||
continue;
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c)
|
||||
{ return !isspace(c); }));
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c)
|
||||
{ return !isspace(c); })
|
||||
.base(),
|
||||
s.end());
|
||||
return s;
|
||||
}
|
||||
|
||||
if (line.front() == '[' && line.back() == ']')
|
||||
std::vector<std::string> splitList(const std::string &s, char delim)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
std::string item;
|
||||
std::istringstream ss(s);
|
||||
|
||||
while (std::getline(ss, item, delim))
|
||||
{
|
||||
section = line.substr(1, line.size() - 2);
|
||||
continue;
|
||||
item = trim(item);
|
||||
if (!item.empty())
|
||||
result.push_back(item);
|
||||
}
|
||||
|
||||
auto pos = line.find('=');
|
||||
if (pos == std::string::npos)
|
||||
continue;
|
||||
|
||||
std::string key = trim(line.substr(0, pos));
|
||||
std::string val = trim(line.substr(pos + 1));
|
||||
data_[section][key] = val;
|
||||
return result;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string IniParser::get(const std::string §ion,
|
||||
const std::string &key,
|
||||
const std::string &def) const
|
||||
{
|
||||
auto s = data_.find(section);
|
||||
if (s == data_.end())
|
||||
return def;
|
||||
auto k = s->second.find(key);
|
||||
if (k == s->second.end())
|
||||
return def;
|
||||
return k->second;
|
||||
}
|
||||
bool IniParser::load(const std::string &path)
|
||||
{
|
||||
std::ifstream f(path);
|
||||
if (!f)
|
||||
return false;
|
||||
|
||||
std::string line, section;
|
||||
while (std::getline(f, line))
|
||||
{
|
||||
line = trim(line);
|
||||
if (line.empty() || line[0] == '#' || line[0] == ';')
|
||||
continue;
|
||||
|
||||
if (line.front() == '[' && line.back() == ']')
|
||||
{
|
||||
section = line.substr(1, line.size() - 2);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto pos = line.find('=');
|
||||
if (pos == std::string::npos)
|
||||
continue;
|
||||
|
||||
std::string key = trim(line.substr(0, pos));
|
||||
std::string val = trim(line.substr(pos + 1));
|
||||
data[section][key] = val;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string IniParser::get(const std::string §ion,
|
||||
const std::string &key,
|
||||
const std::string &def) const
|
||||
{
|
||||
auto s = data.find(section);
|
||||
if (s == data.end())
|
||||
return def;
|
||||
auto k = s->second.find(key);
|
||||
if (k == s->second.end())
|
||||
return def;
|
||||
return k->second;
|
||||
}
|
||||
} // namespace config
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class IniParser
|
||||
namespace config
|
||||
{
|
||||
public:
|
||||
bool load(const std::string &path);
|
||||
std::string trim(std::string s);
|
||||
std::vector<std::string> splitList(const std::string &s, char delim = ',');
|
||||
|
||||
std::string get(const std::string §ion,
|
||||
const std::string &key,
|
||||
const std::string &def = "") const;
|
||||
class IniParser
|
||||
{
|
||||
public:
|
||||
bool load(const std::string &path);
|
||||
|
||||
private:
|
||||
using Section = std::unordered_map<std::string, std::string>;
|
||||
std::unordered_map<std::string, Section> data_;
|
||||
};
|
||||
std::string get(const std::string §ion,
|
||||
const std::string &key,
|
||||
const std::string &def = "") const;
|
||||
|
||||
private:
|
||||
using Section = std::unordered_map<std::string, std::string>;
|
||||
std::unordered_map<std::string, Section> data;
|
||||
};
|
||||
} // namespace config
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
#include "Display/Graphics/Color.h"
|
||||
|
||||
namespace Config::Server
|
||||
namespace config::server
|
||||
{
|
||||
struct BarStyleConfig
|
||||
{
|
||||
Display::Graphics::Color background;
|
||||
Display::Graphics::Color fill;
|
||||
display::graphics::Color background;
|
||||
display::graphics::Color fill;
|
||||
};
|
||||
|
||||
} // namespace Config::Server
|
||||
} // namespace config::server
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
#include "Config/Common/IniParser.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace Config::Server
|
||||
namespace config::server
|
||||
{
|
||||
static Display::Graphics::Color parseColor(const std::string &s)
|
||||
static display::graphics::Color parseColor(const std::string &s)
|
||||
{
|
||||
int r, g, b;
|
||||
char c;
|
||||
@@ -36,4 +36,4 @@ namespace Config::Server
|
||||
|
||||
return cfg;
|
||||
}
|
||||
} // namespace Config::Server
|
||||
} // namespace config::server
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "Config/Server/NetworkConfig.h"
|
||||
#include "Config/Server/TextStyleConfig.h"
|
||||
|
||||
namespace Config::Server
|
||||
namespace config::server
|
||||
{
|
||||
class Config
|
||||
{
|
||||
@@ -18,4 +18,4 @@ namespace Config::Server
|
||||
TextStyleConfig textStyle;
|
||||
BarStyleConfig barStyle;
|
||||
};
|
||||
} // namespace Config::Server
|
||||
} // namespace config::server
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace Config::Server
|
||||
namespace config::server
|
||||
{
|
||||
struct DisplayConfig
|
||||
{
|
||||
int rotation = 0;
|
||||
std::chrono::milliseconds refreshMs = std::chrono::milliseconds(1000);
|
||||
};
|
||||
} // namespace Config::Server
|
||||
} // namespace config::server
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
namespace Config::Server
|
||||
namespace config::server
|
||||
{
|
||||
struct NetworkConfig
|
||||
{
|
||||
@@ -11,4 +11,4 @@ namespace Config::Server
|
||||
int listenPort = 5005;
|
||||
std::chrono::milliseconds intervalMs = std::chrono::milliseconds(1000);
|
||||
};
|
||||
} // namespace Config::Server
|
||||
} // namespace config::server
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Config::Server
|
||||
namespace config::server
|
||||
{
|
||||
struct TextStyleConfig
|
||||
{
|
||||
std::string fontPath;
|
||||
int fontSize = 10;
|
||||
};
|
||||
} // namespace Config::Server
|
||||
} // namespace config::server
|
||||
|
||||
@@ -11,9 +11,9 @@ target_link_libraries(DisplayGraphics
|
||||
PRIVATE
|
||||
Model::All
|
||||
Helpers::All
|
||||
Display::UI::Text
|
||||
Display::UI::HostBlock
|
||||
Display::UI::Header
|
||||
display::ui::Text
|
||||
display::ui::HostBlock
|
||||
display::ui::Header
|
||||
)
|
||||
|
||||
target_include_directories(DisplayGraphics
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
namespace Display::Graphics
|
||||
namespace display::graphics
|
||||
{
|
||||
struct Color
|
||||
{
|
||||
@@ -35,4 +35,4 @@ namespace Display::Graphics
|
||||
inline Color Red() { return {255, 0, 0}; }
|
||||
inline Color Green() { return {0, 255, 0}; }
|
||||
inline Color Blue() { return {0, 0, 255}; }
|
||||
} // namespace Display::Graphics
|
||||
} // namespace display::graphics
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Display::Graphics
|
||||
namespace display::graphics
|
||||
{
|
||||
Framebuffer::Framebuffer(const char *device, FramebufferRotation rotation) : rotation(rotation)
|
||||
{
|
||||
@@ -135,4 +135,4 @@ namespace Display::Graphics
|
||||
c.a = 255;
|
||||
return c;
|
||||
}
|
||||
} // namespace Display::Graphics
|
||||
} // namespace display::graphics
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "Display/Graphics/Color.h"
|
||||
#include "Display/Graphics/FramebufferRotation.h"
|
||||
|
||||
namespace Display::Graphics
|
||||
namespace display::graphics
|
||||
{
|
||||
class Framebuffer
|
||||
{
|
||||
@@ -40,4 +40,4 @@ namespace Display::Graphics
|
||||
size_t size;
|
||||
int line_length;
|
||||
};
|
||||
} // namespace Display::Graphics
|
||||
} // namespace display::graphics
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace Display::Graphics
|
||||
namespace display::graphics
|
||||
{
|
||||
enum class FramebufferRotation
|
||||
{
|
||||
@@ -9,4 +9,4 @@ namespace Display::Graphics
|
||||
R180,
|
||||
R270
|
||||
};
|
||||
} // namespace Display::Graphics
|
||||
} // namespace display::graphics
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "Renderer.h"
|
||||
|
||||
namespace Display::Graphics
|
||||
namespace display::graphics
|
||||
{
|
||||
Renderer::Renderer(Framebuffer &framebuffer, Model::HostRegistry ®istry)
|
||||
Renderer::Renderer(Framebuffer &framebuffer, model::HostRegistry ®istry)
|
||||
: framebuffer(framebuffer),
|
||||
registry(registry)
|
||||
{
|
||||
@@ -15,7 +15,7 @@ namespace Display::Graphics
|
||||
header.draw(framebuffer, textRenderer, START_X, START_Y);
|
||||
|
||||
int blocksPerRow =
|
||||
(SCREEN_WIDTH + BLOCK_GAP) / (Display::UI::HostBlock::BLOCK_WIDTH + BLOCK_GAP);
|
||||
(SCREEN_WIDTH + BLOCK_GAP) / (display::ui::hostblock::BLOCK_WIDTH + BLOCK_GAP);
|
||||
if (blocksPerRow < 1)
|
||||
blocksPerRow = 1;
|
||||
|
||||
@@ -26,8 +26,8 @@ namespace Display::Graphics
|
||||
int col = index % blocksPerRow;
|
||||
int row = index / blocksPerRow;
|
||||
|
||||
int x = START_X + col * (Display::UI::HostBlock::BLOCK_WIDTH + BLOCK_GAP);
|
||||
int y = START_Y + header.height() + BLOCK_GAP + row * (Display::UI::HostBlock::BLOCK_HEIGHT + BLOCK_GAP);
|
||||
int x = START_X + col * (display::ui::hostblock::BLOCK_WIDTH + BLOCK_GAP);
|
||||
int y = START_Y + header.height() + BLOCK_GAP + row * (display::ui::hostblock::BLOCK_HEIGHT + BLOCK_GAP);
|
||||
|
||||
hostblock.draw(
|
||||
framebuffer,
|
||||
@@ -42,4 +42,4 @@ namespace Display::Graphics
|
||||
|
||||
framebuffer.present();
|
||||
}
|
||||
} // namespace Display::Graphics
|
||||
} // namespace display::graphics
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "Display/Graphics/Color.h"
|
||||
#include <string>
|
||||
|
||||
namespace Display::Graphics
|
||||
namespace display::graphics
|
||||
{
|
||||
static constexpr int START_X = 0; // 0
|
||||
static constexpr int START_Y = 0; // 20
|
||||
@@ -18,7 +18,7 @@ namespace Display::Graphics
|
||||
class Renderer
|
||||
{
|
||||
public:
|
||||
Renderer(Framebuffer &framebuffer, Model::HostRegistry ®istry);
|
||||
Renderer(Framebuffer &framebuffer, model::HostRegistry ®istry);
|
||||
|
||||
Renderer(const Renderer &) = delete;
|
||||
Renderer &operator=(const Renderer &) = delete;
|
||||
@@ -27,9 +27,9 @@ namespace Display::Graphics
|
||||
|
||||
private:
|
||||
Framebuffer &framebuffer;
|
||||
Model::HostRegistry ®istry;
|
||||
Display::UI::Text::Renderer textRenderer;
|
||||
Display::UI::Header::Header header;
|
||||
Display::UI::HostBlock::HostBlock hostblock;
|
||||
model::HostRegistry ®istry;
|
||||
display::ui::text::Renderer textRenderer;
|
||||
display::ui::header::Header header;
|
||||
display::ui::hostblock::HostBlock hostblock;
|
||||
};
|
||||
} // namespace Display::Graphics
|
||||
} // namespace display::graphics
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#include "Display/UI/Bar/Bar.h"
|
||||
#include <algorithm>
|
||||
|
||||
namespace Display::UI::Bar
|
||||
namespace display::ui::bar
|
||||
{
|
||||
Bar::Bar(int width, int height, Orientation orientation, Style style)
|
||||
: width(width), height(height), orientation(orientation), style(style)
|
||||
{
|
||||
}
|
||||
|
||||
void Bar::draw(Display::Graphics::Framebuffer &framebuffer, int x, int y, float value)
|
||||
void Bar::draw(display::graphics::Framebuffer &framebuffer, int x, int y, float value)
|
||||
{
|
||||
value = std::clamp(value, 0.0f, 1.0f);
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Display::UI::Bar
|
||||
framebuffer.fillRect(x, y, width, height, style.background);
|
||||
|
||||
// 2. Рисуем заполнение
|
||||
Display::Graphics::Color fillColor = valueToColor(value);
|
||||
display::graphics::Color fillColor = valueToColor(value);
|
||||
|
||||
if (orientation == Orientation::Horizontal)
|
||||
{
|
||||
@@ -37,13 +37,13 @@ namespace Display::UI::Bar
|
||||
}
|
||||
}
|
||||
|
||||
Display::Graphics::Color Bar::valueToColor(float value)
|
||||
display::graphics::Color Bar::valueToColor(float value)
|
||||
{
|
||||
value = std::clamp(value, 0.0f, 1.0f);
|
||||
if (value <= 0.5f)
|
||||
{
|
||||
float t = value / 0.5f;
|
||||
return Display::Graphics::Color{
|
||||
return display::graphics::Color{
|
||||
static_cast<uint8_t>(t * 255), // R: 0 → 255
|
||||
180, // G: остаётся 180
|
||||
0 // B
|
||||
@@ -52,10 +52,10 @@ namespace Display::UI::Bar
|
||||
else
|
||||
{
|
||||
float t = (value - 0.5f) / 0.5f;
|
||||
return Display::Graphics::Color{
|
||||
return display::graphics::Color{
|
||||
255, // R: остаётся 255
|
||||
static_cast<uint8_t>(180 * (1.0f - t)), // G: 180 → 0
|
||||
0};
|
||||
}
|
||||
}
|
||||
} // namespace Display::UI::Bar
|
||||
} // namespace display::ui::bar
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
#include "Display/UI/Bar/Orientation.h"
|
||||
#include "Display/UI/Bar/Style.h"
|
||||
|
||||
namespace Display::UI::Bar
|
||||
namespace display::ui::bar
|
||||
{
|
||||
class Bar
|
||||
{
|
||||
public:
|
||||
Bar(int width, int height, Orientation orientation = Orientation::Horizontal, Style style = {});
|
||||
|
||||
void draw(Display::Graphics::Framebuffer &framebuffer,
|
||||
void draw(display::graphics::Framebuffer &framebuffer,
|
||||
int x, int y,
|
||||
float value); // 0.0 .. 1.0
|
||||
|
||||
@@ -21,6 +21,6 @@ namespace Display::UI::Bar
|
||||
Orientation orientation;
|
||||
Style style;
|
||||
|
||||
Display::Graphics::Color valueToColor(float value);
|
||||
display::graphics::Color valueToColor(float value);
|
||||
};
|
||||
} // namespace Display::UI::Bar
|
||||
} // namespace display::ui::bar
|
||||
|
||||
@@ -4,7 +4,7 @@ add_library(DisplayUIBar
|
||||
Bar.cpp
|
||||
)
|
||||
|
||||
add_library(Display::UI::Bar ALIAS DisplayUIBar)
|
||||
add_library(display::ui::Bar ALIAS DisplayUIBar)
|
||||
|
||||
target_link_libraries(DisplayUIBar
|
||||
PUBLIC
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace Display::UI::Bar
|
||||
namespace display::ui::bar
|
||||
{
|
||||
enum class Orientation
|
||||
{
|
||||
Horizontal,
|
||||
Vertical
|
||||
};
|
||||
} // namespace Display::UI::Bar
|
||||
} // namespace display::ui::bar
|
||||
@@ -3,19 +3,19 @@
|
||||
#include "Display/Graphics/Color.h"
|
||||
#include "Display/UI/Theme/Theme.h"
|
||||
|
||||
namespace Display::UI::Bar
|
||||
namespace display::ui::bar
|
||||
{
|
||||
struct Style
|
||||
{
|
||||
Display::Graphics::Color background;
|
||||
Display::Graphics::Color fill;
|
||||
Display::Graphics::Color border;
|
||||
display::graphics::Color background;
|
||||
display::graphics::Color fill;
|
||||
display::graphics::Color border;
|
||||
bool drawBorder;
|
||||
|
||||
Style(
|
||||
Display::Graphics::Color background = Display::UI::Theme::Bar::BACKGROUND,
|
||||
Display::Graphics::Color fill = Display::UI::Theme::Bar::FILL,
|
||||
Display::Graphics::Color border = Display::UI::Theme::Bar::BORDER,
|
||||
display::graphics::Color background = display::ui::theme::bar::BACKGROUND,
|
||||
display::graphics::Color fill = display::ui::theme::bar::FILL,
|
||||
display::graphics::Color border = display::ui::theme::bar::BORDER,
|
||||
bool drawBorder = true)
|
||||
: background(background),
|
||||
fill(fill),
|
||||
@@ -24,4 +24,4 @@ namespace Display::UI::Bar
|
||||
{
|
||||
}
|
||||
};
|
||||
} // namespace Display::UI::Bar
|
||||
} // namespace display::ui::bar
|
||||
|
||||
@@ -4,12 +4,12 @@ add_library(DisplayUIHeader
|
||||
Header.cpp
|
||||
)
|
||||
|
||||
add_library(Display::UI::Header ALIAS DisplayUIHeader)
|
||||
add_library(display::ui::Header ALIAS DisplayUIHeader)
|
||||
|
||||
target_link_libraries(DisplayUIHeader
|
||||
PUBLIC
|
||||
Display::Graphics
|
||||
Display::UI::Text
|
||||
display::ui::Text
|
||||
)
|
||||
|
||||
target_include_directories(DisplayUIHeader
|
||||
|
||||
@@ -11,20 +11,20 @@
|
||||
#include "Display/UI/Theme/Theme.h"
|
||||
#include "Display/Graphics/Color.h"
|
||||
|
||||
namespace Display::UI::Header
|
||||
namespace display::ui::header
|
||||
{
|
||||
Header::Header()
|
||||
{
|
||||
}
|
||||
|
||||
void Header::draw(
|
||||
Graphics::Framebuffer &fb,
|
||||
UI::Text::Renderer &text,
|
||||
display::graphics::Framebuffer &fb,
|
||||
display::ui::text::Renderer &text,
|
||||
int x, int y)
|
||||
{
|
||||
// ===== Block background =====
|
||||
fb.fillRect(x, y, HEADER_WIDTH, HEADER_HEIGHT, Display::UI::Theme::Header::BACKGROUND);
|
||||
fb.drawRect(x, y, HEADER_WIDTH, HEADER_HEIGHT, Display::UI::Theme::Header::BORDER);
|
||||
fb.fillRect(x, y, HEADER_WIDTH, HEADER_HEIGHT, display::ui::theme::header::BACKGROUND);
|
||||
fb.drawRect(x, y, HEADER_WIDTH, HEADER_HEIGHT, display::ui::theme::header::BORDER);
|
||||
|
||||
int cursorY = y + PADDING;
|
||||
|
||||
@@ -33,9 +33,9 @@ namespace Display::UI::Header
|
||||
x + PADDING + 120 - 2,
|
||||
cursorY + 8,
|
||||
getCurrentDateTime(),
|
||||
Display::UI::Theme::Text::TEXT,
|
||||
Display::UI::Theme::Text::OUTLINE,
|
||||
Display::UI::Text::Font{std::string(HEADER_FONT_NAME.begin(), HEADER_FONT_NAME.end()), HEADER_FONT_SIZE});
|
||||
display::ui::theme::text::TEXT,
|
||||
display::ui::theme::text::OUTLINE,
|
||||
display::ui::text::Font{std::string(HEADER_FONT_NAME.begin(), HEADER_FONT_NAME.end()), HEADER_FONT_SIZE});
|
||||
}
|
||||
|
||||
std::string Header::getCurrentDateTime()
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "Display/Graphics/Color.h"
|
||||
#include "Display/UI/Text/Renderer.h"
|
||||
|
||||
namespace Display::UI::Header
|
||||
namespace display::ui::header
|
||||
{
|
||||
// ===== Layout =====
|
||||
constexpr int HEADER_WIDTH = 240; // 116
|
||||
@@ -25,8 +25,8 @@ namespace Display::UI::Header
|
||||
static constexpr int height() { return HEADER_HEIGHT; }
|
||||
|
||||
void draw(
|
||||
Graphics::Framebuffer &fb,
|
||||
UI::Text::Renderer &text,
|
||||
display::graphics::Framebuffer &fb,
|
||||
display::ui::text::Renderer &text,
|
||||
int x, int y);
|
||||
|
||||
private:
|
||||
|
||||
@@ -4,13 +4,13 @@ add_library(DisplayUIHostBlock
|
||||
HostBlock.cpp
|
||||
)
|
||||
|
||||
add_library(Display::UI::HostBlock ALIAS DisplayUIHostBlock)
|
||||
add_library(display::ui::HostBlock ALIAS DisplayUIHostBlock)
|
||||
|
||||
target_link_libraries(DisplayUIHostBlock
|
||||
PUBLIC
|
||||
Display::Graphics
|
||||
Display::UI::Text
|
||||
Display::UI::Bar
|
||||
display::ui::Text
|
||||
display::ui::Bar
|
||||
)
|
||||
|
||||
target_include_directories(DisplayUIHostBlock
|
||||
|
||||
@@ -8,33 +8,33 @@
|
||||
#include "Display/Graphics/Color.h"
|
||||
#include "Display/UI/Text/Helpers.h"
|
||||
|
||||
namespace Display::UI::HostBlock
|
||||
namespace display::ui::hostblock
|
||||
{
|
||||
HostBlock::HostBlock()
|
||||
: cpuBar(
|
||||
CPU_BAR_WIDTH,
|
||||
CPU_BAR_HEIGHT,
|
||||
Display::UI::Bar::Orientation::Vertical,
|
||||
Display::UI::Bar::Style{Display::Graphics::Color{40, 40, 40}, Display::Graphics::Color{0, 180, 0}, Display::Graphics::Color{80, 80, 80}, true}),
|
||||
display::ui::bar::Orientation::Vertical,
|
||||
display::ui::bar::Style{display::graphics::Color{40, 40, 40}, display::graphics::Color{0, 180, 0}, display::graphics::Color{80, 80, 80}, true}),
|
||||
memBar(
|
||||
BLOCK_WIDTH - PADDING * 2,
|
||||
MEM_BAR_HEIGHT,
|
||||
Display::UI::Bar::Orientation::Horizontal,
|
||||
Display::UI::Bar::Style{Display::Graphics::Color{40, 40, 40}, Display::Graphics::Color{0, 120, 200}, Display::Graphics::Color{80, 80, 80}, true})
|
||||
display::ui::bar::Orientation::Horizontal,
|
||||
display::ui::bar::Style{display::graphics::Color{40, 40, 40}, display::graphics::Color{0, 120, 200}, display::graphics::Color{80, 80, 80}, true})
|
||||
|
||||
{
|
||||
}
|
||||
|
||||
void HostBlock::draw(
|
||||
Graphics::Framebuffer &fb,
|
||||
UI::Text::Renderer &text,
|
||||
display::graphics::Framebuffer &fb,
|
||||
display::ui::text::Renderer &text,
|
||||
int x, int y,
|
||||
const std::string &hostname,
|
||||
const Metrics::Host &metrics)
|
||||
const metrics::Host &metrics)
|
||||
{
|
||||
// ===== Block background =====
|
||||
fb.fillRect(x, y, BLOCK_WIDTH, BLOCK_HEIGHT, Display::UI::Theme::HostBlock::BACKGROUND);
|
||||
fb.drawRect(x, y, BLOCK_WIDTH, BLOCK_HEIGHT, Display::UI::Theme::HostBlock::BORDER);
|
||||
fb.fillRect(x, y, BLOCK_WIDTH, BLOCK_HEIGHT, display::ui::theme::hostblock::BACKGROUND);
|
||||
fb.drawRect(x, y, BLOCK_WIDTH, BLOCK_HEIGHT, display::ui::theme::hostblock::BORDER);
|
||||
|
||||
int cursorY = y + PADDING;
|
||||
|
||||
@@ -44,18 +44,18 @@ namespace Display::UI::HostBlock
|
||||
cursorY,
|
||||
BLOCK_WIDTH - PADDING * 2,
|
||||
HEADER_HEIGHT,
|
||||
Display::UI::Theme::HostBlock::HEADER);
|
||||
display::ui::theme::hostblock::HEADER);
|
||||
|
||||
text.drawTextOutlined(
|
||||
fb,
|
||||
x + PADDING + 2,
|
||||
cursorY + HEADER_HEIGHT - 3,
|
||||
hostname,
|
||||
Display::UI::Theme::Text::TEXT,
|
||||
Display::UI::Theme::Text::OUTLINE,
|
||||
// Display::UI::Text::Font{"LiberationSans-Regular", HEADER_FONT_SIZE});
|
||||
// Display::UI::Text::Font{"PixelFive-Regular", 5});
|
||||
Display::UI::Text::Font{std::string(HEADER_FONT_NAME.begin(), HEADER_FONT_NAME.end()), HEADER_FONT_SIZE});
|
||||
display::ui::theme::text::TEXT,
|
||||
display::ui::theme::text::OUTLINE,
|
||||
// display::ui::text::Font{"LiberationSans-Regular", HEADER_FONT_SIZE});
|
||||
// display::ui::text::Font{"PixelFive-Regular", 5});
|
||||
display::ui::text::Font{std::string(HEADER_FONT_NAME.begin(), HEADER_FONT_NAME.end()), HEADER_FONT_SIZE});
|
||||
|
||||
cursorY += HEADER_HEIGHT + SECTION_GAP;
|
||||
|
||||
@@ -96,10 +96,10 @@ namespace Display::UI::HostBlock
|
||||
text.drawTextOutlined(fb,
|
||||
x + MEM_BAR_TEXT_PADDING_X,
|
||||
cursorY + 8,
|
||||
"M: " + Display::UI::Text::formatFloat(metrics.memory.mem_used / 1073741824) + "/" + Display::UI::Text::formatFloat(metrics.memory.mem_total / 1073741824),
|
||||
Display::UI::Theme::Text::TEXT,
|
||||
Display::UI::Theme::Text::OUTLINE,
|
||||
Display::UI::Text::Font{"PixelFive-Regular", 5});
|
||||
"M: " + display::ui::text::formatFloat(metrics.memory.mem_used / 1073741824) + "/" + display::ui::text::formatFloat(metrics.memory.mem_total / 1073741824),
|
||||
display::ui::theme::text::TEXT,
|
||||
display::ui::theme::text::OUTLINE,
|
||||
display::ui::text::Font{"PixelFive-Regular", 5});
|
||||
|
||||
cursorY += MEM_BAR_HEIGHT + SECTION_GAP;
|
||||
|
||||
@@ -115,10 +115,10 @@ namespace Display::UI::HostBlock
|
||||
text.drawTextOutlined(fb,
|
||||
x + MEM_BAR_TEXT_PADDING_X,
|
||||
cursorY + 8,
|
||||
"S: " + Display::UI::Text::formatFloat(metrics.memory.swap_used / 1073741824) + "/" + Display::UI::Text::formatFloat(metrics.memory.swap_total / 1073741824),
|
||||
Display::UI::Theme::Text::TEXT,
|
||||
Display::UI::Theme::Text::OUTLINE,
|
||||
Display::UI::Text::Font{"PixelFive-Regular", 5});
|
||||
"S: " + display::ui::text::formatFloat(metrics.memory.swap_used / 1073741824) + "/" + display::ui::text::formatFloat(metrics.memory.swap_total / 1073741824),
|
||||
display::ui::theme::text::TEXT,
|
||||
display::ui::theme::text::OUTLINE,
|
||||
display::ui::text::Font{"PixelFive-Regular", 5});
|
||||
|
||||
cursorY += MEM_BAR_HEIGHT + SECTION_GAP;
|
||||
}
|
||||
@@ -137,10 +137,10 @@ namespace Display::UI::HostBlock
|
||||
text.drawTextOutlined(fb,
|
||||
x + MEM_BAR_TEXT_PADDING_X,
|
||||
by + 8,
|
||||
metrics.disks[i].name + ": " + Display::UI::Text::formatFloat(metrics.disks[i].used / 1073741824) + "/" + Display::UI::Text::formatFloat(metrics.disks[i].total / 1073741824),
|
||||
Display::UI::Theme::Text::TEXT,
|
||||
Display::UI::Theme::Text::OUTLINE,
|
||||
Display::UI::Text::Font{"PixelFive-Regular", 5});
|
||||
metrics.disks[i].name + ": " + display::ui::text::formatFloat(metrics.disks[i].used / 1073741824) + "/" + display::ui::text::formatFloat(metrics.disks[i].total / 1073741824),
|
||||
display::ui::theme::text::TEXT,
|
||||
display::ui::theme::text::OUTLINE,
|
||||
display::ui::text::Font{"PixelFive-Regular", 5});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "Display/UI/Bar/Bar.h"
|
||||
#include "Metrics/Host.h"
|
||||
|
||||
namespace Display::UI::HostBlock
|
||||
namespace display::ui::hostblock
|
||||
{
|
||||
// ===== Layout =====
|
||||
constexpr int BLOCK_WIDTH = 118; // 118
|
||||
@@ -41,14 +41,14 @@ namespace Display::UI::HostBlock
|
||||
static constexpr int height() { return BLOCK_HEIGHT; }
|
||||
|
||||
void draw(
|
||||
Graphics::Framebuffer &fb,
|
||||
UI::Text::Renderer &text,
|
||||
display::graphics::Framebuffer &fb,
|
||||
display::ui::text::Renderer &text,
|
||||
int x, int y,
|
||||
const std::string &hostname,
|
||||
const Metrics::Host &metrics);
|
||||
const metrics::Host &metrics);
|
||||
|
||||
private:
|
||||
UI::Bar::Bar cpuBar;
|
||||
UI::Bar::Bar memBar;
|
||||
display::ui::bar::Bar cpuBar;
|
||||
display::ui::bar::Bar memBar;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ add_library(DisplayUIText
|
||||
)
|
||||
|
||||
|
||||
add_library(Display::UI::Text ALIAS DisplayUIText)
|
||||
add_library(display::ui::Text ALIAS DisplayUIText)
|
||||
|
||||
find_package(Freetype REQUIRED)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
struct Font
|
||||
{
|
||||
@@ -21,4 +21,4 @@ namespace Display::UI::Text
|
||||
return std::hash<std::string>()(k.name) ^ std::hash<int>()(k.size);
|
||||
}
|
||||
};
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <filesystem>
|
||||
#include "Helpers/Paths.h"
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
struct Fonts::Library
|
||||
{
|
||||
@@ -38,7 +38,7 @@ namespace Display::UI::Text
|
||||
void Fonts::loadAllFonts()
|
||||
{
|
||||
loadFonts("/usr/share/fonts/truetype");
|
||||
loadFonts(Helpers::initPaths().exeDir);
|
||||
loadFonts(helpers::initPaths().exeDir);
|
||||
}
|
||||
|
||||
void Fonts::loadFonts(const std::string &path)
|
||||
@@ -116,4 +116,4 @@ namespace Display::UI::Text
|
||||
}
|
||||
return glyphs.emplace(key, std::move(glyph)).first->second;
|
||||
}
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "Display/UI/Text/Glyph.h"
|
||||
#include "Display/UI/Text/GlyphKey.h"
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
class Fonts
|
||||
{
|
||||
@@ -25,4 +25,4 @@ namespace Display::UI::Text
|
||||
void loadAllFonts();
|
||||
void loadFonts(const std::string &path);
|
||||
};
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
struct Glyph
|
||||
{
|
||||
@@ -12,4 +12,4 @@ namespace Display::UI::Text
|
||||
int advance;
|
||||
std::vector<uint8_t> buffer; // grayscale bitmap
|
||||
};
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <string>
|
||||
#include "Font.h"
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
struct GlyphKey
|
||||
{
|
||||
@@ -25,4 +25,4 @@ namespace Display::UI::Text
|
||||
return h1 ^ (h2 << 1);
|
||||
}
|
||||
};
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
std::string formatFloat(float value, int decimals)
|
||||
{
|
||||
@@ -17,4 +17,4 @@ namespace Display::UI::Text
|
||||
std::snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
|
||||
return std::string(buffer);
|
||||
}
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
std::string formatFloat(float value, int decimals = 1);
|
||||
std::string formatDouble(double value, int decimals = 1);
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
void Renderer::drawText(Display::Graphics::Framebuffer &fb, int x, int y,
|
||||
const std::string &text, const Display::Graphics::Color &color, const Font &font)
|
||||
void Renderer::drawText(display::graphics::Framebuffer &fb, int x, int y,
|
||||
const std::string &text, const display::graphics::Color &color, const Font &font)
|
||||
{
|
||||
int penX = x;
|
||||
int baseline = y;
|
||||
@@ -27,8 +27,8 @@ namespace Display::UI::Text
|
||||
if (!alpha)
|
||||
continue;
|
||||
|
||||
Display::Graphics::Color bg = fb.getPixel(px, py);
|
||||
Display::Graphics::Color out;
|
||||
display::graphics::Color bg = fb.getPixel(px, py);
|
||||
display::graphics::Color out;
|
||||
out.r = (alpha * color.r + (255 - alpha) * bg.r) / 255;
|
||||
out.g = (alpha * color.g + (255 - alpha) * bg.g) / 255;
|
||||
out.b = (alpha * color.b + (255 - alpha) * bg.b) / 255;
|
||||
@@ -41,11 +41,11 @@ namespace Display::UI::Text
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::drawTextOutlined(Display::Graphics::Framebuffer &fb,
|
||||
void Renderer::drawTextOutlined(display::graphics::Framebuffer &fb,
|
||||
int x, int y,
|
||||
const std::string &text,
|
||||
const Display::Graphics::Color &colorText,
|
||||
const Display::Graphics::Color &colorOutline,
|
||||
const display::graphics::Color &colorText,
|
||||
const display::graphics::Color &colorOutline,
|
||||
const Font &font)
|
||||
{
|
||||
// outline
|
||||
@@ -83,4 +83,4 @@ namespace Display::UI::Text
|
||||
}
|
||||
*/
|
||||
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
@@ -8,14 +8,14 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
|
||||
class Renderer
|
||||
{
|
||||
public:
|
||||
void drawText(Display::Graphics::Framebuffer &fb, int x, int y, const std::string &text, const Display::Graphics::Color &color = Display::Graphics::Color{255, 255, 255}, const Font &font = {"LiberationSans-Regular", 12});
|
||||
void drawTextOutlined(Display::Graphics::Framebuffer &fb, int x, int y, const std::string &text, const Display::Graphics::Color &colorText = Display::Graphics::Color{255, 255, 255}, const Display::Graphics::Color &colorOutline = Display::Graphics::Color{0, 0, 0}, const Font &font = {"LiberationSans-Regular", 12});
|
||||
void drawText(display::graphics::Framebuffer &fb, int x, int y, const std::string &text, const display::graphics::Color &color = display::graphics::Color{255, 255, 255}, const Font &font = {"LiberationSans-Regular", 12});
|
||||
void drawTextOutlined(display::graphics::Framebuffer &fb, int x, int y, const std::string &text, const display::graphics::Color &colorText = display::graphics::Color{255, 255, 255}, const display::graphics::Color &colorOutline = display::graphics::Color{0, 0, 0}, const Font &font = {"LiberationSans-Regular", 12});
|
||||
|
||||
// int measureWidth(const std::string &text);
|
||||
// int measureHeight(const std::string &text);
|
||||
@@ -24,4 +24,4 @@ namespace Display::UI::Text
|
||||
Fonts fonts;
|
||||
};
|
||||
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <string_view>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Display::UI::Text
|
||||
namespace display::ui::text
|
||||
{
|
||||
|
||||
inline bool utf8Next(std::string_view &s, uint32_t &codepoint)
|
||||
@@ -36,4 +36,4 @@ namespace Display::UI::Text
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Display::UI::Text
|
||||
} // namespace display::ui::text
|
||||
@@ -2,7 +2,7 @@ message(STATUS "···Configuring Theme")
|
||||
|
||||
add_library(Theme INTERFACE)
|
||||
|
||||
add_library(Display::UI::Theme ALIAS Theme)
|
||||
add_library(display::ui::Theme ALIAS Theme)
|
||||
|
||||
target_link_libraries(Theme
|
||||
INTERFACE
|
||||
|
||||
@@ -2,34 +2,34 @@
|
||||
|
||||
#include "Display/Graphics/Color.h"
|
||||
|
||||
namespace Display::UI::Theme
|
||||
namespace display::ui::theme
|
||||
{
|
||||
namespace Text
|
||||
namespace text
|
||||
{
|
||||
const Display::Graphics::Color TEXT{220, 220, 220};
|
||||
const Display::Graphics::Color OUTLINE{20, 20, 20};
|
||||
} // namespace Text
|
||||
const display::graphics::Color TEXT{220, 220, 220};
|
||||
const display::graphics::Color OUTLINE{20, 20, 20};
|
||||
} // namespace text
|
||||
|
||||
namespace Header
|
||||
namespace header
|
||||
{
|
||||
// ===== Header =====
|
||||
const Display::Graphics::Color BACKGROUND{20, 20, 20};
|
||||
const Display::Graphics::Color BORDER{60, 60, 60};
|
||||
const Display::Graphics::Color HEADER{30, 30, 30};
|
||||
} // namespace Header
|
||||
const display::graphics::Color BACKGROUND{20, 20, 20};
|
||||
const display::graphics::Color BORDER{60, 60, 60};
|
||||
const display::graphics::Color HEADER{30, 30, 30};
|
||||
} // namespace header
|
||||
|
||||
namespace HostBlock
|
||||
namespace hostblock
|
||||
{
|
||||
// ===== HostBlock =====
|
||||
const Display::Graphics::Color BACKGROUND{20, 20, 20};
|
||||
const Display::Graphics::Color BORDER{60, 60, 60};
|
||||
const Display::Graphics::Color HEADER{30, 30, 30};
|
||||
} // namespace HostBlock
|
||||
const display::graphics::Color BACKGROUND{20, 20, 20};
|
||||
const display::graphics::Color BORDER{60, 60, 60};
|
||||
const display::graphics::Color HEADER{30, 30, 30};
|
||||
} // namespace hostblock
|
||||
|
||||
namespace Bar
|
||||
namespace bar
|
||||
{
|
||||
const Display::Graphics::Color BACKGROUND{30, 30, 30};
|
||||
const Display::Graphics::Color FILL{0, 180, 0};
|
||||
const Display::Graphics::Color BORDER{80, 80, 80};
|
||||
} // namespace Bar
|
||||
} // namespace Display::UI::Theme
|
||||
const display::graphics::Color BACKGROUND{30, 30, 30};
|
||||
const display::graphics::Color FILL{0, 180, 0};
|
||||
const display::graphics::Color BORDER{80, 80, 80};
|
||||
} // namespace bar
|
||||
} // namespace display::ui::theme
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
|
||||
namespace Helpers
|
||||
namespace helpers
|
||||
{
|
||||
std::filesystem::path getExecutablePath()
|
||||
{
|
||||
@@ -34,4 +34,4 @@ namespace Helpers
|
||||
p.assetsDir = p.exeDir / "assets";
|
||||
return p;
|
||||
}
|
||||
} // namespace Helpers
|
||||
} // namespace helpers
|
||||
|
||||
@@ -10,10 +10,10 @@
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Config::Client::Config config = Config::Client::Config::load("client.ini");
|
||||
Network::Client client(config.network.serverHost, config.network.serverPort);
|
||||
Metrics::Collector collector;
|
||||
Network::Agent agent(std::move(client), std::move(collector), config.network.intervalMs);
|
||||
config::client::Config config = config::client::Config::load("client.ini");
|
||||
network::Client client(config.network.serverHost, config.network.serverPort);
|
||||
metrics::Collector collector(config.collector.disks);
|
||||
network::Agent agent(std::move(client), std::move(collector), config.network.intervalMs);
|
||||
|
||||
agent.start();
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@ target_link_libraries(${TARGET_NAME_SERVER}
|
||||
PRIVATE
|
||||
Config::Server
|
||||
Display::Graphics
|
||||
Display::UI::Bar
|
||||
Display::UI::Text
|
||||
display::ui::Bar
|
||||
display::ui::Text
|
||||
Helpers::All
|
||||
Metrics::All
|
||||
Model::All
|
||||
|
||||
@@ -12,11 +12,11 @@
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Config::Server::Config config = Config::Server::Config::load("server.ini");
|
||||
Model::HostRegistry registry;
|
||||
Display::Graphics::Framebuffer fb("/dev/fb1", Display::Graphics::FramebufferRotation::R270);
|
||||
Display::Graphics::Renderer renderer(fb, registry);
|
||||
Network::Server server(config.network.listenPort, registry);
|
||||
config::server::Config config = config::server::Config::load("server.ini");
|
||||
model::HostRegistry registry;
|
||||
display::graphics::Framebuffer fb("/dev/fb1", display::graphics::FramebufferRotation::R270);
|
||||
display::graphics::Renderer renderer(fb, registry);
|
||||
network::Server server(config.network.listenPort, registry);
|
||||
|
||||
server.start();
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
|
||||
namespace Metrics
|
||||
namespace metrics
|
||||
{
|
||||
|
||||
Collector::Collector()
|
||||
Collector::Collector(const std::vector<std::string> &disks) : disks(disks)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -33,7 +33,8 @@ namespace Metrics
|
||||
prevCpu = curCpu;
|
||||
|
||||
host.memory = readMemory();
|
||||
host.disks.push_back(readDisk());
|
||||
for (const std::string &disk : disks)
|
||||
host.disks.push_back(readDisk(disk.c_str()));
|
||||
readLoad(host.load1, host.load5, host.load15);
|
||||
host.uptime = readUptime();
|
||||
host.hostname = readHostname();
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#pragma once
|
||||
#include "Metrics/Host.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace Metrics
|
||||
namespace metrics
|
||||
{
|
||||
class Collector
|
||||
{
|
||||
public:
|
||||
Collector(const std::vector<std::string> &disks = {"/"});
|
||||
explicit Collector();
|
||||
|
||||
Host collect();
|
||||
@@ -22,6 +24,7 @@ namespace Metrics
|
||||
std::pair<CpuTimes, std::vector<CpuTimes>> prevCpu;
|
||||
|
||||
std::pair<CpuTimes, std::vector<CpuTimes>> readCpuTimes();
|
||||
const std::vector<std::string> disks;
|
||||
float cpuLoad(const CpuTimes &prev, const CpuTimes &cur);
|
||||
Memory readMemory();
|
||||
Disk readDisk(const char *path = "/");
|
||||
@@ -29,4 +32,4 @@ namespace Metrics
|
||||
uint64_t readUptime();
|
||||
std::string readHostname();
|
||||
};
|
||||
} // namespace Metrics
|
||||
} // namespace metrics
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
namespace Metrics
|
||||
namespace metrics
|
||||
{
|
||||
struct Cpu
|
||||
{
|
||||
float totalLoad;
|
||||
std::vector<float> coreLoads; // длина = num_cores
|
||||
};
|
||||
} // namespace Metrics
|
||||
} // namespace metrics
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace Metrics
|
||||
namespace metrics
|
||||
{
|
||||
struct Disk
|
||||
{
|
||||
@@ -10,4 +10,4 @@ namespace Metrics
|
||||
float free;
|
||||
float total;
|
||||
};
|
||||
} // namespace Metrics
|
||||
} // namespace metrics
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "Network/Common/Buffer.h"
|
||||
|
||||
namespace Metrics
|
||||
namespace metrics
|
||||
{
|
||||
|
||||
// ---- Вспомогательные функции для float <-> uint32_t ----
|
||||
@@ -29,7 +29,7 @@ namespace Metrics
|
||||
// ---- Сериализация ----
|
||||
std::vector<uint8_t> Host::serialize() const
|
||||
{
|
||||
Network::Buffer buf;
|
||||
network::Buffer buf;
|
||||
|
||||
buf.writeUint8(1); // version
|
||||
buf.writeString(hostname);
|
||||
@@ -62,7 +62,7 @@ namespace Metrics
|
||||
Host Host::deserialize(const std::vector<uint8_t> &data)
|
||||
{
|
||||
Host h;
|
||||
Network::Buffer buf(const_cast<std::vector<uint8_t> &>(data));
|
||||
network::Buffer buf(const_cast<std::vector<uint8_t> &>(data));
|
||||
|
||||
uint8_t version = buf.readUint8();
|
||||
if (version != 1)
|
||||
@@ -98,4 +98,4 @@ namespace Metrics
|
||||
|
||||
return h;
|
||||
}
|
||||
} // namespace Metrics
|
||||
} // namespace metrics
|
||||
+2
-2
@@ -7,7 +7,7 @@
|
||||
#include "Metrics/Memory.h"
|
||||
#include "Metrics/Disk.h"
|
||||
|
||||
namespace Metrics
|
||||
namespace metrics
|
||||
{
|
||||
struct Host
|
||||
{
|
||||
@@ -24,4 +24,4 @@ namespace Metrics
|
||||
// Десериализация из вектора байт
|
||||
static Host deserialize(const std::vector<uint8_t> &data);
|
||||
};
|
||||
} // namespace Metrics
|
||||
} // namespace metrics
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace Metrics
|
||||
namespace metrics
|
||||
{
|
||||
struct Memory
|
||||
{
|
||||
@@ -11,4 +11,4 @@ namespace Metrics
|
||||
float swap_total;
|
||||
float swap_available;
|
||||
};
|
||||
} // namespace Metrics
|
||||
} // namespace metrics
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
#include "Model/HostRegistry.h"
|
||||
|
||||
namespace Model
|
||||
namespace model
|
||||
{
|
||||
void HostRegistry::update(const std::string &host, const Metrics::Host &m)
|
||||
void HostRegistry::update(const std::string &host, const metrics::Host &m)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
hosts[host] = m;
|
||||
}
|
||||
|
||||
std::map<std::string, Metrics::Host> HostRegistry::snapshot()
|
||||
std::map<std::string, metrics::Host> HostRegistry::snapshot()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
return hosts;
|
||||
}
|
||||
} // namespace Model
|
||||
} // namespace model
|
||||
|
||||
@@ -4,16 +4,16 @@
|
||||
#include <mutex>
|
||||
#include "Metrics/Host.h"
|
||||
|
||||
namespace Model
|
||||
namespace model
|
||||
{
|
||||
class HostRegistry
|
||||
{
|
||||
public:
|
||||
void update(const std::string &host, const Metrics::Host &m);
|
||||
std::map<std::string, Metrics::Host> snapshot();
|
||||
void update(const std::string &host, const metrics::Host &m);
|
||||
std::map<std::string, metrics::Host> snapshot();
|
||||
|
||||
private:
|
||||
std::map<std::string, Metrics::Host> hosts;
|
||||
std::map<std::string, metrics::Host> hosts;
|
||||
std::mutex mutex;
|
||||
};
|
||||
} // namespace Model
|
||||
} // namespace model
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#include "Network/Client/Agent.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace Network
|
||||
namespace network
|
||||
{
|
||||
|
||||
Agent::Agent(
|
||||
Client client,
|
||||
Metrics::Collector collector,
|
||||
metrics::Collector collector,
|
||||
std::chrono::milliseconds interval)
|
||||
: client(std::move(client)), collector(std::move(collector)), interval(interval)
|
||||
{
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
|
||||
namespace Network
|
||||
namespace network
|
||||
{
|
||||
|
||||
class Agent
|
||||
@@ -15,7 +15,7 @@ namespace Network
|
||||
public:
|
||||
Agent(
|
||||
Client client,
|
||||
Metrics::Collector collector,
|
||||
metrics::Collector collector,
|
||||
std::chrono::milliseconds interval);
|
||||
|
||||
~Agent();
|
||||
@@ -27,7 +27,7 @@ namespace Network
|
||||
void run();
|
||||
|
||||
Client client;
|
||||
Metrics::Collector collector;
|
||||
metrics::Collector collector;
|
||||
std::chrono::milliseconds interval;
|
||||
|
||||
std::thread worker;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
namespace Network
|
||||
namespace network
|
||||
{
|
||||
|
||||
Client::Client(const std::string &host, uint16_t port)
|
||||
@@ -53,7 +53,7 @@ namespace Network
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool Client::sendMetrics(const Metrics::Host &metrics)
|
||||
bool Client::sendMetrics(const metrics::Host &metrics)
|
||||
{
|
||||
auto buffer = metrics.serialize();
|
||||
ssize_t sent = sendto(sockfd, buffer.data(), buffer.size(), 0,
|
||||
@@ -66,4 +66,4 @@ namespace Network
|
||||
return sent == static_cast<ssize_t>(buffer.size());
|
||||
}
|
||||
|
||||
} // namespace Network
|
||||
} // namespace network
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <netinet/in.h>
|
||||
#include "Metrics/Host.h"
|
||||
|
||||
namespace Network
|
||||
namespace network
|
||||
{
|
||||
|
||||
class Client
|
||||
@@ -19,11 +19,11 @@ namespace Network
|
||||
Client &operator=(Client &&other) noexcept;
|
||||
|
||||
// Отправить данные
|
||||
bool sendMetrics(const Metrics::Host &metrics);
|
||||
bool sendMetrics(const metrics::Host &metrics);
|
||||
|
||||
private:
|
||||
int sockfd;
|
||||
struct sockaddr_in serverAddr;
|
||||
};
|
||||
|
||||
} // namespace Network
|
||||
} // namespace network
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "Network/Common/Buffer.h"
|
||||
|
||||
namespace Network
|
||||
namespace network
|
||||
{
|
||||
void Buffer::writeUint8(uint8_t v)
|
||||
{
|
||||
@@ -83,4 +83,4 @@ namespace Network
|
||||
if (pos + n > buffer.size())
|
||||
throw std::runtime_error("Buffer underrun");
|
||||
}
|
||||
} // namespace Network
|
||||
} // namespace network
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <stdexcept>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
namespace Network
|
||||
namespace network
|
||||
{
|
||||
class Buffer
|
||||
{
|
||||
@@ -34,4 +34,4 @@ namespace Network
|
||||
std::vector<uint8_t> buffer;
|
||||
size_t pos;
|
||||
};
|
||||
} // namespace Network
|
||||
} // namespace network
|
||||
|
||||
@@ -5,23 +5,23 @@
|
||||
#include <iostream>
|
||||
#include "Metrics/Host.h"
|
||||
|
||||
namespace Network
|
||||
namespace network
|
||||
{
|
||||
|
||||
Server::Server(uint16_t port, Model::HostRegistry ®istry)
|
||||
: port_(port), registry_(registry), running_(false)
|
||||
Server::Server(uint16_t port, model::HostRegistry ®istry)
|
||||
: port(port), registry(registry), running(false)
|
||||
{
|
||||
|
||||
sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd_ < 0)
|
||||
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
throw std::runtime_error("Failed to create UDP socket");
|
||||
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
addr.sin_port = htons(port_);
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
if (bind(sockfd_, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) < 0)
|
||||
if (bind(sockfd, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) < 0)
|
||||
{
|
||||
throw std::runtime_error("Failed to bind UDP socket");
|
||||
}
|
||||
@@ -30,20 +30,20 @@ namespace Network
|
||||
Server::~Server()
|
||||
{
|
||||
stop();
|
||||
close(sockfd_);
|
||||
close(sockfd);
|
||||
}
|
||||
|
||||
void Server::start()
|
||||
{
|
||||
running_ = true;
|
||||
serverThread_ = std::thread(&Server::run, this);
|
||||
running = true;
|
||||
serverThread = std::thread(&Server::run, this);
|
||||
}
|
||||
|
||||
void Server::stop()
|
||||
{
|
||||
running_ = false;
|
||||
if (serverThread_.joinable())
|
||||
serverThread_.join();
|
||||
running = false;
|
||||
if (serverThread.joinable())
|
||||
serverThread.join();
|
||||
}
|
||||
|
||||
void Server::run()
|
||||
@@ -51,17 +51,17 @@ namespace Network
|
||||
constexpr size_t BUF_SIZE = 4096;
|
||||
uint8_t buffer[BUF_SIZE];
|
||||
|
||||
while (running_)
|
||||
while (running)
|
||||
{
|
||||
fd_set readfds;
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(sockfd_, &readfds);
|
||||
FD_SET(sockfd, &readfds);
|
||||
|
||||
timeval tv{};
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 500 * 1000; // 500 ms
|
||||
|
||||
int ret = select(sockfd_ + 1, &readfds, nullptr, nullptr, &tv);
|
||||
int ret = select(sockfd + 1, &readfds, nullptr, nullptr, &tv);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
@@ -77,12 +77,12 @@ namespace Network
|
||||
continue;
|
||||
}
|
||||
|
||||
if (FD_ISSET(sockfd_, &readfds))
|
||||
if (FD_ISSET(sockfd, &readfds))
|
||||
{
|
||||
sockaddr_in clientAddr{};
|
||||
socklen_t addrLen = sizeof(clientAddr);
|
||||
ssize_t len = recvfrom(
|
||||
sockfd_,
|
||||
sockfd,
|
||||
buffer,
|
||||
BUF_SIZE,
|
||||
0,
|
||||
@@ -95,8 +95,8 @@ namespace Network
|
||||
try
|
||||
{
|
||||
std::vector<uint8_t> data(buffer, buffer + len);
|
||||
auto metrics = Metrics::Host::deserialize(data);
|
||||
registry_.update(metrics.hostname, metrics);
|
||||
auto metrics = metrics::Host::deserialize(data);
|
||||
registry.update(metrics.hostname, metrics);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
@@ -106,4 +106,4 @@ namespace Network
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Network
|
||||
} // namespace network
|
||||
@@ -4,13 +4,13 @@
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Network
|
||||
namespace network
|
||||
{
|
||||
|
||||
class Server
|
||||
{
|
||||
public:
|
||||
Server(uint16_t port, Model::HostRegistry ®istry);
|
||||
Server(uint16_t port, model::HostRegistry ®istry);
|
||||
~Server();
|
||||
|
||||
void start();
|
||||
@@ -19,11 +19,11 @@ namespace Network
|
||||
private:
|
||||
void run();
|
||||
|
||||
int sockfd_;
|
||||
uint16_t port_;
|
||||
std::thread serverThread_;
|
||||
std::atomic<bool> running_;
|
||||
Model::HostRegistry ®istry_;
|
||||
int sockfd;
|
||||
uint16_t port;
|
||||
std::thread serverThread;
|
||||
std::atomic<bool> running;
|
||||
model::HostRegistry ®istry;
|
||||
};
|
||||
|
||||
} // namespace Network
|
||||
} // namespace network
|
||||
Reference in New Issue
Block a user