Skip to content

Commit 8abd3da

Browse files
committed
Merge #55: Add doc/ folder, split up readme and add more usage documentation
1a3dc8d Add documentation about interface definitions (Russell Yanofsky) 5539208 Split up README.md (Russell Yanofsky) Pull request description: Top commit has no ACKs. Tree-SHA512: 3df1e93a2df96d9ebd806828d61dac801b696a846f5b7abc14f9713e6279b35cb19ddb6c7033ea4584203663eb248f26a591d2b4df04cc205f676b42487aa68e
2 parents 805eb73 + 1a3dc8d commit 8abd3da

File tree

4 files changed

+78
-58
lines changed

4 files changed

+78
-58
lines changed

README.md

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,8 @@
11
# libmultiprocess
22

3-
## Summary
3+
`libmultiprocess` is a C++ library and code generator making it easy to call functions and reference objects in different processes.
44

5-
C++ library and code generator making it easy to call functions and reference objects in different processes.
5+
For more information see the [usage instructions](doc/usage.md), [installation instructions](doc/install.md), or [design documentation](doc/design.md).
66

7-
## Description
8-
9-
Given an interface description of an object with one or more methods, libmultiprocess generates:
10-
11-
* A C++ `ProxyClient` class with an implementation of each interface method that sends a request over a socket, waits for a response, and returns the result.
12-
* A C++ `ProxyServer` class that listens for requests over a socket and calls a wrapped C++ object implementating the same interface to actually execute the requests.
13-
14-
The function call ⇆ request translation supports input and output arguments, standard types like `unique_ptr`, `vector`, `map`, and `optional`, and bidirectional calls between processes through interface pointer and `std::function` arguments.
15-
16-
If the wrapped C++ object inherits from an abstract base class declaring virtual methods, the generated `ProxyClient` objects can inherit from the same class, allowing interprocess calls to replace local calls without changes to existing code.
17-
18-
There is also optional support for thread mapping, so each thread making interprocess calls can have a dedicated thread processing requests from it, and callbacks from processing threads are executed on corresponding request threads (so recursive mutexes and thread names function as expected in callbacks).
19-
20-
### What is `kj`?
21-
22-
KJ is a concurrency framework [bundled with
23-
capnproto](https://capnproto.org/cxxrpc.html#kj-concurrency-framework); it is used as a
24-
basis in this library to construct the event-loop necessary to service IPC requests.
25-
26-
## Example
27-
28-
A simple interface description can be found at [test/mp/test/foo.capnp](test/mp/test/foo.capnp), implementation in [test/mp/test/foo.h](test/mp/test/foo.h), and usage in [test/mp/test/test.cpp](test/mp/test/test.cpp).
29-
30-
A more complete example can be found in [example](example/) and run with:
31-
32-
```sh
33-
make -C build example
34-
build/example/mpexample
35-
```
36-
37-
## Future directions
38-
39-
_libmultiprocess_ uses the [Cap'n Proto](https://capnproto.org) interface description language and protocol, but it could be extended or changed to use a different IDL/protocol like [gRPC](https://grpc.io). The nice thing about _Cap'n Proto_ compared to _gRPC_ and most other lower level protocols is that it allows interface pointers (_Services_ in gRPC parlance) to be passed as method arguments and return values, so object references and bidirectional requests work out of the box. Supporting a lower-level protocol would require writing adding maps and tracking code to proxy objects.
40-
41-
_libmultiprocess_ is currently compatible with sandboxing but could add platform-specific sandboxing support or integration with a sandboxing library like [SAPI](https://github.com/google/sandboxed-api).
42-
43-
## Installation
44-
45-
Installation currently requires Cap'n Proto:
46-
47-
```sh
48-
apt install libcapnp-dev capnproto
49-
brew install capnp
50-
dnf install capnproto
51-
```
52-
53-
Installation steps are:
54-
55-
```sh
56-
mkdir build
57-
cd build
58-
cmake ..
59-
make
60-
make check # Optionally build and run tests
61-
make install
62-
```
7+
If you have any questions, comments, or feedback, please submit an [issue](https://github.com/chaincodelabs/libmultiprocess/issues/new).
8+
Duplicate issues are perfectly fine and all discussion about the project is welcome, since there isn't another discussion forum currently.

doc/design.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# libmultiprocess Design
2+
3+
Given an interface description of an object with one or more methods, libmultiprocess generates:
4+
5+
* A C++ `ProxyClient` class with an implementation of each interface method that sends a request over a socket, waits for a response, and returns the result.
6+
* A C++ `ProxyServer` class that listens for requests over a socket and calls a wrapped C++ object implementating the same interface to actually execute the requests.
7+
8+
The function call ⇆ request translation supports input and output arguments, standard types like `unique_ptr`, `vector`, `map`, and `optional`, and bidirectional calls between processes through interface pointer and `std::function` arguments.
9+
10+
If the wrapped C++ object inherits from an abstract base class declaring virtual methods, the generated `ProxyClient` objects can inherit from the same class, allowing interprocess calls to replace local calls without changes to existing code.
11+
12+
There is also optional support for thread mapping, so each thread making interprocess calls can have a dedicated thread processing requests from it, and callbacks from processing threads are executed on corresponding request threads (so recursive mutexes and thread names function as expected in callbacks).
13+
14+
## Interface descriptions
15+
16+
As explained in the [usage](usage.md) document, interface descriptions need to be consumed both by the _libmultiprocess_ code generator, and by C++ code that calls and implements the interfaces. The C++ code only needs to know about C++ arguments and return types, while the code generator only needs to know about capnp arguments and return types, but both need to know class and method names, so the corresponding `.h` and `.capnp` source files contain some of the same information, and have to be kept in sync manually when methods or parameters change. Despite the redundancy, reconciling the interface definitions is designed to be _straightforward_ and _safe_. _Straightforward_ because there is no need to write manual serialization code or use awkward intermediate types like [`UniValue`](https://github.com/bitcoin/bitcoin/blob/master/src/univalue/include/univalue.h) instead of native types. _Safe_ because if there are any inconsistencies between API and data definitions (even minor ones like using a narrow int data type for a wider int API input), there are errors at build time instead of errors or bugs at runtime.
17+
18+
In the future, it would be possible to combine API and data definitions together using [C++ attributes](https://en.cppreference.com/w/cpp/language/attributes). To do this we would add attributes to the API definition files, and then generate the data definitions from the API definitions and attributes. I didn't take this approach mostly because it would be extra work, but also because until c++ standardizes reflection, this would require either hooking into compiler APIs like https://github.com/RosettaCommons/binder, or parsing c++ code manually like http://www.swig.org/.
19+
20+
## What is `kj`?
21+
22+
KJ is a concurrency framework [bundled with
23+
capnproto](https://capnproto.org/cxxrpc.html#kj-concurrency-framework); it is used as a
24+
basis in this library to construct the event-loop necessary to service IPC requests.
25+
26+
## Future directions
27+
28+
_libmultiprocess_ uses the [Cap'n Proto](https://capnproto.org) interface description language and protocol, but it could be extended or changed to use a different IDL/protocol like [gRPC](https://grpc.io). The nice thing about _Cap'n Proto_ compared to _gRPC_ and most other lower level protocols is that it allows interface pointers (_Services_ in gRPC parlance) to be passed as method arguments and return values, so object references and bidirectional requests work out of the box. Supporting a lower-level protocol would require writing adding maps and tracking code to proxy objects.
29+
30+
_libmultiprocess_ is currently compatible with sandboxing but could add platform-specific sandboxing support or integration with a sandboxing library like [SAPI](https://github.com/google/sandboxed-api).

doc/install.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# libmultiprocess Installation
2+
3+
Installation currently requires Cap'n Proto:
4+
5+
```sh
6+
apt install libcapnp-dev capnproto
7+
brew install capnp
8+
dnf install capnproto
9+
```
10+
11+
Installation steps are:
12+
13+
```sh
14+
mkdir build
15+
cd build
16+
cmake ..
17+
make
18+
make check # Optionally build and run tests
19+
make install
20+
```

doc/usage.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# libmultiprocess Usage
2+
3+
## Overview
4+
5+
_libmultiprocess_ is a library and code generator that allows calling C++ class interfaces across different processes. For an interface to be available from other processses, it needs two definitions:
6+
7+
- An **API definition** declaring how the interface is called. Included examples: [calculator.h](https://github.com/chaincodelabs/libmultiprocess/blob/master/example/calculator.h), [printer.h](https://github.com/chaincodelabs/libmultiprocess/blob/master/example/printer.h), [init.h](https://github.com/chaincodelabs/libmultiprocess/blob/master/example/init.h). Bitcoin examples: [node.h](https://github.com/ryanofsky/bitcoin/blob/ipc-export/src/interfaces/node.h), [wallet.h](https://github.com/ryanofsky/bitcoin/blob/ipc-export/src/interfaces/wallet.h), [echo.h](https://github.com/ryanofsky/bitcoin/blob/ipc-export/src/interfaces/echo.h), [init.h](https://github.com/ryanofsky/bitcoin/blob/ipc-export/src/interfaces/init.h).
8+
9+
- A **data definition** declaring how interface calls get sent across the wire. Included examples: [calculator.capnp](https://github.com/chaincodelabs/libmultiprocess/blob/master/example/calculator.capnp), [printer.capnp](https://github.com/chaincodelabs/libmultiprocess/blob/master/example/printer.capnp), [init.capnp](https://github.com/chaincodelabs/libmultiprocess/blob/master/example/init.capnp). Bitcoin examples: [node.capnp](https://github.com/ryanofsky/bitcoin/blob/ipc-export/src/ipc/capnp/node.capnp), [wallet.capnp](https://github.com/ryanofsky/bitcoin/blob/ipc-export/src/ipc/capnp/wallet.capnp), [echo.capnp](https://github.com/ryanofsky/bitcoin/blob/ipc-export/src/ipc/capnp/echo.capnp), [init.capnp](https://github.com/ryanofsky/bitcoin/blob/ipc-export/src/ipc/capnp/init.capnp).
10+
11+
The `*.capnp` data definition files are consumed by the _libmultiprocess_ code generator and each `X.capnp` file generates `X.capnp.c++`, `X.capnp.h`, `X.capnp.proxy-client.c++`, `X.capnp.proxy-server.c++`, `X.capnp.proxy-types.c++`, `X.capnp.proxy-types.h`, and `X.capnp.proxy.h` output files. The generated files include `mp::ProxyClient<Interface>` and `mp::ProxyServer<Interface>` class specializations for all the interfaces in the `.capnp` files. These allow methods on C++ objects in one process to be called from other processes over IPC sockets.
12+
13+
The `ProxyServer` objects help translate IPC requests from a socket to method calls on a local object. The `ProxyServer` objects are just used internally by the `mp::ServeStream(loop, socket, wrapped_object)` and `mp::ListenConnections(loop, socket, wrapped_object)` functions, and aren't exposed externally. The `ProxyClient` classes are exposed, and returned from the `mp::ConnectStream(loop, socket)` function and meant to be used directly. The classes implement methods described in `.capnp` definitions, and whenever any method is called, a request with the method arguments is sent over the associated IPC connection, and the corresponding `wrapped_object` method on the other end of the connection is called, with the `ProxyClient` method blocking until it returns and forwarding back any return value to the `ProxyClient` method caller.
14+
15+
## Example
16+
17+
A simple interface description can be found at [test/mp/test/foo.capnp](test/mp/test/foo.capnp), implementation in [test/mp/test/foo.h](test/mp/test/foo.h), and usage in [test/mp/test/test.cpp](test/mp/test/test.cpp).
18+
19+
A more complete example can be found in [example](example/) and run with:
20+
21+
```sh
22+
make -C build example
23+
build/example/mpexample
24+
```

0 commit comments

Comments
 (0)