Fray Engine
Loading...
Searching...
No Matches
console_list.hpp
Go to the documentation of this file.
1/* console_list.hpp */
2
3#ifndef _FRAY_CONSOLE_LIST_HPP_
4#define _FRAY_CONSOLE_LIST_HPP_
5
6#include <cstddef>
7#include <cstdint>
8#include <cstdio>
9#include <deque>
10#include <mutex>
11#include <optional>
12#include <set>
13#include <string>
14#include <unordered_map>
15#include <utility>
16
17#include "fray.pb.h"
18
19namespace fray
20{
21 namespace console
22 {
30 {
31 public:
36 explicit EntryList(
37 std::size_t capacity = 5000)
38 : _capacity(capacity ? capacity : 1) {}
39
45 ConsoleEntry add(
46 ConsoleEntry entry)
47 {
48 std::lock_guard<std::mutex> lock(_mutex);
49 const auto found = _entries.find(entry.uid());
50 if (found != _entries.end())
51 return found->second;
52 entry.set_revision(1);
53 entry.set_received_count(0);
54 remember(entry);
55 return entry;
56 }
57
60 {
62 ConsoleEntry entry;
64 bool added;
65 };
66
76 std::optional<Confirmation> confirm(
77 ConsoleEntry fallback,
78 ProcessKind sender = PROCESS_KIND_UNSPECIFIED,
79 std::uint32_t senderId = 0)
80 {
81 std::lock_guard<std::mutex> lock(_mutex);
82 const auto found = _entries.find(fallback.uid());
83 if (found == _entries.end())
84 {
85 fallback.set_status(fallback.destination() == PROCESS_KIND_MULTICAST
86 ? CONSOLE_COMMAND_STATUS_PENDING : CONSOLE_COMMAND_STATUS_RECEIVED);
87 fallback.set_revision(1);
88 remember(fallback);
89 return Confirmation{fallback, true};
90 }
91 if (found->second.status() == CONSOLE_COMMAND_STATUS_RECEIVED)
92 return std::nullopt;
93 if (found->second.destination() == PROCESS_KIND_MULTICAST)
94 {
95 bool expected = false;
96 for (const auto& recipient : found->second.recipients())
97 if (recipient.kind() == sender && recipient.id() == senderId)
98 expected = true;
99 if (!expected)
100 {
101 std::fprintf(stderr, "Cannot confirm multicast message %s: unexpected recipient %d:%u.\n",
102 fallback.uid().c_str(), static_cast<int>(sender), senderId);
103 return std::nullopt;
104 }
105 if (!_receipts[fallback.uid()].emplace(sender, senderId).second)
106 return std::nullopt;
107 found->second.set_received_count(static_cast<std::uint32_t>(_receipts[fallback.uid()].size()));
108 if (found->second.received_count() ==
109 static_cast<std::uint32_t>(found->second.recipients_size()))
110 found->second.set_status(CONSOLE_COMMAND_STATUS_RECEIVED);
111 found->second.set_revision(found->second.revision() + 1);
112 return Confirmation{found->second, false};
113 }
114 found->second.set_status(CONSOLE_COMMAND_STATUS_RECEIVED);
115 found->second.set_revision(found->second.revision() + 1);
116 return Confirmation{found->second, false};
117 }
118
120 std::size_t size() const
121 {
122 std::lock_guard<std::mutex> lock(_mutex);
123 return _entries.size();
124 }
125
126 private:
132 const ConsoleEntry& entry)
133 {
134 _entries.emplace(entry.uid(), entry);
135 _order.push_back(entry.uid());
136 while (_order.size() > _capacity)
137 {
138 _entries.erase(_order.front());
139 _receipts.erase(_order.front());
140 _order.pop_front();
141 }
142 }
143
145 mutable std::mutex _mutex;
147 std::size_t _capacity;
149 std::unordered_map<std::string, ConsoleEntry> _entries;
151 std::unordered_map<std::string, std::set<std::pair<ProcessKind, std::uint32_t>>> _receipts;
153 std::deque<std::string> _order;
154 };
155 }
156}
157
158#endif
void remember(const ConsoleEntry &entry)
Stores an entry and forgets the oldest ones beyond capacity. The caller holds the lock.
std::deque< std::string > _order
UIDs oldest first, used to forget the oldest entry.
std::mutex _mutex
Guards the entries and their order.
ConsoleEntry add(ConsoleEntry entry)
Adds an entry, or returns the one already held under the same UID.
std::unordered_map< std::string, std::set< std::pair< ProcessKind, std::uint32_t > > > _receipts
std::size_t _capacity
Most entries kept.
std::optional< Confirmation > confirm(ConsoleEntry fallback, ProcessKind sender=PROCESS_KIND_UNSPECIFIED, std::uint32_t senderId=0)
Records that a command was received. A direct entry becomes received. A multicast entry becomes recei...
std::size_t size() const
Number of entries currently held.
std::unordered_map< std::string, ConsoleEntry > _entries
Entries by UID.
EntryList(std::size_t capacity=5000)
Creates an empty list.