-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcept.hpp
More file actions
89 lines (72 loc) · 3.26 KB
/
Copy pathconcept.hpp
File metadata and controls
89 lines (72 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once
#include <concepts>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <vector>
#include "thread_safe_queue.hpp"
namespace lite {
enum DeserializeResult { kGood, kBad, kIndeterminate };
template <typename Request, typename CacheKey, typename CacheEntry>
concept IsCacheEntry = requires(CacheEntry entry, CacheKey key) {
{ entry.ToRequest(key) } -> std::convertible_to<std::shared_ptr<Request>>;
};
template <typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6>
class Logger;
template <typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6>
class Cache;
template <typename Application, typename Request, typename Response,
typename ConnectionInfo, typename CacheKey, typename CacheEntry>
concept IsApplication = requires(
Application app, std::shared_ptr<Request> req,
std::shared_ptr<Response> resp, ConnectionInfo conn_info,
ThreadSafeQueue<std::pair<std::shared_ptr<Request>, bool>>
pending_requests, // true: request forward from client, false:
// request generated during replay
std::vector<std::shared_ptr<Request>> related_requests,
Cache<Application, Request, Response, ConnectionInfo, CacheKey, CacheEntry>
*cache,
Logger<Application, Request, Response, ConnectionInfo, CacheKey, CacheEntry>
*logger,
bool flow_control // true: reject this request if it will trigger
// replay packets
) {
// Find the corresponding requests of the response, return a subset of the
// requests that contain information about state changes
{
app.Match(resp, conn_info, pending_requests)
} -> std::convertible_to<std::pair<std::vector<std::shared_ptr<Request>>,
bool>>; // pair<related_requests,
// forward response>
// Update the states during normal time
{ app.NormalUpdate(resp, related_requests, conn_info, cache) };
// Handle response of requests sent by Replay (those Match() = (_, false))
// TODO: let the application to be able to retry the request
{ app.HandleReplayResponse(resp, related_requests, conn_info, cache) };
// Perform any operation during emergency time
{
app.EmergencyServe(std::move(req), conn_info, cache, logger, flow_control)
} -> std::convertible_to<std::pair<Response, bool>>; // true: close the
// connection after
// sending the response
// Hook function for switching from normal to emergency mode
{ app.NormalToEmergencyHook() };
// Hook function for switching from emergency to normal mode
{ app.EmergencyToNormalHook() };
// Hook function for establishing emergency connection
{
app.EmergencyConnectionEstablishHook(conn_info)
} -> std::convertible_to<std::optional<Response>>;
};
template <typename ProtocolMessage>
concept IsProtocolMessage =
requires(ProtocolMessage m, uint8_t *&begin, uint8_t *end) {
{
m.Serialize()
} -> std::convertible_to<std::shared_ptr<std::vector<uint8_t>>>;
{ m.Deserialize(begin, end) } -> std::convertible_to<DeserializeResult>;
};
} // namespace lite