/src/ntopng/include/FifoQueue.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * (C) 2014-26 - ntop.org |
4 | | * |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 | | * |
20 | | */ |
21 | | |
22 | | #ifndef _FIFO_QUEUE_H |
23 | | #define _FIFO_QUEUE_H |
24 | | |
25 | | #include "ntop_includes.h" |
26 | | |
27 | | template <typename T> |
28 | | class FifoQueue { |
29 | | protected: |
30 | | Mutex m; |
31 | | std::queue<T> q; |
32 | | u_int32_t max_size; |
33 | | u_int64_t num_enqueued, num_not_enqueued, num_dequeued; |
34 | | |
35 | | public: |
36 | 40 | FifoQueue(u_int32_t queue_size) { |
37 | 40 | max_size = queue_size; |
38 | 40 | num_enqueued = num_not_enqueued = num_dequeued = 0; |
39 | 40 | } FifoQueue<char*>::FifoQueue(unsigned int) Line | Count | Source | 36 | 32 | FifoQueue(u_int32_t queue_size) { | 37 | 32 | max_size = queue_size; | 38 | 32 | num_enqueued = num_not_enqueued = num_dequeued = 0; | 39 | 32 | } |
FifoQueue<ndpi_serializer*>::FifoQueue(unsigned int) Line | Count | Source | 36 | 8 | FifoQueue(u_int32_t queue_size) { | 37 | 8 | max_size = queue_size; | 38 | 8 | num_enqueued = num_not_enqueued = num_dequeued = 0; | 39 | 8 | } |
Unexecuted instantiation: FifoQueue<CHTSPoint*>::FifoQueue(unsigned int) Unexecuted instantiation: FifoQueue<AlertFifoItem*>::FifoQueue(unsigned int) |
40 | 40 | virtual ~FifoQueue() { ; }FifoQueue<char*>::~FifoQueue() Line | Count | Source | 40 | 32 | virtual ~FifoQueue() { ; } |
FifoQueue<ndpi_serializer*>::~FifoQueue() Line | Count | Source | 40 | 8 | virtual ~FifoQueue() { ; } |
Unexecuted instantiation: FifoQueue<CHTSPoint*>::~FifoQueue() Unexecuted instantiation: FifoQueue<AlertFifoItem*>::~FifoQueue() |
41 | | |
42 | | /* |
43 | | Subclasses might override this as sometimes the buffer |
44 | | needs to be duplicated as for strings |
45 | | */ |
46 | 0 | bool enqueue(T item) { |
47 | 0 | bool rv; |
48 | |
|
49 | 0 | m.lock(__FILE__, __LINE__); |
50 | |
|
51 | 0 | if (canEnqueue()) { |
52 | 0 | q.push(item); |
53 | 0 | rv = true; |
54 | 0 | } else |
55 | 0 | rv = false; |
56 | |
|
57 | 0 | if (rv) |
58 | 0 | num_enqueued++; |
59 | 0 | else |
60 | 0 | num_not_enqueued++; |
61 | |
|
62 | 0 | m.unlock(__FILE__, __LINE__); |
63 | |
|
64 | 0 | return (rv); |
65 | 0 | } Unexecuted instantiation: FifoQueue<CHTSPoint*>::enqueue(CHTSPoint*) Unexecuted instantiation: FifoQueue<AlertFifoItem*>::enqueue(AlertFifoItem*) |
66 | | |
67 | 0 | T dequeue() { |
68 | 0 | T rv; |
69 | |
|
70 | 0 | m.lock(__FILE__, __LINE__); |
71 | |
|
72 | 0 | if (q.empty()) |
73 | 0 | rv = static_cast<T>(NULL); |
74 | 0 | else { |
75 | 0 | rv = q.front(); |
76 | 0 | q.pop(); |
77 | 0 | num_dequeued++; |
78 | 0 | } |
79 | 0 | m.unlock(__FILE__, __LINE__); |
80 | |
|
81 | 0 | return (rv); |
82 | 0 | } Unexecuted instantiation: FifoQueue<char*>::dequeue() Unexecuted instantiation: FifoQueue<ndpi_serializer*>::dequeue() |
83 | | |
84 | 1.36k | inline bool canEnqueue() { return (getLength() < max_size); }FifoQueue<char*>::canEnqueue() Line | Count | Source | 84 | 1.36k | inline bool canEnqueue() { return (getLength() < max_size); } |
Unexecuted instantiation: FifoQueue<ndpi_serializer*>::canEnqueue() Unexecuted instantiation: FifoQueue<CHTSPoint*>::canEnqueue() Unexecuted instantiation: FifoQueue<AlertFifoItem*>::canEnqueue() |
85 | 1.36k | inline u_int32_t getLength() { return (q.size()); }FifoQueue<char*>::getLength() Line | Count | Source | 85 | 1.36k | inline u_int32_t getLength() { return (q.size()); } |
Unexecuted instantiation: FifoQueue<ndpi_serializer*>::getLength() Unexecuted instantiation: FifoQueue<CHTSPoint*>::getLength() Unexecuted instantiation: FifoQueue<AlertFifoItem*>::getLength() |
86 | 0 | inline bool empty() { return (q.empty()); }Unexecuted instantiation: FifoQueue<char*>::empty() Unexecuted instantiation: FifoQueue<AlertFifoItem*>::empty() |
87 | 0 | inline float fillPct() { |
88 | 0 | u_int64_t pending = num_enqueued - num_dequeued; |
89 | 0 | if (pending == 0) return 0; |
90 | 0 | return (float)pending / max_size * 100; |
91 | 0 | }; |
92 | 0 | inline void lua(lua_State* vm, const char* table_name) { |
93 | 0 | lua_newtable(vm); |
94 | | /* The percentage of not enqueued, with reference to the total number of not |
95 | | * enqueued plus enqueued */ |
96 | 0 | lua_push_uint64_table_entry( |
97 | 0 | vm, "pct_not_enqueued", |
98 | 0 | num_not_enqueued / (float)(num_not_enqueued + num_enqueued + 1) * 100); |
99 | |
|
100 | 0 | lua_pushstring(vm, table_name); |
101 | 0 | lua_insert(vm, -2); |
102 | 0 | lua_settable(vm, -3); |
103 | 0 | } |
104 | | }; |
105 | | |
106 | | #endif /* _FIFO_QUEUE_H */ |