2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-10 12:28:48 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-08-08 20:43:30 +02:00
|
|
|
#include "NetworkStatisticsWidget.h"
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/GroupBox.h>
|
|
|
|
#include <LibGUI/JsonArrayModel.h>
|
2022-10-29 21:52:20 +02:00
|
|
|
#include <LibGUI/Menu.h>
|
|
|
|
#include <LibGUI/Process.h>
|
2020-09-22 00:21:28 +02:00
|
|
|
#include <LibGUI/SortingProxyModel.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/TableView.h>
|
2021-07-26 13:00:05 +02:00
|
|
|
#include <LibGfx/Painter.h>
|
2019-08-08 20:43:30 +02:00
|
|
|
|
2022-03-18 23:10:33 +01:00
|
|
|
REGISTER_WIDGET(SystemMonitor, NetworkStatisticsWidget)
|
|
|
|
|
|
|
|
namespace SystemMonitor {
|
|
|
|
|
2020-02-23 12:07:13 +01:00
|
|
|
NetworkStatisticsWidget::NetworkStatisticsWidget()
|
2019-08-08 20:43:30 +02:00
|
|
|
{
|
2019-10-02 20:26:19 +02:00
|
|
|
on_first_show = [this](auto&) {
|
2020-03-04 09:43:54 +01:00
|
|
|
set_layout<GUI::VerticalBoxLayout>();
|
Userland+LibGUI: Add shorthand versions of the Margins constructor
This allows for typing [8] instead of [8, 8, 8, 8] to specify the same
margin on all edges, for example. The constructors follow CSS' style of
specifying margins. The added constructors are:
- Margins(int all): Sets the same margin on all edges.
- Margins(int vertical, int horizontal): Sets the first argument to top
and bottom margins, and the second argument to left and right margins.
- Margins(int top, int vertical, int bottom): Sets the first argument to
the top margin, the second argument to the left and right margins,
and the third argument to the bottom margin.
2021-08-17 00:11:38 +00:00
|
|
|
layout()->set_margins(4);
|
2019-10-02 20:26:19 +02:00
|
|
|
set_fill_with_background_color(true);
|
2019-08-08 20:43:30 +02:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
m_network_connected_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/network-connected.png"sv).release_value_but_fixme_should_propagate_errors();
|
|
|
|
m_network_disconnected_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/network-disconnected.png"sv).release_value_but_fixme_should_propagate_errors();
|
2021-07-26 13:00:05 +02:00
|
|
|
|
2021-11-06 19:30:59 +01:00
|
|
|
m_network_link_down_bitmap = Gfx::Bitmap::try_create(m_network_connected_bitmap->format(), m_network_connected_bitmap->size()).release_value_but_fixme_should_propagate_errors();
|
2021-07-26 13:00:05 +02:00
|
|
|
{
|
|
|
|
Gfx::Painter painter(*m_network_link_down_bitmap);
|
|
|
|
painter.blit_filtered({}, *m_network_connected_bitmap, m_network_connected_bitmap->rect(), [](Color color) {
|
|
|
|
return color.to_grayscale();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
auto& adapters_group_box = add<GUI::GroupBox>("Adapters"sv);
|
2020-03-04 19:07:55 +01:00
|
|
|
adapters_group_box.set_layout<GUI::VerticalBoxLayout>();
|
2021-11-13 11:47:46 +01:00
|
|
|
adapters_group_box.layout()->set_margins(6);
|
2020-12-29 18:57:29 +01:00
|
|
|
adapters_group_box.set_fixed_height(120);
|
2019-08-08 20:43:30 +02:00
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
m_adapter_table_view = adapters_group_box.add<GUI::TableView>();
|
2019-08-10 10:36:07 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Vector<GUI::JsonArrayModel::FieldSpec> net_adapters_fields;
|
2021-07-26 13:00:05 +02:00
|
|
|
net_adapters_fields.empend("", Gfx::TextAlignment::CenterLeft,
|
|
|
|
[this](JsonObject const& object) -> GUI::Variant {
|
2022-07-11 17:32:29 +00:00
|
|
|
if (!object.get("link_up"sv).as_bool())
|
2021-07-26 13:00:05 +02:00
|
|
|
return *m_network_link_down_bitmap;
|
|
|
|
else
|
2022-07-11 17:32:29 +00:00
|
|
|
return object.get("ipv4_address"sv).as_string_or(""sv).is_empty() ? *m_network_disconnected_bitmap : *m_network_connected_bitmap;
|
2021-07-26 13:00:05 +02:00
|
|
|
});
|
2020-02-06 11:56:38 +01:00
|
|
|
net_adapters_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft);
|
|
|
|
net_adapters_fields.empend("class_name", "Class", Gfx::TextAlignment::CenterLeft);
|
|
|
|
net_adapters_fields.empend("mac_address", "MAC", Gfx::TextAlignment::CenterLeft);
|
2021-07-28 14:23:39 +02:00
|
|
|
net_adapters_fields.empend("Link status", Gfx::TextAlignment::CenterLeft,
|
2022-12-04 18:02:33 +00:00
|
|
|
[](JsonObject const& object) -> DeprecatedString {
|
2022-07-11 17:32:29 +00:00
|
|
|
if (!object.get("link_up"sv).as_bool())
|
2021-07-28 14:23:39 +02:00
|
|
|
return "Down";
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::formatted("{} Mb/s {}-duplex", object.get("link_speed"sv).to_i32(),
|
2022-07-11 17:32:29 +00:00
|
|
|
object.get("link_full_duplex"sv).as_bool() ? "full"sv : "half"sv);
|
2021-07-28 14:23:39 +02:00
|
|
|
});
|
2021-07-30 19:58:17 +02:00
|
|
|
net_adapters_fields.empend("IPv4", Gfx::TextAlignment::CenterLeft,
|
2022-12-04 18:02:33 +00:00
|
|
|
[](JsonObject const& object) -> DeprecatedString {
|
2022-07-11 17:32:29 +00:00
|
|
|
return object.get("ipv4_address"sv).as_string_or(""sv);
|
2021-07-30 19:58:17 +02:00
|
|
|
});
|
2020-02-06 11:56:38 +01:00
|
|
|
net_adapters_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_adapters_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_adapters_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_adapters_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight);
|
2022-10-14 21:55:17 +03:00
|
|
|
m_adapter_model = GUI::JsonArrayModel::create("/sys/kernel/net/adapters", move(net_adapters_fields));
|
2021-12-24 12:12:04 +00:00
|
|
|
m_adapter_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_adapter_model)));
|
2022-10-29 21:52:20 +02:00
|
|
|
m_adapter_context_menu = MUST(GUI::Menu::try_create());
|
|
|
|
m_adapter_context_menu->add_action(GUI::Action::create(
|
|
|
|
"Open in Network Settings...", MUST(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/network.png"sv)), [this](GUI::Action&) {
|
|
|
|
m_adapter_table_view->selection().for_each_index([this](GUI::ModelIndex const& index) {
|
|
|
|
auto adapter_name = index.sibling_at_column(1).data().as_string();
|
|
|
|
GUI::Process::spawn_or_show_error(window(), "/bin/Escalator"sv, Array { "/bin/NetworkSettings", adapter_name.characters() });
|
|
|
|
});
|
|
|
|
},
|
|
|
|
this));
|
|
|
|
m_adapter_table_view->on_context_menu_request = [this](GUI::ModelIndex const& index, GUI::ContextMenuEvent const& event) {
|
|
|
|
if (!index.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto adapter_name = index.sibling_at_column(1).data().as_string();
|
|
|
|
if (adapter_name == "loop") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_adapter_context_menu->popup(event.screen_position());
|
|
|
|
};
|
2019-08-08 20:43:30 +02:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
auto& tcp_sockets_group_box = add<GUI::GroupBox>("TCP Sockets"sv);
|
2021-07-24 16:47:53 -04:00
|
|
|
tcp_sockets_group_box.set_layout<GUI::VerticalBoxLayout>();
|
2021-11-13 11:47:46 +01:00
|
|
|
tcp_sockets_group_box.layout()->set_margins(6);
|
2019-08-08 20:43:30 +02:00
|
|
|
|
2021-07-24 16:47:53 -04:00
|
|
|
m_tcp_socket_table_view = tcp_sockets_group_box.add<GUI::TableView>();
|
2019-08-10 10:36:07 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Vector<GUI::JsonArrayModel::FieldSpec> net_tcp_fields;
|
2020-02-06 11:56:38 +01:00
|
|
|
net_tcp_fields.empend("peer_address", "Peer", Gfx::TextAlignment::CenterLeft);
|
|
|
|
net_tcp_fields.empend("peer_port", "Port", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_tcp_fields.empend("local_address", "Local", Gfx::TextAlignment::CenterLeft);
|
|
|
|
net_tcp_fields.empend("local_port", "Port", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_tcp_fields.empend("state", "State", Gfx::TextAlignment::CenterLeft);
|
|
|
|
net_tcp_fields.empend("ack_number", "Ack#", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_tcp_fields.empend("sequence_number", "Seq#", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_tcp_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_tcp_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_tcp_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_tcp_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight);
|
2022-10-14 21:55:17 +03:00
|
|
|
m_tcp_socket_model = GUI::JsonArrayModel::create("/sys/kernel/net/tcp", move(net_tcp_fields));
|
2021-12-24 12:12:04 +00:00
|
|
|
m_tcp_socket_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_tcp_socket_model)));
|
2021-05-17 21:56:09 -04:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
auto& udp_sockets_group_box = add<GUI::GroupBox>("UDP Sockets"sv);
|
2021-07-24 16:47:53 -04:00
|
|
|
udp_sockets_group_box.set_layout<GUI::VerticalBoxLayout>();
|
2021-11-13 11:47:46 +01:00
|
|
|
udp_sockets_group_box.layout()->set_margins(6);
|
2021-07-24 16:47:53 -04:00
|
|
|
|
|
|
|
m_udp_socket_table_view = udp_sockets_group_box.add<GUI::TableView>();
|
2021-05-17 21:56:09 -04:00
|
|
|
|
|
|
|
Vector<GUI::JsonArrayModel::FieldSpec> net_udp_fields;
|
|
|
|
net_udp_fields.empend("peer_address", "Peer", Gfx::TextAlignment::CenterLeft);
|
|
|
|
net_udp_fields.empend("peer_port", "Port", Gfx::TextAlignment::CenterRight);
|
|
|
|
net_udp_fields.empend("local_address", "Local", Gfx::TextAlignment::CenterLeft);
|
|
|
|
net_udp_fields.empend("local_port", "Port", Gfx::TextAlignment::CenterRight);
|
2022-10-14 21:55:17 +03:00
|
|
|
m_udp_socket_model = GUI::JsonArrayModel::create("/sys/kernel/net/udp", move(net_udp_fields));
|
2021-12-24 12:12:04 +00:00
|
|
|
m_udp_socket_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_udp_socket_model)));
|
2019-08-08 20:43:30 +02:00
|
|
|
|
2020-02-23 10:57:42 +01:00
|
|
|
m_update_timer = add<Core::Timer>(
|
2019-10-02 20:26:19 +02:00
|
|
|
1000, [this] {
|
|
|
|
update_models();
|
2020-02-23 10:57:42 +01:00
|
|
|
});
|
2019-08-08 20:43:30 +02:00
|
|
|
|
2019-10-02 20:26:19 +02:00
|
|
|
update_models();
|
|
|
|
};
|
2019-08-08 20:43:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkStatisticsWidget::update_models()
|
|
|
|
{
|
2022-02-17 08:19:38 +01:00
|
|
|
m_adapter_model->update();
|
|
|
|
m_tcp_socket_model->update();
|
|
|
|
m_udp_socket_model->update();
|
2019-08-08 20:43:30 +02:00
|
|
|
}
|
2022-03-18 23:10:33 +01:00
|
|
|
|
|
|
|
}
|