Skip to content

Commit 745c9ce

Browse files
committed
multiprocess: Add Ipc and Init interface definitions
1 parent 5d62d7f commit 745c9ce

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/Makefile.am

+3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ BITCOIN_CORE_H = \
159159
init/common.h \
160160
interfaces/chain.h \
161161
interfaces/handler.h \
162+
interfaces/init.h \
163+
interfaces/ipc.h \
162164
interfaces/node.h \
163165
interfaces/wallet.h \
164166
key.h \
@@ -559,6 +561,7 @@ libbitcoin_util_a_SOURCES = \
559561
compat/strnlen.cpp \
560562
fs.cpp \
561563
interfaces/handler.cpp \
564+
interfaces/init.cpp \
562565
logging.cpp \
563566
random.cpp \
564567
randomenv.cpp \

src/interfaces/init.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <interfaces/chain.h>
6+
#include <interfaces/init.h>
7+
#include <interfaces/node.h>
8+
#include <interfaces/wallet.h>
9+
10+
namespace interfaces {
11+
std::unique_ptr<Node> Init::makeNode() { return {}; }
12+
std::unique_ptr<Chain> Init::makeChain() { return {}; }
13+
std::unique_ptr<WalletClient> Init::makeWalletClient(Chain& chain) { return {}; }
14+
Ipc* Init::ipc() { return nullptr; }
15+
} // namespace interfaces

src/interfaces/init.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_INTERFACES_INIT_H
6+
#define BITCOIN_INTERFACES_INIT_H
7+
8+
#include <memory>
9+
10+
struct NodeContext;
11+
12+
namespace interfaces {
13+
class Chain;
14+
class Ipc;
15+
class Node;
16+
class WalletClient;
17+
18+
//! Initial interface created when a process is first started, and used to give
19+
//! and get access to other interfaces (Node, Chain, Wallet, etc).
20+
//!
21+
//! There is a different Init interface implementation for each process
22+
//! (bitcoin-gui, bitcoin-node, bitcoin-wallet, bitcoind, bitcoin-qt) and each
23+
//! implementation can implement the make methods for interfaces it supports.
24+
//! The default make methods all return null.
25+
class Init
26+
{
27+
public:
28+
virtual ~Init() = default;
29+
virtual std::unique_ptr<Node> makeNode();
30+
virtual std::unique_ptr<Chain> makeChain();
31+
virtual std::unique_ptr<WalletClient> makeWalletClient(Chain& chain);
32+
virtual Ipc* ipc();
33+
};
34+
} // namespace interfaces
35+
36+
#endif // BITCOIN_INTERFACES_INIT_H

src/interfaces/ipc.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_INTERFACES_IPC_H
6+
#define BITCOIN_INTERFACES_IPC_H
7+
8+
#include <functional>
9+
#include <memory>
10+
#include <typeindex>
11+
12+
namespace interfaces {
13+
class Init;
14+
15+
//! Interface providing access to interprocess-communication (IPC)
16+
//! functionality.
17+
class Ipc
18+
{
19+
public:
20+
virtual ~Ipc() = default;
21+
22+
//! Spawn a child process returning pointer to its Init interface.
23+
virtual std::unique_ptr<Init> spawnProcess(const char* exe_name) = 0;
24+
25+
//! If this is a spawned process, block and handle requests from the parent
26+
//! process by forwarding them to this process's Init interface, then return
27+
//! true. If this is not a spawned child process, return false.
28+
virtual bool startSpawnedProcess(int argc, char* argv[], int& exit_status) = 0;
29+
30+
//! Add cleanup callback to remote interface that will run when the
31+
//! interface is deleted.
32+
template<typename Interface>
33+
void addCleanup(Interface& iface, std::function<void()> cleanup)
34+
{
35+
addCleanup(typeid(Interface), &iface, std::move(cleanup));
36+
}
37+
38+
protected:
39+
//! Internal implementation of public addCleanup method (above) as a
40+
//! type-erased virtual function, since template functions can't be virtual.
41+
virtual void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) = 0;
42+
};
43+
44+
//! Return implementation of Ipc interface.
45+
std::unique_ptr<Ipc> MakeIpc(const char* exe_name, const char* process_argv0, Init& init);
46+
} // namespace interfaces
47+
48+
#endif // BITCOIN_INTERFACES_IPC_H

0 commit comments

Comments
 (0)