Skip to content

Commit

Permalink
multiprocess: Add Ipc and Init interface definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanofsky committed Apr 23, 2021
1 parent 5d62d7f commit 745c9ce
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ BITCOIN_CORE_H = \
init/common.h \
interfaces/chain.h \
interfaces/handler.h \
interfaces/init.h \
interfaces/ipc.h \
interfaces/node.h \
interfaces/wallet.h \
key.h \
Expand Down Expand Up @@ -559,6 +561,7 @@ libbitcoin_util_a_SOURCES = \
compat/strnlen.cpp \
fs.cpp \
interfaces/handler.cpp \
interfaces/init.cpp \
logging.cpp \
random.cpp \
randomenv.cpp \
Expand Down
15 changes: 15 additions & 0 deletions src/interfaces/init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <interfaces/chain.h>
#include <interfaces/init.h>
#include <interfaces/node.h>
#include <interfaces/wallet.h>

namespace interfaces {
std::unique_ptr<Node> Init::makeNode() { return {}; }
std::unique_ptr<Chain> Init::makeChain() { return {}; }
std::unique_ptr<WalletClient> Init::makeWalletClient(Chain& chain) { return {}; }
Ipc* Init::ipc() { return nullptr; }
} // namespace interfaces
36 changes: 36 additions & 0 deletions src/interfaces/init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_INTERFACES_INIT_H
#define BITCOIN_INTERFACES_INIT_H

#include <memory>

struct NodeContext;

namespace interfaces {
class Chain;
class Ipc;
class Node;
class WalletClient;

//! Initial interface created when a process is first started, and used to give
//! and get access to other interfaces (Node, Chain, Wallet, etc).
//!
//! There is a different Init interface implementation for each process
//! (bitcoin-gui, bitcoin-node, bitcoin-wallet, bitcoind, bitcoin-qt) and each
//! implementation can implement the make methods for interfaces it supports.
//! The default make methods all return null.
class Init
{
public:
virtual ~Init() = default;
virtual std::unique_ptr<Node> makeNode();
virtual std::unique_ptr<Chain> makeChain();
virtual std::unique_ptr<WalletClient> makeWalletClient(Chain& chain);
virtual Ipc* ipc();
};
} // namespace interfaces

#endif // BITCOIN_INTERFACES_INIT_H
48 changes: 48 additions & 0 deletions src/interfaces/ipc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_INTERFACES_IPC_H
#define BITCOIN_INTERFACES_IPC_H

#include <functional>
#include <memory>
#include <typeindex>

namespace interfaces {
class Init;

//! Interface providing access to interprocess-communication (IPC)
//! functionality.
class Ipc
{
public:
virtual ~Ipc() = default;

//! Spawn a child process returning pointer to its Init interface.
virtual std::unique_ptr<Init> spawnProcess(const char* exe_name) = 0;

//! If this is a spawned process, block and handle requests from the parent
//! process by forwarding them to this process's Init interface, then return
//! true. If this is not a spawned child process, return false.
virtual bool startSpawnedProcess(int argc, char* argv[], int& exit_status) = 0;

//! Add cleanup callback to remote interface that will run when the
//! interface is deleted.
template<typename Interface>
void addCleanup(Interface& iface, std::function<void()> cleanup)
{
addCleanup(typeid(Interface), &iface, std::move(cleanup));
}

protected:
//! Internal implementation of public addCleanup method (above) as a
//! type-erased virtual function, since template functions can't be virtual.
virtual void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) = 0;
};

//! Return implementation of Ipc interface.
std::unique_ptr<Ipc> MakeIpc(const char* exe_name, const char* process_argv0, Init& init);
} // namespace interfaces

#endif // BITCOIN_INTERFACES_IPC_H

0 comments on commit 745c9ce

Please sign in to comment.