OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
parsec/agent/runners/lua/server.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include "server.hpp"
7
8#include "agent/format.hpp"
9#include "impl.hpp"
11#include "util/rpc/format.hpp"
13
14#include <cassert>
15#include <future>
16
18 server::server(std::unique_ptr<server_type> srv,
19 std::shared_ptr<broker::interface> broker,
20 std::shared_ptr<logging::log> log,
21 const cbdc::parsec::config& cfg)
22 : server_interface(std::move(broker), std::move(log), cfg),
23 m_srv(std::move(srv)) {
24 m_srv->register_handler_callback(
25 [&](request req, server_type::response_callback_type callback) {
26 return request_handler(std::move(req), std::move(callback));
27 });
28 }
29
31 m_log->trace("Agent server shutting down...");
32 m_srv.reset();
33 m_log->trace("Shut down agent server");
34 }
35
36 auto server::init() -> bool {
37 return m_srv->init();
38 }
39
40 auto server::request_handler(request req,
41 server_type::response_callback_type callback)
42 -> bool {
43 m_log->trace("received request with m_function ",
44 req.m_function.to_hex(),
45 " and param size ",
46 req.m_param.size());
47 auto id = m_next_id++;
48 auto a = [&]() {
49 auto agent = std::make_shared<impl>(
50 m_log,
51 m_cfg,
53 m_broker,
54 req.m_function,
55 req.m_param,
56 [this, id, callback](interface::exec_return_type res) {
57 auto success = std::holds_alternative<return_type>(res);
58 if(!success) {
59 auto ec = std::get<interface::error_code>(res);
60 if(ec == interface::error_code::retry) {
61 m_retry_queue.push(id);
62 return;
63 }
64 }
65 callback(res);
66 m_cleanup_queue.push(id);
67 },
69 req.m_is_readonly_run,
70 m_secp,
71 m_threads);
72 {
73 std::unique_lock l(m_agents_mut);
74 m_agents.emplace(id, agent);
75 }
76 return agent;
77 }();
78 return a->exec();
79 }
80}
std::variant< return_type, error_code > exec_return_type
Return type from function execution.
auto init() -> bool override
Initializes the server.
Runner factory for agents to intiantiate new runners of a particular type while only worrying about t...
static constexpr auto initial_lock_type
Lock type to acquire when requesting the function code.
Agent contract execution RPC request message.
Configuration parameters for a phase two system.