parity-clib: async C bindings to RPC requests + subscribe/unsubscribe to websocket events (#9920)
* feat(parity-clib asynchronous rpc queries) * feat(seperate bindings for ws and rpc) * Subscribing to websockets for the full-client works * feat(c binding unsubscribe_from_websocket) * fix(tests): tweak CMake build config * Enforce C+11 * refactor(parity-cpp-example) : `cpp:ify` * fix(typedefs) : revert typedefs parity-clib * docs(nits) * fix(simplify websocket_unsubscribe) * refactor(cpp example) : more subscriptions * fix(callback type) : address grumbles on callback * Use it the example to avoid using global variables * docs(nits) - don't mention `arc` * fix(jni bindings): fix compile errors * feat(java example and updated java bindings) * fix(java example) : run both full and light client * fix(Java shutdown) : unsubscribe to sessions Forgot to pass the JNIEnv environment since it is an instance method * feat(return valid JString) * Remove Java dependency by constructing a valid Java String in the callback * fix(logger) : remove `rpc` trace log * fix(format) * fix(parity-clib): remove needless callback `type` * fix(parity-clib-examples) : update examples * `cpp` example pass in a struct instead to determines `callback kind` * `java` add a instance variable the class `Callback` to determine `callback kind` * fix(review comments): docs and format * Update parity-clib/src/java.rs Co-Authored-By: niklasad1 <niklasadolfsson1@gmail.com> * fix(bad merge + spelling) * fix(move examples to parity-clib/examples)
This commit is contained in:
committed by
Afri Schoedon
parent
2bb79614f6
commit
b4f8bba843
109
parity-clib/examples/java/Main.java
Normal file
109
parity-clib/examples/java/Main.java
Normal file
@@ -0,0 +1,109 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import io.parity.ethereum.Parity;
|
||||
|
||||
class Main {
|
||||
public static final int ONE_MINUTE_AS_MILLIS = 60 * 1000;
|
||||
|
||||
public static final String[] rpc_queries = {
|
||||
"{\"method\":\"parity_versionInfo\",\"params\":[],\"id\":1,\"jsonrpc\":\"2.0\"}",
|
||||
"{\"method\":\"eth_getTransactionReceipt\",\"params\":[\"0x444172bef57ad978655171a8af2cfd89baa02a97fcb773067aef7794d6913fff\"],\"id\":1,\"jsonrpc\":\"2.0\"}",
|
||||
"{\"method\":\"eth_estimateGas\",\"params\":[{\"from\":\"0x0066Dc48bb833d2B59f730F33952B3c29fE926F5\"}],\"id\":1,\"jsonrpc\":\"2.0\"}",
|
||||
"{\"method\":\"eth_getBalance\",\"params\":[\"0x0066Dc48bb833d2B59f730F33952B3c29fE926F5\"],\"id\":1,\"jsonrpc\":\"2.0\"}"
|
||||
};
|
||||
|
||||
public static final String[] ws_queries = {
|
||||
"{\"method\":\"parity_subscribe\",\"params\":[\"eth_getBalance\",[\"0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826\",\"latest\"]],\"id\":1,\"jsonrpc\":\"2.0\"}",
|
||||
"{\"method\":\"parity_subscribe\",\"params\":[\"parity_netPeers\"],\"id\":1,\"jsonrpc\":\"2.0\"}",
|
||||
"{\"method\":\"eth_subscribe\",\"params\":[\"newHeads\"],\"id\":1,\"jsonrpc\":\"2.0\"}"
|
||||
};
|
||||
|
||||
public static void runParity(String[] config) {
|
||||
Parity parity = new Parity(config);
|
||||
|
||||
Callback rpcCallback = new Callback(1);
|
||||
Callback webSocketCallback = new Callback(2);
|
||||
|
||||
for (String query : rpc_queries) {
|
||||
parity.rpcQuery(query, ONE_MINUTE_AS_MILLIS, rpcCallback);
|
||||
}
|
||||
|
||||
while (rpcCallback.getNumCallbacks() != 4);
|
||||
|
||||
Vector<Long> sessions = new Vector<Long>();
|
||||
|
||||
for (String ws : ws_queries) {
|
||||
long session = parity.subscribeWebSocket(ws, webSocketCallback);
|
||||
sessions.add(session);
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(ONE_MINUTE_AS_MILLIS);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
for (long session : sessions) {
|
||||
parity.unsubscribeWebSocket(session);
|
||||
}
|
||||
|
||||
// Force GC to destroy parity
|
||||
parity = null;
|
||||
System.gc();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[] full = {"--no-ipc" , "--jsonrpc-apis=all", "--chain", "kovan"};
|
||||
String[] light = {"--no-ipc", "--light", "--jsonrpc-apis=all", "--chain", "kovan"};
|
||||
|
||||
runParity(full);
|
||||
|
||||
try {
|
||||
Thread.sleep(ONE_MINUTE_AS_MILLIS);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
runParity(light);
|
||||
}
|
||||
}
|
||||
|
||||
class Callback {
|
||||
private AtomicInteger counter;
|
||||
private final int callbackType;
|
||||
|
||||
public Callback(int type) {
|
||||
counter = new AtomicInteger();
|
||||
callbackType = type;
|
||||
}
|
||||
|
||||
public void callback(Object response) {
|
||||
response = (String) response;
|
||||
if (callbackType == 1) {
|
||||
System.out.println("rpc: " + response);
|
||||
} else if (callbackType == 2) {
|
||||
System.out.println("ws: " + response);
|
||||
}
|
||||
counter.getAndIncrement();
|
||||
}
|
||||
|
||||
public int getNumCallbacks() {
|
||||
return counter.intValue();
|
||||
}
|
||||
}
|
||||
9
parity-clib/examples/java/README.md
Normal file
9
parity-clib/examples/java/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
parity-clib: Java example
|
||||
===================================
|
||||
|
||||
An example Java application to demonstrate how to use `jni` bindings to parity-ethereum. Note, that the example is built in debug-mode to reduce the build time. If you want to use it in real project use release-mode instead to facilitate all compiler optimizations.
|
||||
|
||||
## How to compile and run
|
||||
|
||||
1. Make sure you have installed [JDK](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
|
||||
2. Run `run.sh`
|
||||
15
parity-clib/examples/java/run.sh
Executable file
15
parity-clib/examples/java/run.sh
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
FLAGS="-Xlint:deprecation"
|
||||
PARITY_JAVA="../../Parity.java"
|
||||
# parity-clib must be built with feature `jni` in debug-mode to work
|
||||
PARITY_LIB=".:../../../target/debug/"
|
||||
|
||||
# build
|
||||
cd ..
|
||||
cargo build --features jni
|
||||
cd -
|
||||
javac $FLAGS -d $PWD $PARITY_JAVA
|
||||
javac $FLAGS *.java
|
||||
# Setup the path `libparity.so` and run
|
||||
java -Djava.library.path=$PARITY_LIB Main
|
||||
Reference in New Issue
Block a user