libkazv
Loading...
Searching...
No Matches
sdk.hpp
Go to the documentation of this file.
1/*
2 * This file is part of libkazv.
3 * SPDX-FileCopyrightText: 2020-2021 Tusooa Zhu <tusooa@kazv.moe>
4 * SPDX-License-Identifier: AGPL-3.0-or-later
5 */
6
7#pragma once
8#include <libkazv-config.hpp>
9#include <lager/store.hpp>
10
11#include <random>
12
13#include <store.hpp>
14
15#include "sdk-model.hpp"
17#include "client.hpp"
20#include "random-generator.hpp"
21
22
23namespace Kazv
24{
28 template<class EventLoop, class Xform, class ...Enhancers>
29 class Sdk
30 {
33 using ActionT = typename ModelT::Action;
34 using CursorT = lager::reader<ModelT>;
35 using CursorTSP = std::shared_ptr<CursorT>;
36
37 using StoreT = decltype(
38 makeStore<ActionT>(
39 std::declval<ModelT>(),
41 std::declval<EventLoop>(),
42 lager::with_deps(
43 std::ref(detail::declref<JobInterface>()),
44 std::ref(detail::declref<EventInterface>()),
45 lager::dep::as<SdkModelCursorKey>(std::declval<std::function<CursorTSP()>>()),
46 std::ref(detail::declref<RandomInterface>()),
47 std::ref(detail::declref<VerificationTracker>())
48#ifdef KAZV_USE_THREAD_SAFETY_HELPER
49 , std::ref(detail::declref<EventLoopThreadIdKeeper>())
50#endif
51 ),
52 std::declval<Enhancers>()...)
53 );
54
56#ifdef KAZV_USE_THREAD_SAFETY_HELPER
57 , EventLoopThreadIdKeeper &
58#endif
59 >;
60
62 public:
63 Sdk(ModelT model,
64 JobInterface &jobHandler,
65 EventInterface &eventEmitter,
66 EventLoop &&eventLoop,
67 Xform &&xform,
68 Enhancers &&...enhancers)
69 : m_d(std::make_unique<Private>(
70 std::move(model), jobHandler, eventEmitter,
71 std::forward<EventLoop>(eventLoop), std::forward<Xform>(xform),
72 std::forward<Enhancers>(enhancers)...)) {}
73
80 ContextT context() const {
81 return m_d->store;
82 }
83
89 Client client() const {
90 return {Client::InEventLoopTag{}, ContextT(m_d->store)};
91 }
92
103 template<class EL>
104 auto createSecondaryRoot(EL &&eventLoop, ModelT initialModel = ModelT{}) const {
105 auto secondaryStore = lager::make_store<ModelT>(
106 std::move(initialModel),
107 std::forward<EL>(eventLoop),
108 lager::with_reducer([](auto &&, auto next) { return next; }));
109
110 lager::context<ModelT> secondaryCtx = secondaryStore;
111
113 .then([secondaryCtx, d=m_d.get()](auto &&) {
114 lager::watch(*(d->sdk),
115 [secondaryCtx](auto next) { secondaryCtx.dispatch(std::move(next)); });
116 });
117
118 return secondaryStore;
119 }
120
133 Client clientFromSecondaryRoot(lager::reader<ModelT> sr) const {
134 return Client(sr, ContextT(m_d->store));
135 }
136
137 private:
138 struct Private
139 {
140 Private(ModelT model,
141 JobInterface &jobHandler,
142 EventInterface &eventEmitter,
143 EventLoop &&eventLoop,
144 Xform &&xform,
145 Enhancers &&...enhancers)
147 , vt(VerificationUtils::DeviceIdentity())
148 , store(makeStore<ActionT>(
149 std::move(model),
150 &ModelT::update,
151 std::forward<EventLoop>(eventLoop),
152 lager::with_deps(
153 std::ref(jobHandler),
154 std::ref(eventEmitter),
155 lager::dep::as<SdkModelCursorKey>(
156 std::function<CursorTSP()>([this] { return sdk; })),
157 std::ref(rg.value()),
158 std::ref(vt)
159#ifdef KAZV_USE_THREAD_SAFETY_HELPER
160 , std::ref(keeper)
161#endif
162 ),
163 std::forward<Enhancers>(enhancers)...))
164 , sdk(std::make_shared<lager::reader<ModelT>>(store.reader().xform(std::forward<Xform>(xform))))
165 {
166#ifdef KAZV_USE_THREAD_SAFETY_HELPER
167 store.context().createResolvedPromise(EffectStatus{})
168 .then([this](auto &&) {
169 keeper.set(std::this_thread::get_id());
170 });
171#endif
172 }
173#ifdef KAZV_USE_THREAD_SAFETY_HELPER
174 EventLoopThreadIdKeeper keeper;
175#endif
176 std::optional<RandomInterface> rg;
177 VerificationTracker vt;
178 StoreT store;
179 CursorTSP sdk;
180 };
181
182 std::unique_ptr<Private> m_d;
183 };
184
203 template<class EventLoop, class Xform, class ...Enhancers>
204 inline auto makeSdk(SdkModel sdk,
205 JobInterface &jobHandler,
206 EventInterface &eventEmitter,
207 EventLoop &&eventLoop,
208 Xform &&xform,
209 Enhancers &&...enhancers)
210 -> Sdk<EventLoop, Xform, Enhancers...>
211 {
212 return { std::move(sdk),
213 jobHandler,
214 eventEmitter,
215 std::forward<EventLoop>(eventLoop),
216 std::forward<Xform>(xform),
217 std::forward<Enhancers>(enhancers)... };
218 }
219
224 {
225 return Crypto::constructRandomSize();
226 }
227
228 template<class EventLoop, class Xform, class ...Enhancers>
229 [[deprecated("Use deterministic makeDefaultSdkWithCryptoRandom instead. In the future, this will be removed.")]]
231 JobInterface &jobHandler,
232 EventInterface &eventEmitter,
233 EventLoop &&eventLoop,
234 Xform &&xform,
235 Enhancers &&...enhancers)
236 -> Sdk<EventLoop, Xform, Enhancers...>
237 {
238 auto m = SdkModel{};
240
241 return makeSdk(std::move(m),
242 jobHandler,
243 eventEmitter,
244 std::forward<EventLoop>(eventLoop),
245 std::forward<Xform>(xform),
246 std::forward<Enhancers>(enhancers)...);
247 }
248
269 template<class PH, class Xform, class ...Enhancers>
271 RandomData random,
272 JobInterface &jobHandler,
273 EventInterface &eventEmitter,
274 PH &&ph,
275 Xform &&xform,
276 Enhancers &&...enhancers)
277 -> Sdk<PH, Xform, Enhancers...>
278 {
279 auto m = SdkModel{};
280 m.client.crypto = Crypto(RandomTag{}, std::move(random));
281
282 return makeSdk(std::move(m),
283 jobHandler,
284 eventEmitter,
285 std::forward<PH>(ph),
286 std::forward<Xform>(xform),
287 std::forward<Enhancers>(enhancers)...);
288 }
289
290
299 {
300 return lager::with_deps(std::ref(random));
301 }
302}
Represent a Matrix client.
Definition client.hpp:87
Definition context.hpp:130
PromiseT createResolvedPromise(RetType v) const
Definition context.hpp:185
Definition crypto.hpp:36
Definition eventinterface.hpp:15
A movable wrapper around std::random_device.
Definition random-generator.hpp:103
Definition random-generator.hpp:16
Contain the single source of truth of a matrix sdk.
Definition sdk.hpp:30
Sdk(ModelT model, JobInterface &jobHandler, EventInterface &eventEmitter, EventLoop &&eventLoop, Xform &&xform, Enhancers &&...enhancers)
Definition sdk.hpp:63
ContextT context() const
Get the context associated with this.
Definition sdk.hpp:80
Client client() const
Get a Client representing this.
Definition sdk.hpp:89
auto createSecondaryRoot(EL &&eventLoop, ModelT initialModel=ModelT{}) const
Create a secondary root for this Sdk.
Definition sdk.hpp:104
Client clientFromSecondaryRoot(lager::reader< ModelT > sr) const
Get a Client representing this.
Definition sdk.hpp:133
Definition location.hpp:10
@ Private
Definition client-model.hpp:41
auto makeSdk(SdkModel sdk, JobInterface &jobHandler, EventInterface &eventEmitter, EventLoop &&eventLoop, Xform &&xform, Enhancers &&...enhancers) -> Sdk< EventLoop, Xform, Enhancers... >
Create an sdk with the provided model.
Definition sdk.hpp:204
lager::dep::key< SdkModelCursorTag, lager::dep::fn< std::shared_ptr< lager::reader< SdkModel > > > > SdkModelCursorKey
Definition sdk-model-cursor-tag.hpp:23
std::string RandomData
Definition crypto-util.hpp:35
RandomData genRandomData(int len)
Definition crypto-util.hpp:66
std::size_t makeDefaultSdkWithCryptoRandomSize()
Definition sdk.hpp:223
auto withRandomGenerator(RandomInterface &random)
An enhancer to use a custom random generator.
Definition sdk.hpp:298
auto makeDefaultSdkWithCryptoRandom(RandomData random, JobInterface &jobHandler, EventInterface &eventEmitter, PH &&ph, Xform &&xform, Enhancers &&...enhancers) -> Sdk< PH, Xform, Enhancers... >
Create an sdk with a default-constructed model, and a Crypto constructed with user-provided random da...
Definition sdk.hpp:270
auto makeDefaultEncryptedSdk(JobInterface &jobHandler, EventInterface &eventEmitter, EventLoop &&eventLoop, Xform &&xform, Enhancers &&...enhancers) -> Sdk< EventLoop, Xform, Enhancers... >
Definition sdk.hpp:230
auto makeStore(Model &&initialModel, Reducer &&reducer, PH &&ph, Enhancers &&...enhancers)
Definition store.hpp:141
Definition clientutil.hpp:140
Definition client-model.hpp:59
std::optional< immer::box< Crypto > > crypto
Definition client-model.hpp:85
Definition client.hpp:102
Definition jobinterface.hpp:21
The tag to indicate that a constructor should use user-provided random data.
Definition crypto-util.hpp:33
Definition sdk-model.hpp:26
SdkAction Action
Definition sdk-model.hpp:32
static SdkResult update(SdkModel s, SdkAction a)
Definition sdk-model.cpp:23
ClientModel client
Definition sdk-model.hpp:27
A stateful tracker for all verification processes.
Definition verification-tracker.hpp:98