Changeset View
Changeset View
Standalone View
Standalone View
plugins/process/network/helper/Accumulator.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * This file is part of KSysGuard. | ||||
3 | * Copyright 2019 Arjen Hiemstra <ahiemstra@heimr.nl> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU General Public License as | ||||
7 | * published by the Free Software Foundation; either version 2 of | ||||
8 | * the License or (at your option) version 3 or any later version | ||||
9 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
10 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
11 | * defined in Section 14 of version 3 of the license. | ||||
12 | * | ||||
13 | * This program is distributed in the hope that it will be useful, | ||||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
16 | * GNU General Public License for more details. | ||||
17 | * | ||||
18 | * You should have received a copy of the GNU General Public License | ||||
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
20 | */ | ||||
21 | | ||||
22 | #include "Accumulator.h" | ||||
23 | | ||||
24 | #include "Capture.h" | ||||
25 | #include "ConnectionMapping.h" | ||||
26 | #include "Packet.h" | ||||
27 | | ||||
28 | using namespace std::chrono_literals; | ||||
29 | | ||||
30 | Accumulator::Accumulator(std::shared_ptr<Capture> capture, std::shared_ptr<ConnectionMapping> mapping) | ||||
31 | { | ||||
32 | m_capture = capture; | ||||
33 | m_mapping = mapping; | ||||
34 | | ||||
35 | m_running = true; | ||||
36 | m_thread = std::thread { &Accumulator::loop, this }; | ||||
37 | } | ||||
38 | | ||||
39 | Accumulator::PidDataCounterHash Accumulator::data() | ||||
40 | { | ||||
41 | auto tmp = m_data; | ||||
42 | | ||||
43 | auto toErase = std::vector<int>{}; | ||||
44 | for (auto &entry : m_data) { | ||||
45 | if (entry.second.first == 0 && entry.second.second == 0) { | ||||
46 | toErase.push_back(entry.first); | ||||
47 | } else { | ||||
48 | entry.second.first = 0; | ||||
49 | entry.second.second = 0; | ||||
50 | } | ||||
51 | } | ||||
52 | | ||||
53 | std::for_each(toErase.cbegin(), toErase.cend(), [this](int pid) { m_data.erase(pid); }); | ||||
54 | | ||||
55 | return tmp; | ||||
56 | } | ||||
57 | | ||||
58 | void Accumulator::stop() | ||||
59 | { | ||||
60 | m_running = false; | ||||
61 | if (m_thread.joinable()) { | ||||
62 | m_thread.join(); | ||||
63 | } | ||||
64 | } | ||||
65 | | ||||
66 | void Accumulator::loop() | ||||
67 | { | ||||
68 | while (m_running) { | ||||
69 | auto packet = m_capture->nextPacket(); | ||||
70 | | ||||
71 | auto result = m_mapping->pidForPacket(packet); | ||||
72 | if (result.pid == 0) | ||||
73 | continue; | ||||
74 | | ||||
75 | addData(result.direction, packet, result.pid); | ||||
76 | } | ||||
77 | } | ||||
78 | | ||||
79 | void Accumulator::addData(Packet::Direction direction, const Packet &packet, int pid) | ||||
80 | { | ||||
81 | auto itr = m_data.find(pid); | ||||
82 | if (itr == m_data.end()) { | ||||
83 | m_data.emplace(pid, InboundOutboundData{0, 0}); | ||||
84 | } | ||||
85 | | ||||
86 | if (direction == Packet::Direction::Inbound) { | ||||
87 | m_data[pid].first += packet.size(); | ||||
88 | } else { | ||||
89 | m_data[pid].second += packet.size(); | ||||
90 | }; | ||||
91 | } |