|
| 1 | +// Copyright 2018 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Parity. |
| 3 | + |
| 4 | +// Parity is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Parity is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Parity. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! Note that all the structs and functions here are documented in `parity.h`, to avoid |
| 18 | +//! duplicating documentation. |
| 19 | +
|
| 20 | +extern crate parity; |
| 21 | + |
| 22 | +use std::os::raw::{c_char, c_void, c_int}; |
| 23 | +use std::panic; |
| 24 | +use std::ptr; |
| 25 | +use std::slice; |
| 26 | + |
| 27 | +#[repr(C)] |
| 28 | +pub struct ParityParams { |
| 29 | + pub configuration: *mut c_void, |
| 30 | + pub on_client_restart_cb: extern "C" fn(*mut c_void, *const c_char, usize), |
| 31 | + pub on_client_restart_cb_custom: *mut c_void, |
| 32 | +} |
| 33 | + |
| 34 | +#[no_mangle] |
| 35 | +pub extern fn parity_config_from_cli(args: *const *const c_char, args_lens: *const usize, len: usize, output: *mut *mut c_void) -> c_int { |
| 36 | + unsafe { |
| 37 | + panic::catch_unwind(|| { |
| 38 | + *output = ptr::null_mut(); |
| 39 | + |
| 40 | + let args = { |
| 41 | + let arg_ptrs = slice::from_raw_parts(args, len); |
| 42 | + let arg_lens = slice::from_raw_parts(args_lens, len); |
| 43 | + |
| 44 | + let mut args = Vec::with_capacity(len + 1); |
| 45 | + args.push("parity".to_owned()); |
| 46 | + |
| 47 | + for (&arg, &len) in arg_ptrs.iter().zip(arg_lens.iter()) { |
| 48 | + let string = slice::from_raw_parts(arg as *const u8, len); |
| 49 | + match String::from_utf8(string.to_owned()) { |
| 50 | + Ok(a) => args.push(a), |
| 51 | + Err(_) => return 1, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + args |
| 56 | + }; |
| 57 | + |
| 58 | + match parity::Configuration::parse_cli(&args) { |
| 59 | + Ok(mut cfg) => { |
| 60 | + // Always disable the auto-updater when used as a library. |
| 61 | + cfg.args.arg_auto_update = "none".to_owned(); |
| 62 | + |
| 63 | + let cfg = Box::into_raw(Box::new(cfg)); |
| 64 | + *output = cfg as *mut _; |
| 65 | + 0 |
| 66 | + }, |
| 67 | + Err(_) => { |
| 68 | + 1 |
| 69 | + }, |
| 70 | + } |
| 71 | + }).unwrap_or(1) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[no_mangle] |
| 76 | +pub extern fn parity_config_destroy(cfg: *mut c_void) { |
| 77 | + unsafe { |
| 78 | + let _ = panic::catch_unwind(|| { |
| 79 | + let _cfg = Box::from_raw(cfg as *mut parity::Configuration); |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[no_mangle] |
| 85 | +pub extern fn parity_start(cfg: *const ParityParams, output: *mut *mut c_void) -> c_int { |
| 86 | + unsafe { |
| 87 | + panic::catch_unwind(|| { |
| 88 | + *output = ptr::null_mut(); |
| 89 | + let cfg: &ParityParams = &*cfg; |
| 90 | + |
| 91 | + let config = Box::from_raw(cfg.configuration as *mut parity::Configuration); |
| 92 | + |
| 93 | + let on_client_restart_cb = { |
| 94 | + struct Cb(extern "C" fn(*mut c_void, *const c_char, usize), *mut c_void); |
| 95 | + unsafe impl Send for Cb {} |
| 96 | + unsafe impl Sync for Cb {} |
| 97 | + impl Cb { |
| 98 | + fn call(&self, new_chain: String) { |
| 99 | + let cb = self.0; |
| 100 | + cb(self.1, new_chain.as_bytes().as_ptr() as *const _, new_chain.len()) |
| 101 | + } |
| 102 | + } |
| 103 | + let cb = Cb(cfg.on_client_restart_cb, cfg.on_client_restart_cb_custom); |
| 104 | + move |new_chain: String| { cb.call(new_chain); } |
| 105 | + }; |
| 106 | + |
| 107 | + let action = match parity::start(*config, on_client_restart_cb, || {}) { |
| 108 | + Ok(action) => action, |
| 109 | + Err(_) => return 1, |
| 110 | + }; |
| 111 | + |
| 112 | + match action { |
| 113 | + parity::ExecutionAction::Instant(Some(s)) => { println!("{}", s); 0 }, |
| 114 | + parity::ExecutionAction::Instant(None) => 0, |
| 115 | + parity::ExecutionAction::Running(client) => { |
| 116 | + *output = Box::into_raw(Box::<parity::RunningClient>::new(client)) as *mut c_void; |
| 117 | + 0 |
| 118 | + } |
| 119 | + } |
| 120 | + }).unwrap_or(1) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[no_mangle] |
| 125 | +pub extern fn parity_destroy(client: *mut c_void) { |
| 126 | + unsafe { |
| 127 | + let _ = panic::catch_unwind(|| { |
| 128 | + let client = Box::from_raw(client as *mut parity::RunningClient); |
| 129 | + client.shutdown(); |
| 130 | + }); |
| 131 | + } |
| 132 | +} |
0 commit comments