Yarilo  v0.9
WPA2 Decrypter & Packet Analyzer
log_queue.h
Go to the documentation of this file.
1 #ifndef SNIFF_LOG_QUEUE
2 #define SNIFF_LOG_QUEUE
3 
4 #include "proto/service.pb.h"
5 #include <condition_variable>
6 #include <mutex>
7 #include <vector>
8 
9 namespace yarilo {
10 
17 class LogQueue {
18 public:
23  LogQueue(uint64_t queueMaxSize) : queueMaxSize(queueMaxSize) {
24  queue.reserve(queueMaxSize);
25  }
26 
33  bool insert(proto::LogEntry *item) {
34  {
35  std::unique_lock<std::mutex> lock(queueMutex);
36  if (!cvInsert.wait_for(lock, std::chrono::seconds(2),
37  [&queue = queue, &queueMaxSize = queueMaxSize] {
38  return queue.size() < queueMaxSize;
39  }))
40  return false;
41  queue.emplace_back(item);
42  }
43 
44  cvFetch.notify_all();
45  return true;
46  }
47 
54  bool fetch_all(std::vector<proto::LogEntry *> &refFetchedItems) {
55  {
56  std::unique_lock<std::mutex> lock(queueMutex);
57  if (!cvFetch.wait_for(lock, std::chrono::seconds(2),
58  [&queue = queue] { return !queue.empty(); }))
59  return false;
60  refFetchedItems = std::move(queue);
61  queue.clear();
62  }
63 
64  cvInsert.notify_all();
65  return true;
66  }
67 
68 private:
69  std::vector<proto::LogEntry *> queue;
70  uint64_t queueMaxSize;
71  std::mutex queueMutex;
72  std::condition_variable cvFetch;
73  std::condition_variable cvInsert;
74 };
75 
76 } // namespace yarilo
77 
78 #endif // SNIFF_LOG_QUEUE
A queue for storing log entries with thread-safe operations.
Definition: log_queue.h:17
bool fetch_all(std::vector< proto::LogEntry * > &refFetchedItems)
Fetches all log entries from the queue. This method blocks until there are items available to fetch.
Definition: log_queue.h:54
bool insert(proto::LogEntry *item)
Inserts a log entry into the queue. This method blocks until space is available in the queue.
Definition: log_queue.h:33
LogQueue(uint64_t queueMaxSize)
Definition: log_queue.h:23
Definition: access_point.cpp:22