Add a Java interface (#9346)

* Add a Java interface

* Use system ABI

* Forgot exception

* Fix param for parity_rpc

* Address concerns
This commit is contained in:
Pierre Krieger
2018-09-05 17:17:12 +02:00
committed by Andronik Ordian
parent 72fd1fa58d
commit ab9843cb00
4 changed files with 230 additions and 1 deletions

View File

@@ -12,6 +12,7 @@ crate-type = ["cdylib", "staticlib"]
[dependencies]
panic_hook = { path = "../util/panic_hook" }
parity-ethereum = { path = "../", default-features = false }
jni = { version = "0.10.1", optional = true }
[features]
default = []

63
parity-clib/Parity.java Normal file
View File

@@ -0,0 +1,63 @@
// 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/>.
package io.parity.ethereum;
/**
* Interface to the Parity client.
*/
public class Parity {
/**
* Starts the Parity client with the CLI options passed as an array of strings.
*
* Each space-delimited option corresponds to an array entry.
* For example: `["--port", "12345"]`
*
* @param options The CLI options to start Parity with
*/
public Parity(String[] options) {
long config = configFromCli(options);
inner = build(config);
}
/** Performs a synchronous RPC query.
*
* Note that this will block the current thread until the query is finished. You are
* encouraged to create a background thread if you don't want to block.
*
* @param query The JSON-encoded RPC query to perform
* @return A JSON-encoded result
*/
public String rpcQuery(String query) {
return rpcQueryNative(inner, query);
}
@Override
protected void finalize() {
destroy(inner);
}
static {
System.loadLibrary("parity");
}
private static native long configFromCli(String[] cliOptions);
private static native long build(long config);
private static native void destroy(long inner);
private static native String rpcQueryNative(long inner, String rpc);
private long inner;
}

View File

@@ -17,6 +17,8 @@
//! Note that all the structs and functions here are documented in `parity.h`, to avoid
//! duplicating documentation.
#[cfg(feature = "jni")]
extern crate jni;
extern crate parity_ethereum;
extern crate panic_hook;
@@ -26,6 +28,11 @@ use std::ptr;
use std::slice;
use std::str;
#[cfg(feature = "jni")]
use std::mem;
#[cfg(feature = "jni")]
use jni::{JNIEnv, objects::JClass, objects::JString, sys::jlong, sys::jobjectArray};
#[repr(C)]
pub struct ParityParams {
pub configuration: *mut c_void,
@@ -117,7 +124,7 @@ pub unsafe extern fn parity_destroy(client: *mut c_void) {
}
#[no_mangle]
pub unsafe extern fn parity_rpc(client: *mut c_void, query: *const char, len: usize, out_str: *mut c_char, out_len: *mut usize) -> c_int {
pub unsafe extern fn parity_rpc(client: *mut c_void, query: *const c_char, len: usize, out_str: *mut c_char, out_len: *mut usize) -> c_int {
panic::catch_unwind(|| {
let client: &mut parity_ethereum::RunningClient = &mut *(client as *mut parity_ethereum::RunningClient);
@@ -163,3 +170,98 @@ impl CallbackStr {
}
}
}
#[cfg(feature = "jni")]
#[no_mangle]
pub unsafe extern "system" fn Java_io_parity_ethereum_Parity_configFromCli(env: JNIEnv, _: JClass, cli: jobjectArray) -> jlong {
let cli_len = env.get_array_length(cli).expect("invalid Java bindings");
let mut jni_strings = Vec::with_capacity(cli_len as usize);
let mut opts = Vec::with_capacity(cli_len as usize);
let mut opts_lens = Vec::with_capacity(cli_len as usize);
for n in 0 .. cli_len {
let elem = env.get_object_array_element(cli, n).expect("invalid Java bindings");
let elem_str: JString = elem.into();
match env.get_string(elem_str) {
Ok(s) => {
opts.push(s.as_ptr());
opts_lens.push(s.to_bytes().len());
jni_strings.push(s);
},
Err(err) => {
let _ = env.throw_new("java/lang/Exception", err.to_string());
0
}
};
}
let mut out = ptr::null_mut();
match parity_config_from_cli(opts.as_ptr(), opts_lens.as_ptr(), cli_len as usize, &mut out) {
0 => out as usize as jlong,
_ => {
let _ = env.throw_new("java/lang/Exception", "failed to create config object");
0
},
}
}
#[cfg(feature = "jni")]
#[no_mangle]
pub unsafe extern "system" fn Java_io_parity_ethereum_Parity_build(env: JNIEnv, _: JClass, config: jlong) -> jlong {
let params = ParityParams {
configuration: config as usize as *mut c_void,
.. mem::zeroed()
};
let mut out = ptr::null_mut();
match parity_start(&params, &mut out) {
0 => out as usize as jlong,
_ => {
let _ = env.throw_new("java/lang/Exception", "failed to start Parity");
0
},
}
}
#[cfg(feature = "jni")]
#[no_mangle]
pub unsafe extern "system" fn Java_io_parity_ethereum_Parity_destroy(_env: JNIEnv, _: JClass, parity: jlong) {
let parity = parity as usize as *mut c_void;
parity_destroy(parity);
}
#[cfg(feature = "jni")]
#[no_mangle]
pub unsafe extern "system" fn Java_io_parity_ethereum_Parity_rpcQueryNative<'a>(env: JNIEnv<'a>, _: JClass, parity: jlong, rpc: JString) -> JString<'a> {
let parity = parity as usize as *mut c_void;
let rpc = match env.get_string(rpc) {
Ok(s) => s,
Err(err) => {
let _ = env.throw_new("java/lang/Exception", err.to_string());
return env.new_string("").expect("Creating an empty string never fails");
},
};
let mut out_len = 255;
let mut out = [0u8; 256];
match parity_rpc(parity, rpc.as_ptr(), rpc.to_bytes().len(), out.as_mut_ptr() as *mut c_char, &mut out_len) {
0 => (),
_ => {
let _ = env.throw_new("java/lang/Exception", "failed to perform RPC query");
return env.new_string("").expect("Creating an empty string never fails");
},
}
let out = str::from_utf8(&out[..out_len])
.expect("parity always generates an UTF-8 RPC response");
match env.new_string(out) {
Ok(s) => s,
Err(err) => {
let _ = env.throw_new("java/lang/Exception", err.to_string());
return env.new_string("").expect("Creating an empty string never fails");
}
}
}