CCF
Loading...
Searching...
No Matches
custom_protocol_subsystem.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6#include "ccf/node/session.h"
8#include "ccf/rpc_context.h"
11
12#include <functional>
13#include <memory>
14
15namespace ccf
16{
18 {
19 protected:
21 std::map<std::string, CreateSessionFn> session_creation_functions;
22
23 public:
25 node_state(node_state_)
26 {}
27
28 void install(
29 const std::string& protocol_name,
30 CreateSessionFn create_session_f) override
31 {
32 session_creation_functions[protocol_name] = create_session_f;
33 }
34
35 void uninstall(const std::string& protocol_name) override
36 {
37 session_creation_functions.erase(protocol_name);
38 }
39
40 std::shared_ptr<Session> create_session(
41 const std::string& protocol_name,
42 ccf::tls::ConnID conn_id,
43 const std::unique_ptr<tls::Context>&& ctx) override
44 {
45 auto it = session_creation_functions.find(protocol_name);
46 if (it != session_creation_functions.end())
47 {
48 return it->second(conn_id, std::move(ctx));
49 }
50 throw std::logic_error(fmt::format(
51 "Session creation function for protocol '{}' has not been installed",
52 protocol_name));
53 }
54
55 std::shared_ptr<Essentials> get_essentials() override
56 {
57 std::shared_ptr<Essentials> r = std::make_shared<Essentials>();
59 auto store = node_state.get_store();
60 r->tx = std::make_shared<ccf::kv::ReadOnlyTx>(store.get());
61 r->ctx = std::make_shared<ccf::endpoints::ReadOnlyEndpointContext>(
62 nullptr, *r->tx);
63 return r;
64 }
65 };
66}
Definition node_interface.h:22
virtual ringbuffer::AbstractWriterFactory & get_writer_factory()=0
virtual std::shared_ptr< ccf::kv::Store > get_store()=0
Definition custom_protocol_subsystem_interface.h:24
std::function< std::shared_ptr< Session >(ccf::tls::ConnID, const std::unique_ptr< tls::Context > &&)> CreateSessionFn
Definition custom_protocol_subsystem_interface.h:27
Definition custom_protocol_subsystem.h:18
void install(const std::string &protocol_name, CreateSessionFn create_session_f) override
Definition custom_protocol_subsystem.h:28
std::map< std::string, CreateSessionFn > session_creation_functions
Definition custom_protocol_subsystem.h:21
std::shared_ptr< Essentials > get_essentials() override
Definition custom_protocol_subsystem.h:55
CustomProtocolSubsystem(AbstractNodeState &node_state_)
Definition custom_protocol_subsystem.h:24
std::shared_ptr< Session > create_session(const std::string &protocol_name, ccf::tls::ConnID conn_id, const std::unique_ptr< tls::Context > &&ctx) override
Definition custom_protocol_subsystem.h:40
AbstractNodeState & node_state
Definition custom_protocol_subsystem.h:20
void uninstall(const std::string &protocol_name) override
Definition custom_protocol_subsystem.h:35
virtual WriterPtr create_writer_to_outside()=0
int64_t ConnID
Definition custom_protocol_subsystem_interface.h:20
Definition app_interface.h:14