Serenity Operating System
at portability 100 lines 5.2 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "NetworkStatisticsWidget.h" 28#include <LibGUI/BoxLayout.h> 29#include <LibGUI/GroupBox.h> 30#include <LibGUI/JsonArrayModel.h> 31#include <LibGUI/TableView.h> 32 33NetworkStatisticsWidget::NetworkStatisticsWidget() 34{ 35 on_first_show = [this](auto&) { 36 set_layout(make<GUI::VerticalBoxLayout>()); 37 layout()->set_margins({ 4, 4, 4, 4 }); 38 set_fill_with_background_color(true); 39 40 auto adapters_group_box = add<GUI::GroupBox>("Adapters"); 41 adapters_group_box->set_layout(make<GUI::VerticalBoxLayout>()); 42 adapters_group_box->layout()->set_margins({ 6, 16, 6, 6 }); 43 adapters_group_box->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); 44 adapters_group_box->set_preferred_size(0, 120); 45 46 m_adapter_table_view = adapters_group_box->add<GUI::TableView>(); 47 m_adapter_table_view->set_size_columns_to_fit_content(true); 48 49 Vector<GUI::JsonArrayModel::FieldSpec> net_adapters_fields; 50 net_adapters_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft); 51 net_adapters_fields.empend("class_name", "Class", Gfx::TextAlignment::CenterLeft); 52 net_adapters_fields.empend("mac_address", "MAC", Gfx::TextAlignment::CenterLeft); 53 net_adapters_fields.empend("ipv4_address", "IPv4", Gfx::TextAlignment::CenterLeft); 54 net_adapters_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight); 55 net_adapters_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight); 56 net_adapters_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight); 57 net_adapters_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight); 58 m_adapter_table_view->set_model(GUI::JsonArrayModel::create("/proc/net/adapters", move(net_adapters_fields))); 59 60 auto sockets_group_box = add<GUI::GroupBox>("Sockets"); 61 sockets_group_box->set_layout(make<GUI::VerticalBoxLayout>()); 62 sockets_group_box->layout()->set_margins({ 6, 16, 6, 6 }); 63 sockets_group_box->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill); 64 sockets_group_box->set_preferred_size(0, 0); 65 66 m_socket_table_view = sockets_group_box->add<GUI::TableView>(); 67 m_socket_table_view->set_size_columns_to_fit_content(true); 68 69 Vector<GUI::JsonArrayModel::FieldSpec> net_tcp_fields; 70 net_tcp_fields.empend("peer_address", "Peer", Gfx::TextAlignment::CenterLeft); 71 net_tcp_fields.empend("peer_port", "Port", Gfx::TextAlignment::CenterRight); 72 net_tcp_fields.empend("local_address", "Local", Gfx::TextAlignment::CenterLeft); 73 net_tcp_fields.empend("local_port", "Port", Gfx::TextAlignment::CenterRight); 74 net_tcp_fields.empend("state", "State", Gfx::TextAlignment::CenterLeft); 75 net_tcp_fields.empend("ack_number", "Ack#", Gfx::TextAlignment::CenterRight); 76 net_tcp_fields.empend("sequence_number", "Seq#", Gfx::TextAlignment::CenterRight); 77 net_tcp_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight); 78 net_tcp_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight); 79 net_tcp_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight); 80 net_tcp_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight); 81 m_socket_table_view->set_model(GUI::JsonArrayModel::create("/proc/net/tcp", move(net_tcp_fields))); 82 83 m_update_timer = add<Core::Timer>( 84 1000, [this] { 85 update_models(); 86 }); 87 88 update_models(); 89 }; 90} 91 92NetworkStatisticsWidget::~NetworkStatisticsWidget() 93{ 94} 95 96void NetworkStatisticsWidget::update_models() 97{ 98 m_adapter_table_view->model()->update(); 99 m_socket_table_view->model()->update(); 100}