"Fossies" - the Fresh Open Source Software Archive 
Member "tcpflow-1.6.1/src/netviz/port_histogram.cpp" (19 Feb 2021, 2331 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "port_histogram.cpp" see the
Fossies "Dox" file reference documentation.
1 /**
2 * port_histogram.cpp:
3 * Show packets received vs port
4 *
5 * This source file is public domain, as it is not based on the original tcpflow.
6 *
7 * Author: Michael Shick <mike@shick.in>
8 *
9 */
10
11 #include "config.h"
12
13 #ifdef HAVE_LIBCAIRO
14 #include "tcpflow.h"
15
16 #include "port_histogram.h"
17
18 #include <math.h>
19 #include <algorithm>
20
21 using namespace std;
22
23 const size_t port_histogram::bucket_count = 10;
24
25 bool port_histogram::descending_counts::operator()(const port_count &a,
26 const port_count &b)
27 {
28 if(a.count > b.count) {
29 return true;
30 }
31 if(a.count < b.count) {
32 return false;
33 }
34 return a.port < b.port;
35 }
36
37 void port_histogram::increment(uint16_t port, uint64_t delta)
38 {
39 port_counts[port] += delta;
40 data_bytes_ingested += delta;
41 buckets_dirty = true;
42 }
43
44 const port_histogram::port_count &port_histogram::at(size_t index)
45 {
46 refresh_buckets();
47
48 return buckets.at(index);
49 }
50
51 size_t port_histogram::size()
52 {
53 refresh_buckets();
54
55 return buckets.size();
56 }
57
58 uint64_t port_histogram::ingest_count() const
59 {
60 return data_bytes_ingested;
61 }
62
63 port_histogram::port_count_vector::const_iterator port_histogram::begin()
64 {
65 refresh_buckets();
66
67 return buckets.begin();
68 }
69 port_histogram::port_count_vector::const_iterator port_histogram::end()
70 {
71 refresh_buckets();
72
73 return buckets.end();
74 }
75 port_histogram::port_count_vector::const_reverse_iterator port_histogram::rbegin()
76 {
77 refresh_buckets();
78
79 return buckets.rbegin();
80 }
81 port_histogram::port_count_vector::const_reverse_iterator port_histogram::rend()
82 {
83 refresh_buckets();
84
85 return buckets.rend();
86 }
87
88 void port_histogram::refresh_buckets()
89 {
90 if(!buckets_dirty) {
91 return;
92 }
93
94 buckets.clear();
95
96 for(port_counts_t::const_iterator it = port_counts.begin();
97 it != port_counts.end(); it++) {
98 buckets.push_back(port_count(it->first, it->second));
99 }
100
101 if(buckets.size() <= bucket_count) {
102 sort(buckets.begin(), buckets.end(), descending_counts());
103 }
104 else {
105 partial_sort(buckets.begin(), buckets.begin() + bucket_count,
106 buckets.end(), descending_counts());
107 }
108
109 if(buckets.size() > bucket_count) {
110 buckets.erase(buckets.begin() + bucket_count, buckets.end());
111 }
112
113 buckets_dirty = false;
114 }
115 #endif