NetworkStatisticsWidget.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "NetworkStatisticsWidget.h"
  27. #include <LibGUI/GBoxLayout.h>
  28. #include <LibGUI/GGroupBox.h>
  29. #include <LibGUI/GJsonArrayModel.h>
  30. #include <LibGUI/GTableView.h>
  31. NetworkStatisticsWidget::NetworkStatisticsWidget(GUI::Widget* parent)
  32. : GUI::LazyWidget(parent)
  33. {
  34. on_first_show = [this](auto&) {
  35. set_layout(make<GUI::VBoxLayout>());
  36. layout()->set_margins({ 4, 4, 4, 4 });
  37. set_fill_with_background_color(true);
  38. auto adapters_group_box = GUI::GroupBox::construct("Adapters", this);
  39. adapters_group_box->set_layout(make<GUI::VBoxLayout>());
  40. adapters_group_box->layout()->set_margins({ 6, 16, 6, 6 });
  41. adapters_group_box->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  42. adapters_group_box->set_preferred_size(0, 120);
  43. m_adapter_table_view = GUI::TableView::construct(adapters_group_box);
  44. m_adapter_table_view->set_size_columns_to_fit_content(true);
  45. Vector<GUI::JsonArrayModel::FieldSpec> net_adapters_fields;
  46. net_adapters_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft);
  47. net_adapters_fields.empend("class_name", "Class", Gfx::TextAlignment::CenterLeft);
  48. net_adapters_fields.empend("mac_address", "MAC", Gfx::TextAlignment::CenterLeft);
  49. net_adapters_fields.empend("ipv4_address", "IPv4", Gfx::TextAlignment::CenterLeft);
  50. net_adapters_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight);
  51. net_adapters_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight);
  52. net_adapters_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight);
  53. net_adapters_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight);
  54. m_adapter_table_view->set_model(GUI::JsonArrayModel::create("/proc/net/adapters", move(net_adapters_fields)));
  55. auto sockets_group_box = GUI::GroupBox::construct("Sockets", this);
  56. sockets_group_box->set_layout(make<GUI::VBoxLayout>());
  57. sockets_group_box->layout()->set_margins({ 6, 16, 6, 6 });
  58. sockets_group_box->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  59. sockets_group_box->set_preferred_size(0, 0);
  60. m_socket_table_view = GUI::TableView::construct(sockets_group_box);
  61. m_socket_table_view->set_size_columns_to_fit_content(true);
  62. Vector<GUI::JsonArrayModel::FieldSpec> net_tcp_fields;
  63. net_tcp_fields.empend("peer_address", "Peer", Gfx::TextAlignment::CenterLeft);
  64. net_tcp_fields.empend("peer_port", "Port", Gfx::TextAlignment::CenterRight);
  65. net_tcp_fields.empend("local_address", "Local", Gfx::TextAlignment::CenterLeft);
  66. net_tcp_fields.empend("local_port", "Port", Gfx::TextAlignment::CenterRight);
  67. net_tcp_fields.empend("state", "State", Gfx::TextAlignment::CenterLeft);
  68. net_tcp_fields.empend("ack_number", "Ack#", Gfx::TextAlignment::CenterRight);
  69. net_tcp_fields.empend("sequence_number", "Seq#", Gfx::TextAlignment::CenterRight);
  70. net_tcp_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight);
  71. net_tcp_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight);
  72. net_tcp_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight);
  73. net_tcp_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight);
  74. m_socket_table_view->set_model(GUI::JsonArrayModel::create("/proc/net/tcp", move(net_tcp_fields)));
  75. m_update_timer = Core::Timer::construct(
  76. 1000, [this] {
  77. update_models();
  78. },
  79. this);
  80. update_models();
  81. };
  82. }
  83. NetworkStatisticsWidget::~NetworkStatisticsWidget()
  84. {
  85. }
  86. void NetworkStatisticsWidget::update_models()
  87. {
  88. m_adapter_table_view->model()->update();
  89. m_socket_table_view->model()->update();
  90. }