Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
146 lines (129 loc) · 3.11 KB
/
Copy pathmain.cpp
File metadata and controls
146 lines (129 loc) · 3.11 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "storage.hpp"
#include "comms.hpp"
#include <nlohmann/json.hpp>
#include <string>
using json = nlohmann::json;
struct key
{
public:
int id;
friend bool operator==(const key& lhs, const key& rhs)
{
if(lhs.id == rhs.id)
return true;
return false;
}
static key from_json(json j)
{
try
{
key k;
k.id = j["id"];
return k;
}
catch(const std::exception& e)
{
std::cout << __LINE__ << " : " << e.what() << "\n";
throw;
}
}
static json to_json(key k)
{
return json
{
{"id", k.id},
};
}
};
struct value
{
public:
std::string data;
friend bool operator==(const value& lhs, const value& rhs)
{
if(lhs.data == rhs.data)
return true;
return false;
}
static value from_json(json j)
{
try
{
value v;
v.data = j["data"];
return v;
}
catch(const std::exception& e)
{
std::cout << __LINE__ << " : " << e.what() << "\n";
throw;
}
}
static json to_json(value v)
{
return json
{
{"data", v.data},
};
}
};
namespace std
{
template<>
struct hash<key>
{
std::size_t operator()(const key& k) const
{
std::hash<string> hasher;
return hasher(std::to_string(k.id));
}
};
template<>
struct hash<value>
{
std::size_t operator()(const value& v) const
{
return (std::hash<string>()(v.data));
}
};
}
int main()
{
asio::io_context io_context(2);
Channel cancellation_channel(io_context, 2);
UChannel<IdMessage> msg_channel(io_context, 10);
UChannel<IdMessage> response_channel(io_context, 10);
Storage<value, key> storage(io_context, 5, msg_channel, response_channel);
// This comms should have some sort of authentication
// and be used to receive commands.
// Make another for getting and inserting data.
Comms command_receiver(io_context, cancellation_channel, 8001, msg_channel, response_channel);
// test: get and insert.
co_spawn(io_context, [](Storage<value, key>& storage) -> asio::awaitable<void>
{
std::vector<std::tuple<key, value>> data;
key k;
k.id = 5;
value v;
v.data = "the cache is working!";
data.push_back(std::make_tuple(k, v));
co_await storage.insert(data);
std::optional<value> result = co_await storage.get(k);
if(result.has_value())
std::cout << value::to_json(*result).dump() << "\n";
else
std::cout << "doesn't exist\n";
k.id = 6;
result = co_await storage.get(k);
if(result.has_value())
{
std::cout << value::to_json(*result).dump() << "\n";
}
else
{
std::cout << "entry does not exist in cache...\n";
}
co_return;
}(storage), asio::detached);
io_context.run();
}