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 virtual 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 virtual void uninstall(const std::string& protocol_name) override
36 {
37 session_creation_functions.erase(protocol_name);
38 }
39
40 virtual 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 else
51 {
52 throw std::logic_error(fmt::format(
53 "Session creation function for protocol '{}' has not been installed",
54 protocol_name));
55 }
56 }
57
58 virtual std::shared_ptr<Essentials> get_essentials() override
59 {
60 std::shared_ptr<Essentials> r = std::make_shared<Essentials>();
62 auto store = node_state.get_store();
63 r->tx = std::make_shared<ccf::kv::ReadOnlyTx>(store.get());
64 r->ctx = std::make_shared<ccf::endpoints::ReadOnlyEndpointContext>(
65 nullptr, *r->tx);
66 return r;
67 }
68 };
69}
Definition node_interface.h:23
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
virtual 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
std::map< std::string, CreateSessionFn > session_creation_functions
Definition custom_protocol_subsystem.h:21
CustomProtocolSubsystem(AbstractNodeState &node_state_)
Definition custom_protocol_subsystem.h:24
AbstractNodeState & node_state
Definition custom_protocol_subsystem.h:20
virtual std::shared_ptr< Essentials > get_essentials() override
Definition custom_protocol_subsystem.h:58
virtual void install(const std::string &protocol_name, CreateSessionFn create_session_f) override
Definition custom_protocol_subsystem.h:28
virtual 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