Small case namespaces

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